-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdevice_control.cpp
More file actions
438 lines (415 loc) · 17 KB
/
device_control.cpp
File metadata and controls
438 lines (415 loc) · 17 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#include "device_control.h"
#include "structs.h"
#include "touch_input.h"
#include <string.h>
#ifdef TARGET_NRF
#include <bluefruit.h>
extern "C" {
#include "nrf_soc.h"
}
extern "C" void bootloader_util_app_start(uint32_t start_addr);
#endif
#ifdef TARGET_ESP32
#include <esp_system.h>
#include "driver/gpio.h"
#include "esp32-hal-gpio.h"
#endif
extern uint8_t rebootFlag;
extern struct GlobalConfig globalConfig;
extern uint8_t activeLedInstance;
extern bool ledFlashActive;
extern uint8_t ledFlashPosition;
extern uint8_t dynamicreturndata[11];
extern uint8_t buttonStateCount;
extern volatile bool buttonEventPending;
extern volatile uint8_t lastChangedButtonIndex;
extern "C" uint8_t pinToButtonIndex[64];
void updatemsdata();
void cleanupDirectWriteState(bool refreshDisplay);
void sendResponse(uint8_t* response, uint8_t len);
void writeSerial(String message, bool newLine = true);
struct ButtonState {
uint8_t button_id;
uint8_t press_count;
volatile uint32_t last_press_time;
volatile uint8_t current_state;
uint8_t byte_index;
uint8_t pin;
uint8_t instance_index;
bool initialized;
uint8_t pin_offset;
bool inverted;
};
#define MAX_BUTTONS 32
extern ButtonState buttonStates[MAX_BUTTONS];
void connect_callback(uint16_t conn_handle) {
(void)conn_handle;
writeSerial("=== BLE CLIENT CONNECTED ===", true);
rebootFlag = 0;
updatemsdata();
}
void disconnect_callback(uint16_t conn_handle, uint8_t reason) {
(void)conn_handle;
(void)reason;
writeSerial("=== BLE CLIENT DISCONNECTED ===", true);
writeSerial("Disconnect reason: " + String(reason), true);
cleanupDirectWriteState(true);
}
void reboot(){
writeSerial("=== REBOOT COMMAND (0x000F) ===", true);
delay(100);
#ifdef TARGET_NRF
NVIC_SystemReset();
#endif
#ifdef TARGET_ESP32
esp_restart();
#endif
}
void handleLedActivate(uint8_t* data, uint16_t len) {
writeSerial("=== handleLedActivate START ===", true);
writeSerial("Command length: " + String(len) + " bytes", true);
if (len < 1) {
writeSerial("ERROR: LED activate command too short (len=" + String(len) + ", need at least 1 byte for instance)", true);
uint8_t errorResponse[] = {0xFF, 0x73, 0x01, 0x00};
sendResponse(errorResponse, sizeof(errorResponse));
return;
}
uint8_t ledInstance = data[0];
writeSerial("LED instance: " + String(ledInstance), true);
if (ledInstance >= globalConfig.led_count) {
writeSerial("ERROR: LED instance " + String(ledInstance) + " out of range (led_count=" + String(globalConfig.led_count) + ")", true);
uint8_t errorResponse[] = {0xFF, 0x73, 0x02, 0x00};
sendResponse(errorResponse, sizeof(errorResponse));
return;
}
struct LedConfig* led = &globalConfig.leds[ledInstance];
activeLedInstance = ledInstance;
uint8_t* ledcfg = led->reserved;
if (len >= 13) {
memcpy(ledcfg, data + 1, 12);
}
ledFlashActive = true;
ledFlashPosition = 0;
ledFlashLogic();
ledFlashActive = false;
uint8_t successResponse[] = {0x00, 0x73, 0x00, 0x00};
sendResponse(successResponse, sizeof(successResponse));
writeSerial("=== handleLedActivate END ===", true);
}
void processButtonEvents() {
if (buttonEventPending) {
buttonEventPending = false;
uint32_t currentTime = millis();
uint8_t changedButtonIndex = lastChangedButtonIndex;
lastChangedButtonIndex = 0xFF;
writeSerial("Button event pending: " + String(changedButtonIndex));
if (changedButtonIndex < MAX_BUTTONS && buttonStates[changedButtonIndex].initialized) {
ButtonState* btn = &buttonStates[changedButtonIndex];
if (btn->current_state == 1) {
bool resetCount = false;
if (btn->last_press_time == 0 || currentTime - btn->last_press_time > 5000) {
resetCount = true;
}
if (btn->last_press_time > 0) {
for (uint8_t j = 0; j < buttonStateCount; j++) {
if (j != changedButtonIndex && buttonStates[j].initialized &&
buttonStates[j].last_press_time > 0 &&
buttonStates[j].last_press_time > btn->last_press_time &&
(currentTime - buttonStates[j].last_press_time) < 5000) {
resetCount = true;
break;
}
}
}
if (resetCount) {
for (uint8_t j = 0; j < buttonStateCount; j++) {
if (buttonStates[j].initialized) {
buttonStates[j].press_count = 0;
}
}
btn->press_count = 1;
}
btn->last_press_time = currentTime;
}
}
if (changedButtonIndex < MAX_BUTTONS && buttonStates[changedButtonIndex].initialized) {
ButtonState* btn = &buttonStates[changedButtonIndex];
bool pinState = digitalRead(btn->pin);
bool logicalPressed = btn->inverted ? !pinState : pinState;
writeSerial("Pin state: " + String(pinState) + ", Logical pressed: " + String(logicalPressed) + ",inverted: " + String(btn->inverted));
uint8_t logicalState = logicalPressed ? 1 : 0;
btn->current_state = logicalState;
writeSerial("Button: " + String(btn->button_id) + ", Press count: " + String(btn->press_count) + ", Current state: " + String(btn->current_state));
uint8_t buttonData = (btn->button_id & 0x07) |
((btn->press_count & 0x0F) << 3) |
((btn->current_state & 0x01) << 7);
if (btn->byte_index < 11) {
dynamicreturndata[btn->byte_index] = buttonData;
}
}
updatemsdata();
}
}
void flashLed(uint8_t color, uint8_t brightness) {
if (activeLedInstance == 0xFF) {
for (uint8_t i = 0; i < globalConfig.led_count; i++) {
if (globalConfig.leds[i].led_type == 1) {
activeLedInstance = i;
break;
}
}
if (activeLedInstance == 0xFF) return;
}
struct LedConfig* led = &globalConfig.leds[activeLedInstance];
uint8_t ledRedPin = led->led_1_r;
uint8_t ledGreenPin = led->led_2_g;
uint8_t ledBluePin = led->led_3_b;
bool invertRed = (led->led_flags & 0x01) != 0;
bool invertGreen = (led->led_flags & 0x02) != 0;
bool invertBlue = (led->led_flags & 0x04) != 0;
uint8_t colorred = (color >> 5) & 0b00000111;
uint8_t colorgreen = (color >> 2) & 0b00000111;
uint8_t colorblue = color & 0b00000011;
for (uint16_t i = 0; i < brightness; i++) {
digitalWrite(ledRedPin, invertRed ? !(colorred >= 7) : (colorred >= 7));
digitalWrite(ledGreenPin, invertGreen ? !(colorgreen >= 7) : (colorgreen >= 7));
digitalWrite(ledBluePin, invertBlue ? !(colorblue >= 3) : (colorblue >= 3));
delayMicroseconds(100);
digitalWrite(ledRedPin, invertRed ? !(colorred >= 1) : (colorred >= 1));
digitalWrite(ledGreenPin, invertGreen ? !(colorgreen >= 1) : (colorgreen >= 1));
delayMicroseconds(100);
digitalWrite(ledRedPin, invertRed ? !(colorred >= 6) : (colorred >= 6));
digitalWrite(ledGreenPin, invertGreen ? !(colorgreen >= 6) : (colorgreen >= 6));
digitalWrite(ledBluePin, invertBlue ? !(colorblue >= 1) : (colorblue >= 1));
delayMicroseconds(100);
digitalWrite(ledRedPin, invertRed ? !(colorred >= 2) : (colorred >= 2));
digitalWrite(ledGreenPin, invertGreen ? !(colorgreen >= 2) : (colorgreen >= 2));
delayMicroseconds(100);
digitalWrite(ledRedPin, invertRed ? !(colorred >= 5) : (colorred >= 5));
digitalWrite(ledGreenPin, invertGreen ? !(colorgreen >= 5) : (colorgreen >= 5));
delayMicroseconds(100);
digitalWrite(ledRedPin, invertRed ? !(colorred >= 3) : (colorred >= 3));
digitalWrite(ledGreenPin, invertGreen ? !(colorgreen >= 3) : (colorgreen >= 3));
digitalWrite(ledBluePin, invertBlue ? !(colorblue >= 2) : (colorblue >= 2));
delayMicroseconds(100);
digitalWrite(ledRedPin, invertRed ? !(colorred >= 4) : (colorred >= 4));
digitalWrite(ledGreenPin, invertGreen ? !(colorgreen >= 4) : (colorgreen >= 4));
delayMicroseconds(100);
digitalWrite(ledRedPin, invertRed ? HIGH : LOW);
digitalWrite(ledGreenPin, invertGreen ? HIGH : LOW);
digitalWrite(ledBluePin, invertBlue ? HIGH : LOW);
}
}
void ledFlashLogic() {
writeSerial("=== ledFlashLogic START ===");
if (!ledFlashActive) return;
if (activeLedInstance == 0xFF) {
for (uint8_t i = 0; i < globalConfig.led_count; i++) {
if (globalConfig.leds[i].led_type == 1) {
activeLedInstance = i;
break;
}
}
if (activeLedInstance == 0xFF) return;
}
struct LedConfig* led = &globalConfig.leds[activeLedInstance];
uint8_t* ledcfg = led->reserved;
uint8_t brightness = ((ledcfg[0] >> 4) & 0x0F) + 1;
uint8_t mode = ledcfg[0] & 0x0F;
if (mode == 1) {
const uint8_t interloopdelayfactor = 100;
const uint8_t loopdelayfactor = 100;
uint8_t c1 = ledcfg[1], c2 = ledcfg[4], c3 = ledcfg[7];
uint8_t loop1delay = (ledcfg[2] >> 4) & 0x0F, loop2delay = (ledcfg[5] >> 4) & 0x0F, loop3delay = (ledcfg[8] >> 4) & 0x0F;
uint8_t loopcnt1 = ledcfg[2] & 0x0F, loopcnt2 = ledcfg[5] & 0x0F, loopcnt3 = ledcfg[8] & 0x0F;
uint8_t ildelay1 = ledcfg[3], ildelay2 = ledcfg[6], ildelay3 = ledcfg[9];
uint8_t grouprepeats = ledcfg[10] + 1;
while (ledFlashActive) {
if (ledFlashPosition >= grouprepeats && grouprepeats != 255) {
brightness = 0;
ledcfg[0] = 0x00;
ledFlashPosition = 0;
break;
}
for (int i = 0; i < loopcnt1; i++) { flashLed(c1, brightness); delay(loop1delay * loopdelayfactor); }
delay(ildelay1 * interloopdelayfactor);
for (int i = 0; i < loopcnt2; i++) { flashLed(c2, brightness); delay(loop2delay * loopdelayfactor); }
delay(ildelay2 * interloopdelayfactor);
for (int i = 0; i < loopcnt3; i++) { flashLed(c3, brightness); delay(loop3delay * loopdelayfactor); }
delay(ildelay3 * interloopdelayfactor);
ledFlashPosition++;
}
}
writeSerial("=== ledFlashLogic END ===");
}
#ifdef TARGET_ESP32
void IRAM_ATTR handleButtonISR(uint8_t buttonIndex) {
#else
void handleButtonISR(uint8_t buttonIndex) {
#endif
if (buttonIndex >= MAX_BUTTONS || !buttonStates[buttonIndex].initialized) return;
ButtonState* btn = &buttonStates[buttonIndex];
bool pinState = digitalRead(btn->pin);
bool pressed = btn->inverted ? !pinState : pinState;
uint8_t newState = pressed ? 1 : 0;
if (newState != btn->current_state) {
btn->current_state = newState;
lastChangedButtonIndex = buttonIndex;
if (pressed && btn->press_count < 15) btn->press_count++;
buttonEventPending = true;
}
}
#ifdef TARGET_ESP32
void IRAM_ATTR buttonISR(void* arg) {
uint8_t buttonIndex = (uint8_t)(uintptr_t)arg;
handleButtonISR(buttonIndex);
}
#elif defined(TARGET_NRF)
void buttonISRGeneric() {
for (uint8_t i = 0; i < buttonStateCount; i++) {
if (buttonStates[i].initialized) {
ButtonState* btn = &buttonStates[i];
bool pinState = digitalRead(btn->pin);
bool pressed = btn->inverted ? !pinState : pinState;
uint8_t newState = pressed ? 1 : 0;
if (newState != btn->current_state) {
handleButtonISR(i);
break;
}
}
}
}
#endif
void initButtons() {
writeSerial("=== Initializing Buttons ===");
buttonStateCount = 0;
for (uint8_t i = 0; i < MAX_BUTTONS; i++) {
buttonStates[i].initialized = false;
buttonStates[i].button_id = 0;
buttonStates[i].press_count = 0;
buttonStates[i].last_press_time = 0;
buttonStates[i].current_state = 0;
buttonStates[i].byte_index = 0xFF;
buttonStates[i].pin = 0xFF;
buttonStates[i].instance_index = 0xFF;
}
if (globalConfig.binary_input_count == 0) return;
for (uint8_t instanceIdx = 0; instanceIdx < globalConfig.binary_input_count; instanceIdx++) {
struct BinaryInputs* input = &globalConfig.binary_inputs[instanceIdx];
if (input->input_type != 1) continue;
if (input->button_data_byte_index > 10) continue;
uint8_t* instancePins[8] = {
&input->reserved_pin_1,&input->reserved_pin_2,&input->reserved_pin_3,&input->reserved_pin_4,
&input->reserved_pin_5,&input->reserved_pin_6,&input->reserved_pin_7,&input->reserved_pin_8
};
for (uint8_t pinIdx = 0; pinIdx < 8; pinIdx++) {
uint8_t pin = *instancePins[pinIdx];
if (pin == 0xFF) continue;
if (touch_input_gpio_is_touch_int(pin)) {
writeSerial("Button: skip pin " + String(pin) + " (reserved for GT911 INT)", true);
continue;
}
if (buttonStateCount >= MAX_BUTTONS) break;
ButtonState* btn = &buttonStates[buttonStateCount];
btn->button_id = (input->instance_number * 8) + pinIdx;
if (btn->button_id > 7) btn->button_id = btn->button_id % 8;
btn->byte_index = input->button_data_byte_index;
btn->pin = pin;
btn->instance_index = instanceIdx;
btn->press_count = 0;
btn->last_press_time = 0;
btn->pin_offset = pinIdx;
btn->inverted = (input->invert & (1 << pinIdx)) != 0;
pinMode(pin, INPUT);
bool hasPullup = (input->pullups & (1 << pinIdx)) != 0;
#ifdef TARGET_ESP32
bool hasPulldown = (input->pulldowns & (1 << pinIdx)) != 0;
if (hasPullup) pinMode(pin, INPUT_PULLUP);
else if (hasPulldown) pinMode(pin, INPUT_PULLDOWN);
#elif defined(TARGET_NRF)
if (hasPullup) pinMode(pin, INPUT_PULLUP);
#endif
delay(10);
bool initialPinState = digitalRead(pin);
bool initialPressed = btn->inverted ? !initialPinState : initialPinState;
btn->current_state = initialPressed ? 1 : 0;
#ifdef TARGET_ESP32
attachInterruptArg(pin, buttonISR, (void*)(uintptr_t)buttonStateCount, CHANGE);
#elif defined(TARGET_NRF)
attachInterrupt(pin, buttonISRGeneric, CHANGE);
#endif
btn->initialized = true;
buttonStateCount++;
}
}
if (buttonStateCount > 0) {
#ifdef TARGET_ESP32
for (uint8_t i = 0; i < buttonStateCount; i++) {
if (buttonStates[i].initialized) {
gpio_intr_disable((gpio_num_t)digitalPinToGPIONumber(buttonStates[i].pin));
}
}
#elif defined(TARGET_NRF)
for (uint8_t i = 0; i < buttonStateCount; i++) {
if (buttonStates[i].initialized) {
detachInterrupt(buttonStates[i].pin);
}
}
#endif
delay(50);
buttonEventPending = false;
lastChangedButtonIndex = 0xFF;
for (uint8_t i = 0; i < buttonStateCount; i++) {
if (!buttonStates[i].initialized) continue;
ButtonState* btn = &buttonStates[i];
bool pinState = digitalRead(btn->pin);
bool initialPressed = btn->inverted ? !pinState : pinState;
btn->current_state = initialPressed ? 1 : 0;
btn->press_count = 0;
btn->last_press_time = 0;
}
#ifdef TARGET_ESP32
for (uint8_t i = 0; i < buttonStateCount; i++) {
if (buttonStates[i].initialized) {
gpio_intr_enable((gpio_num_t)digitalPinToGPIONumber(buttonStates[i].pin));
}
}
#elif defined(TARGET_NRF)
for (uint8_t i = 0; i < buttonStateCount; i++) {
if (!buttonStates[i].initialized) continue;
uint8_t pin = buttonStates[i].pin;
attachInterrupt(pin, buttonISRGeneric, CHANGE);
}
#endif
}
}
void enterDFUMode() {
writeSerial("=== ENTER DFU MODE COMMAND (0x0051) ===", true);
#ifdef TARGET_NRF
writeSerial("Preparing to enter DFU bootloader mode...", true);
Bluefruit.Advertising.restartOnDisconnect(false);
if (Bluefruit.connected()) {
writeSerial("Disconnecting BLE...", true);
Bluefruit.disconnect(Bluefruit.connHandle());
delay(100);
}
sd_power_gpregret_clr(0, 0xFF);
sd_power_gpregret_set(0, 0xB1);
sd_softdevice_disable();
NVIC->ICER[0] = 0xFFFFFFFF;
NVIC->ICPR[0] = 0xFFFFFFFF;
#if defined(__NRF_NVIC_ISER_COUNT) && __NRF_NVIC_ISER_COUNT == 2
NVIC->ICER[1] = 0xFFFFFFFF;
NVIC->ICPR[1] = 0xFFFFFFFF;
#endif
sd_softdevice_vector_table_base_set(NRF_UICR->NRFFW[0]);
__set_CONTROL(0);
bootloader_util_app_start(NRF_UICR->NRFFW[0]);
while (1) {}
#endif
#ifdef TARGET_ESP32
writeSerial("ESP32: Rebooting (OTA typically handled via WiFi)", true);
delay(100);
esp_restart();
#endif
}