Skip to content

Commit

Permalink
Simplify come battery logic
Browse files Browse the repository at this point in the history
  • Loading branch information
JuantAldea authored and egzumer committed Dec 13, 2023
1 parent a19579a commit 8c7f736
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 89 deletions.
136 changes: 68 additions & 68 deletions helper/battery.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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)
Expand All @@ -116,7 +111,7 @@ void BATTERY_GetReadings(const bool bDisplayBatteryLevel)
gBatteryDisplayLevel = i;
break;
}
}
}
}


Expand Down Expand Up @@ -160,64 +155,69 @@ void BATTERY_GetReadings(const bool bDisplayBatteryLevel)
if (bDisplayBatteryLevel)
UI_DisplayBattery(gBatteryDisplayLevel, gLowBatteryBlink);
}

if(!gLowBatteryConfirmed)
gUpdateDisplay = true;

lowBatteryCountdown = 0;
}
}

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();
}
}
}
6 changes: 3 additions & 3 deletions helper/battery.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

35 changes: 17 additions & 18 deletions ui/battery.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down

0 comments on commit 8c7f736

Please sign in to comment.