1 Star 0 Fork 0

DFRobot/DFRobotBLEGamepad

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
DFRobotBLEGamepad.cpp 17.59 KB
一键复制 编辑 原始数据 按行查看 历史
jimaobian 提交于 2016-08-04 18:59 +08:00 . Add the joystick button
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
/*!
* @file DFRobotBLEGamepad.cpp
* @brief DFRobot's Wireless Gamepad - Arduino with Bluetooth 4.0 (SKU:DFR0304)
* @n This is the library for DFRobot's Wireless Gamepad V2.0
* @n [Schematics]([http://www.dfrobot.com/wiki/index.php?title=BLE_Wireless_Gamepad_V2_SKU:_DFR0304])
*
* @copyright [DFRobot](http://www.dfrobot.com), 2016
* @copyright GNU Lesser General Public License
*
* @author [Jeffrey](Angelo.qiao@dfrobot.com)
* @version V2.0
* @date 2016-07-18
*/
#include "DFRobotBLEGamepad.h"
#include <Arduino.h>
#include "string.h"
/*
* Call Constructor
*/
//DFRobotBLEGamepad::DFRobotBLEGamepad(HardwareSerial& theSerial ):bleSerial( theSerial ){}
DFRobotBLEGamepad::DFRobotBLEGamepad(){}
/*
* Initialize Class Variables
*/
void (*DFRobotBLEGamepad::userButtonUpCallback)(void);
void (*DFRobotBLEGamepad::userButtonDownCallback)(void);
void (*DFRobotBLEGamepad::userButtonLeftCallback)(void);
void (*DFRobotBLEGamepad::userButtonRightCallback)(void);
void (*DFRobotBLEGamepad::userButtonLeftF1Callback)(void);
void (*DFRobotBLEGamepad::userButtonLeftF2Callback)(void);
void (*DFRobotBLEGamepad::userButtonLeftStickCallback)(void);
void(*DFRobotBLEGamepad::userButton4Callback)(void);
void(*DFRobotBLEGamepad::userButton2Callback)(void);
void(*DFRobotBLEGamepad::userButton1Callback)(void);
void(*DFRobotBLEGamepad::userButton3Callback)(void);
void(*DFRobotBLEGamepad::userButtonRightF1Callback)(void);
void(*DFRobotBLEGamepad::userButtonRightF2Callback)(void);
void(*DFRobotBLEGamepad::userButtonRightStickCallback)(void);
/*
* User configuration for the baudrate of the BLE communication
*/
void DFRobotBLEGamepad::begin(Stream& theSerial) {
bleSerial = &theSerial;
debugSerial.begin(115200); // config the default debug Serial port
initRecvDataPack();
_joystickRightX = 127;
_joystickRightY = 127;
_joystickLeftX = 127;
_joystickLeftY = 127;
rDataPack.prbpayType.switchButtonUp = RELEASED;
rDataPack.prbpayType.switchButtonDown = RELEASED;
rDataPack.prbpayType.switchButtonLeft = RELEASED;
rDataPack.prbpayType.switchButtonRight = RELEASED;
rDataPack.prbpayType.switchButtonLeftF1 = RELEASED;
rDataPack.prbpayType.switchButtonLeftF2 = RELEASED;
rDataPack.prbpayType.switchButtonLeftStick = RELEASED;
rDataPack.prbpayType.switchButton4 = RELEASED;
rDataPack.prbpayType.switchButton2 = RELEASED;
rDataPack.prbpayType.switchButton1 = RELEASED;
rDataPack.prbpayType.switchButton3 = RELEASED;
rDataPack.prbpayType.switchButtonRightF1 = RELEASED;
rDataPack.prbpayType.switchButtonRightF2 = RELEASED;
rDataPack.prbpayType.switchButtonRightStick = RELEASED;
memset(bleDataArray, 0x00, sizeof(bleDataArray));
bleDataIndex = 0;
}
/*
* Function: Check if any new commands valid from BLE communication
* True - new command received
* False - no new command
*/
boolean DFRobotBLEGamepad::available() {
/*
* function introduction:
* push the new valid data to the data buffer package
* throw away the invalid byte
* parse the data package when the command length is matching the protocol
*/
byte inputByte = 0x00;
while (bleSerial->available())
{
inputByte = bleSerial->read();
if (inputByte == DEFAULTHEADER1)
{
memset(bleDataArray, 0x00, sizeof(bleDataArray)); // Reset the BLEdataArraay
bleDataIndex = 0;
rDataPack.commandFlag = true;
bleDataArray[bleDataIndex] = inputByte;
bleDataIndex++;
}
else if(rDataPack.commandFlag == true)
{
bleDataArray[bleDataIndex] = inputByte;
bleDataIndex++;
if (bleDataIndex == DEFAULTPACKLENGTH)
break;
}
}
if (bleDataIndex < 14) return false;
/*
* print the raw data package pushed to the queue.
* It will disable the later functions!!! So please don't print it if it's not necessary for you
*/
#if DEBUGDATARAW
if ( DEBUGDATARAW ) {
debugSerial.println(F("DFRobotBLEGamepad availalbe -> new data package!"));
for ( int i = 0; i < rDataPack.commandLength; i++ ) {
debugSerial.print(bleDataArray[i], HEX);
}
debugSerial.println();
}
#endif
//print the data package length
#if DEBUGPARSER
if (DEBUGPARSER) {
debugSerial.print( F("DFRobotBLEGamepad availalbe -> bleQueue Counter: ") );
debugSerial.print(sizeof(bleDataArray));
debugSerial.println();
}
#endif
/*
* check if the length and command Flag is valid, then parse the new command package received
*/
if ( rDataPack.commandFlag ) {
rDataPack.parseState = bleDataPackageParser(); //parse the command package
if(rDataPack.parseState == PARSESUCCESS){ //if parse successfully
//debugSerial.println(F("parse sucess"));
updateJoystickVal(); //update the variables
// if press the button, trigger the user button event functions
if( ( rDataPack.bpayType.switchButtonUp == PRESSED ) &&
userButtonUpCallback ){
userButtonUpCallback();
rDataPack.bpayType.switchButtonUp = RELEASED;
}
if(( rDataPack.bpayType.switchButtonDown == PRESSED ) &&
userButtonDownCallback ) {
userButtonDownCallback();
rDataPack.bpayType.switchButtonDown = RELEASED;
}
if( ( rDataPack.bpayType.switchButtonLeft == PRESSED ) &&
userButtonLeftCallback ) {
userButtonLeftCallback();
rDataPack.bpayType.switchButtonLeft = RELEASED;
}
if( ( rDataPack.bpayType.switchButtonRight == PRESSED ) &&
userButtonRightCallback ) {
userButtonRightCallback();
rDataPack.bpayType.switchButtonRight = RELEASED;
}
if( ( rDataPack.bpayType.switchButtonLeftF1 == PRESSED) &&
userButtonLeftF1Callback ) {
userButtonLeftF1Callback();
rDataPack.bpayType.switchButtonLeftF1 = RELEASED;
}
if(( rDataPack.bpayType.switchButtonLeftF2 == PRESSED) &&
userButtonLeftF2Callback ) {
userButtonLeftF2Callback();
rDataPack.bpayType.switchButtonLeftF2 = RELEASED;
}
if ((rDataPack.bpayType.switchButtonLeftStick == PRESSED) &&
userButtonLeftStickCallback) {
userButtonLeftStickCallback();
rDataPack.bpayType.switchButtonLeftStick = RELEASED;
}
// if press the button, trigger the user button event functions
if ( (rDataPack.bpayType.switchButton4 == PRESSED) &&
userButton4Callback) {
userButton4Callback();
rDataPack.bpayType.switchButton4 = RELEASED;
}
if ( (rDataPack.bpayType.switchButton2 == PRESSED) &&
userButton2Callback) {
userButton2Callback();
rDataPack.bpayType.switchButton2 = RELEASED;
}
if ( (rDataPack.bpayType.switchButton1 == PRESSED) &&
userButton1Callback) {
userButton1Callback();
rDataPack.bpayType.switchButton1 = RELEASED;
}
if ( (rDataPack.bpayType.switchButton3 == PRESSED) &&
userButton3Callback) {
userButton3Callback();
rDataPack.bpayType.switchButton3 = RELEASED;
}
if ( (rDataPack.bpayType.switchButtonRightF1 == PRESSED) &&
userButtonRightF1Callback) {
userButtonRightF1Callback();
rDataPack.bpayType.switchButtonRightF1 = RELEASED;
}
if ((rDataPack.bpayType.switchButtonRightF2 == PRESSED) &&
userButtonRightF2Callback) {
userButtonRightF2Callback();
rDataPack.bpayType.switchButtonRightF2 = RELEASED;
}
if ((rDataPack.bpayType.switchButtonRightStick == PRESSED) &&
userButtonRightStickCallback) {
userButtonRightStickCallback();
rDataPack.bpayType.switchButtonRightStick = RELEASED;
}
return true;
}
else {
//debugSerial.println(F("parse error"));
}
}
return false;
}
int DFRobotBLEGamepad::readJoystickRightX() {
return _joystickRightX;
}
int DFRobotBLEGamepad::readJoystickRightY() {
return _joystickRightY;
}
int DFRobotBLEGamepad::readJoystickLeftX() {
return _joystickLeftX;
}
int DFRobotBLEGamepad::readJoystickLeftY() {
return _joystickLeftY;
}
boolean DFRobotBLEGamepad::readSwitchUp() {
return this->rDataPack.bpayType.switchButtonUp;
}
boolean DFRobotBLEGamepad::readSwitchDown() {
return this->rDataPack.bpayType.switchButtonDown;
}
boolean DFRobotBLEGamepad::readSwitchLeft() {
return this->rDataPack.bpayType.switchButtonLeft;
}
boolean DFRobotBLEGamepad::readSwitchRight() {
return this->rDataPack.bpayType.switchButtonRight;
}
boolean DFRobotBLEGamepad::readSwitchLeftF1() {
return this->rDataPack.bpayType.switchButtonLeftF1;
}
boolean DFRobotBLEGamepad::readSwitchLeftF2() {
return this->rDataPack.bpayType.switchButtonLeftF2;
}
boolean DFRobotBLEGamepad::readSwitchLeftStick() {
return this->rDataPack.bpayType.switchButtonLeftStick;
}
boolean DFRobotBLEGamepad::readSwitch4() {
return this->rDataPack.bpayType.switchButton4;
}
boolean DFRobotBLEGamepad::readSwitch2() {
return this->rDataPack.bpayType.switchButton2;
}
boolean DFRobotBLEGamepad::readSwitch1() {
return this->rDataPack.bpayType.switchButton1;
}
boolean DFRobotBLEGamepad::readSwitch3() {
return this->rDataPack.bpayType.switchButton3;
}
boolean DFRobotBLEGamepad::readSwitchRightF1() {
return this->rDataPack.bpayType.switchButtonRightF1;
}
boolean DFRobotBLEGamepad::readSwitchRightF2() {
return this->rDataPack.bpayType.switchButtonRightF2;
}
boolean DFRobotBLEGamepad::readSwitchRightStick() {
return this->rDataPack.bpayType.switchButtonRightStick;
}
/*
* sets function called on Switch UP pressed
*/
void DFRobotBLEGamepad::ButtonUpIsPressed(void (*function)(void)){
userButtonUpCallback = function;
}
void DFRobotBLEGamepad::ButtonDownIsPressed(void (*function)(void)){
userButtonDownCallback = function;
}
void DFRobotBLEGamepad::ButtonLeftIsPressed(void (*function)(void)){
userButtonLeftCallback = function;
}
void DFRobotBLEGamepad::ButtonRightIsPressed(void (*function)(void)){
userButtonRightCallback = function;
}
void DFRobotBLEGamepad::ButtonLeftF1IsPressed(void (*function)(void)){
userButtonLeftF1Callback = function;
}
void DFRobotBLEGamepad::ButtonLeftF2IsPressed(void (*function)(void)){
userButtonLeftF2Callback = function;
}
void DFRobotBLEGamepad::ButtonLeftStickIsPressed(void(*function)(void)) {
userButtonLeftStickCallback = function;
}
void DFRobotBLEGamepad::Button4IsPressed(void(*function)(void)) {
userButton4Callback = function;
}
void DFRobotBLEGamepad::Button2IsPressed(void(*function)(void)) {
userButton2Callback = function;
}
void DFRobotBLEGamepad::Button1IsPressed(void(*function)(void)) {
userButton1Callback = function;
}
void DFRobotBLEGamepad::Button3IsPressed(void(*function)(void)) {
userButton3Callback = function;
}
void DFRobotBLEGamepad::ButtonRightF1IsPressed(void(*function)(void)) {
userButtonRightF1Callback = function;
}
void DFRobotBLEGamepad::ButtonRightF2IsPressed(void(*function)(void)) {
userButtonRightF2Callback = function;
}
void DFRobotBLEGamepad::ButtonRightStickIsPressed(void(*function)(void)) {
userButtonRightStickCallback = function;
}
byte DFRobotBLEGamepad::digitalButtonParser( void ){
byte length = 0x00;
this->rDataPack.prbpayType.switchButtonUp = this->rDataPack.bpayType.switchButtonUp;
this->rDataPack.prbpayType.switchButtonDown = this->rDataPack.bpayType.switchButtonDown;
this->rDataPack.prbpayType.switchButtonLeft = this->rDataPack.bpayType.switchButtonLeft;
this->rDataPack.prbpayType.switchButtonRight = this->rDataPack.bpayType.switchButtonRight;
this->rDataPack.prbpayType.switchButtonLeftF1 = this->rDataPack.bpayType.switchButtonLeftF1;
this->rDataPack.prbpayType.switchButtonLeftF2 = this->rDataPack.bpayType.switchButtonLeftF2;
this->rDataPack.prbpayType.switchButtonLeftStick = this->rDataPack.bpayType.switchButtonLeftStick;
this->rDataPack.prbpayType.switchButton4 = this->rDataPack.bpayType.switchButton4;
this->rDataPack.prbpayType.switchButton2 = this->rDataPack.bpayType.switchButton2;
this->rDataPack.prbpayType.switchButton1 = this->rDataPack.bpayType.switchButton1;
this->rDataPack.prbpayType.switchButton3 = this->rDataPack.bpayType.switchButton3;
this->rDataPack.prbpayType.switchButtonRightF1 = this->rDataPack.bpayType.switchButtonRightF1;
this->rDataPack.prbpayType.switchButtonRightF2 = this->rDataPack.bpayType.switchButtonRightF2;
this->rDataPack.prbpayType.switchButtonRightStick = this->rDataPack.bpayType.switchButtonRightStick;
if (rDataPack.digitalButton[0] & DIGITALBUTTONLeftStick) {
rDataPack.bpayType.switchButtonLeftStick = PRESSED;
length++;
}
else {
rDataPack.bpayType.switchButtonLeftStick = RELEASED;
}
if( rDataPack.digitalButton[0] & DIGITALBUTTONLeftF1 ){
rDataPack.bpayType.switchButtonLeftF1 = PRESSED;
length++;
}else{
rDataPack.bpayType.switchButtonLeftF1 = RELEASED;
}
if( rDataPack.digitalButton[0] & DIGITALBUTTONLeftF2 ){
rDataPack.bpayType.switchButtonLeftF2 = PRESSED;
length++;
}else{
rDataPack.bpayType.switchButtonLeftF2 = RELEASED;
}
if( rDataPack.digitalButton[1] & DIGITALBUTTONUp ){
rDataPack.bpayType.switchButtonUp = PRESSED;
length++;
}else{
rDataPack.bpayType.switchButtonUp = RELEASED;
}
if( rDataPack.digitalButton[1] & DIGITALBUTTONDown ){
rDataPack.bpayType.switchButtonDown = PRESSED;
length++;
}else{
rDataPack.bpayType.switchButtonDown = RELEASED;
}
if( rDataPack.digitalButton[1] & DIGITALBUTTONLeft ){
rDataPack.bpayType.switchButtonLeft = PRESSED;
length++;
}else{
rDataPack.bpayType.switchButtonLeft = RELEASED;
}
if( rDataPack.digitalButton[1] & DIGITALBUTTONRight ){
rDataPack.bpayType.switchButtonRight = PRESSED;
length++;
}else{
rDataPack.bpayType.switchButtonRight = RELEASED;
}
if (rDataPack.digitalButton[0] & DIGITALBUTTONRightStick) {
rDataPack.bpayType.switchButtonRightStick = PRESSED;
length++;
}
else {
rDataPack.bpayType.switchButtonRightStick = RELEASED;
}
if (rDataPack.digitalButton[0] & DIGITALBUTTONRightF1) {
rDataPack.bpayType.switchButtonRightF1 = PRESSED;
length++;
}
else {
rDataPack.bpayType.switchButtonRightF1 = RELEASED;
}
if (rDataPack.digitalButton[0] & DIGITALBUTTONRightF2) {
rDataPack.bpayType.switchButtonRightF2 = PRESSED;
length++;
}
else {
rDataPack.bpayType.switchButtonRightF2 = RELEASED;
}
if (rDataPack.digitalButton[1] & DIGITALBUTTON4) {
rDataPack.bpayType.switchButton4 = PRESSED;
length++;
}
else {
rDataPack.bpayType.switchButton4 = RELEASED;
}
if (rDataPack.digitalButton[1] & DIGITALBUTTON2) {
rDataPack.bpayType.switchButton2 = PRESSED;
length++;
}
else {
rDataPack.bpayType.switchButton2 = RELEASED;
}
if (rDataPack.digitalButton[1] & DIGITALBUTTON1) {
rDataPack.bpayType.switchButton1 = PRESSED;
length++;
}
else {
rDataPack.bpayType.switchButton1 = RELEASED;
}
if (rDataPack.digitalButton[1] & DIGITALBUTTON3) {
rDataPack.bpayType.switchButton3 = PRESSED;
length++;
}
else {
rDataPack.bpayType.switchButton3 = RELEASED;
}
return length;
}
/*
*Private functions
*/
int DFRobotBLEGamepad::bleDataPackageParser() {
/*
* 0x10 - Parse success
* 0x11 - Wrong header charactors
* 0x12 - Wrong button number
* 0x13 - Check Sum Error
*/
byte calculateSum = 0;
rDataPack.header1 = bleDataArray[0], calculateSum ^= rDataPack.header1; //get the data from queue
rDataPack.header2 = bleDataArray[1], calculateSum ^= rDataPack.header2;
if( rDataPack.header1 != DEFAULTHEADER1 ) return 0x11; //check 1st and 2nd header byte
if( rDataPack.header2 != DEFAULTHEADER2 ) return 0x11;
rDataPack.address = bleDataArray[2], calculateSum ^= rDataPack.address; //get address byte
rDataPack.dataLength = bleDataArray[3],calculateSum ^= rDataPack.dataLength; //get data length
rDataPack.buttonCommond = bleDataArray[4],calculateSum ^= rDataPack.buttonCommond; //get commond byte
for (int i = 0; i < 4; i++) //get digitalButton-bit
{
rDataPack.digitalButton[i] = bleDataArray[i + 5], calculateSum ^= rDataPack.digitalButton[i];
}
int digitalButtonLength = this->digitalButtonParser();
#if DEBUGCHECKSUM
if ( DEBUGCHECKSUM ) { //check if the length is valid
debugSerial.print(F( "Parser -> digitalButtonLength: ") );
debugSerial.println( digitalButtonLength );
}
#endif
if( digitalButtonLength > MAXBUTTONNUMBER )
return 0x12;
//read 4 byte joystick data package
for (int i = 0; i < 4; i++)
{
rDataPack.joystickPayload[i] = bleDataArray[i+9], calculateSum ^= rDataPack.joystickPayload[i];
}
rDataPack.checkSum = bleDataArray[13]; //get the check sum byte
#if DEBUGCHECKSUM
if ( DEBUGCHECKSUM ) {
debugSerial.print( F("Parser -> sum calculation: " ));
debugSerial.println( calculateSum );
debugSerial.print( F("Parser -> checkSum byte value: ") );
debugSerial.println( rDataPack.checkSum );
}
#endif
/*
* check sum and update the parse state value
* if the checksum byte is not correct, return 0x12
*/
rDataPack.commandFlag = false; //clear the command flag to finish the parse step and wait for the next data package
if ( rDataPack.checkSum == calculateSum )
return PARSESUCCESS;
else
return 0x13;
}
/*
*init the variables
*/
void DFRobotBLEGamepad::initRecvDataPack() {
rDataPack.commandFlag = false;
rDataPack.commandLength = DEFAULTPACKLENGTH;
rDataPack.parseState = PARSESUCCESS;
}
void DFRobotBLEGamepad::updateJoystickVal(){
_joystickRightX = rDataPack.joystickPayload[0];
_joystickRightY = rDataPack.joystickPayload[1];
_joystickLeftX = rDataPack.joystickPayload[2];
_joystickLeftY = rDataPack.joystickPayload[3];
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/dfrobot/DFRobotBLEGamepad.git
git@gitee.com:dfrobot/DFRobotBLEGamepad.git
dfrobot
DFRobotBLEGamepad
DFRobotBLEGamepad
master

搜索帮助