Skip to content

Commit 461a261

Browse files
committed
Update voltage monitoring to use esp libraries
1 parent badf437 commit 461a261

3 files changed

Lines changed: 41 additions & 13 deletions

File tree

tinyGS/src/ConfigManager/ConfigManager.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ ConfigManager::ConfigManager()
5353
: IotWebConf2(thingName, &dnsServer, &server, initialApPassword, configVersion), server(80), gsConfigHtmlFormatProvider(*this), boards({
5454
//OLED_add, OLED_SDA, OLED_SCL, OLED_RST, PROG_BUTTON, BOARD_LED, L_SX127X?, L_NSS, L_DI00, L_DI01, L_BUSSY, L_RST, L_MISO, L_MOSI, L_SCK, L_TCXO_V, RX_EN, TX_EN, ADC_CTL, BAT_AIN, VBAT_SCALE, BOARD
5555
#if CONFIG_IDF_TARGET_ESP32S3
56-
{ 0x3c, 17, 18, 21, 0, 35, RADIO_SX1262, 8, UNUSED, 14, 13, 12, 11, 10, 9, 1.6f, UNUSED, UNUSED, 37, 1, 0.00405f, "150–960Mhz - HELTEC LORA32 V3 SX1262" }, // SX1262
56+
{ 0x3c, 17, 18, 21, 0, 35, RADIO_SX1262, 8, UNUSED, 14, 13, 12, 11, 10, 9, 1.6f, UNUSED, UNUSED, 37, 1, 5.1205f, "150–960Mhz - HELTEC LORA32 V3 SX1262" }, // SX1262
5757
{ 0x3c, 17, 18, UNUSED, 0, 35, RADIO_SX1278, 8, 6, 14, UNUSED, 12, 11, 10, 9, 0.0f, UNUSED, UNUSED, UNUSED, UNUSED, UNUSED, "Custom ESP32-S3 433MHz SX1278" }, // SX1278 @g4lile0
5858
{ 0x3c, 17, 18, UNUSED, 0, 3, RADIO_SX1262, 10, UNUSED, 1, 4, 5, 13, 11, 12, 1.6f, UNUSED, UNUSED, UNUSED, UNUSED, UNUSED, "433 Mhz TTGO T-Beam Sup SX1262 V1.0" }, // SX1268 @ Stephen
5959
{ 0x3c, 17, 18, UNUSED, 0, 37, RADIO_SX1280, 7, UNUSED, 9, UNUSED, 8, 3, 6, 5, 0.0f, 21, 10, UNUSED, UNUSED, UNUSED, "2.4Ghz LILYGO SX1280" }, // SX1280 @ K4KDR
@@ -263,7 +263,11 @@ void ConfigManager::handleDashboard()
263263
s += "<tr><td>Radio </td><td>" + String(Radio::getInstance().isReady() ? "<span class='G'>READY</span>" : "<span class='R'>NOT READY</span>") + "</td></tr>";
264264
s += "<tr><td>Noise floor </td><td>" + String(status.modeminfo.currentRssi) + "</td></tr>";
265265
if (status.vbat > 0) {
266-
s += "<tr><td>Battery </td><td>" + String(status.vbat) + "V " + String(getBatteryPercentage()) + "&percnt;</td></tr>";
266+
if(getBatteryPercentage() > 100.0f) { // Charging
267+
s += "<tr><td>Power </td><td>USB " + String(status.vbat) + "V</span></td></tr>";
268+
} else {
269+
s += "<tr><td>Power </td><td>Bat " + String(getBatteryPercentage(), 0) + "&percnt;" + String(status.vbat) + "V</span></td></tr>";
270+
}
267271
} else {
268272
// Empty if battery monitoring not enabled
269273
s += "<tr><td></td><td></td></tr>";
@@ -505,7 +509,11 @@ void ConfigManager::handleRefreshWorldmap()
505509
radio.currentRssi ();
506510
data_string += String(status.modeminfo.currentRssi) + ",";
507511
if (status.vbat > 0) {
508-
data_string += String(status.vbat) + "V " + String(getBatteryPercentage()) + "&percnt;,";
512+
if(getBatteryPercentage() > 100.0f) { // Charging
513+
data_string += "USB " + String(status.vbat) + "V,";
514+
} else {
515+
data_string += "Bat " + String(getBatteryPercentage(), 0) + "&percnt; " + String(status.vbat) + "V,";
516+
}
509517
} else {
510518
// if battery monitoring not enabled
511519
data_string += ",";

tinyGS/src/Display/Display.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,11 @@ void drawFrame3(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int1
239239
if (status.vbat != 0.0)
240240
{
241241
display->setTextAlignment(TEXT_ALIGN_LEFT);
242-
display->drawString(x, 45 + y, "Bat: " + String(status.vbat) + "V " + (getBatteryPercentage()) + "%");
242+
if(getBatteryPercentage() > 100.0f) {
243+
display->drawString(x, 45 + y, "Pow: USB " + String(status.vbat) + "V");
244+
} else {
245+
display->drawString(x, 45 + y, "Pow: Bat " + String(getBatteryPercentage(), 0) + "% " + String(status.vbat) + "V");
246+
}
243247
}
244248
}
245249

tinyGS/src/Power/Battery.cpp

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
// Battery.cpp
22
// Battery monitoring helpers for tinyGS
33

4+
#include <stdlib.h>
45
#include "Battery.h"
56
#include "../ConfigManager/ConfigManager.h"
67
#include "../Logger/Logger.h"
8+
#include <esp_adc_cal.h>
9+
10+
esp_adc_cal_characteristics_t adc_chars;
11+
#define DEFAULT_VREF 1100
12+
713

814
// Arduino Framework Battery Writeup
915
// https://digitalconcepts.net.au/arduino/index.php?op=Battery
@@ -28,18 +34,20 @@ void initBatteryMonitoring(void)
2834
// Use 12-bit resolution and 11dB attenuation for ~0-3.6V range
2935
analogReadResolution(12);
3036
analogSetAttenuation(ADC_11db);
37+
// Characterize ADC for voltage conversion
38+
esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_11db, ADC_WIDTH_BIT_12, DEFAULT_VREF, &adc_chars);
3139
}
3240
checkBattery(); // Initial read to seed status.vbat
3341
}
3442
}
3543

