From d7cf7e2eb4e0517805bd4a710af6ba0962a9d72f Mon Sep 17 00:00:00 2001 From: geeksville Date: Wed, 12 Aug 2020 10:42:25 -0700 Subject: [PATCH] Allow advanced users to set arbitrary spreadfactor/codingrate/bandwidth --- proto | 2 +- src/mesh/NodeDB.cpp | 4 +-- src/mesh/RadioInterface.cpp | 12 ++++--- src/mesh/RadioInterface.h | 15 +++------ src/mesh/RadioLibInterface.cpp | 59 +++++++++++++++++++++------------- 5 files changed, 51 insertions(+), 41 deletions(-) diff --git a/proto b/proto index 0523977d..c715e506 160000 --- a/proto +++ b/proto @@ -1 +1 @@ -Subproject commit 0523977d1f6c378cf78859044e2edbdba45150fa +Subproject commit c715e506df9ab1a76293d1c20dd5c904b009e1ea diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 92f47adf..206d0023 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -29,7 +29,7 @@ DeviceState versions used to be defined in the .proto file but really only this #define here. */ -#define DEVICESTATE_CUR_VER 10 +#define DEVICESTATE_CUR_VER 11 #define DEVICESTATE_MIN_VER DEVICESTATE_CUR_VER #ifndef NO_ESP32 @@ -93,7 +93,7 @@ void NodeDB::resetRadioConfig() // so incompatible radios can talk together channelSettings.modem_config = ChannelSettings_ModemConfig_Bw125Cr48Sf4096; // slow and long range - channelSettings.tx_power = 23; + channelSettings.tx_power = 0; // default memcpy(&channelSettings.psk.bytes, &defaultpsk, sizeof(channelSettings.psk)); channelSettings.psk.size = sizeof(defaultpsk); strcpy(channelSettings.name, "Default"); diff --git a/src/mesh/RadioInterface.cpp b/src/mesh/RadioInterface.cpp index ad255d62..4967954e 100644 --- a/src/mesh/RadioInterface.cpp +++ b/src/mesh/RadioInterface.cpp @@ -115,22 +115,26 @@ unsigned long hash(char *str) return hash; } +#define POWER_DEFAULT 17 + /** * Pull our channel settings etc... from protobufs to the dumb interface settings */ void RadioInterface::applyModemConfig() { // Set up default configuration - // No Sync Words in LORA mode. - modemConfig = (ModemConfigChoice)channelSettings.modem_config; + // No Sync Words in LORA mode + + power = channelSettings.tx_power; + if (power == 0) + power = POWER_DEFAULT; // Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM int channel_num = hash(channelSettings.name) % NUM_CHANNELS; freq = CH0 + CH_SPACING * channel_num; - power = channelSettings.tx_power; DEBUG_MSG("Set radio: name=%s, config=%u, ch=%d, power=%d\n", channelSettings.name, channelSettings.modem_config, channel_num, - channelSettings.tx_power); + power); } ErrorCode SimRadio::send(MeshPacket *p) diff --git a/src/mesh/RadioInterface.h b/src/mesh/RadioInterface.h index 38951519..b8cba208 100644 --- a/src/mesh/RadioInterface.h +++ b/src/mesh/RadioInterface.h @@ -1,10 +1,10 @@ #pragma once +#include "../concurrency/NotifiedWorkerThread.h" #include "MemoryPool.h" #include "MeshTypes.h" #include "Observer.h" #include "PointerQueue.h" -#include "../concurrency/NotifiedWorkerThread.h" #include "mesh.pb.h" #define MAX_TX_QUEUE 16 // max number of packets which can be waiting for transmission @@ -31,13 +31,6 @@ typedef struct { uint8_t flags; } PacketHeader; -typedef enum { - Bw125Cr45Sf128 = 0, ///< Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on. Default medium range - Bw500Cr45Sf128, ///< Bw = 500 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on. Fast+short range - Bw31_25Cr48Sf512, ///< Bw = 31.25 kHz, Cr = 4/8, Sf = 512chips/symbol, CRC on. Slow+long range - Bw125Cr48Sf4096, ///< Bw = 125 kHz, Cr = 4/8, Sf = 4096chips/symbol, CRC on. Slow+long range -} ModemConfigChoice; - /** * Basic operations all radio chipsets must implement. * @@ -72,9 +65,7 @@ class RadioInterface : protected concurrency::NotifiedWorkerThread void deliverToReceiver(MeshPacket *p); public: - float freq = 915.0; // FIXME, init all these params from user setings - int8_t power = 17; - ModemConfigChoice modemConfig; + float freq = 915.0; /** pool is the pool we will alloc our rx packets from * rxDest is where we will send any rx packets, it becomes receivers responsibility to return packet to the pool @@ -116,6 +107,8 @@ class RadioInterface : protected concurrency::NotifiedWorkerThread virtual bool reconfigure() = 0; protected: + int8_t power = 17; // Set by applyModemConfig() + /*** * given a packet set sendingPacket and decode the protobufs into radiobuf. Returns # of bytes to send (including the * PacketHeader & payload). diff --git a/src/mesh/RadioLibInterface.cpp b/src/mesh/RadioLibInterface.cpp index 4551edd2..bfaefb1f 100644 --- a/src/mesh/RadioLibInterface.cpp +++ b/src/mesh/RadioLibInterface.cpp @@ -1,5 +1,6 @@ #include "RadioLibInterface.h" #include "MeshTypes.h" +#include "NodeDB.h" #include "mesh-pb-constants.h" #include #include @@ -64,29 +65,41 @@ void RadioLibInterface::applyModemConfig() { RadioInterface::applyModemConfig(); - switch (modemConfig) { - case Bw125Cr45Sf128: ///< Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on. Default medium range - bw = 125; - cr = 5; - sf = 7; - break; - case Bw500Cr45Sf128: ///< Bw = 500 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on. Fast+short range - bw = 500; - cr = 5; - sf = 7; - break; - case Bw31_25Cr48Sf512: ///< Bw = 31.25 kHz, Cr = 4/8, Sf = 512chips/symbol, CRC on. Slow+long range - bw = 31.25; - cr = 8; - sf = 9; - break; - case Bw125Cr48Sf4096: - bw = 125; - cr = 8; - sf = 12; - break; - default: - assert(0); // Unknown enum + if (channelSettings.spread_factor == 0) { + switch (channelSettings.modem_config) { + case ChannelSettings_ModemConfig_Bw125Cr45Sf128: ///< Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on. Default medium + ///< range + bw = 125; + cr = 5; + sf = 7; + break; + case ChannelSettings_ModemConfig_Bw500Cr45Sf128: ///< Bw = 500 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on. Fast+short + ///< range + bw = 500; + cr = 5; + sf = 7; + break; + case ChannelSettings_ModemConfig_Bw31_25Cr48Sf512: ///< Bw = 31.25 kHz, Cr = 4/8, Sf = 512chips/symbol, CRC on. Slow+long + ///< range + bw = 31.25; + cr = 8; + sf = 9; + break; + case ChannelSettings_ModemConfig_Bw125Cr48Sf4096: + bw = 125; + cr = 8; + sf = 12; + break; + default: + assert(0); // Unknown enum + } + } else { + sf = channelSettings.spread_factor; + cr = channelSettings.coding_rate; + bw = channelSettings.bandwidth; + + if (bw == 31) // This parameter is not an integer + bw = 31.25; } }