Last active
August 25, 2019 09:08
-
-
Save jw910731/01a49778f929d5495cc6724ef3b415ba to your computer and use it in GitHub Desktop.
2019麗山FRC7589新生營藍牙接收模組
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "bluetooth.h" | |
| BluetoothHandler::BluetoothHandler(HardwareSerial *s, int baudRate):input(s), lSpd(0), rSpd(0), svoDeg(0), state(false){ | |
| input->begin(baudRate); | |
| } | |
| BluetoothHandler::BluetoothHandler(HardwareSerial *s){ | |
| BluetoothHandler(s, 9600); | |
| } | |
| void BluetoothHandler::receive(){ | |
| if(input->available() > 0){ | |
| String s = input->readString(); | |
| buf += s; | |
| int state = 0, tail = 0; | |
| bool mul = 1; | |
| for(int i = 0; i < buf.length() ; ++i){ | |
| unsigned char raw = buf[i]; | |
| if(!state){ | |
| switch(raw){ | |
| //Left Wheel | |
| case 'M': | |
| case 'm': | |
| mul = -1; // negtive | |
| case 'L': | |
| case 'l': | |
| state = 1; | |
| break; | |
| // Right Wheel | |
| case 'S': | |
| case 's': | |
| mul = -1; // negtive | |
| case 'R': | |
| case 'r': | |
| state = 2; | |
| break; | |
| //Servo | |
| case 'H': | |
| case 'h': | |
| state = 3; | |
| break; | |
| // on off autonomous mode | |
| case 'a': | |
| state = true; | |
| ++tail; | |
| break; | |
| case 'A': | |
| ++tail; | |
| state = false; | |
| break; | |
| default: | |
| ++tail; | |
| break; | |
| } | |
| } | |
| else{ | |
| switch(state){ | |
| case 1: | |
| lSpd = int(raw) * mul; | |
| break; | |
| case 2: | |
| rSpd = int(raw) * mul; | |
| break; | |
| case 3: | |
| svoDeg = max(0, min(180, int(raw))); | |
| break; | |
| default: | |
| break; | |
| } | |
| state = 0; | |
| mul = 1; | |
| tail += 2; | |
| } | |
| } | |
| buf = buf.substring(tail); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef BLUETOOTH_H | |
| #define BLUETOOTH_H | |
| #include "Arduino.h" | |
| class BluetoothHandler{ | |
| private: | |
| HardwareSerial *input; | |
| int lSpd, rSpd, svoDeg; | |
| bool state; | |
| String buf; | |
| public: | |
| BluetoothHandler(HardwareSerial *s, int baudRate); | |
| BluetoothHandler(HardwareSerial *s); | |
| // Must call receive first to get latest info from serial | |
| void receive(); | |
| // true = remote control, false = autonomous | |
| bool getState(){return state;} | |
| int getLeft(){return lSpd;} | |
| int getRight(){return rSpd;} | |
| int getServo(){return svoDeg;} | |
| }; | |
| #endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment