-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathTimer_main.cpp
More file actions
562 lines (493 loc) · 13.8 KB
/
Timer_main.cpp
File metadata and controls
562 lines (493 loc) · 13.8 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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
#include <Arduino.h>
#if defined(PLATFORM_ESP8266)
#include <espnow.h>
#include <ESP8266WiFi.h>
#elif defined(PLATFORM_ESP32)
#include <esp_now.h>
#include <esp_wifi.h>
#include <WiFi.h>
#endif
#include "msp.h"
#include "msptypes.h"
#include "logging.h"
#include "config.h"
#include "common.h"
#include "options.h"
#include "helpers.h"
#include "device.h"
#include "devWIFI.h"
#include "devButton.h"
#include "devLED.h"
/////////// DEFINES ///////////
#define BINDING_TIMEOUT 30000
#define NO_BINDING_TIMEOUT 120000
#define BINDING_LED_PAUSE 1000
/////////// GLOBALS ///////////
uint8_t sendAddress[6];
const uint8_t version[] = {LATEST_VERSION};
connectionState_e connectionState = starting;
unsigned long bindingStart = 0;
unsigned long rebootTime = 0;
bool cacheFull = false;
bool sendCached = false;
bool tempUID = false;
bool isBinding = false;
bool espnowCTS = true;
bool sendSuccess = false;
device_t *ui_devices[] = {
#ifdef PIN_LED
&LED_device,
#endif
#ifdef PIN_BUTTON
&Button_device,
#endif
&WIFI_device,
};
#if defined(PLATFORM_ESP32)
int maxAttempt = 5;
int sendAttempt = 0;
QueueHandle_t rxqueue = xQueueCreate(10, sizeof(mspPacket_t));
QueueHandle_t txqueue = xQueueCreate(500, sizeof(mspPacket_t));
SemaphoreHandle_t semaphore = xSemaphoreCreateBinary();
#endif
/////////// CLASS OBJECTS ///////////
MSP msp;
ELRS_EEPROM eeprom;
TimerBackpackConfig config;
mspPacket_t cachedVTXPacket;
mspPacket_t cachedHTPacket;
/////////// FUNCTION DEFS ///////////
void ProcessMSPPacketFromTimer(mspPacket_t *packet, uint32_t now);
int sendMSPViaEspnow(mspPacket_t *packet);
void resetBootCounter();
void registerPeer(uint8_t* address);
/////////////////////////////////////
#if defined(PLATFORM_ESP32)
// This seems to need to be global, as per this page,
// otherwise we get errors about invalid peer:
// https://rntlab.com/question/espnow-peer-interface-is-invalid/
esp_now_peer_info_t peerInfo;
#endif
void RebootIntoWifi(wifi_service_t service = WIFI_SERVICE_UPDATE)
{
DBGLN("Rebooting into wifi update mode...");
config.SetStartWiFiOnBoot(true);
config.Commit();
rebootTime = millis();
}
void ProcessMSPPacketFromPeer(mspPacket_t *packet)
{
if (connectionState == binding)
{
DBGLN("Processing Binding Packet...");
if (packet->function == MSP_ELRS_BIND)
{
config.SetGroupAddress(packet->payload);
DBGLN("MSP_ELRS_BIND MAC = ");
for (int i = 0; i < 6; i++)
{
DBG("%x", packet->payload[i]); // Debug prints
DBG(",");
}
DBG(""); // Extra line for serial output readability
resetBootCounter();
connectionState = running;
}
return;
}
switch (packet->function) {
case MSP_ELRS_BACKPACK_SET_RECORDING_STATE: {
mspPacket_t out;
out.reset();
out.makeResponse();
out.function = MSP_ELRS_BACKPACK_SET_RECORDING_STATE;
msp.sendPacket(packet, &Serial);
}
}
}
#if defined(PLATFORM_ESP32)
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status)
{
xSemaphoreTake(semaphore, (TickType_t)512);
espnowCTS = true;
if (status == ESP_NOW_SEND_SUCCESS)
sendSuccess = true;
else
sendSuccess = false;
xSemaphoreGive(semaphore);
}
#endif
// espnow on-receive callback
#if defined(PLATFORM_ESP8266)
void OnDataRecv(uint8_t * mac_addr, uint8_t *data, uint8_t data_len)
#elif defined(PLATFORM_ESP32)
void OnDataRecv(const uint8_t * mac_addr, const uint8_t *data, int data_len)
#endif
{
DBGLN("ESP NOW DATA:");
for(int i = 0; i < data_len; i++)
{
if (msp.processReceivedByte(data[i]))
{
// Finished processing a complete packet
// Only process packets from a bound MAC address
if (connectionState == binding ||
(
firmwareOptions.uid[0] == mac_addr[0] &&
firmwareOptions.uid[1] == mac_addr[1] &&
firmwareOptions.uid[2] == mac_addr[2] &&
firmwareOptions.uid[3] == mac_addr[3] &&
firmwareOptions.uid[4] == mac_addr[4]
)
)
{
#if defined(PLATFORM_ESP8266)
ProcessMSPPacketFromTimer(msp.getReceivedPacket(), millis());
#elif defined(PLATFORM_ESP32)
xQueueSend(rxqueue, msp.getReceivedPacket(), (TickType_t)1024);
#endif
}
msp.markPacketReceived();
}
}
blinkLED();
}
void SendVersionResponse()
{
mspPacket_t out;
out.reset();
out.makeResponse();
out.function = MSP_ELRS_GET_BACKPACK_VERSION;
for (size_t i = 0 ; i < sizeof(version) ; i++)
{
out.addByte(version[i]);
}
msp.sendPacket(&out, &Serial);
}
void SendInProgressResponse()
{
mspPacket_t out;
const uint8_t *response = (const uint8_t *)"P";
out.reset();
out.makeResponse();
out.function = MSP_ELRS_BACKPACK_SET_MODE;
for (uint32_t i = 0 ; i < 1 ; i++)
{
out.addByte(response[i]);
}
msp.sendPacket(&out, &Serial);
}
void ProcessMSPPacketFromTimer(mspPacket_t *packet, uint32_t now)
{
if (connectionState == binding)
{
DBGLN("Processing Binding Packet...");
if (packet->function == MSP_ELRS_BIND)
{
config.SetGroupAddress(packet->payload);
DBGLN("MSP_ELRS_BIND MAC = ");
for (int i = 0; i < 6; i++)
{
DBG("%x", packet->payload[i]); // Debug prints
DBG(",");
}
DBG(""); // Extra line for serial output readability
resetBootCounter();
connectionState = running;
}
return;
}
switch (packet->function)
{
case MSP_ELRS_BACKPACK_SET_MODE:
{
if (packet->payloadSize == 1)
{
if (packet->payload[0] == 'B')
{
DBGLN("Enter binding mode...");
bindingStart = now;
connectionState = binding;
isBinding = true;
}
else if (packet->payload[0] == 'W')
{
DBGLN("Enter WIFI mode...");
connectionState = wifiUpdate;
devicesTriggerEvent();
}
SendInProgressResponse();
}
}
case MSP_ELRS_GET_BACKPACK_VERSION:
DBGLN("Processing MSP_ELRS_GET_BACKPACK_VERSION...");
SendVersionResponse();
break;
case MSP_ELRS_SET_SEND_UID:
DBGLN("Processing MSP_ELRS_SET_SEND_UID...");
{
uint8_t function = packet->readByte();
// Unregister current peer
esp_now_del_peer(sendAddress);
// Set target send address
if (function == 0x01)
{
uint8_t receivedAddress[6];
receivedAddress[0] = packet->readByte();
receivedAddress[1] = packet->readByte();
receivedAddress[2] = packet->readByte();
receivedAddress[3] = packet->readByte();
receivedAddress[4] = packet->readByte();
receivedAddress[5] = packet->readByte();
// Register new peer address
registerPeer(receivedAddress);
// Set Send address for new target
memset(&sendAddress, 0, sizeof(sendAddress));
memcpy(sendAddress, receivedAddress, 6);
}
// Return to bound send address
else
{
// Re-register stored address
registerPeer(firmwareOptions.uid);
// Set Send address for normal target
memset(&sendAddress, 0, sizeof(sendAddress));
memcpy(sendAddress, firmwareOptions.uid, 6);
}
// Configure MAC address
#if defined(PLATFORM_ESP8266)
wifi_set_macaddr(STATION_IF, sendAddress);
#elif defined(PLATFORM_ESP32)
esp_wifi_set_mac(WIFI_IF_STA, sendAddress);
#endif
break;
}
default:
// transparently forward MSP packets via espnow to any subscribers
sendMSPViaEspnow(packet);
break;
}
}
int sendMSPViaEspnow(mspPacket_t *packet)
{
int esp_err = -1;
uint8_t packetSize = msp.getTotalPacketSize(packet);
uint8_t nowDataOutput[packetSize];
uint8_t result = msp.convertToByteArray(packet, nowDataOutput);
if (!result)
{
// packet could not be converted to array, bail out
return esp_err;
}
esp_err = esp_now_send(sendAddress, (uint8_t *) &nowDataOutput, packetSize);
blinkLED();
return esp_err;
}
void SetSoftMACAddress()
{
if (!firmwareOptions.hasUID)
{
memcpy(firmwareOptions.uid, config.GetGroupAddress(), 6);
}
DBG("EEPROM MAC = ");
for (int i = 0; i < 6; i++)
{
DBG("%x", firmwareOptions.uid[i]); // Debug prints
DBG(",");
}
DBGLN(""); // Extra line for serial output readability
// MAC address can only be set with unicast, so first byte must be even, not odd
firmwareOptions.uid[0] = firmwareOptions.uid[0] & ~0x01;
// Set MAC address to be specific for type of device
firmwareOptions.uid[5] = TIMER_BACKPACK_TYPE_ID;
WiFi.mode(WIFI_STA);
#if defined(PLATFORM_ESP8266)
WiFi.setOutputPower(20.5);
#elif defined(PLATFORM_ESP32)
WiFi.setTxPower(WIFI_POWER_19_5dBm);
#endif
WiFi.begin("network-name", "pass-to-network", 1);
WiFi.disconnect();
// Soft-set the MAC address to the passphrase UID for binding
#if defined(PLATFORM_ESP8266)
wifi_set_macaddr(STATION_IF, firmwareOptions.uid);
#elif defined(PLATFORM_ESP32)
esp_wifi_set_mac(WIFI_IF_STA, firmwareOptions.uid);
esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N | WIFI_PROTOCOL_LR);
#endif
}
void resetBootCounter()
{
config.SetBootCount(0);
config.Commit();
}
void registerPeer(uint8_t* address){
#if defined(PLATFORM_ESP8266)
esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
esp_now_add_peer(address, ESP_NOW_ROLE_COMBO, 1, NULL, 0);
#elif defined(PLATFORM_ESP32)
memset(&peerInfo, 0, sizeof(peerInfo));
memcpy(peerInfo.peer_addr, address, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
if (esp_now_add_peer(&peerInfo) != ESP_OK)
{
DBGLN("ESP-NOW failed to add peer");
return;
}
#endif
}
bool BindingExpired(uint32_t now)
{
return (connectionState == binding) && ((now - bindingStart) > NO_BINDING_TIMEOUT);
}
#if defined(PLATFORM_ESP8266)
// Called from core's user_rf_pre_init() function (which is called by SDK) before setup()
RF_PRE_INIT()
{
// Set whether the chip will do RF calibration or not when power up.
// I believe the Arduino core fakes this (byte 114 of phy_init_data.bin)
// to be 1, but the TX power calibration can pull over 300mA which can
// lock up receivers built with a underspeced LDO (such as the EP2 "SDG")
// Option 2 is just VDD33 measurement
#if defined(RF_CAL_MODE)
system_phy_set_powerup_option(RF_CAL_MODE);
#else
system_phy_set_powerup_option(2);
#endif
}
#endif
void setup()
{
#ifdef DEBUG_LOG
Serial1.begin(115200);
Serial1.setDebugOutput(true);
#endif
Serial.begin(460800);
options_init();
eeprom.Begin();
config.SetStorageProvider(&eeprom);
config.Load();
devicesInit(ui_devices, ARRAY_SIZE(ui_devices));
#ifdef DEBUG_ELRS_WIFI
config.SetStartWiFiOnBoot(true);
#endif
if (config.GetStartWiFiOnBoot())
{
config.SetStartWiFiOnBoot(false);
config.Commit();
connectionState = wifiUpdate;
devicesTriggerEvent();
}
else
{
SetSoftMACAddress();
if (esp_now_init() != 0)
{
DBGLN("Error initializing ESP-NOW");
rebootTime = millis();
}
esp_now_register_recv_cb(OnDataRecv);
#if defined(PLATFORM_ESP32)
esp_now_register_send_cb(OnDataSent);
xSemaphoreGive(semaphore);
#endif
memcpy(sendAddress, firmwareOptions.uid, 6);
}
devicesStart();
if (connectionState == starting)
{
connectionState = running;
}
DBGLN("Setup completed");
}
void loop()
{
uint32_t now = millis();
devicesUpdate(now);
if (BindingExpired(now))
{
connectionState = running;
isBinding = false;
DBGLN("Bind timeout");
}
if (isBinding && connectionState == running)
{
DBGLN("Bind completed");
isBinding = false;
}
if (connectionState == wifiUpdate)
{
return;
}
if (BindingExpired(now))
{
connectionState = running;
}
while (Serial.available())
{
uint8_t c = Serial.read();
if (msp.processReceivedByte(c))
{
#if defined(PLATFORM_ESP8266)
ProcessMSPPacketFromTimer(msp.getReceivedPacket(), now);
#elif defined(PLATFORM_ESP32)
xQueueSend(txqueue, msp.getReceivedPacket(), (TickType_t)1024);
#endif
msp.markPacketReceived();
}
}
// If the reboot time is set and the current time is past the reboot time then reboot.
#if defined(PLATFORM_ESP8266)
if (rebootTime != 0 && now > rebootTime)
ESP.restart();
#elif defined(PLATFORM_ESP32)
if (rebootTime != 0 && now > rebootTime && uxQueueMessagesWaiting(txqueue) == 0 && uxQueueMessagesWaiting(rxqueue) == 0)
ESP.restart();
#endif
#if defined(PLATFORM_ESP32)
// Process packets in sendQueue
xSemaphoreTake(semaphore, (TickType_t)512);
if (uxQueueMessagesWaiting(txqueue) > 0 && espnowCTS)
{
mspPacket_t packet;
xQueuePeek(txqueue, &packet, (TickType_t)512);
uint16_t function = packet.function;
if (function == MSP_ELRS_GET_BACKPACK_VERSION ||
function == MSP_ELRS_BACKPACK_SET_MODE ||
function == MSP_ELRS_SET_SEND_UID)
{
xSemaphoreGive(semaphore);
ProcessMSPPacketFromTimer(&packet, now);
xQueueReceive(txqueue, &packet, (TickType_t)512);
}
else
{
if (++sendAttempt >= maxAttempt || sendSuccess)
{
espnowCTS = false;
xSemaphoreGive(semaphore);
sendAttempt = 0;
ProcessMSPPacketFromTimer(&packet, now);
xQueueReceive(txqueue, &packet, (TickType_t)512);
}
else
{
espnowCTS = false;
xSemaphoreGive(semaphore);
ProcessMSPPacketFromTimer(&packet, now);
}
}
}
else
{
xSemaphoreGive(semaphore);
}
if (uxQueueMessagesWaiting(rxqueue) > 0 && Serial.availableForWrite() == 128)
{
mspPacket_t rxPacket;
if (xQueueReceive(rxqueue, &rxPacket, (TickType_t)512) == pdTRUE)
ProcessMSPPacketFromPeer(&rxPacket);
}
#endif
}