Skip to content

Commit 53fdc19

Browse files
committed
Add ESPNOW wireless trainer mode
1 parent 2b4872c commit 53fdc19

5 files changed

Lines changed: 952 additions & 27 deletions

File tree

include/trainer_api.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
3+
#include <stdint.h>
4+
5+
#if defined(PLATFORM_ESP32) || defined(PLATFORM_ESP8266)
6+
7+
bool TrainerIsAvailable();
8+
bool TrainerIsPaired();
9+
bool TrainerIsPairing();
10+
const uint8_t *TrainerGetPeerMac();
11+
bool TrainerStartPairing();
12+
void TrainerForgetPeer();
13+
14+
#endif

lib/MSP/msptypes.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,11 @@
5656
#define MSP_ELRS_BACKPACK_GET_VERSION 0x0381 // get the bacpack firmware version
5757
#define MSP_ELRS_BACKPACK_GET_STATUS 0x0382 // get the status of the backpack
5858
#define MSP_ELRS_BACKPACK_SET_PTR 0x0383 // forwarded back to TX backpack
59+
60+
#define MSP_ELRS_BACKPACK_CONFIG_TRAINER_MODE 0x32
61+
62+
#define MSP_ELRS_BACKPACK_TRAINER_CHANNELS 0x0390
63+
#define MSP_ELRS_BACKPACK_TRAINER_PAIR_REQ 0x0391
64+
#define MSP_ELRS_BACKPACK_TRAINER_PAIR_ACK 0x0392
65+
#define MSP_ELRS_BACKPACK_TRAINER_FORGET 0x0393
66+
#define MSP_ELRS_BACKPACK_TRAINER_STATUS 0x0394

lib/config/config.cpp

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,28 @@ TxBackpackConfig::Load()
1212
// Check if version number matches
1313
if (m_config.version != (uint32_t)(TX_BACKPACK_CONFIG_VERSION | TX_BACKPACK_CONFIG_MAGIC))
1414
{
15-
// If not, revert to defaults for this version
16-
DBGLN("EEPROM version mismatch! Resetting to defaults...");
17-
SetDefaults();
15+
// If a known previous version is detected, migrate in place; otherwise revert to defaults
16+
if (m_config.version == (uint32_t)(4U | TX_BACKPACK_CONFIG_MAGIC))
17+
{
18+
m_config.version = TX_BACKPACK_CONFIG_VERSION | TX_BACKPACK_CONFIG_MAGIC;
19+
memset(m_config.trainerPeerMac, 0, 6);
20+
m_config.trainerMode = TRAINER_MODE_OFF;
21+
m_modified = true;
22+
Commit();
23+
}
24+
else if (m_config.version == (uint32_t)(5U | TX_BACKPACK_CONFIG_MAGIC))
25+
{
26+
// Version 5 → 6: added trainerMode field
27+
m_config.version = TX_BACKPACK_CONFIG_VERSION | TX_BACKPACK_CONFIG_MAGIC;
28+
m_config.trainerMode = TRAINER_MODE_OFF;
29+
m_modified = true;
30+
Commit();
31+
}
32+
else
33+
{
34+
DBGLN("EEPROM version mismatch! Resetting to defaults...");
35+
SetDefaults();
36+
}
1837
}
1938
}
2039

