ESP32-WiPhone / wiphone-firmware

Arduino ESP32 firmware for the WiPhone
37 stars 4 forks source link

LoRa: set frequency from settings #24

Open rhaamo opened 2 years ago

rhaamo commented 2 years ago

Instead of having to rebuild the firmware on each update

xopr commented 2 years ago

I haven't tested this yet, but I think if the following patch is applied to the source upstream, one could add the following segment to configs.ini (which might make it a valuable addition):

[lora]
lora_freq=868.0

lora.patch:

diff --git a/lora.cpp b/lora.cpp
index 6b03536..ae1833b 100644
--- a/lora.cpp
+++ b/lora.cpp
@@ -43,7 +43,20 @@ void Lora::setup() {
   loraSPI->setPins(HSPI_MISO, HSPI_MOSI, HSPI_SCLK);
   pinMode(RFM95_RST, OUTPUT);
   rf95->init();
-  rf95->setFrequency(RF95_FREQ);
+
+
+  float freq = RF95_FREQ;
+  // Load phone configs
+  {
+    CriticalFile ini(Storage::ConfigsFile);
+    if ((ini.load() || ini.restore()) && !ini.isEmpty()) {
+      if (ini[0].hasKey("v") && !strcmp(ini[0]["v"], "1")) {    // check version of the file format
+        if ( ini.hasSection("lora") )
+          freq = ini["lora"].getFloatValueSafe("lora_freq", RF95_FREQ);
+      }
+    }
+  }
+  rf95->setFrequency(freq);
   rf95->setTxPower(23, false);

   log_v("Free memory after LoRa: %d %d", ESP.getFreeHeap(), heap_caps_get_free_size(MALLOC_CAP_32BIT));

Note that it might need better orchestration of the ini file parsing; it's a bit inline-hacky. Also: it is only applied after reboot: for full functionality, the setFrequency() needs to be taken out of the setup() function.