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

121 lines
3.1 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.
*/
#include "battery.h"
#include "driver/backlight.h"
#include "misc.h"
#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-21 22:06:47 +00:00
volatile uint16_t gPowerSave_10ms;
2023-09-09 07:03:56 +00:00
void BATTERY_GetReadings(bool bDisplayBatteryLevel)
{
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
gBatteryDisplayLevel = 0;
if (Voltage >= gBatteryCalibration[5])
gBatteryDisplayLevel = 11;
else
if (Voltage >= ((gBatteryCalibration[4] + gBatteryCalibration[5]) / 2))
gBatteryDisplayLevel = 10;
else
if (Voltage >= gBatteryCalibration[4])
gBatteryDisplayLevel = 9;
else
if (Voltage >= ((gBatteryCalibration[3] + gBatteryCalibration[4]) / 2))
gBatteryDisplayLevel = 8;
else
if (Voltage >= gBatteryCalibration[3])
gBatteryDisplayLevel = 7;
else
if (Voltage >= ((gBatteryCalibration[2] + gBatteryCalibration[3]) / 2))
2023-09-09 07:03:56 +00:00
gBatteryDisplayLevel = 6;
else
if (Voltage >= gBatteryCalibration[2])
2023-09-09 07:03:56 +00:00
gBatteryDisplayLevel = 5;
else
if (Voltage >= ((gBatteryCalibration[1] + gBatteryCalibration[2]) / 2))
2023-09-09 07:03:56 +00:00
gBatteryDisplayLevel = 4;
else
if (Voltage >= gBatteryCalibration[1])
2023-09-09 07:03:56 +00:00
gBatteryDisplayLevel = 3;
else
if (Voltage >= ((gBatteryCalibration[0] + gBatteryCalibration[1]) / 2))
2023-09-09 07:03:56 +00:00
gBatteryDisplayLevel = 2;
else
if (Voltage >= gBatteryCalibration[0])
2023-09-09 07:03:56 +00:00
gBatteryDisplayLevel = 1;
gBatteryVoltageAverage = (Voltage * 760) / gBatteryCalibration[3];
if ((gScreenToDisplay == DISPLAY_MENU) && gMenuCursor == MENU_VOL)
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)
{
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-09 07:03:56 +00:00
if (bDisplayBatteryLevel)
UI_DisplayBattery(gBatteryDisplayLevel, gLowBatteryBlink);
2023-09-09 07:03:56 +00:00
}
2023-09-09 07:03:56 +00:00
gLowBatteryCountdown = 0;
}
}