From a2fb3d23a1545b80d480208d5df917bbf179bb95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Gjels=C3=B8?= <36234524+gjelsoe@users.noreply.github.com> Date: Sun, 9 Jun 2024 23:03:39 +0200 Subject: [PATCH] Radio Master 900 Bandit Nano Power output interpolation (#4057) * DAC and DB values based on dBm using interpolation * Moved getDACandDB funtion Moved getDACandDB funtion up so it won't conflict with RF95_MAX_POWER * Added DAC output to LOG_INFO Added DAC output to LOG_INFO * Make Trunk Happy --- src/mesh/RF95Interface.cpp | 64 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 3 deletions(-) diff --git a/src/mesh/RF95Interface.cpp b/src/mesh/RF95Interface.cpp index 5677e6eda..c5356ad3b 100644 --- a/src/mesh/RF95Interface.cpp +++ b/src/mesh/RF95Interface.cpp @@ -17,6 +17,48 @@ // if you set power to something higher than 17 or 20 you might fry your board. #define POWER_DEFAULT 17 // How much power to use if the user hasn't set a power level +#ifdef RADIOMASTER_900_BANDIT_NANO +// Structure to hold DAC and DB values +typedef struct { + uint8_t dac; + uint8_t db; +} DACDB; + +// Interpolation function +DACDB interpolate(uint8_t dbm, uint8_t dbm1, uint8_t dbm2, DACDB val1, DACDB val2) { + DACDB result; + double fraction = (double)(dbm - dbm1) / (dbm2 - dbm1); + result.dac = (uint8_t)(val1.dac + fraction * (val2.dac - val1.dac)); + result.db = (uint8_t)(val1.db + fraction * (val2.db - val1.db)); + return result; +} + +// Function to find the correct DAC and DB values based on dBm using interpolation +DACDB getDACandDB(uint8_t dbm) { + // Predefined values + static const struct { + uint8_t dbm; + DACDB values; + } dbmToDACDB[] = { + {20, {168, 2}}, // 100mW + {24, {148, 6}}, // 250mW + {27, {128, 9}}, // 500mW + {30, {90, 12}} // 1000mW + }; + const int numValues = sizeof(dbmToDACDB) / sizeof(dbmToDACDB[0]); + + // Find the interval dbm falls within and interpolate + for (int i = 0; i < numValues - 1; i++) { + if (dbm >= dbmToDACDB[i].dbm && dbm <= dbmToDACDB[i + 1].dbm) { + return interpolate(dbm, dbmToDACDB[i].dbm, dbmToDACDB[i + 1].dbm, dbmToDACDB[i].values, dbmToDACDB[i + 1].values); + } + } + + // Return a default value if no match is found and default to 100mW + DACDB defaultValue = {168, 2}; + return defaultValue; +} +#endif RF95Interface::RF95Interface(LockingArduinoHal *hal, RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, RADIOLIB_PIN_TYPE busy) @@ -52,9 +94,16 @@ bool RF95Interface::init() { RadioLibInterface::init(); +#ifdef RADIOMASTER_900_BANDIT_NANO + // DAC and DB values based on dBm using interpolation + DACDB dacDbValues = getDACandDB(power); + int8_t powerDAC = dacDbValues.dac; + power = dacDbValues.db; +#endif + if (power > RF95_MAX_POWER) // This chip has lower power limits than some power = RF95_MAX_POWER; - + limitPower(); iface = lora = new RadioLibRF95(&module); @@ -67,7 +116,13 @@ bool RF95Interface::init() // enable PA #ifdef RF95_PA_EN #if defined(RF95_PA_DAC_EN) - dacWrite(RF95_PA_EN, RF95_PA_LEVEL); + #ifdef RADIOMASTER_900_BANDIT_NANO + // Use calculated DAC value + dacWrite(RF95_PA_EN, powerDAC); + #else + // Use Value set in /*/variant.h + dacWrite(RF95_PA_EN, RF95_PA_LEVEL); + #endif #endif #endif @@ -107,6 +162,9 @@ bool RF95Interface::init() LOG_INFO("Frequency set to %f\n", getFreq()); LOG_INFO("Bandwidth set to %f\n", bw); LOG_INFO("Power output set to %d\n", power); +#ifdef RADIOMASTER_900_BANDIT_NANO + LOG_INFO("DAC output set to %d\n", powerDAC); +#endif if (res == RADIOLIB_ERR_NONE) res = lora->setCRC(RADIOLIB_SX126X_LORA_CRC_ON); @@ -259,4 +317,4 @@ bool RF95Interface::sleep() #endif return true; -} \ No newline at end of file +}