Simplify come battery logic
This commit is contained in:
158
helper/battery.c
158
helper/battery.c
@@ -49,48 +49,45 @@ const uint16_t lowBatteryPeriod = 30;
|
|||||||
volatile uint16_t gPowerSave_10ms;
|
volatile uint16_t gPowerSave_10ms;
|
||||||
|
|
||||||
|
|
||||||
unsigned int BATTERY_VoltsToPercent(const unsigned int voltage_10mV)
|
const uint16_t Voltage2PercentageTable[][7][2] = {
|
||||||
{
|
[BATTERY_TYPE_1600_MAH] = {
|
||||||
const uint16_t crv1600[][2] = {
|
|
||||||
{828, 100},
|
{828, 100},
|
||||||
{814, 97 },
|
{814, 97 },
|
||||||
{760, 25 },
|
{760, 25 },
|
||||||
{729, 6 },
|
{729, 6 },
|
||||||
{630, 0 },
|
{630, 0 },
|
||||||
{0, 0 }
|
{0, 0 },
|
||||||
};
|
{0, 0 },
|
||||||
|
},
|
||||||
|
|
||||||
const uint16_t crv2200[][2] = {
|
[BATTERY_TYPE_2200_MAH] = {
|
||||||
{832, 100},
|
{832, 100},
|
||||||
{813, 95 },
|
{813, 95 },
|
||||||
{740, 60 },
|
{740, 60 },
|
||||||
{707, 21 },
|
{707, 21 },
|
||||||
{682, 5 },
|
{682, 5 },
|
||||||
{630, 0 },
|
{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;
|
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]) {
|
if (voltage_10mV > crv[i][0]) {
|
||||||
int a = (crv[i - 1][1] - crv[i][1]) * mulipl / (crv[i - 1][0] - crv[i][0]);
|
const 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;
|
const int b = crv[i][1] - a * crv[i][0] / mulipl;
|
||||||
int p = a * voltage_10mV / mulipl + b;
|
const int p = a * voltage_10mV / mulipl + b;
|
||||||
return MIN(p, 100);
|
return MIN(p, 100);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,8 +96,6 @@ void BATTERY_GetReadings(const bool bDisplayBatteryLevel)
|
|||||||
const uint8_t PreviousBatteryLevel = gBatteryDisplayLevel;
|
const uint8_t PreviousBatteryLevel = gBatteryDisplayLevel;
|
||||||
const uint16_t Voltage = (gBatteryVoltages[0] + gBatteryVoltages[1] + gBatteryVoltages[2] + gBatteryVoltages[3]) / 4;
|
const uint16_t Voltage = (gBatteryVoltages[0] + gBatteryVoltages[1] + gBatteryVoltages[2] + gBatteryVoltages[3]) / 4;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
gBatteryVoltageAverage = (Voltage * 760) / gBatteryCalibration[3];
|
gBatteryVoltageAverage = (Voltage * 760) / gBatteryCalibration[3];
|
||||||
|
|
||||||
if(gBatteryVoltageAverage > 890)
|
if(gBatteryVoltageAverage > 890)
|
||||||
@@ -116,7 +111,7 @@ void BATTERY_GetReadings(const bool bDisplayBatteryLevel)
|
|||||||
gBatteryDisplayLevel = i;
|
gBatteryDisplayLevel = i;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -160,7 +155,7 @@ void BATTERY_GetReadings(const bool bDisplayBatteryLevel)
|
|||||||
if (bDisplayBatteryLevel)
|
if (bDisplayBatteryLevel)
|
||||||
UI_DisplayBattery(gBatteryDisplayLevel, gLowBatteryBlink);
|
UI_DisplayBattery(gBatteryDisplayLevel, gLowBatteryBlink);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!gLowBatteryConfirmed)
|
if(!gLowBatteryConfirmed)
|
||||||
gUpdateDisplay = true;
|
gUpdateDisplay = true;
|
||||||
|
|
||||||
@@ -168,56 +163,61 @@ void BATTERY_GetReadings(const bool bDisplayBatteryLevel)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void BATTERY_TimeSlice500ms(void)
|
void BATTERY_TimeSlice500ms(void)
|
||||||
{
|
{
|
||||||
if (gLowBattery)
|
if (!gLowBattery) {
|
||||||
{
|
return;
|
||||||
gLowBatteryBlink = ++lowBatteryCountdown & 1;
|
|
||||||
|
|
||||||
UI_DisplayBattery(0, gLowBatteryBlink);
|
|
||||||
|
|
||||||
if (gCurrentFunction != FUNCTION_TRANSMIT)
|
|
||||||
{ // not transmitting
|
|
||||||
|
|
||||||
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);
|
|
||||||
#ifdef ENABLE_VOICE
|
|
||||||
AUDIO_SetVoiceID(0, VOICE_ID_LOW_VOLTAGE);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
if (gBatteryDisplayLevel == 0)
|
|
||||||
{
|
|
||||||
#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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
gLowBatteryBlink = ++lowBatteryCountdown & 1;
|
||||||
|
|
||||||
|
UI_DisplayBattery(0, gLowBatteryBlink);
|
||||||
|
|
||||||
|
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);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
if (gBatteryDisplayLevel != 0) {
|
||||||
|
#ifdef ENABLE_VOICE
|
||||||
|
AUDIO_PlaySingleVoice(false);
|
||||||
|
#endif
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef ENABLE_VOICE
|
||||||
|
AUDIO_PlaySingleVoice(true);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
gReducedService = true;
|
||||||
|
|
||||||
|
FUNCTION_Select(FUNCTION_POWER_SAVE);
|
||||||
|
|
||||||
|
ST7565_HardwareReset();
|
||||||
|
|
||||||
|
if (gEeprom.BACKLIGHT_TIME < (ARRAY_SIZE(gSubMenu_BACKLIGHT) - 1)) {
|
||||||
|
BACKLIGHT_TurnOff();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -40,9 +40,9 @@ typedef enum {
|
|||||||
BATTERY_TYPE_UNKNOWN
|
BATTERY_TYPE_UNKNOWN
|
||||||
} BATTERY_Type_t;
|
} 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);
|
void BATTERY_TimeSlice500ms(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
35
ui/battery.c
35
ui/battery.c
@@ -21,30 +21,29 @@
|
|||||||
#include "driver/st7565.h"
|
#include "driver/st7565.h"
|
||||||
#include "functions.h"
|
#include "functions.h"
|
||||||
#include "ui/battery.h"
|
#include "ui/battery.h"
|
||||||
|
#include "../misc.h"
|
||||||
|
|
||||||
void UI_DrawBattery(uint8_t* bitmap, uint8_t level, uint8_t blink)
|
void UI_DrawBattery(uint8_t* bitmap, uint8_t level, uint8_t blink)
|
||||||
{
|
{
|
||||||
if (level < 2 && blink == 1) {
|
if (level < 2 && blink == 1) {
|
||||||
memset(bitmap, 0, sizeof(BITMAP_BatteryLevel1));
|
memset(bitmap, 0, sizeof(BITMAP_BatteryLevel1));
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
memcpy(bitmap, BITMAP_BatteryLevel1, sizeof(BITMAP_BatteryLevel1));
|
||||||
memmove(bitmap, BITMAP_BatteryLevel1, sizeof(BITMAP_BatteryLevel1));
|
|
||||||
if (level > 2)
|
if (level <= 2) {
|
||||||
{
|
return;
|
||||||
unsigned int i;
|
}
|
||||||
uint8_t bars = level - 2;
|
|
||||||
if (bars > 4)
|
const uint8_t bars = MAX(4, level - 2);
|
||||||
bars = 4;
|
|
||||||
for (i = 0; i < bars; i++)
|
for (int i = 0; i < bars; i++) {
|
||||||
{
|
#ifndef ENABLE_REVERSE_BAT_SYMBOL
|
||||||
#ifndef ENABLE_REVERSE_BAT_SYMBOL
|
memcpy(bitmap + sizeof(BITMAP_BatteryLevel1) - 4 - (i * 3), BITMAP_BatteryLevel, 2);
|
||||||
memcpy(bitmap + sizeof(BITMAP_BatteryLevel1) - 4 - (i * 3), BITMAP_BatteryLevel, 2);
|
#else
|
||||||
#else
|
memcpy(bitmap + 3 + (i * 3) + 0, BITMAP_BatteryLevel, 2);
|
||||||
memcpy(bitmap + 3 + (i * 3) + 0, BITMAP_BatteryLevel, 2);
|
#endif
|
||||||
#endif
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user