-
Notifications
You must be signed in to change notification settings - Fork 9
/
Communication.cpp
183 lines (168 loc) · 5.17 KB
/
Communication.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
#include "Communication.h"
#include <Arduino.h>
#include "Plunger.h"
#include "HID-Project.h"
#include "Buttons.h"
#include "Accelerometer.h"
#include "Enums.h"
#include "Globals.h"
#include <avr/wdt.h>
Communication::Communication() {
}
void Communication::communicate() {
outputs.checkResetOutputs();
//int maxSerial = Serial.available();
for (int i = 0; i < 9; i++) {
if (Serial.available()) {
incomingData[dataLocation] = Serial.read();
// Serial.print(F("DEBUG,getting serial data: ")); Serial.print(incomingData[dataLocation]); Serial.print(F("\r\n"));
// data sent is always [0][200 + bank offset][output value 1][output value 2][output value 3][output value 4][output value 5][output value 6][output value 7]
// check to make sure we are reading what's expected for the first 2 bytes of data. If not, reset and start the buffer over again
// sample data coming in: 0È0000000
// sample data coming in for test: " d "
// sample data coming in for test all on : " d}}}}}}}"
// sample data coming in for test all off: " d "
if ((dataLocation == 0 && incomingData[0] != firstNumber) || (dataLocation == 1 && (incomingData[1] < bankOffset))) {
dataLocation = 0;
// Serial.print(F("DEBUG,bad data found, resetting positions\r\n"));
} else {
// wait until we have filled 9 slots of data, then do what needs to be done
if (dataLocation == 8) {
// Serial.print(F("DEBUG,9 slots filled, sending outputs\r\n"));
if (incomingData[1] == adminNumber) {
config.lightShowState = DISABLED;
//if (DEBUG) {Serial.print(F("DEBUG,Turning admin on\r\n"));}
// set admin functions{
admin = incomingData[2];
} else if (incomingData[1] == connectionNumber) {
Serial.print(connectedString);
} else if (incomingData[1] == outputSingleNumber) {
outputs.updateOutput(incomingData[2], incomingData[3]);
} else if (incomingData[1] == outputButtonNumber) {
switch(incomingData[2]) {
delay(1000);
case 28:
Gamepad1.xAxis(-32767);
break;
case 29:
Gamepad1.xAxis(32767);
break;
case 30:
Gamepad1.yAxis(-32767);
break;
case 31:
Gamepad1.yAxis(32767);
break;
case 32:
Gamepad1.zAxis(-127);
break;
case 33:
Gamepad1.zAxis(127);
break;
default:
buttons.sendButtonPush(incomingData[2], 1);
Gamepad1.write();
delay(500);
buttons.sendButtonPush(incomingData[2], 0);
}
Gamepad1.write();
delay(500);
Gamepad1.xAxis(0);
Gamepad1.yAxis(0);
Gamepad1.zAxis(0);
Gamepad1.write();
} else {
//normal operation
//if (DEBUG) {Serial.print(F("DEBUG,sending output\r\n"));}
if (config.lightShowState != OUTPUT_RECEIVED) {
lightShow.setLightsOff();
}
config.lightShowState = OUTPUT_RECEIVED_RESET_TIMER;
updateOutputs();
}
dataLocation = 0;
} else {
dataLocation++;
}
}
} else {
break;
}
}
sendAdmin();
}
void Communication::sendAdmin() {
if (admin > 0) {
switch (admin)
{
case BUTTONS:
if (!shouldDelay()) {
buttons.sendButtonState();
}
break;
case OUTPUTS:
if (!shouldDelay()) {
outputs.sendOutputState();
}
break;
case PLUNGER:
if (!shouldDelay()) {
plunger.sendPlungerState();
}
break;
case ACCEL:
if (!shouldDelay()) {
accel.sendAccelerometerState();
}
break;
case SEND_CONFIG:
config.sendConfig();
admin = 0;
break;
case GET_CONFIG:
config.updateConfigFromSerial();
plunger.resetPlunger();
accel.resetAccelerometer();
admin = 0;
break;
case OFF:
admin = 0;
config.lightShowState = OUTPUT_RECEIVED_RESET_TIMER;
outputs.turnOff();
break;
case CONNECT:
Serial.print(connectedString);
admin = 0;
break;
case VERSION:
Serial.print(F("V,1.19.0\r\n"));
admin = 0;
break;
case RESET:
pinMode(11, OUTPUT);
digitalWrite(11, LOW);
wdt_enable(WDTO_15MS);
admin = 0;
}
}
}
bool Communication::shouldDelay() {
if (delayIncrementor < 20) {
delayIncrementor++;
return true;
} else {
delayIncrementor = 0;
return false;
}
}
void Communication::updateOutputs() {
//if (DEBUG) {Serial.print(F("DEBUG,sending output\r\n"));}
int tempBankOffset;
tempBankOffset = incomingData[1] - bankOffset;
for (int i = 2; i < 9; i++) {
if (previousDOFValues[tempBankOffset*7 + i-2] != incomingData[i]) {
outputs.updateOutput(tempBankOffset*7 + i-2, incomingData[i]);
previousDOFValues[tempBankOffset*7 + i-2] = incomingData[i];
}
}
}