Simplify come battery logic

This commit is contained in:
Juan Antonio
2023-12-12 22:39:30 +01:00
committed by egzumer
parent a19579a888
commit 8c7f736797
3 changed files with 99 additions and 100 deletions

View File

@@ -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 }
{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);
}
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)
@@ -170,54 +165,59 @@ void BATTERY_GetReadings(const bool bDisplayBatteryLevel)
void BATTERY_TimeSlice500ms(void)
{
if (gLowBattery)
{
if (!gLowBattery) {
return;
}
gLowBatteryBlink = ++lowBatteryCountdown & 1;
UI_DisplayBattery(0, gLowBatteryBlink);
if (gCurrentFunction != FUNCTION_TRANSMIT)
{ // not transmitting
if (gCurrentFunction == FUNCTION_TRANSMIT) {
return;
}
if (lowBatteryCountdown < lowBatteryPeriod)
{
if (lowBatteryCountdown == lowBatteryPeriod-1 && !gChargingWithTypeC && !gLowBatteryConfirmed)
// not transmitting
if (lowBatteryCountdown < lowBatteryPeriod) {
if (lowBatteryCountdown == lowBatteryPeriod-1 && !gChargingWithTypeC && !gLowBatteryConfirmed) {
AUDIO_PlayBeep(BEEP_500HZ_60MS_DOUBLE_BEEP);
}
else
{
return;
}
lowBatteryCountdown = 0;
if (!gChargingWithTypeC)
{ // not on charge
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);
#endif
}
if (gBatteryDisplayLevel == 0)
{
if (gBatteryDisplayLevel != 0) {
#ifdef ENABLE_VOICE
AUDIO_PlaySingleVoice(false);
#endif
return;
}
#ifdef ENABLE_VOICE
AUDIO_PlaySingleVoice(true);
#endif
gReducedService = true;
//if (gCurrentFunction != FUNCTION_POWER_SAVE)
FUNCTION_Select(FUNCTION_POWER_SAVE);
ST7565_HardwareReset();
if (gEeprom.BACKLIGHT_TIME < (ARRAY_SIZE(gSubMenu_BACKLIGHT) - 1))
BACKLIGHT_TurnOff(); // turn the backlight off
}
#ifdef ENABLE_VOICE
else
AUDIO_PlaySingleVoice(false);
#endif
}
}
}
if (gEeprom.BACKLIGHT_TIME < (ARRAY_SIZE(gSubMenu_BACKLIGHT) - 1)) {
BACKLIGHT_TurnOff();
}
}

View File

@@ -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

View File

@@ -21,23 +21,24 @@
#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++)
{
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
@@ -45,8 +46,6 @@ void UI_DrawBattery(uint8_t* bitmap, uint8_t level, uint8_t blink)
#endif
}
}
}
}
void UI_DisplayBattery(uint8_t level, uint8_t blink)
{