uv-k5-firmware-custom/helper/battery.c

226 lines
5.0 KiB
C
Raw Normal View History

2023-09-09 07:03:56 +00:00
/* Copyright 2023 Dual Tachyon
* https://github.com/DualTachyon
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2023-12-14 10:05:46 +00:00
#include <assert.h>
2023-09-09 07:03:56 +00:00
#include "battery.h"
#include "driver/backlight.h"
2023-10-31 18:58:10 +00:00
#include "driver/st7565.h"
#include "functions.h"
2023-09-09 07:03:56 +00:00
#include "misc.h"
#include "settings.h"
2023-09-09 07:03:56 +00:00
#include "ui/battery.h"
#include "ui/menu.h"
#include "ui/ui.h"
uint16_t gBatteryCalibration[6];
uint16_t gBatteryCurrentVoltage;
uint16_t gBatteryCurrent;
uint16_t gBatteryVoltages[4];
uint16_t gBatteryVoltageAverage;
uint8_t gBatteryDisplayLevel;
bool gChargingWithTypeC;
bool gLowBatteryBlink;
2023-10-31 23:11:04 +00:00
bool gLowBattery;
bool gLowBatteryConfirmed;
2023-09-09 07:03:56 +00:00
uint16_t gBatteryCheckCounter;
2023-10-31 23:11:04 +00:00
typedef enum {
BATTERY_LOW_INACTIVE,
BATTERY_LOW_ACTIVE,
BATTERY_LOW_CONFIRMED
} BatteryLow_t;
2023-10-31 18:58:10 +00:00
uint16_t lowBatteryCountdown;
2023-10-31 23:11:04 +00:00
const uint16_t lowBatteryPeriod = 30;
2023-10-31 18:58:10 +00:00
2023-09-21 22:06:47 +00:00
volatile uint16_t gPowerSave_10ms;
2023-09-09 07:03:56 +00:00
2023-12-12 21:39:30 +00:00
const uint16_t Voltage2PercentageTable[][7][2] = {
[BATTERY_TYPE_1600_MAH] = {
2023-11-01 23:54:48 +00:00
{828, 100},
{814, 97 },
{760, 25 },
{729, 6 },
{630, 0 },
2023-12-12 21:39:30 +00:00
{0, 0 },
{0, 0 },
},
2023-12-12 21:39:30 +00:00
[BATTERY_TYPE_2200_MAH] = {
2023-11-01 23:54:48 +00:00
{832, 100},
{813, 95 },
{740, 60 },
{707, 21 },
{682, 5 },
{630, 0 },
2023-12-12 21:39:30 +00:00
{0, 0 },
},
};
static_assert(ARRAY_SIZE(Voltage2PercentageTable[BATTERY_TYPE_1600_MAH]) ==
ARRAY_SIZE(Voltage2PercentageTable[BATTERY_TYPE_2200_MAH]));
2023-12-12 21:39:30 +00:00
unsigned int BATTERY_VoltsToPercent(const unsigned int voltage_10mV)
{
const uint16_t (*crv)[2] = Voltage2PercentageTable[gEeprom.BATTERY_TYPE];
const int mulipl = 1000;
2023-12-12 21:39:30 +00:00
for (unsigned int i = 1; i < ARRAY_SIZE(Voltage2PercentageTable[BATTERY_TYPE_2200_MAH]); i++) {
if (voltage_10mV > crv[i][0]) {
2023-12-12 21:39:30 +00:00
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);
}
}
2023-12-12 21:39:30 +00:00
return 0;
}
void BATTERY_GetReadings(const bool bDisplayBatteryLevel)
2023-09-09 07:03:56 +00:00
{
const uint8_t PreviousBatteryLevel = gBatteryDisplayLevel;
const uint16_t Voltage = (gBatteryVoltages[0] + gBatteryVoltages[1] + gBatteryVoltages[2] + gBatteryVoltages[3]) / 4;
2023-09-09 07:03:56 +00:00
gBatteryVoltageAverage = (Voltage * 760) / gBatteryCalibration[3];
if(gBatteryVoltageAverage > 890)
2023-11-02 01:44:05 +00:00
gBatteryDisplayLevel = 7; // battery overvoltage
2023-10-31 23:11:04 +00:00
else if(gBatteryVoltageAverage < 630)
gBatteryDisplayLevel = 0; // battery critical
else {
gBatteryDisplayLevel = 1;
2023-11-02 01:44:05 +00:00
const uint8_t levels[] = {5,17,41,65,88};
uint8_t perc = BATTERY_VoltsToPercent(gBatteryVoltageAverage);
2023-11-02 01:44:05 +00:00
for(uint8_t i = 6; i >= 1; i--){
if (perc > levels[i-2]) {
gBatteryDisplayLevel = i;
break;
}
2023-12-12 21:39:30 +00:00
}
2023-10-20 14:49:53 +00:00
}
2023-09-09 07:03:56 +00:00
if ((gScreenToDisplay == DISPLAY_MENU) && UI_MENU_GetCurrentMenuId() == MENU_VOL)
2023-09-09 07:03:56 +00:00
gUpdateDisplay = true;
if (gBatteryCurrent < 501)
{
if (gChargingWithTypeC)
2023-09-18 13:31:14 +00:00
{
gUpdateStatus = true;
gUpdateDisplay = true;
}
2023-09-18 13:31:14 +00:00
gChargingWithTypeC = false;
2023-09-09 07:03:56 +00:00
}
else
{
if (!gChargingWithTypeC)
{
2023-09-18 13:31:14 +00:00
gUpdateStatus = true;
gUpdateDisplay = true;
2023-09-09 07:03:56 +00:00
BACKLIGHT_TurnOn();
}
2023-09-18 13:31:14 +00:00
gChargingWithTypeC = true;
2023-09-09 07:03:56 +00:00
}
if (PreviousBatteryLevel != gBatteryDisplayLevel)
{
2023-10-31 23:11:04 +00:00
if(gBatteryDisplayLevel > 2)
gLowBatteryConfirmed = false;
else if (gBatteryDisplayLevel < 2)
2023-09-18 13:31:14 +00:00
{
2023-10-31 23:11:04 +00:00
gLowBattery = true;
2023-09-18 13:31:14 +00:00
}
2023-09-09 07:03:56 +00:00
else
{
2023-10-31 23:11:04 +00:00
gLowBattery = false;
2023-09-09 07:03:56 +00:00
if (bDisplayBatteryLevel)
UI_DisplayBattery(gBatteryDisplayLevel, gLowBatteryBlink);
2023-09-09 07:03:56 +00:00
}
2023-12-12 21:39:30 +00:00
2023-10-31 23:11:04 +00:00
if(!gLowBatteryConfirmed)
gUpdateDisplay = true;
2023-10-31 18:58:10 +00:00
lowBatteryCountdown = 0;
2023-09-09 07:03:56 +00:00
}
}
2023-10-31 18:58:10 +00:00
2023-12-12 21:39:30 +00:00
void BATTERY_TimeSlice500ms(void)
2023-10-31 18:58:10 +00:00
{
2023-12-12 21:39:30 +00:00
if (!gLowBattery) {
return;
}
2023-10-31 18:58:10 +00:00
2023-12-12 21:39:30 +00:00
gLowBatteryBlink = ++lowBatteryCountdown & 1;
2023-10-31 18:58:10 +00:00
2023-12-12 21:39:30 +00:00
UI_DisplayBattery(0, gLowBatteryBlink);
2023-10-31 18:58:10 +00:00
2023-12-12 21:39:30 +00:00
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);
2023-10-31 23:11:04 +00:00
#ifdef ENABLE_VOICE
2023-12-12 21:39:30 +00:00
AUDIO_SetVoiceID(0, VOICE_ID_LOW_VOLTAGE);
2023-10-31 23:11:04 +00:00
#endif
2023-12-12 21:39:30 +00:00
}
if (gBatteryDisplayLevel != 0) {
2023-10-31 23:11:04 +00:00
#ifdef ENABLE_VOICE
2023-12-12 21:39:30 +00:00
AUDIO_PlaySingleVoice(false);
2023-10-31 23:11:04 +00:00
#endif
2023-12-12 21:39:30 +00:00
return;
}
2023-10-31 18:58:10 +00:00
2023-12-12 21:39:30 +00:00
#ifdef ENABLE_VOICE
AUDIO_PlaySingleVoice(true);
#endif
2023-10-31 18:58:10 +00:00
2023-12-12 21:39:30 +00:00
gReducedService = true;
2023-10-31 18:58:10 +00:00
2023-12-12 21:39:30 +00:00
FUNCTION_Select(FUNCTION_POWER_SAVE);
2023-10-31 18:58:10 +00:00
2023-12-12 21:39:30 +00:00
ST7565_HardwareReset();
if (gEeprom.BACKLIGHT_TIME < (ARRAY_SIZE(gSubMenu_BACKLIGHT) - 1)) {
BACKLIGHT_TurnOff();
2023-10-31 18:58:10 +00:00
}
2023-12-12 21:39:30 +00:00
}