forked from philipellisis/arduino-virtual-pinball-board
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommunication.cpp
160 lines (143 loc) · 4.84 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
#include "Communication.h"
#include <Arduino.h>
#include "Plunger.h"
#include "Buttons.h"
#include "Accelerometer.h"
#include "Enums.h"
Communication::Communication() {
}
void Communication::init(Plunger* plunger, Accelerometer* accel, Buttons* buttons, Config* config, Outputs* outputs, Joystick_* joystick) {
//if (DEBUG) {Serial.print(F("Communication: initializing Communication\r\n"));}
_outputs = outputs;
_plunger = plunger;
_accelerometer = accel;
_buttons = buttons;
_config = config;
_joystick = joystick;
}
void Communication::communicate() {
_outputs->checkResetOutputs();
//int maxSerial = Serial.available();
for (int i = 0; i < 9; i++) {
if (Serial.available()) {
incomingData[dataLocation] = Serial.read();
//if (DEBUG) {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;
//if (DEBUG) {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) {
//if (DEBUG) {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]) {
case 28:
_joystick->setXAxis(-100000);
break;
case 29:
_joystick->setXAxis(100000);
break;
case 30:
_joystick->setYAxis(-100000);
break;
case 31:
_joystick->setYAxis(100000);
break;
case 32:
_joystick->setZAxis(-100000);
break;
case 33:
_joystick->setZAxis(100000);
break;
default:
_joystick->setButton(incomingData[2], 1);
delay(500);
_joystick->setButton(incomingData[2], 0);
}
delay(500);
} else {
//normal operation
//if (DEBUG) {Serial.print(F("DEBUG,sending output\r\n"));}
_config->lightShowState = OUTPUT_RECEIVED_RESET_TIMER;
updateOutputs();
}
dataLocation = 0;
} else {
dataLocation++;
}
}
} else {
break;
}
}
sendAdmin();
}
void Communication::sendAdmin() {
if (admin > 0) {
delay(50);
switch (admin)
{
case BUTTONS:
_buttons->sendButtonState();
break;
case OUTPUTS:
_outputs->sendOutputState();
break;
case PLUNGER:
_plunger->sendPlungerState();
break;
case ACCEL:
_accelerometer->sendAccelerometerState();
break;
case SEND_CONFIG:
_config->sendConfig();
admin = 0;
break;
case GET_CONFIG:
_config->updateConfigFromSerial();
_plunger->resetPlunger();
_accelerometer->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.9.0\r\n"));
admin = 0;
break;
}
}
}
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];
}
}
}