* new ESPNOWRadio driver

* refactored the examples/*/main.cpp modules, moving radio specifics to variants/*/target modules
* new Generic_ESPNOW_* target envs
This commit is contained in:
Scott Powell 2025-03-27 19:34:16 +11:00
parent 9c165add61
commit 2224bddcb5
39 changed files with 568 additions and 249 deletions

View file

@ -7,9 +7,6 @@
#include <SPIFFS.h>
#endif
#define RADIOLIB_STATIC_ONLY 1
#include <RadioLib.h>
#include <helpers/RadioLibWrappers.h>
#include <helpers/ArduinoHelpers.h>
#include <helpers/StaticPoolPacketManager.h>
#include <helpers/SimpleMeshTables.h>
@ -197,7 +194,6 @@ struct NodePrefs { // persisted to file
class MyMesh : public BaseChatMesh {
FILESYSTEM* _fs;
RADIO_CLASS* _phy;
IdentityStore* _identity_store;
NodePrefs _prefs;
uint32_t pending_login;
@ -229,9 +225,9 @@ class MyMesh : public BaseChatMesh {
AckTableEntry expected_ack_table[EXPECTED_ACK_TABLE_SIZE]; // circular table
int next_ack_idx;
void loadMainIdentity(mesh::RNG& trng) {
void loadMainIdentity() {
if (!_identity_store->load("_main", self_id)) {
self_id = mesh::LocalIdentity(&trng); // create new random identity
self_id = radio_new_identity(); // create new random identity
saveMainIdentity(self_id);
}
}
@ -709,8 +705,8 @@ protected:
public:
MyMesh(RADIO_CLASS& phy, RadioLibWrapper& rw, mesh::RNG& rng, mesh::RTCClock& rtc, SimpleMeshTables& tables)
: BaseChatMesh(rw, *new ArduinoMillis(), rng, rtc, *new StaticPoolPacketManager(16), tables), _serial(NULL), _phy(&phy)
MyMesh(mesh::Radio& radio, mesh::RNG& rng, mesh::RTCClock& rtc, SimpleMeshTables& tables)
: BaseChatMesh(radio, *new ArduinoMillis(), rng, rtc, *new StaticPoolPacketManager(16), tables), _serial(NULL)
{
_iter_started = false;
offline_queue_len = 0;
@ -767,7 +763,7 @@ public:
}
}
void begin(FILESYSTEM& fs, mesh::RNG& trng, bool has_display) {
void begin(FILESYSTEM& fs, bool has_display) {
_fs = &fs;
BaseChatMesh::begin();
@ -778,7 +774,7 @@ public:
_identity_store = new IdentityStore(fs, "/identity");
#endif
loadMainIdentity(trng);
loadMainIdentity();
// load persisted prefs
if (_fs->exists("/new_prefs")) {
@ -793,7 +789,8 @@ public:
if (_prefs.ble_pin == 0) {
#ifdef HAS_UI
if (has_display) {
_active_ble_pin = trng.nextInt(100000, 999999); // random pin each session
StdRNG rng;
_active_ble_pin = rng.nextInt(100000, 999999); // random pin each session
} else {
_active_ble_pin = BLE_PIN_CODE; // otherwise static pin
}
@ -814,11 +811,8 @@ public:
addChannel("Public", PUBLIC_GROUP_PSK); // pre-configure Andy's public channel
loadChannels();
_phy->setFrequency(_prefs.freq);
_phy->setSpreadingFactor(_prefs.sf);
_phy->setBandwidth(_prefs.bw);
_phy->setCodingRate(_prefs.cr);
_phy->setOutputPower(_prefs.tx_power_dbm);
radio_set_params(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr);
radio_set_tx_power(_prefs.tx_power_dbm);
}
const char* getNodeName() { return _prefs.node_name; }
@ -1153,10 +1147,7 @@ public:
_prefs.bw = (float)bw / 1000.0;
savePrefs();
_phy->setFrequency(_prefs.freq);
_phy->setSpreadingFactor(_prefs.sf);
_phy->setBandwidth(_prefs.bw);
_phy->setCodingRate(_prefs.cr);
radio_set_params(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr);
MESH_DEBUG_PRINTLN("OK: CMD_SET_RADIO_PARAMS: f=%d, bw=%d, sf=%d, cr=%d", freq, bw, (uint32_t)sf, (uint32_t)cr);
writeOKFrame();
@ -1170,7 +1161,7 @@ public:
} else {
_prefs.tx_power_dbm = cmd_frame[1];
savePrefs();
_phy->setOutputPower(_prefs.tx_power_dbm);
radio_set_tx_power(_prefs.tx_power_dbm);
writeOKFrame();
}
} else if (cmd_frame[0] == CMD_SET_TUNING_PARAMS) {
@ -1431,7 +1422,7 @@ public:
StdRNG fast_rng;
SimpleMeshTables tables;
MyMesh the_mesh(radio, *new WRAPPER_CLASS(radio, board), fast_rng, *new VolatileRTCClock(), tables);
MyMesh the_mesh(radio_driver, fast_rng, *new VolatileRTCClock(), tables);
void halt() {
while (1) ;
@ -1444,9 +1435,7 @@ void setup() {
if (!radio_init()) { halt(); }
fast_rng.begin(radio.random(0x7FFFFFFF));
RadioNoiseListener trng(radio);
fast_rng.begin(radio_get_rng_seed());
#ifdef HAS_UI
DisplayDriver* disp = NULL;
@ -1459,7 +1448,7 @@ void setup() {
#if defined(NRF52_PLATFORM)
InternalFS.begin();
the_mesh.begin(InternalFS, trng,
the_mesh.begin(InternalFS,
#ifdef HAS_UI
disp != NULL
#else
@ -1477,7 +1466,7 @@ void setup() {
the_mesh.startInterface(serial_interface);
#elif defined(ESP32)
SPIFFS.begin(true);
the_mesh.begin(SPIFFS, trng,
the_mesh.begin(SPIFFS,
#ifdef HAS_UI
disp != NULL
#else

View file

@ -7,11 +7,6 @@
#include <SPIFFS.h>
#endif
#ifdef WRAPPER_CLASS
#define RADIOLIB_STATIC_ONLY 1
#include <RadioLib.h>
#endif
#include <helpers/ArduinoHelpers.h>
#include <helpers/StaticPoolPacketManager.h>
#include <helpers/SimpleMeshTables.h>
@ -112,13 +107,6 @@ struct ClientInfo {
class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
FILESYSTEM* _fs;
#ifdef WRAPPER_CLASS
RadioLibWrapper* my_radio;
RADIO_CLASS* _phy;
#else
ESPNOWRadio* my_radio;
#endif
mesh::MainBoard* _board;
unsigned long next_local_advert, next_flood_advert;
bool _logging;
NodePrefs _prefs;
@ -154,9 +142,9 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
stats.batt_milli_volts = board.getBattMilliVolts();
stats.curr_tx_queue_len = _mgr->getOutboundCount();
stats.curr_free_queue_len = _mgr->getFreeCount();
stats.last_rssi = (int16_t) my_radio->getLastRSSI();
stats.n_packets_recv = my_radio->getPacketsRecv();
stats.n_packets_sent = my_radio->getPacketsSent();
stats.last_rssi = (int16_t) radio_driver.getLastRSSI();
stats.n_packets_recv = radio_driver.getPacketsRecv();
stats.n_packets_sent = radio_driver.getPacketsSent();
stats.total_air_time_secs = getTotalAirTime() / 1000;
stats.total_up_time_secs = _ms->getMillis() / 1000;
stats.n_sent_flood = getNumSentFlood();
@ -164,7 +152,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
stats.n_recv_flood = getNumRecvFlood();
stats.n_recv_direct = getNumRecvDirect();
stats.n_full_events = getNumFullEvents();
stats.last_snr = (int16_t)(my_radio->getLastSNR() * 4);
stats.last_snr = (int16_t)(radio_driver.getLastSNR() * 4);
stats.n_direct_dups = ((SimpleMeshTables *)getTables())->getNumDirectDups();
stats.n_flood_dups = ((SimpleMeshTables *)getTables())->getNumFloodDups();
@ -481,17 +469,10 @@ protected:
}
public:
#ifdef WRAPPER_CLASS
MyMesh(RADIO_CLASS& phy, mesh::MainBoard& board, RadioLibWrapper& radio, mesh::MillisecondClock& ms, mesh::RNG& rng, mesh::RTCClock& rtc, mesh::MeshTables& tables)
MyMesh(mesh::MainBoard& board, mesh::Radio& radio, mesh::MillisecondClock& ms, mesh::RNG& rng, mesh::RTCClock& rtc, mesh::MeshTables& tables)
: mesh::Mesh(radio, ms, rng, rtc, *new StaticPoolPacketManager(32), tables),
_phy(&phy), _board(&board), _cli(board, this, &_prefs, this)
#else
MyMesh(mesh::MainBoard& board, ESPNOWRadio& radio, mesh::MillisecondClock& ms, mesh::RNG& rng, mesh::RTCClock& rtc, mesh::MeshTables& tables)
: mesh::Mesh(radio, ms, rng, rtc, *new StaticPoolPacketManager(32), tables),
_board(&board), _cli(board, this, &_prefs, this)
#endif
_cli(board, this, &_prefs, this)
{
my_radio = &radio;
memset(known_clients, 0, sizeof(known_clients));
next_local_advert = next_flood_advert = 0;
_logging = false;
@ -523,13 +504,8 @@ public:
// load persisted prefs
_cli.loadPrefs(_fs);
#ifdef WRAPPER_CLASS
_phy->setFrequency(_prefs.freq);
_phy->setSpreadingFactor(_prefs.sf);
_phy->setBandwidth(_prefs.bw);
_phy->setCodingRate(_prefs.cr);
_phy->setOutputPower(_prefs.tx_power_dbm);
#endif
radio_set_params(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr);
radio_set_tx_power(_prefs.tx_power_dbm);
updateAdvertTimer();
updateFloodAdvertTimer();
@ -597,9 +573,7 @@ public:
}
void setTxPower(uint8_t power_dbm) override {
#ifdef WRAPPER_CLASS
_phy->setOutputPower(power_dbm);
#endif
radio_set_tx_power(power_dbm);
}
void loop() {
@ -633,11 +607,7 @@ VolatileRTCClock fallback_clock;
#endif
AutoDiscoverRTCClock rtc_clock(fallback_clock);
#ifdef WRAPPER_CLASS
MyMesh the_mesh(radio, board, *new WRAPPER_CLASS(radio, board), *new ArduinoMillis(), fast_rng, rtc_clock, tables);
#else
MyMesh the_mesh(board, radio, *new ArduinoMillis(), fast_rng, rtc_clock, tables);
#endif
MyMesh the_mesh(board, radio_driver, *new ArduinoMillis(), fast_rng, rtc_clock, tables);
void halt() {
while (1) ;
@ -657,11 +627,7 @@ void setup() {
if (!radio_init()) { halt(); }
#ifdef WRAPPER_CLASS
fast_rng.begin(radio.random(0x7FFFFFFF));
#else
fast_rng.begin(radio.intID());
#endif
fast_rng.begin(radio_get_rng_seed());
FILESYSTEM* fs;
#if defined(NRF52_PLATFORM)
@ -677,12 +643,7 @@ void setup() {
#endif
if (!store.load("_main", the_mesh.self_id)) {
MESH_DEBUG_PRINTLN("Generating new keypair");
#ifdef WRAPPER_CLASS
RadioNoiseListener rng(radio);
#else
#define rng fast_rng
#endif
the_mesh.self_id = mesh::LocalIdentity(&rng); // create new random identity
the_mesh.self_id = radio_new_identity(); // create new random identity
store.save("_main", the_mesh.self_id);
}

View file

@ -7,11 +7,6 @@
#include <SPIFFS.h>
#endif
#ifdef WRAPPER_CLASS
#define RADIOLIB_STATIC_ONLY 1
#include <RadioLib.h>
#endif
#include <helpers/ArduinoHelpers.h>
#include <helpers/StaticPoolPacketManager.h>
#include <helpers/SimpleMeshTables.h>
@ -147,13 +142,6 @@ struct ServerStats {
class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
FILESYSTEM* _fs;
#ifdef WRAPPER_CLASS
RadioLibWrapper* my_radio;
RADIO_CLASS* _phy;
#else
ESPNOWRadio* my_radio;
#endif
mesh::MainBoard* _board;
unsigned long next_local_advert, next_flood_advert;
bool _logging;
NodePrefs _prefs;
@ -591,9 +579,9 @@ protected:
stats.batt_milli_volts = board.getBattMilliVolts();
stats.curr_tx_queue_len = _mgr->getOutboundCount();
stats.curr_free_queue_len = _mgr->getFreeCount();
stats.last_rssi = (int16_t) my_radio->getLastRSSI();
stats.n_packets_recv = my_radio->getPacketsRecv();
stats.n_packets_sent = my_radio->getPacketsSent();
stats.last_rssi = (int16_t) radio_driver.getLastRSSI();
stats.n_packets_recv = radio_driver.getPacketsRecv();
stats.n_packets_sent = radio_driver.getPacketsSent();
stats.total_air_time_secs = getTotalAirTime() / 1000;
stats.total_up_time_secs = _ms->getMillis() / 1000;
stats.n_sent_flood = getNumSentFlood();
@ -601,7 +589,7 @@ protected:
stats.n_recv_flood = getNumRecvFlood();
stats.n_recv_direct = getNumRecvDirect();
stats.n_full_events = getNumFullEvents();
stats.last_snr = (int16_t)(my_radio->getLastSNR() * 4);
stats.last_snr = (int16_t)(radio_driver.getLastSNR() * 4);
stats.n_direct_dups = ((SimpleMeshTables *)getTables())->getNumDirectDups();
stats.n_flood_dups = ((SimpleMeshTables *)getTables())->getNumFloodDups();
stats.n_posted = _num_posted;
@ -660,17 +648,10 @@ protected:
}
public:
#ifdef WRAPPER_CLASS
MyMesh(RADIO_CLASS& phy, mesh::MainBoard& board, RadioLibWrapper& radio, mesh::MillisecondClock& ms, mesh::RNG& rng, mesh::RTCClock& rtc, mesh::MeshTables& tables)
MyMesh(mesh::MainBoard& board, mesh::Radio& radio, mesh::MillisecondClock& ms, mesh::RNG& rng, mesh::RTCClock& rtc, mesh::MeshTables& tables)
: mesh::Mesh(radio, ms, rng, rtc, *new StaticPoolPacketManager(32), tables),
_phy(&phy), _board(&board), _cli(board, this, &_prefs, this)
#else
MyMesh(mesh::MainBoard& board, ESPNOWRadio& radio, mesh::MillisecondClock& ms, mesh::RNG& rng, mesh::RTCClock& rtc, mesh::MeshTables& tables)
: mesh::Mesh(radio, ms, rng, rtc, *new StaticPoolPacketManager(32), tables),
_board(&board), _cli(board, this, &_prefs, this)
#endif
_cli(board, this, &_prefs, this)
{
my_radio = &radio;
next_local_advert = next_flood_advert = 0;
_logging = false;
@ -712,15 +693,11 @@ public:
// load persisted prefs
_cli.loadPrefs(_fs);
#ifdef WRAPPER_CLASS
_phy->setFrequency(_prefs.freq);
_phy->setSpreadingFactor(_prefs.sf);
_phy->setBandwidth(_prefs.bw);
_phy->setCodingRate(_prefs.cr);
_phy->setOutputPower(_prefs.tx_power_dbm);
#endif
radio_set_params(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr);
radio_set_tx_power(_prefs.tx_power_dbm);
updateAdvertTimer();
updateFloodAdvertTimer();
}
const char* getFirmwareVer() override { return FIRMWARE_VERSION; }
@ -785,9 +762,7 @@ public:
}
void setTxPower(uint8_t power_dbm) override {
#ifdef WRAPPER_CLASS
_phy->setOutputPower(power_dbm);
#endif
radio_set_tx_power(power_dbm);
}
void loop() {
@ -856,11 +831,7 @@ VolatileRTCClock fallback_clock;
#endif
AutoDiscoverRTCClock rtc_clock(fallback_clock);
#ifdef WRAPPER_CLASS
MyMesh the_mesh(radio, board, *new WRAPPER_CLASS(radio, board), *new ArduinoMillis(), fast_rng, rtc_clock, tables);
#else
MyMesh the_mesh(board, radio, *new ArduinoMillis(), fast_rng, rtc_clock, tables);
#endif
MyMesh the_mesh(board, radio_driver, *new ArduinoMillis(), fast_rng, rtc_clock, tables);
void halt() {
while (1) ;
@ -880,11 +851,7 @@ void setup() {
if (!radio_init()) { halt(); }
#ifdef WRAPPER_CLASS
fast_rng.begin(radio.random(0x7FFFFFFF));
#else
fast_rng.begin(radio.intID());
#endif
fast_rng.begin(radio_get_rng_seed());
FILESYSTEM* fs;
#if defined(NRF52_PLATFORM)
@ -899,12 +866,7 @@ void setup() {
#error "need to define filesystem"
#endif
if (!store.load("_main", the_mesh.self_id)) {
#ifdef WRAPPER_CLASS
RadioNoiseListener rng(radio);
#else
#define rng fast_rng
#endif
the_mesh.self_id = mesh::LocalIdentity(&rng); // create new random identity
the_mesh.self_id = radio_new_identity(); // create new random identity
store.save("_main", the_mesh.self_id);
}

View file

@ -7,12 +7,6 @@
#include <SPIFFS.h>
#endif
#ifdef WRAPPER_CLASS
#define RADIOLIB_STATIC_ONLY 1
#include <RadioLib.h>
#include <helpers/RadioLibWrappers.h>
#endif
#include <helpers/ArduinoHelpers.h>
#include <helpers/StaticPoolPacketManager.h>
#include <helpers/SimpleMeshTables.h>
@ -269,12 +263,7 @@ protected:
}
public:
#ifdef WRAPPER_CLASS
MyMesh(RadioLibWrapper& radio, StdRNG& rng, mesh::RTCClock& rtc, SimpleMeshTables& tables)
#else
MyMesh(mesh::Radio& radio, StdRNG& rng, mesh::RTCClock& rtc, SimpleMeshTables& tables)
#endif
: BaseChatMesh(radio, *new ArduinoMillis(), rng, rtc, *new StaticPoolPacketManager(16), tables)
{
// defaults
@ -534,12 +523,7 @@ public:
StdRNG fast_rng;
SimpleMeshTables tables;
#ifdef WRAPPER_CLASS
MyMesh the_mesh(*new WRAPPER_CLASS(radio, board), fast_rng, *new VolatileRTCClock(), tables);
#else
MyMesh the_mesh(radio, fast_rng, *new VolatileRTCClock(), tables);
#endif
MyMesh the_mesh(radio_driver, fast_rng, *new VolatileRTCClock(), tables);
void halt() {
while (1) ;
@ -552,11 +536,7 @@ void setup() {
if (!radio_init()) { halt(); }
#ifdef WRAPPER_CLASS
fast_rng.begin(radio.random(0x7FFFFFFF));
#else
fast_rng.begin(millis());
#endif
fast_rng.begin(radio_get_rng_seed());
#if defined(NRF52_PLATFORM)
InternalFS.begin();
@ -568,14 +548,8 @@ void setup() {
#error "need to define filesystem"
#endif
#ifdef WRAPPER_CLASS
if (LORA_FREQ != the_mesh.getFreqPref()) {
radio.setFrequency(the_mesh.getFreqPref());
}
if (LORA_TX_POWER != the_mesh.getTxPowerPref()) {
radio.setOutputPower(the_mesh.getTxPowerPref());
}
#endif
radio_set_params(the_mesh.getFreqPref(), LORA_BW, LORA_SF, LORA_CR);
radio_set_tx_power(the_mesh.getTxPowerPref());
the_mesh.showWelcome();