@@ -54,6 +73,8 @@ TxBackpackConfig::SetDefaults()
5473
m_config.wifiService = WIFI_SERVICE_UPDATE;
5574
m_config.mavlinkListenPort = 14555; // Default MavLink listen port
5675
m_config.mavlinkSendPort = 14550; // Default MavLink send port
76+
memset(m_config.trainerPeerMac, 0, 6);
77+
m_config.trainerMode = TRAINER_MODE_OFF;
5778
m_modified = true;
5879
Commit();
5980
}
@@ -99,6 +120,7 @@ TxBackpackConfig::SetTelemMode(telem_mode_t mode)
99120
m_config.telemMode = mode;
100121
m_modified = true;
101122
}
123+
102124
void
103125
TxBackpackConfig::SetMavlinkListenPort(uint16_t port)
104126
{
@@ -112,6 +134,38 @@ TxBackpackConfig::SetMavlinkSendPort(uint16_t port)
112134
m_config.mavlinkSendPort = port;
113135
m_modified = true;
114136
}
137+
138+
bool
139+
TxBackpackConfig::IsTrainerPaired() const
140+
{
141+
for (int i = 0; i < 6; i++)
142+
{
143+
if (m_config.trainerPeerMac[i] != 0) return true;
144+
}
145+
return false;
146+
}
147+
148+
void
149+
TxBackpackConfig::SetTrainerPeer(const uint8_t mac[6])
150+
{
151+
memcpy(m_config.trainerPeerMac, mac, 6);
152+
m_modified = true;
153+
}
154+
155+
void
156+
TxBackpackConfig::ClearTrainerPeer()
157+
{
158+
memset(m_config.trainerPeerMac, 0, 6);
159+
m_modified = true;
160+
}
161+
162+
void
163+
TxBackpackConfig::SetTrainerMode(trainer_mode_t mode)
164+
{
165+
m_config.trainerMode = mode;
166+
m_modified = true;
167+
}
168+
115169
#endif
116170

117171
/////////////////////////////////////////////////////

lib/config/config.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#define VRX_BACKPACK_CONFIG_MAGIC (0b10U << 30)
88
#define TIMER_BACKPACK_CONFIG_MAGIC (0b11U << 30)
99

10-
#define TX_BACKPACK_CONFIG_VERSION 4
10+
#define TX_BACKPACK_CONFIG_VERSION 6
1111
#define VRX_BACKPACK_CONFIG_VERSION 5
1212
#define TIMER_BACKPACK_CONFIG_VERSION 3
1313

@@ -24,6 +24,12 @@ typedef enum {
2424
BACKPACK_TELEM_MODE_BLUETOOTH,
2525
} telem_mode_t;
2626

27+
typedef enum {
28+
TRAINER_MODE_OFF,
29+
TRAINER_MODE_MASTER,
30+
TRAINER_MODE_SLAVE,
31+
} trainer_mode_t;
32+
2733
#if defined(TARGET_TX_BACKPACK)
2834

2935
typedef struct {
@@ -36,6 +42,8 @@ typedef struct {
3642
telem_mode_t telemMode;
3743
uint16_t mavlinkListenPort;
3844
uint16_t mavlinkSendPort;
45+
uint8_t trainerPeerMac[6];
46+
trainer_mode_t trainerMode; // persisted so it survives TLM_MODE reboots
3947
} tx_backpack_config_t;
4048

4149
class TxBackpackConfig
@@ -54,6 +62,9 @@ class TxBackpackConfig
5462
telem_mode_t GetTelemMode() { return m_config.telemMode; }
5563
uint16_t GetMavlinkListenPort() const { return m_config.mavlinkListenPort; }
5664
uint16_t GetMavlinkSendPort() const { return m_config.mavlinkSendPort; }
65+
const uint8_t *GetTrainerPeerMac() const { return m_config.trainerPeerMac; }
66+
bool IsTrainerPaired() const;
67+
trainer_mode_t GetTrainerMode() const { return m_config.trainerMode; }
5768

5869
// Setters
5970
void SetStorageProvider(ELRS_EEPROM *eeprom);
@@ -66,6 +77,9 @@ class TxBackpackConfig
6677
void SetTelemMode(telem_mode_t mode);
6778
void SetMavlinkListenPort(uint16_t port);
6879
void SetMavlinkSendPort(uint16_t port);
80+
void SetTrainerPeer(const uint8_t mac[6]);
81+
void ClearTrainerPeer();
82+
void SetTrainerMode(trainer_mode_t mode);
6983

7084
private:
7185
tx_backpack_config_t m_config;
@@ -225,4 +239,4 @@ class TimerBackpackConfig
225239

226240
extern TimerBackpackConfig config;
227241

228-
#endif
242+
#endif

0 commit comments

Comments
 (0)