Skip to content

Commit

Permalink
Fix Battery Bar ???
Browse files Browse the repository at this point in the history
  • Loading branch information
spm81 committed Feb 22, 2024
1 parent c2bf47e commit d38da83
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 7 deletions.
6 changes: 2 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

TARGET = firmware

#This is not being used anywhere
#ENABLE_LTO := 1

#======== STOCK QUANSHENG FERATURES ========#
ENABLE_AIRCOPY := 0
Expand All @@ -32,13 +30,13 @@ ENABLE_AM_FIX_ON_SPECTRUM := 1
ENABLE_SQUELCH_MORE_SENSITIVE := 0
# Restore FM in 1 second after RX - 0 bytes
ENABLE_FMRADIO_FAST_RESTORE := 1

# Scan List Editor
ENABLE_SCANLIST := 0

# Battery percentage - 296 bytes
ENABLE_STATUS_BATTERY_PERC := 1
# Show current while charging - 136 bytes Thanks Tunas1337
ENABLE_BATTERY_CHARGING := 0
ENABLE_BATTERY_CHARGING := 1
# Invert LCD Colors
ENABLE_LCD_INVERT_OPTION := 0
#ENABLE_LCD_CONTRAST_OPTION := 0 # WIP
Expand Down
10 changes: 7 additions & 3 deletions app/spectrum.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include "../app/spectrum.h"
#include "../helper/battery.h"
#include "finput.h"
#include <string.h>
#define F_MIN FrequencyBandTable[0].lower
Expand Down Expand Up @@ -692,6 +693,7 @@ static void DrawSpectrum() {
}
}