3644
// Drive the ADC control pin to the enabled state (if present) and wait
37-
// briefly for voltages to stabilise before taking a reading.
45+
// briefly for voltages to stabilize before taking a reading.
3846
inline void enableADCReading(board_t board)
3947
{
4048
if (board.ADC_CTL != UNUSED) {
4149
digitalWrite(board.ADC_CTL, ADC_READ_ENABLE);
42-
delay(50); // Allow voltage to stabilise before sampling
50+
delay(50); // Allow voltage to stabilize before sampling
4351
}
4452
}
4553

@@ -69,13 +77,17 @@ void checkBattery(void)
6977

7078
enableADCReading(board);
7179

72-
// Read raw ADC and convert using board-specific scale factor
73-
uint16_t raw = analogRead(board.VBAT_AIN);
80+
// Read raw ADC
81+
int raw = adc1_get_raw(ADC1_CHANNEL_0);
7482

7583
disableADCReading(board);
7684

85+
//Convert adc_reading to voltage in mV
86+
uint32_t voltage = esp_adc_cal_raw_to_voltage(raw, &adc_chars);
87+
7788
// Convert and smooth: seed or simple average with previous value
78-
float measured = board.VBAT_SCALE * raw;
89+
float measured = (board.VBAT_SCALE * voltage) / 1000.0f; // convert mV to V
90+
Log::debug(PSTR("adc raw: %d, voltage: %f V"), raw, measured);
7991
if (status.vbat == 0.0) {
8092
status.vbat = measured; // initial seed
8193
} else {
@@ -87,13 +99,17 @@ void checkBattery(void)
8799
}
88100

89101
// Convert the current battery voltage status.vbat to a percentage.
90-
// Assumes a linear discharge curve between 3.0V (0%) and 3.7V (100%).
102+
// Assumes a linear discharge curve between 3.0V (0%) and 4.15V (100%).
103+
// Returns 999.0f if the voltage is above 4.2V, indicating charging state.
91104
float getBatteryPercentage(void)
92105
{
93-
const float maxVoltage = 3.7f; // Voltage at 100%
94-
const float minVoltage = 3.0f; // Voltage at 0%
106+
const float poweredVoltage = 4.23f; // Voltage above 100%
107+
const float maxVoltage = 4.15f; // Voltage at 100%
108+
const float minVoltage = 3.5f; // Voltage at 0%
95109
float voltage = status.vbat;
96-
if (voltage >= maxVoltage) {
110+
if (voltage >= poweredVoltage) {
111+
return 999.0f; // Plugged in / charging
112+
} else if (voltage >= maxVoltage) {
97113
return 100.0f;
98114
} else if (voltage <= minVoltage) {
99115
return 0.0f;

0 commit comments

Comments
 (0)