-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoBLE.cpp
275 lines (207 loc) · 6.97 KB
/
GoBLE.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include "GoBLE.h"
#include <Arduino.h>
_GoBLE Goble;
/*
* The following constants tell, for each accelerometer
* axis, which values are returned when the axis measures
* zero acceleration.
*/
_GoBLE::_GoBLE() {
}
void _GoBLE::begin() {
Serial.begin(115200);
initRecvDataPack();
_joystickX = 127;
_joystickY = 127;
for (int i = 0; i < MAXBUTTONID; i++) {
_button[i] = RELEASED;
}
for (int i = 0; i < 20; i++) bleQueue.push(0x00);
for (int i = 0; i < 20; i++) bleQueue.pop();
}
void _GoBLE::begin(unsigned int baudrate) {
Serial.begin(baudrate);
initRecvDataPack();
_joystickX = 127;
_joystickY = 127;
for (int i = 0; i < MAXBUTTONID; i++) {
_button[i] = RELEASED;
}
}
boolean _GoBLE::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
*/
if (Serial.available()) bleDataReceiver();
if (DEBUGDATARAW) {
Serial.println("GoBLE availalbe -> new data package!");
for (int i = 0; i < rDataPack.commandLength; i++) {
Serial.print(bleQueue.pop(), HEX);
}
Serial.println();
}
if (DEBUGPARSER) {
Serial.print("GoBLE availalbe -> bleQueue Counter: ");
Serial.print(bleQueue.count());
Serial.println();
}
if (rDataPack.commandFlag && bleQueue.count() == rDataPack.commandLength) {
rDataPack.parseState = bleDataPackageParser();
if(rDataPack.parseState == PARSESUCCESS){
updateJoystickVal();
updateButtonState();
return true;
}
}
return false;
}
int _GoBLE::readJoystickX() {
return _joystickX;
}
int _GoBLE::readJoystickY() {
return _joystickY;
}
boolean _GoBLE::readSwitchUp() {
return _button[SWITCH_UP];
}
boolean _GoBLE::readSwitchDown() {
return _button[SWITCH_DOWN];
}
boolean _GoBLE::readSwitchLeft() {
return _button[SWITCH_LEFT];
}
boolean _GoBLE::readSwitchRight() {
return _button[SWITCH_RIGHT];
}
boolean _GoBLE::readSwitchSelect() {
return _button[SWITCH_SELECT];
}
boolean _GoBLE::readSwitchStart() {
return _button[SWITCH_START];
}
// Private functions
int _GoBLE::bleDataPackageParser() {
/*
0x10 - Parse success
0x11 - Wrong header charactors
0x12 - Wrong button number
0x13 - Check Sum Error
*/
byte calculateSum = 0;
rDataPack.header1 = bleQueue.pop(), calculateSum += rDataPack.header1;
rDataPack.header2 = bleQueue.pop(), calculateSum += rDataPack.header2;
if(rDataPack.header1 != DEFAULTHEADER1) return 0x11;
if(rDataPack.header2 != DEFAULTHEADER2) return 0x11;
rDataPack.address = bleQueue.pop(), calculateSum += rDataPack.address;
rDataPack.latestDigitalButtonNumber = rDataPack.digitalButtonNumber;
rDataPack.digitalButtonNumber = bleQueue.pop(), calculateSum += rDataPack.digitalButtonNumber;
int digitalButtonLength = rDataPack.digitalButtonNumber;
if (DEBUGCHECKSUM) {
Serial.print("Parser -> digitalButtonLength: ");
Serial.println(digitalButtonLength);
}
if(digitalButtonLength > MAXBUTTONNUMBER) return 0x12;
rDataPack.joystickPosition = bleQueue.pop(), calculateSum += rDataPack.joystickPosition;
// read button data package - dynamic button payload length
for (int buttonPayloadPointer = 0; buttonPayloadPointer < digitalButtonLength; buttonPayloadPointer++) {
rDataPack.buttonPayload[buttonPayloadPointer] = bleQueue.pop();
calculateSum += rDataPack.buttonPayload[buttonPayloadPointer];
}
// read 4 byte joystick data package
for (int i = 0; i < 4; i++) rDataPack.joystickPayload[i] = bleQueue.pop(), calculateSum += rDataPack.joystickPayload[i];
rDataPack.checkSum = bleQueue.pop();
if (DEBUGCHECKSUM) {
Serial.print("Parser -> sum calculation: ");
Serial.println(calculateSum);
Serial.print("Parser -> checkSum byte value: ");
Serial.println(rDataPack.checkSum);
}
// check sum and update the parse state value
// if the checksum byte is not correct, return 0x12
rDataPack.commandFlag = false;
if (rDataPack.checkSum == calculateSum) return PARSESUCCESS;
else return 0x13;
}
void _GoBLE::bleDataReceiver() {
byte inputByte = Serial.read();
if (DEBUGDATARECEIVER) {
Serial.print("bleDataReceiver -> new data:");
Serial.println(inputByte, HEX);
}
// throw the trash data and restore the useful data to the queue buffer
if (inputByte == DEFAULTHEADER1 || rDataPack.commandFlag == true) {
bleQueue.push(inputByte);
rDataPack.commandFlag = true;
// auto adjust the command length based on the button command value
if (bleQueue.count() == PACKBUTTONSIGN) {
// max button input at one moment should less than 6 buttons
if (inputByte > 0 && inputByte < MAXBUTTONNUMBER) {
// default command length + button number
rDataPack.commandLength = DEFAULTPACKLENGTH + inputByte;
if (DEBUGDATARECEIVER) Serial.print("bleDataReceiver -> Command Length:"), Serial.println(rDataPack.commandLength);
}
else rDataPack.commandLength = DEFAULTPACKLENGTH;
}
}
}
void _GoBLE::initRecvDataPack() {
rDataPack.commandFlag = false;
rDataPack.commandLength = DEFAULTPACKLENGTH;
rDataPack.parseState = PARSESUCCESS;
rDataPack.digitalButtonNumber = 0;
rDataPack.latestDigitalButtonNumber = 0;
}
void _GoBLE::updateJoystickVal(){
_joystickX = rDataPack.joystickPayload[0];
_joystickY = rDataPack.joystickPayload[1];
}
void _GoBLE::updateButtonState(){
if (rDataPack.digitalButtonNumber == 0 && rDataPack.latestDigitalButtonNumber != 0) {
for (int i = 0; i < MAXBUTTONID; i++) {
if (_button[i] == PRESSED) {
if (DEBUGUPDATEBUTTON) {
Serial.print("updateButtonState -> clear Pressed button number: ");
Serial.println(i);
}
_button[i] = RELEASED;
}
}
}
for (int i = 0; i < rDataPack.digitalButtonNumber; i++) _button[rDataPack.buttonPayload[i]] = PRESSED;
}
/*
unsigned int _GoBLE::readChannel(byte channel) {
digitalWrite(MUX_ADDR_PINS[0], (channel & 1) ? HIGH : LOW);
digitalWrite(MUX_ADDR_PINS[1], (channel & 2) ? HIGH : LOW);
digitalWrite(MUX_ADDR_PINS[2], (channel & 4) ? HIGH : LOW);
digitalWrite(MUX_ADDR_PINS[3], (channel & 8) ? HIGH : LOW);
// workaround to cope with lack of pullup resistor on joystick switch
if (channel == CH_JOYSTICK_SW) {
pinMode(MUX_COM_PIN, INPUT_PULLUP);
unsigned int joystickSwitchState = (digitalRead(MUX_COM_PIN) == HIGH) ? 1023 : 0;
digitalWrite(MUX_COM_PIN, LOW);
return joystickSwitchState;
}
else
return analogRead(MUX_COM_PIN);
}
boolean _GoBLE::readButton(byte ch) {
if (ch >= SWITCH_1 && ch <= SWITCH_5) {
ch;
}
switch(ch) {
}
unsigned int val = readChannel(ch);
return (val > 512) ? HIGH : LOW;
}
boolean _GoBLE::readJoystickButton() {
if (readChannel(CH_JOYSTICK_SW) == 1023) {
return HIGH;
} else if (readChannel(CH_JOYSTICK_SW) == 0) {
return LOW;
}
}
*/