/*
static void UpdateBatteryInfo() {
for (uint8_t i = 0; i < 4; i++) {
BOARD_ADC_GetBatteryInfo(&gBatteryVoltages[i], &gBatteryCurrent);
Expand All @@ -707,7 +709,7 @@ static void UpdateBatteryInfo() {
}
}
}

*/
static void DrawStatus() {

if (currentState == SPECTRUM) {
Expand Down Expand Up @@ -1202,7 +1204,8 @@ void OnKeyDownStill(KEY_Code_t key) {
case KEY_PTT:
#endif
// start transmit
UpdateBatteryInfo();
// UpdateBatteryInfo();
BATTERY_GetReadings(true);
if (gBatteryDisplayLevel == 6) {
txAllowState = VFO_STATE_VOL_HIGH;
} else if (IsTXAllowed(GetOffsetedF(gCurrentVfo, fMeasure))) {
Expand Down Expand Up @@ -1542,7 +1545,8 @@ static void Tick() {
}
if (++batteryUpdateTimer > 4096) {
batteryUpdateTimer = 0;
UpdateBatteryInfo();
//UpdateBatteryInfo();
BATTERY_GetReadings(true);
redrawStatus = true;
}
if (redrawStatus) {
Expand Down
111 changes: 111 additions & 0 deletions helper/battery.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ bool gLowBatteryConfirmed;
uint16_t gBatteryCheckCounter;
volatile uint16_t gBatterySave;

#define OVERVOLTAGE_THRESHOLD 890
#define BATTERY_OVERVOLTAGE 7
#define CRITICAL_THRESHOLD 630
#define BATTERY_CRITICAL 0
#define CHARGING_THRESHOLD 501
#define LOW_BATTERY_THRESHOLD 2


typedef enum {
BATTERY_LOW_INACTIVE,
Expand Down Expand Up @@ -234,6 +241,7 @@ void BATTERY_GetReadings(bool bDisplayBatteryLevel)
*/
////1

/*
void BATTERY_GetReadings(bool bDisplayBatteryLevel) {
uint8_t PreviousBatteryLevel = gBatteryDisplayLevel;
uint16_t Voltage = Mid(gBatteryVoltages, ARRAY_SIZE(gBatteryVoltages));
Expand Down Expand Up @@ -276,4 +284,107 @@ void BATTERY_GetReadings(bool bDisplayBatteryLevel) {
}
}
*/

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)
gBatteryDisplayLevel = 7; // battery overvoltage
else if(gBatteryVoltageAverage < 630)
gBatteryDisplayLevel = 0; // battery critical
else {
gBatteryDisplayLevel = 1;
const uint8_t levels[] = {5,25,50,75,95};
uint8_t perc = BATTERY_VoltsToPercent(gBatteryVoltageAverage);
for(uint8_t i = 6; i >= 1; i--){
if (perc > levels[i-2]) {
gBatteryDisplayLevel = i;
break;
}
}
}


if (gScreenToDisplay == DISPLAY_MENU && gMenuCursor == MENU_VOL)
gUpdateDisplay = true;

if ((gBatteryCurrent < 501) != gChargingWithTypeC) {
gUpdateStatus = true;
gChargingWithTypeC = (gBatteryCurrent >= 501);
if (!gChargingWithTypeC)
BACKLIGHT_TurnOn();
}

if (PreviousBatteryLevel != gBatteryDisplayLevel) {
gLowBattery = (gBatteryDisplayLevel < 2);
gLowBatteryCountdown = 0;
if (!gLowBattery && bDisplayBatteryLevel)
UI_DisplayBattery(gBatteryDisplayLevel);
}
}
/*
void BATTERY_GetReadings(bool bDisplayBatteryLevel) {
uint8_t PreviousBatteryLevel = gBatteryDisplayLevel;
// Calcula a média das voltagens da bateria
const uint16_t Voltage = (gBatteryVoltages[0] + gBatteryVoltages[1] + gBatteryVoltages[2] + gBatteryVoltages[3]) / 4;
// Calcula a média da voltagem ajustada
gBatteryVoltageAverage = (Voltage * 760) / gBatteryCalibration[3];
// Verifica o nível de bateria com base na voltagem média
if(gBatteryVoltageAverage > OVERVOLTAGE_THRESHOLD)
gBatteryDisplayLevel = BATTERY_OVERVOLTAGE;
else if(gBatteryVoltageAverage < CRITICAL_THRESHOLD)
gBatteryDisplayLevel = BATTERY_CRITICAL;
else {
// Calcula o nível de bateria com base na porcentagem de voltagem média
gBatteryDisplayLevel = 1;
const uint8_t batteryLevels[] = {5, 25, 50, 75, 88};
uint8_t percentage = BATTERY_VoltsToPercent(gBatteryVoltageAverage);
for(uint8_t i = 6; i >= 1; i--) {
if (percentage > batteryLevels[i-2]) {
gBatteryDisplayLevel = i;
break;
}
}
}
// Verifica se o menu de exibição está ativo para atualização
if ((gScreenToDisplay == DISPLAY_MENU) && (gMenuCursor == MENU_VOL))
gUpdateDisplay = true;
// Verifica o status de carregamento e atualiza conforme necessário
if (gBatteryCurrent < CHARGING_THRESHOLD) {
if (gChargingWithTypeC)
gUpdateStatus = true;
gChargingWithTypeC = false;
} else {
if (!gChargingWithTypeC) {
gUpdateStatus = true;
BACKLIGHT_TurnOn();
}
gChargingWithTypeC = true;
}
// Verifica se houve mudanças no nível de bateria e atualiza os estados correspondentes
if (PreviousBatteryLevel != gBatteryDisplayLevel) {
if(gBatteryDisplayLevel > LOW_BATTERY_THRESHOLD)
gLowBatteryConfirmed = false;
else if (gBatteryDisplayLevel < LOW_BATTERY_THRESHOLD)
gLowBattery = true;
else {
gLowBattery = false;
if (bDisplayBatteryLevel)
UI_DisplayBattery(gBatteryDisplayLevel);
}
gLowBatteryCountdown = 0;
}
}
*/

0 comments on commit d38da83

Please sign in to comment.