Skip to content

Commit 4cafee2

Browse files
authored
Merge pull request #192 from pkendall64/fix-binding
Fix binding behaviour
2 parents 0d3ea52 + 0ad9baa commit 4cafee2

File tree

2 files changed

+44
-23
lines changed

2 files changed

+44
-23
lines changed

src/Tx_main.cpp

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ void sendMSPViaEspnow(mspPacket_t *packet);
7272
// otherwise we get errors about invalid peer:
7373
// https://rntlab.com/question/espnow-peer-interface-is-invalid/
7474
esp_now_peer_info_t peerInfo;
75+
esp_now_peer_info_t bindingInfo;
7576
#endif
7677

7778
void RebootIntoWifi(wifi_service_t service = WIFI_SERVICE_UPDATE)
@@ -189,23 +190,6 @@ void HandleConfigMsg(mspPacket_t *packet)
189190

190191
void ProcessMSPPacketFromTX(mspPacket_t *packet)
191192
{
192-
if (packet->function == MSP_ELRS_BIND)
193-
{
194-
config.SetGroupAddress(packet->payload);
195-
DBG("MSP_ELRS_BIND = ");
196-
for (int i = 0; i < 6; i++)
197-
{
198-
DBG("%x", packet->payload[i]); // Debug prints
199-
DBG(",");
200-
}
201-
DBG(""); // Extra line for serial output readability
202-
config.Commit();
203-
// delay(500); // delay may not be required
204-
sendMSPViaEspnow(packet);
205-
// delay(500); // delay may not be required
206-
rebootTime = millis(); // restart to set SetSoftMACAddress
207-
}
208-
209193
switch (packet->function)
210194
{
211195
case MSP_SET_VTX_CONFIG:
@@ -215,35 +199,63 @@ void ProcessMSPPacketFromTX(mspPacket_t *packet)
215199
// transparently forward MSP packets via espnow to any subscribers
216200
sendMSPViaEspnow(packet);
217201
break;
202+
218203
case MSP_ELRS_SET_VRX_BACKPACK_WIFI_MODE:
219204
DBGLN("Processing MSP_ELRS_SET_VRX_BACKPACK_WIFI_MODE...");
220205
sendMSPViaEspnow(packet);
221206
break;
207+
222208
case MSP_ELRS_SET_TX_BACKPACK_WIFI_MODE:
223209
DBGLN("Processing MSP_ELRS_SET_TX_BACKPACK_WIFI_MODE...");
224210
RebootIntoWifi();
225211
break;
212+
226213
case MSP_ELRS_GET_BACKPACK_VERSION:
227214
DBGLN("Processing MSP_ELRS_GET_BACKPACK_VERSION...");
228215
SendVersionResponse();
229216
break;
217+
230218
case MSP_ELRS_BACKPACK_SET_HEAD_TRACKING:
231219
DBGLN("Processing MSP_ELRS_BACKPACK_SET_HEAD_TRACKING...");
232220
cachedHTPacket = *packet;
233221
cacheFull = true;
234222
sendMSPViaEspnow(packet);
235223
break;
224+
236225
case MSP_ELRS_BACKPACK_CRSF_TLM:
237226
DBGLN("Processing MSP_ELRS_BACKPACK_CRSF_TLM...");
238227
if (config.GetTelemMode() != BACKPACK_TELEM_MODE_OFF)
239228
{
240229
sendMSPViaEspnow(packet);
241230
}
242231
break;
232+
243233
case MSP_ELRS_BACKPACK_CONFIG:
244234
DBGLN("Processing MSP_ELRS_BACKPACK_CONFIG...");
245235
HandleConfigMsg(packet);
246236
break;
237+
238+
case MSP_ELRS_BIND:
239+
DBG("MSP_ELRS_BIND = ");
240+
for (int i = 0; i < 6; i++)
241+
{
242+
DBG("%x", packet->payload[i]); // Debug prints
243+
DBG(",");
244+
}
245+
DBG(""); // Extra line for serial output readability
246+
247+
// If the BIND address is different to our current one,
248+
// then we save it and reboot so it can take effect
249+
if (memcmp(packet->payload, config.GetGroupAddress(), 6) != 0)
250+
{
251+
config.SetGroupAddress(packet->payload);
252+
config.Commit();
253+
rebootTime = millis(); // restart to set SetSoftMACAddress
254+
return;
255+
}
256+
sendMSPViaEspnow(packet);
257+
break;
258+
247259
default:
248260
// transparently forward MSP packets via espnow to any subscribers
249261
sendMSPViaEspnow(packet);
@@ -348,12 +360,12 @@ RF_PRE_INIT()
348360

349361
void setup()
350362
{
351-
#ifdef DEBUG_LOG
352-
Serial1.begin(115200);
353-
Serial1.setDebugOutput(true);
354-
#endif
355-
Serial.begin(460800);
363+
#ifdef DEBUG_LOG
364+
LOGGING_UART.begin(115200);
365+
LOGGING_UART.setDebugOutput(true);
366+
#endif
356367
Serial.setRxBufferSize(4096);
368+
Serial.begin(460800);
357369

358370
options_init();
359371

@@ -401,6 +413,14 @@ void setup()
401413
DBGLN("ESP-NOW failed to add peer");
402414
return;
403415
}
416+
memcpy(bindingInfo.peer_addr, bindingAddress, 6);
417+
bindingInfo.channel = 0;
418+
bindingInfo.encrypt = false;
419+
if (esp_now_add_peer(&bindingInfo) != ESP_OK)
420+
{
421+
DBGLN("ESP-NOW failed to add binding peer");
422+
return;
423+
}
404424
#endif
405425

406426
esp_now_register_recv_cb(OnDataRecv);

targets/common.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ board_build.partitions = min_spiffs.csv
5959
upload_speed = 460800
6060
monitor_speed = 460800
6161
upload_resetmethod = nodemcu
62-
board_build.f_cpu = 240000000L
62+
board_build.f_cpu = 160000000L
6363
build_flags =
6464
-D PLATFORM_ESP32=1
65+
-D ARDUINO_USB_MODE=1
6566

6667
# ------------------------- COMMON ESP32S3 DEFINITIONS -----------------
6768
[env_common_esp32s3]

0 commit comments

Comments
 (0)