From 8c7f7367979c2a133f9c613961c44624bb725eea Mon Sep 17 00:00:00 2001 From: Juan Antonio Date: Tue, 12 Dec 2023 22:39:30 +0100 Subject: [PATCH] Simplify come battery logic --- helper/battery.c | 136 +++++++++++++++++++++++------------------------ helper/battery.h | 6 +-- ui/battery.c | 35 ++++++------ 3 files changed, 88 insertions(+), 89 deletions(-) diff --git a/helper/battery.c b/helper/battery.c index 13f43c13b..03b1a17d9 100644 --- a/helper/battery.c +++ b/helper/battery.c @@ -49,48 +49,45 @@ const uint16_t lowBatteryPeriod = 30; volatile uint16_t gPowerSave_10ms; -unsigned int BATTERY_VoltsToPercent(const unsigned int voltage_10mV) -{ - const uint16_t crv1600[][2] = { +const uint16_t Voltage2PercentageTable[][7][2] = { + [BATTERY_TYPE_1600_MAH] = { {828, 100}, {814, 97 }, {760, 25 }, {729, 6 }, {630, 0 }, - {0, 0 } - }; + {0, 0 }, + {0, 0 }, + }, - const uint16_t crv2200[][2] = { + [BATTERY_TYPE_2200_MAH] = { {832, 100}, {813, 95 }, {740, 60 }, {707, 21 }, {682, 5 }, {630, 0 }, - {0, 0 } - }; - - const BATTERY_Type_t type = gEeprom.BATTERY_TYPE; - const uint16_t(*crv)[2]; - uint8_t size; - if (type == BATTERY_TYPE_2200_MAH) { - crv = crv2200; - size = ARRAY_SIZE(crv2200); - } - else { - crv = crv1600; - size = ARRAY_SIZE(crv1600); - } + {0, 0 }, + }, +}; + +static_assert(ARRAY_SIZE(Voltage2PercentageTable[BATTERY_TYPE_1600_MAH]) == + ARRAY_SIZE(Voltage2PercentageTable[BATTERY_TYPE_2200_MAH])); + +unsigned int BATTERY_VoltsToPercent(const unsigned int voltage_10mV) +{ + const uint16_t (*crv)[2] = Voltage2PercentageTable[gEeprom.BATTERY_TYPE]; const int mulipl = 1000; - for (int i = 1; i < size; i++) { + for (unsigned int i = 1; i < ARRAY_SIZE(Voltage2PercentageTable[BATTERY_TYPE_2200_MAH]); i++) { if (voltage_10mV > crv[i][0]) { - int a = (crv[i - 1][1] - crv[i][1]) * mulipl / (crv[i - 1][0] - crv[i][0]); - int b = crv[i][1] - a * crv[i][0] / mulipl; - int p = a * voltage_10mV / mulipl + b; + const int a = (crv[i - 1][1] - crv[i][1]) * mulipl / (crv[i - 1][0] - crv[i][0]); + const int b = crv[i][1] - a * crv[i][0] / mulipl; + const int p = a * voltage_10mV / mulipl + b; return MIN(p, 100); } } + return 0; } @@ -99,8 +96,6 @@ void BATTERY_GetReadings(const bool bDisplayBatteryLevel) const uint8_t PreviousBatteryLevel = gBatteryDisplayLevel; const uint16_t Voltage = (gBatteryVoltages[0] + gBatteryVoltages[1] + gBatteryVoltages[2] + gBatteryVoltages[3]) / 4; - - gBatteryVoltageAverage = (Voltage * 760) / gBatteryCalibration[3]; if(gBatteryVoltageAverage > 890) @@ -116,7 +111,7 @@ void BATTERY_GetReadings(const bool bDisplayBatteryLevel) gBatteryDisplayLevel = i; break; } - } + } } @@ -160,7 +155,7 @@ void BATTERY_GetReadings(const bool bDisplayBatteryLevel) if (bDisplayBatteryLevel) UI_DisplayBattery(gBatteryDisplayLevel, gLowBatteryBlink); } - + if(!gLowBatteryConfirmed) gUpdateDisplay = true; @@ -168,56 +163,61 @@ void BATTERY_GetReadings(const bool bDisplayBatteryLevel) } } -void BATTERY_TimeSlice500ms(void) +void BATTERY_TimeSlice500ms(void) { - if (gLowBattery) - { - gLowBatteryBlink = ++lowBatteryCountdown & 1; + if (!gLowBattery) { + return; + } - UI_DisplayBattery(0, gLowBatteryBlink); + gLowBatteryBlink = ++lowBatteryCountdown & 1; - if (gCurrentFunction != FUNCTION_TRANSMIT) - { // not transmitting + UI_DisplayBattery(0, gLowBatteryBlink); - if (lowBatteryCountdown < lowBatteryPeriod) - { - if (lowBatteryCountdown == lowBatteryPeriod-1 && !gChargingWithTypeC && !gLowBatteryConfirmed) - AUDIO_PlayBeep(BEEP_500HZ_60MS_DOUBLE_BEEP); - } - else - { - lowBatteryCountdown = 0; - - if (!gChargingWithTypeC) - { // not on charge - if(!gLowBatteryConfirmed) { - AUDIO_PlayBeep(BEEP_500HZ_60MS_DOUBLE_BEEP); + if (gCurrentFunction == FUNCTION_TRANSMIT) { + return; + } + + // not transmitting + + if (lowBatteryCountdown < lowBatteryPeriod) { + if (lowBatteryCountdown == lowBatteryPeriod-1 && !gChargingWithTypeC && !gLowBatteryConfirmed) { + AUDIO_PlayBeep(BEEP_500HZ_60MS_DOUBLE_BEEP); + } + return; + } + + lowBatteryCountdown = 0; + + if (gChargingWithTypeC) { + return; + } + + // not on charge + if (!gLowBatteryConfirmed) { + AUDIO_PlayBeep(BEEP_500HZ_60MS_DOUBLE_BEEP); #ifdef ENABLE_VOICE - AUDIO_SetVoiceID(0, VOICE_ID_LOW_VOLTAGE); + AUDIO_SetVoiceID(0, VOICE_ID_LOW_VOLTAGE); #endif - } - if (gBatteryDisplayLevel == 0) - { + } + + if (gBatteryDisplayLevel != 0) { #ifdef ENABLE_VOICE - AUDIO_PlaySingleVoice(true); + AUDIO_PlaySingleVoice(false); #endif + return; + } - gReducedService = true; +#ifdef ENABLE_VOICE + AUDIO_PlaySingleVoice(true); +#endif - //if (gCurrentFunction != FUNCTION_POWER_SAVE) - FUNCTION_Select(FUNCTION_POWER_SAVE); + gReducedService = true; - ST7565_HardwareReset(); + FUNCTION_Select(FUNCTION_POWER_SAVE); - if (gEeprom.BACKLIGHT_TIME < (ARRAY_SIZE(gSubMenu_BACKLIGHT) - 1)) - BACKLIGHT_TurnOff(); // turn the backlight off - } -#ifdef ENABLE_VOICE - else - AUDIO_PlaySingleVoice(false); -#endif - } - } - } + ST7565_HardwareReset(); + + if (gEeprom.BACKLIGHT_TIME < (ARRAY_SIZE(gSubMenu_BACKLIGHT) - 1)) { + BACKLIGHT_TurnOff(); } -} \ No newline at end of file +} diff --git a/helper/battery.h b/helper/battery.h index c951bd96d..c21a14e43 100644 --- a/helper/battery.h +++ b/helper/battery.h @@ -40,9 +40,9 @@ typedef enum { BATTERY_TYPE_UNKNOWN } BATTERY_Type_t; -unsigned int BATTERY_VoltsToPercent(const unsigned int voltage_10mV); -void BATTERY_GetReadings(const bool bDisplayBatteryLevel); + +unsigned int BATTERY_VoltsToPercent(unsigned int voltage_10mV); +void BATTERY_GetReadings(bool bDisplayBatteryLevel); void BATTERY_TimeSlice500ms(void); #endif - diff --git a/ui/battery.c b/ui/battery.c index 1a3e8a079..1a3e23369 100644 --- a/ui/battery.c +++ b/ui/battery.c @@ -21,30 +21,29 @@ #include "driver/st7565.h" #include "functions.h" #include "ui/battery.h" +#include "../misc.h" void UI_DrawBattery(uint8_t* bitmap, uint8_t level, uint8_t blink) { if (level < 2 && blink == 1) { memset(bitmap, 0, sizeof(BITMAP_BatteryLevel1)); + return; } - else - { - memmove(bitmap, BITMAP_BatteryLevel1, sizeof(BITMAP_BatteryLevel1)); - if (level > 2) - { - unsigned int i; - uint8_t bars = level - 2; - if (bars > 4) - bars = 4; - for (i = 0; i < bars; i++) - { - #ifndef ENABLE_REVERSE_BAT_SYMBOL - memcpy(bitmap + sizeof(BITMAP_BatteryLevel1) - 4 - (i * 3), BITMAP_BatteryLevel, 2); - #else - memcpy(bitmap + 3 + (i * 3) + 0, BITMAP_BatteryLevel, 2); - #endif - } - } + + memcpy(bitmap, BITMAP_BatteryLevel1, sizeof(BITMAP_BatteryLevel1)); + + if (level <= 2) { + return; + } + + const uint8_t bars = MAX(4, level - 2); + + for (int i = 0; i < bars; i++) { +#ifndef ENABLE_REVERSE_BAT_SYMBOL + memcpy(bitmap + sizeof(BITMAP_BatteryLevel1) - 4 - (i * 3), BITMAP_BatteryLevel, 2); +#else + memcpy(bitmap + 3 + (i * 3) + 0, BITMAP_BatteryLevel, 2); +#endif } }