Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions include/trainer_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include <stdint.h>

#if defined(PLATFORM_ESP32) || defined(PLATFORM_ESP8266)

bool TrainerIsAvailable();
bool TrainerIsPaired();
bool TrainerIsPairing();
const uint8_t *TrainerGetPeerMac();
bool TrainerStartPairing();
void TrainerForgetPeer();

#endif
8 changes: 8 additions & 0 deletions lib/MSP/msptypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,11 @@
#define MSP_ELRS_BACKPACK_GET_VERSION 0x0381 // get the bacpack firmware version
#define MSP_ELRS_BACKPACK_GET_STATUS 0x0382 // get the status of the backpack
#define MSP_ELRS_BACKPACK_SET_PTR 0x0383 // forwarded back to TX backpack

#define MSP_ELRS_BACKPACK_CONFIG_TRAINER_MODE 0x32

#define MSP_ELRS_BACKPACK_TRAINER_CHANNELS 0x0390
#define MSP_ELRS_BACKPACK_TRAINER_PAIR_REQ 0x0391
#define MSP_ELRS_BACKPACK_TRAINER_PAIR_ACK 0x0392
#define MSP_ELRS_BACKPACK_TRAINER_FORGET 0x0393
#define MSP_ELRS_BACKPACK_TRAINER_STATUS 0x0394
60 changes: 57 additions & 3 deletions lib/config/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,28 @@ TxBackpackConfig::Load()
// Check if version number matches
if (m_config.version != (uint32_t)(TX_BACKPACK_CONFIG_VERSION | TX_BACKPACK_CONFIG_MAGIC))
{
// If not, revert to defaults for this version
DBGLN("EEPROM version mismatch! Resetting to defaults...");
SetDefaults();
// If a known previous version is detected, migrate in place; otherwise revert to defaults
if (m_config.version == (uint32_t)(4U | TX_BACKPACK_CONFIG_MAGIC))
{
m_config.version = TX_BACKPACK_CONFIG_VERSION | TX_BACKPACK_CONFIG_MAGIC;
memset(m_config.trainerPeerMac, 0, 6);
m_config.trainerMode = TRAINER_MODE_OFF;
m_modified = true;
Commit();
}
else if (m_config.version == (uint32_t)(5U | TX_BACKPACK_CONFIG_MAGIC))
{
// Version 5 → 6: added trainerMode field
m_config.version = TX_BACKPACK_CONFIG_VERSION | TX_BACKPACK_CONFIG_MAGIC;
m_config.trainerMode = TRAINER_MODE_OFF;
m_modified = true;
Commit();
}
else
{
DBGLN("EEPROM version mismatch! Resetting to defaults...");
SetDefaults();
}
}
}

Expand Down Expand Up @@ -54,6 +73,8 @@ TxBackpackConfig::SetDefaults()
m_config.wifiService = WIFI_SERVICE_UPDATE;
m_config.mavlinkListenPort = 14555; // Default MavLink listen port
m_config.mavlinkSendPort = 14550; // Default MavLink send port
memset(m_config.trainerPeerMac, 0, 6);
m_config.trainerMode = TRAINER_MODE_OFF;
m_modified = true;
Commit();
}
Expand Down Expand Up @@ -99,6 +120,7 @@ TxBackpackConfig::SetTelemMode(telem_mode_t mode)
m_config.telemMode = mode;
m_modified = true;
}

void
TxBackpackConfig::SetMavlinkListenPort(uint16_t port)
{
Expand All @@ -112,6 +134,38 @@ TxBackpackConfig::SetMavlinkSendPort(uint16_t port)
m_config.mavlinkSendPort = port;
m_modified = true;
}

bool
TxBackpackConfig::IsTrainerPaired() const
{
for (int i = 0; i < 6; i++)
{
if (m_config.trainerPeerMac[i] != 0) return true;
}
return false;
}

void
TxBackpackConfig::SetTrainerPeer(const uint8_t mac[6])
{
memcpy(m_config.trainerPeerMac, mac, 6);
m_modified = true;
}

void
TxBackpackConfig::ClearTrainerPeer()
{
memset(m_config.trainerPeerMac, 0, 6);
m_modified = true;
}

void
TxBackpackConfig::SetTrainerMode(trainer_mode_t mode)
{
m_config.trainerMode = mode;
m_modified = true;
}

#endif

/////////////////////////////////////////////////////
Expand Down
18 changes: 16 additions & 2 deletions lib/config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#define VRX_BACKPACK_CONFIG_MAGIC (0b10U << 30)
#define TIMER_BACKPACK_CONFIG_MAGIC (0b11U << 30)

#define TX_BACKPACK_CONFIG_VERSION 4
#define TX_BACKPACK_CONFIG_VERSION 6
#define VRX_BACKPACK_CONFIG_VERSION 5
#define TIMER_BACKPACK_CONFIG_VERSION 3

Expand All @@ -24,6 +24,12 @@ typedef enum {
BACKPACK_TELEM_MODE_BLUETOOTH,
} telem_mode_t;

typedef enum {
TRAINER_MODE_OFF,
TRAINER_MODE_MASTER,
TRAINER_MODE_SLAVE,
} trainer_mode_t;

#if defined(TARGET_TX_BACKPACK)

typedef struct {
Expand All @@ -36,6 +42,8 @@ typedef struct {
telem_mode_t telemMode;
uint16_t mavlinkListenPort;
uint16_t mavlinkSendPort;
uint8_t trainerPeerMac[6];
trainer_mode_t trainerMode; // persisted so it survives TLM_MODE reboots
} tx_backpack_config_t;

class TxBackpackConfig
Expand All @@ -54,6 +62,9 @@ class TxBackpackConfig
telem_mode_t GetTelemMode() { return m_config.telemMode; }
uint16_t GetMavlinkListenPort() const { return m_config.mavlinkListenPort; }
uint16_t GetMavlinkSendPort() const { return m_config.mavlinkSendPort; }
const uint8_t *GetTrainerPeerMac() const { return m_config.trainerPeerMac; }
bool IsTrainerPaired() const;
trainer_mode_t GetTrainerMode() const { return m_config.trainerMode; }

// Setters
void SetStorageProvider(ELRS_EEPROM *eeprom);
Expand All @@ -66,6 +77,9 @@ class TxBackpackConfig
void SetTelemMode(telem_mode_t mode);
void SetMavlinkListenPort(uint16_t port);
void SetMavlinkSendPort(uint16_t port);
void SetTrainerPeer(const uint8_t mac[6]);
void ClearTrainerPeer();
void SetTrainerMode(trainer_mode_t mode);

private:
tx_backpack_config_t m_config;
Expand Down Expand Up @@ -225,4 +239,4 @@ class TimerBackpackConfig

extern TimerBackpackConfig config;

#endif
#endif
Loading