GoKit Arduino简化版
盖个楼,第一贴:把板载马达封装成一个类,方便操作。类定义:
/******** (C) COPYRIGHT 2015 Gizwits *********
* @file : Motor.h
* @author : William Yip
* @date : 6 Dec 2015
* @version : 1.0
* @brief : 连接、增值、开放、中立、安全、自有、自由、生态
************************
* History:
************************
THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH
CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS A
RESULT, GIZWITS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR
CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF
SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION
CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
************************/
#ifndef _MOTOR_H_
#define _MOTOR_H_
// Motor
#define PIN_D4_MOTOR_ANODE 4
#define PIN_D5_MOTOR_CATHODE 5
#define MOTOR_MAX_SPEED 255
#define MOTOR_MIN_SPEED (-255)
class Motor
{
public:
Motor();
~Motor();
void Rotate(int speed);
};
#endif // _MOTOR_H_
类实现:
/****** (C) COPYRIGHT 2015 Gizwits ******
* @file : Motor.cpp
* @author : William Yip
* @date : 6 Dec 2015
* @version : 1.0
* @brief : 连接、增值、开放、中立、安全、自有、自由、生态
****************
* History:
****************
THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH
CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS A
RESULT, GIZWITS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR
CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF
SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION
CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
****************/
#include <Arduino.h>
#include "motor.h"
/**
@desc GoKit板载马达的简单控制类,主要作PWM控制演示用,占用D4/D5两只GPIO引脚。
*/
Motor::Motor()
{
// 初始化引脚工作模式
pinMode(PIN_D4_MOTOR_ANODE, OUTPUT);
pinMode(PIN_D5_MOTOR_CATHODE, OUTPUT);
// 初始化引脚电平
analogWrite(PIN_D4_MOTOR_ANODE, LOW);
analogWrite(PIN_D5_MOTOR_CATHODE, LOW);
}
Motor::~Motor()
{
// 复位引脚电压
analogWrite(PIN_D4_MOTOR_ANODE, LOW);
analogWrite(PIN_D5_MOTOR_CATHODE, LOW);
}
/**
@desc 使板载马达按指定速度转动
@param speed 只在-255 ~ 255间有效,正转最大速度 255,反转最大速度 -255。如果输入值超过范围,会自动修正到范围边界值。
*/
void Motor::Rotate(int speed)
{
// 输入修正,限制在-255 ~ 255之间
speed = (speed < MOTOR_MIN_SPEED) ? MOTOR_MIN_SPEED
: (speed > MOTOR_MAX_SPEED) ? MOTOR_MAX_SPEED
: speed;
// 把正反向速度,分为正负极各自的PWM值
int pwmAnode = (speed > 0) ? speed : 0;
int pwmCathode = (speed < 0) ? speed : 0;
// 执行到输出脚
analogWrite(PIN_D4_MOTOR_ANODE, pwmAnode);
analogWrite(PIN_D5_MOTOR_CATHODE, pwmCathode);
}
调用例子:
#include "Motor.h"
static Motor * motor = NULL;
static uint32_t ts = 0;
void loop() {
// put your main code here, to run repeatedly:
// 马达调用的示例代码:每500毫秒随机换一个转速
if (NULL == motor)
motor = new Motor;
if (millis() - ts < 500)
return;
ts = millis();
motor->Rotate(random(-255, 255));
}
点赞点赞点赞:D 第二贴,红外光电反射传感(ST188)
类定义:
/** (C) COPYRIGHT 2015 Gizwits **
* @file : IRSensor.h
* @author : William Yip
* @date : 6 Dec 2015
* @version : 1.0
* @brief : 连接、增值、开放、中立、安全、自有、自由、生态
****
* History:
****
THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH
CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS A
RESULT, GIZWITS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR
CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF
SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION
CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
****/
#ifndef _IR_SENSOR_H_
#define _IR_SENSOR_H_
// IRSensor
#define PIN_D2_IR_SENSOR 2
class IRSensor
{
public:
IRSensor();
~IRSensor();
private:
static void onChanged();
};
#endif // _IR_SENSOR_H_
类实现:
/** (C) COPYRIGHT 2015 Gizwits **
* @file : IRSensor.cpp
* @author : William Yip
* @date : 6 Dec 2015
* @version : 1.0
* @brief : 连接、增值、开放、中立、安全、自有、自由、生态
***
* History:
***
THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH
CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS A
RESULT, GIZWITS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR
CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF
SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION
CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
***/
#include <Arduino.h>
#include "IRSensor.h"
#include "GoKit.h"
/*
@desc GoKit板载红外动作探测器的简单控制类,主要作中断处理、引脚监听上报演示用,占用D2一只GPIO引脚。
*/
IRSensor::IRSensor()
{
// 初始化监听引脚变化的中断处理函数
attachInterrupt(digitalPinToInterrupt(PIN_D2_IR_SENSOR), IRSensor::onChanged, CHANGE);
}
IRSensor::~IRSensor()
{
// 复位引脚
detachInterrupt(digitalPinToInterrupt(PIN_D2_IR_SENSOR));
}
/* @desc 中断处理函数,会在PIN2_IR引脚电平改变时被触发,开发者可以自由增加自定义的处理逻辑代码 */
void IRSensor::onChanged()
{
// TODO: you can do more when IR is triggered.
GoKit::onIrChanged();
}
调用例子:
#include "IRSensor.h"
static IRSensor * irSensor = NULL;
void GoKit::onIrChanged()
{
// TODO: process ir sensor changed value here
}
void setup()
{
pGoKit->irSensor = new IRSensor;
}
申请是stm32底板,为什么换成自用的arduino uon r3就.不行?已经烧录好程序。 好文,不错,精简了不少
页:
[1]