From 2796973ad3dd3469bdf45e7abd95fe88c916a337 Mon Sep 17 00:00:00 2001 From: Krzysiek Egzmont Date: Tue, 31 Oct 2023 19:40:00 +0100 Subject: [PATCH] Use the custom battery percentage calculations for battery icon and low battery beeping --- app/menu.c | 10 +++++----- helper/battery.c | 30 ++++++++++++++++++++---------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/app/menu.c b/app/menu.c index ce65b5c..c75d003 100644 --- a/app/menu.c +++ b/app/menu.c @@ -778,12 +778,12 @@ void MENU_AcceptSetting(void) case MENU_BATCAL: { // voltages are averages between discharge curves of 1600 and 2200 mAh - gBatteryCalibration[0] = (520ul * gSubMenuSelection) / 760; // 5.20V empty, blinking above this value, reduced functionality below - gBatteryCalibration[1] = (689ul * gSubMenuSelection) / 760; // 6.89V, ~5%, 1 bars above this value - gBatteryCalibration[2] = (724ul * gSubMenuSelection) / 760; // 7.24V, ~17%, 2 bars above this value + // gBatteryCalibration[0] = (520ul * gSubMenuSelection) / 760; // 5.20V empty, blinking above this value, reduced functionality below + // gBatteryCalibration[1] = (689ul * gSubMenuSelection) / 760; // 6.89V, ~5%, 1 bars above this value + // gBatteryCalibration[2] = (724ul * gSubMenuSelection) / 760; // 7.24V, ~17%, 2 bars above this value gBatteryCalibration[3] = gSubMenuSelection; // 7.6V, ~29%, 3 bars above this value - gBatteryCalibration[4] = (771ul * gSubMenuSelection) / 760; // 7.71V, ~65%, 4 bars above this value - gBatteryCalibration[5] = 2300; + // gBatteryCalibration[4] = (771ul * gSubMenuSelection) / 760; // 7.71V, ~65%, 4 bars above this value + // gBatteryCalibration[5] = 2300; SETTINGS_SaveBatteryCalibration(gBatteryCalibration); return; } diff --git a/helper/battery.c b/helper/battery.c index 5394f63..2157589 100644 --- a/helper/battery.c +++ b/helper/battery.c @@ -42,7 +42,7 @@ unsigned int BATTERY_VoltsToPercent(const unsigned int voltage_10mV) {814, 100}, {756, 24 }, {729, 7 }, - {597, 0 }, + {620, 0 }, {0, 0} }; @@ -51,7 +51,7 @@ unsigned int BATTERY_VoltsToPercent(const unsigned int voltage_10mV) {740, 60}, {707, 21}, {680, 5}, - {505, 0}, + {620, 0}, {0, 0} }; @@ -84,17 +84,27 @@ void BATTERY_GetReadings(const bool bDisplayBatteryLevel) const uint8_t PreviousBatteryLevel = gBatteryDisplayLevel; const uint16_t Voltage = (gBatteryVoltages[0] + gBatteryVoltages[1] + gBatteryVoltages[2] + gBatteryVoltages[3]) / 4; - gBatteryDisplayLevel = 0; - - for(uint8_t i = 6; i > 0; i--){ - if (Voltage > gBatteryCalibration[i-1]) { - gBatteryDisplayLevel = i; - break; - } - } + gBatteryVoltageAverage = (Voltage * 760) / gBatteryCalibration[3]; + if(gBatteryVoltageAverage > 840) + gBatteryDisplayLevel = 6; // battery overvoltage + else if(gBatteryVoltageAverage < 620) + gBatteryDisplayLevel = 0; // battery critical + else { + gBatteryDisplayLevel = 1; + const uint8_t levels[] = {5,25,50,75}; + uint8_t perc = BATTERY_VoltsToPercent(gBatteryVoltageAverage); + for(uint8_t i = 5; i >= 1; i--){ + if (perc > levels[i-2]) { + gBatteryDisplayLevel = i; + break; + } + } + } + + if ((gScreenToDisplay == DISPLAY_MENU) && GetCurrentMenuId() == MENU_VOL) gUpdateDisplay = true;