-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputGUI.ino
More file actions
241 lines (221 loc) · 6.46 KB
/
InputGUI.ino
File metadata and controls
241 lines (221 loc) · 6.46 KB
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
#if defined (INPUT_GUI)
#define INBUF_SIZE 512
// V: removed TX-buffer to reduce memory consumption
// V: RX-Buffer is not used anyhow
// #define TX_BUFFER_SIZE 512
// #define RX_BUFFER_SIZE 512
#define MSP_SETUP 150
#define MSP_COLORS 151
#define MSP_CONFIGS 152
#define MSP_SET_COLORS 230
#define MSP_SET_CONFIGS 231
#define MSP_SAVE_COLORS 232
#define MSP_SAVE_CONFIGS 233
#define MSP_NEXT_MODE 234
#define MSP_PREV_MODE 235
#define MSP_NEXT_CONFIG 236
#define MSP_PREV_CONFIG 237
#define HEADER_IDLE 0
#define HEADER_START 1
#define HEADER_M 2
#define HEADER_ARROW 3
#define HEADER_SIZE 4
#define HEADER_CMD 5
#define HEADER_ERR 6
uint8_t checksum, indRX, cmdMSP;
// uint8_t serialBufferRX[RX_BUFFER_SIZE];
volatile uint8_t serialHeadRX, serialTailRX;
// v: removed TX-buffer to reduce memory consumption
// volatile uint8_t serialHeadTX, serialTailTX;
// v: removed TX-buffer to reduce memory consumption
// uint8_t serialBufferTX[TX_BUFFER_SIZE];
uint8_t inBuf[INBUF_SIZE];
void setupInputGUI(){
Serial.begin(115200);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
}
void loopInputGUI(){
serialCom();
}
void skip8(){
indRX++;
}
void skip16(){
indRX+=2;
}
void skip32(){
indRX+=4;
}
uint32_t read32() {
uint32_t t = read16();
t+= (uint32_t)read16()<<16;
return t;
}
uint16_t read16() {
uint16_t t = read8();
t+= (uint16_t)read8()<<8;
return t;
}
uint8_t read8() {
return inBuf[indRX++]&0xff;
}
void headSerialReply(uint32_t s) {
headSerialResponse(0, s);
}
void tailSerialReply() {
serialize8(checksum);
UartSendData();
}
void UartSendData() {
// v: Since no buffer needs to be sent this method is not necessary anymore
/*
while(serialHeadTX != serialTailTX) {
if (++serialTailTX >= TX_BUFFER_SIZE) serialTailTX = 0;
Serial.write(serialBufferTX[serialTailTX]);
}
*/
}
//V: todo still need to check for size over 16bit
void headSerialResponse(uint8_t err, uint32_t s) {
serialize8('$');
serialize8('M');
serialize8(err ? '!' : '>');
checksum = 0; // start calculating a new checksum
//V: transfer size as 16 bit value (parameter changed to 32 bit even
serialize16(s);
serialize8(cmdMSP);
}
void serialize32(uint32_t a) {
serialize8((a ) & 0xFF);
serialize8((a>> 8) & 0xFF);
serialize8((a>>16) & 0xFF);
serialize8((a>>24) & 0xFF);
}
void serialize16(int16_t a) {
serialize8((a ) & 0xFF);
serialize8((a>>8) & 0xFF);
}
void serialize8(uint8_t a) {
//V: Direct write to Serial line to reduce mem consumptiion
Serial.write(a);
checksum ^= a;
}
void evaluateCommand() {
switch(cmdMSP) {
case MSP_SET_COLORS:
headSerialReply(0);
for (int i=0; i<MAX_EEPROM_COLORS; i++){
uint8_t r = read8();
uint8_t g = read8();
uint8_t b = read8();
CRGB rgb = CRGB(r, g, b);
storedColorsRGB[i] = rgb;
}
break;
case MSP_SAVE_COLORS:
headSerialReply(0);
writeColors();
break;
case MSP_SAVE_CONFIGS:
headSerialReply(0);
writeLeds();
break;
case MSP_SET_CONFIGS:
headSerialReply(0);
config = read8();
for (int i=0; i<((NUM_LEDS/2)+1); i++){
storedLedColors[config][i] = read8();
}
break;
case MSP_CONFIGS:
readLeds();
headSerialReply(MAX_LED_CONFIGS * ((NUM_LEDS/2)+1));
for (int i=0; i<MAX_LED_CONFIGS; i++){
for (int j=0; j<((NUM_LEDS/2)+1); j++){
serialize8(storedLedColors[i][j]);
}
}
break;
case MSP_SETUP:
headSerialReply(4);
serialize8(NUM_ARMS);
serialize8(LEDS_PER_ARM);
serialize8(MAX_EEPROM_COLORS);
serialize8(MAX_LED_CONFIGS);
break;
case MSP_COLORS:
readColors();
headSerialReply(3*MAX_EEPROM_COLORS);
for (int i=0; i<MAX_EEPROM_COLORS; i++){
serialize8(storedColorsRGB[i].r);
serialize8(storedColorsRGB[i].g);
serialize8(storedColorsRGB[i].b);
}
break;
case MSP_NEXT_MODE:
headSerialReply(0);
mode = (mode+1) % NUM_MODES;
break;
case MSP_PREV_MODE:
headSerialReply(0);
mode = (mode+(NUM_MODES-1)) % NUM_MODES;
break;
case MSP_NEXT_CONFIG:
headSerialReply(0);
config = (config+1) % MAX_LED_CONFIGS;
break;
case MSP_PREV_CONFIG:
headSerialReply(0);
config = (config+(MAX_LED_CONFIGS-1)) % MAX_LED_CONFIGS;
break;
}
tailSerialReply();
}
void serialCom() {
uint8_t c,n;
static uint8_t offset;
static uint8_t dataSize;
uint8_t c_state = HEADER_IDLE;
while (Serial.available()>0) {
digitalWrite(13, HIGH);
//V: No TX-Buffer anymore
// uint8_t bytesTXBuff = ((uint8_t)(serialHeadTX-serialTailTX))%TX_BUFFER_SIZE; // indicates the number of occupied bytes in TX buffer
// if (bytesTXBuff > TX_BUFFER_SIZE - 50 ) return; // ensure there is enough free TX buffer to go further (50 bytes margin)
c = Serial.read();
// regular data handling to detect and handle MSP and other data
if (c_state == HEADER_IDLE) {
c_state = (c=='$') ? HEADER_START : HEADER_IDLE;
} else if (c_state == HEADER_START) {
c_state = (c=='M') ? HEADER_M : HEADER_IDLE;
} else if (c_state == HEADER_M) {
c_state = (c=='<') ? HEADER_ARROW : HEADER_IDLE;
} else if (c_state == HEADER_ARROW) {
if (c > INBUF_SIZE) { // now we are expecting the payload size
c_state = HEADER_IDLE;
continue;
}
dataSize = c;
offset = 0;
checksum = 0;
indRX = 0;
checksum ^= c;
c_state = HEADER_SIZE; // the command is to follow
} else if (c_state == HEADER_SIZE) {
cmdMSP = c;
checksum ^= c;
c_state = HEADER_CMD;
} else if (c_state == HEADER_CMD && offset < dataSize) {
checksum ^= c;
inBuf[offset++] = c;
} else if (c_state == HEADER_CMD && offset >= dataSize) {
if (checksum == c) { // compare calculated and transferred checksum
evaluateCommand(); // we got a valid packet, evaluate it
}
c_state = HEADER_IDLE;
}
digitalWrite(13, LOW);
delay(1);
}
}
#endif