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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "battery.h"
|
|
|
|
#include "driver/backlight.h"
|
|
|
|
#include "misc.h"
|
2023-10-30 22:20:32 +00:00
|
|
|
#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 gLowBattery;
|
|
|
|
bool gLowBatteryBlink;
|
|
|
|
uint16_t gBatteryCheckCounter;
|
2023-09-18 07:30:24 +00:00
|
|
|
|
2023-09-21 22:06:47 +00:00
|
|
|
volatile uint16_t gPowerSave_10ms;
|
2023-09-09 07:03:56 +00:00
|
|
|
|
2023-10-01 21:14:45 +00:00
|
|
|
|
|
|
|
unsigned int BATTERY_VoltsToPercent(const unsigned int voltage_10mV)
|
|
|
|
{
|
2023-10-30 22:20:32 +00:00
|
|
|
const uint16_t crv1600[][2] = {
|
|
|
|
{814, 100},
|
|
|
|
{756, 24 },
|
|
|
|
{729, 7 },
|
2023-10-31 18:40:00 +00:00
|
|
|
{620, 0 },
|
2023-10-30 22:20:32 +00:00
|
|
|
{0, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
const uint16_t crv2200[][2] = {
|
|
|
|
{823, 100},
|
|
|
|
{740, 60},
|
|
|
|
{707, 21},
|
|
|
|
{680, 5},
|
2023-10-31 18:40:00 +00:00
|
|
|
{620, 0},
|
2023-10-30 22:20:32 +00:00
|
|
|
{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);
|
|
|
|
}
|
|
|
|
|
|
|
|
const int mulipl = 1000;
|
|
|
|
for (int i = 1; i < size; 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;
|
|
|
|
return MIN(p, 100);
|
|
|
|
}
|
|
|
|
}
|
2023-10-01 21:14:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BATTERY_GetReadings(const bool bDisplayBatteryLevel)
|
2023-09-09 07:03:56 +00:00
|
|
|
{
|
2023-09-25 06:12:08 +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
|
|
|
|
2023-10-31 18:40:00 +00:00
|
|
|
|
2023-09-25 06:12:08 +00:00
|
|
|
|
2023-10-31 18:40:00 +00:00
|
|
|
gBatteryVoltageAverage = (Voltage * 760) / gBatteryCalibration[3];
|
|
|
|
|
|
|
|
if(gBatteryVoltageAverage > 840)
|
|
|
|
gBatteryDisplayLevel = 6; // battery overvoltage
|
|
|
|
else if(gBatteryVoltageAverage < 620)
|
|
|
|
gBatteryDisplayLevel = 0; // battery critical
|
|
|
|
else {
|
|
|
|
gBatteryDisplayLevel = 1;
|
|
|
|
const uint8_t levels[] = {5,25,50,75};
|
|
|
|
uint8_t perc = BATTERY_VoltsToPercent(gBatteryVoltageAverage);
|
|
|
|
for(uint8_t i = 5; i >= 1; i--){
|
|
|
|
if (perc > levels[i-2]) {
|
|
|
|
gBatteryDisplayLevel = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-10-20 14:49:53 +00:00
|
|
|
}
|
2023-09-09 07:03:56 +00:00
|
|
|
|
|
|
|
|
2023-10-16 13:13:34 +00:00
|
|
|
if ((gScreenToDisplay == DISPLAY_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-25 06:12:08 +00:00
|
|
|
|
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-25 06:12:08 +00:00
|
|
|
|
2023-09-18 13:31:14 +00:00
|
|
|
gChargingWithTypeC = true;
|
2023-09-09 07:03:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (PreviousBatteryLevel != gBatteryDisplayLevel)
|
|
|
|
{
|
|
|
|
if (gBatteryDisplayLevel < 2)
|
2023-09-18 13:31:14 +00:00
|
|
|
{
|
2023-09-09 07:03:56 +00:00
|
|
|
gLowBattery = true;
|
2023-09-18 13:31:14 +00:00
|
|
|
}
|
2023-09-09 07:03:56 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
gLowBattery = false;
|
2023-09-25 06:12:08 +00:00
|
|
|
|
2023-09-09 07:03:56 +00:00
|
|
|
if (bDisplayBatteryLevel)
|
2023-09-25 06:12:08 +00:00
|
|
|
UI_DisplayBattery(gBatteryDisplayLevel, gLowBatteryBlink);
|
2023-09-09 07:03:56 +00:00
|
|
|
}
|
2023-09-25 06:12:08 +00:00
|
|
|
|
2023-09-09 07:03:56 +00:00
|
|
|
gLowBatteryCountdown = 0;
|
|
|
|
}
|
|
|
|
}
|