From 3f23ba9b2dd2cfc471842d8ddbd6e71f0549118b Mon Sep 17 00:00:00 2001 From: Olexandr Tyshchenko Date: Sun, 8 Nov 2026 18:07:58 +0200 Subject: [PATCH] Companion: set/get adc.multiplier CLI command, persisted in prefs Repeaters, room servers and sensors already let the battery ADC multiplier be tuned with 'set adc.multiplier' (CommonCLI). The companion firmware had no equivalent, so a board whose divider differs from the build default (common on DIY ProMicro / Faketec boards) always reported a wrong battery voltage. Adds the same command to the companion CLI. The value is stored in the companion prefs (comp.adc_mult, 0 = board default) and applied at startup via MainBoard::setAdcMultiplier(). --- docs/companion_cli.md | 14 ++++++++++++++ examples/companion_radio/MyMesh.cpp | 29 ++++++++++++++++++++++++++++ examples/companion_radio/NodePrefs.h | 2 ++ 3 files changed, 45 insertions(+) diff --git a/docs/companion_cli.md b/docs/companion_cli.md index 31a05584..cbc4773a 100644 --- a/docs/companion_cli.md +++ b/docs/companion_cli.md @@ -65,6 +65,20 @@ Since version 1.18 meshcore companion radio firmware has a CLI interface, which --- +#### Fine-tune the battery reading +**Usage:** +- `get adc.multiplier` +- `set adc.multiplier ` + +**Parameters:** +- `value`: ADC multiplier (0.0-10.0) + +**Default:** `0.0` (value defined by board) + +**Note:** Returns "Error: unsupported" if the board doesn't support it. Same command as on repeaters, for boards whose battery divider resistors vary (e.g. DIY ProMicro / Faketec). + +--- + ### Radio Radio parameters are common to all roles and handled by `CommonRadioPrefs`. diff --git a/examples/companion_radio/MyMesh.cpp b/examples/companion_radio/MyMesh.cpp index 755c56d1..fd7dcd18 100644 --- a/examples/companion_radio/MyMesh.cpp +++ b/examples/companion_radio/MyMesh.cpp @@ -993,6 +993,8 @@ void MyMesh::begin() { _prefs.tx_power_dbm = constrain(_prefs.tx_power_dbm, -9, MAX_LORA_TX_POWER); _prefs.gps_enabled = constrain(_prefs.gps_enabled, 0, 1); // Ensure boolean 0 or 1 _prefs.gps_interval = constrain(_prefs.gps_interval, 0, 86400); // Max 24 hours + _prefs.adc_multiplier = constrain(_prefs.adc_multiplier, 0.0f, 10.0f); + if (!board.setAdcMultiplier(_prefs.adc_multiplier)) _prefs.adc_multiplier = 0.0f; _active_ble_pin = _prefs.ble_pin; @@ -2210,6 +2212,33 @@ bool MyMesh::handleCommand(const char* command, uint32_t sender_timestamp, char* return true; } + if (memcmp(command, "set adc.multiplier ", 19) == 0) { // same as CommonCLI + float mult = atof(&command[19]); + if (mult < 0.0f || mult > 10.0f) { + strcpy(reply, "Error, must be from 0 to 10"); + } else if (board.setAdcMultiplier(mult)) { + _prefs.adc_multiplier = mult; + savePrefs(); + if (mult == 0.0f) { + strcpy(reply, "OK - using default board multiplier"); + } else { + sprintf(reply, "OK - multiplier set to %.3f", mult); + } + } else { + strcpy(reply, "Error: unsupported"); + } + return true; + } + if (strcmp(command, "get adc.multiplier") == 0) { + float mult = board.getAdcMultiplier(); + if (mult == 0.0f) { + strcpy(reply, "Error: unsupported"); + } else { + sprintf(reply, "> %.3f", mult); + } + return true; + } + if (strcmp(command, "get tz.offset") == 0) { float q = _prefs.tz_offset * 4; sprintf(reply, (q == (int)q && ((int)q & 1)) ? "> %.2f" : "> %.1f", _prefs.tz_offset); diff --git a/examples/companion_radio/NodePrefs.h b/examples/companion_radio/NodePrefs.h index c5d1df04..88770dcf 100644 --- a/examples/companion_radio/NodePrefs.h +++ b/examples/companion_radio/NodePrefs.h @@ -48,6 +48,7 @@ public: char default_scope_name[31]; uint8_t default_scope_key[16]; float tz_offset = 0; + float adc_multiplier = 0; // battery ADC multiplier, 0 = board default #ifdef ENABLE_WIFI_INTERFACE #ifndef WIFI_SSID #define WIFI_SSID "" @@ -194,6 +195,7 @@ private: def("tel_loc", _parent->telemetry_mode_loc); def("tel_env", _parent->telemetry_mode_env); def("tz_offset", _parent->tz_offset); + def("adc_mult", _parent->adc_multiplier); } public: CompanionPrefs(NodePrefs* parent) : _parent(parent) { }