Make battery readout more accurate (closer to reality)

This commit is contained in:
Sjef Verhoeven PE5PVB
2024-05-30 11:40:34 +02:00
parent e611a5188a
commit 383eff8011

View File

@@ -416,6 +416,8 @@ void setup() {
gpio_set_drive_capability((gpio_num_t) 21, GPIO_DRIVE_CAP_0);
gpio_set_drive_capability((gpio_num_t) 22, GPIO_DRIVE_CAP_0);
gpio_set_drive_capability((gpio_num_t) 23, GPIO_DRIVE_CAP_0);
analogReadResolution(12);
setupmode = true;
EEPROM.begin(EE_TOTAL_CNT);
if (EEPROM.readByte(EE_BYTE_CHECKBYTE) != EE_CHECKBYTE_VALUE) DefaultSettings(hardwaremodel);
@@ -3667,14 +3669,22 @@ void ShowRSSI() {
}
void ShowBattery() {
static uint32_t batupdatetimer = 0;
if (millis() >= batupdatetimer + TIMER_BAT_TIMER) {
batupdatetimer = millis();
} else {
return;
}
uint16_t v = 0;
if (!wifi) v = analogRead(BATTERY_PIN);
uint32_t adcSum = 0;
for (int i = 0; i < 16; i++) {
adcSum += analogRead(BATTERY_PIN);
delay(1);
}
int v = adcSum / 16;
battery = map(constrain(v, BAT_LEVEL_EMPTY, BAT_LEVEL_FULL), BAT_LEVEL_EMPTY, BAT_LEVEL_FULL, 0, BAT_LEVEL_STAGE);
byte batteryprobe = map(constrain(v, BAT_LEVEL_EMPTY, BAT_LEVEL_FULL), BAT_LEVEL_EMPTY, BAT_LEVEL_FULL, 0, 20);
@@ -3688,7 +3698,7 @@ void ShowBattery() {
tft.fillRoundRect(313, 13, 4, 6, 2, ActiveColor);
}
if (batteryoptions != BATTERY_VALUE && batteryoptions != BATTERY_PERCENT && battery != 0) {
tft.fillRoundRect(279, 8, (battery * 8) , 16, 2, BarInsignificantColor);
tft.fillRoundRect(279, 8, (battery * 8), 16, 2, BarInsignificantColor);
} else {
tft.fillRoundRect(279, 8, 33, 16, 2, BackgroundColor);
}
@@ -3701,9 +3711,8 @@ void ShowBattery() {
batteryVold = 0;
vPerold = 0;
if (!wifi && batterydetect) {
float batteryV = constrain((((float)v / 4095.0) * 3.3 * (1100 / 1000.0) * 2.0), 0.0, 5.0);
float batteryV = constrain(v / 4095.0 * 3.3 * 2.0, 0.0, 5.0);
float vPer = constrain((batteryV - BATTERY_LOW_VALUE) / (BATTERY_FULL_VALUE - BATTERY_LOW_VALUE), 0.0, 0.99) * 100;
if (abs(batteryV - batteryVold) > 0.05 && batteryoptions == BATTERY_VALUE) {