Add battery voltage reading to web UI, apply fixed VBAT multiplier, closes #3

This commit is contained in:
2026-03-02 21:01:58 +01:00
parent c41a2932ba
commit 6ecae74483
6 changed files with 43 additions and 8 deletions

View File

@@ -20,8 +20,10 @@ void initBatteryADC() {
float readBatteryVoltage() {
// 8 quick reads, no delay() calls, no analogReference() change
int32_t raw=0; for (int i=0; i<8; i++) raw += analogRead(PIN_VBAT_READ); raw /= 8;
// Seeed XIAO nRF52840 Sense: 1MΩ + 510kΩ voltage divider on VBAT → multiply by 1510/510
return (raw / 4096.0f) * 3.0f * (1510.0f / 510.0f);
// Seeed XIAO nRF52840 Sense: 1MΩ + 510kΩ voltage divider on VBAT.
// Theoretical ratio is 1510/510 = 2.961, but real resistor tolerances
// shift the actual ratio. Calibrated: 3.90V actual / 3.78V reported → ×1.0317.
return (raw / 4096.0f) * 3.0f * (1510.0f / 510.0f) * 1.0317f;
}
int batteryPercent(float v) {

View File

@@ -1,5 +1,6 @@
#include "ble_config.h"
#include "tap.h"
#include "battery.h"
#include <Adafruit_LittleFS.h>
#include <InternalFileSystem.h>
@@ -200,6 +201,9 @@ void pushTelemetry(unsigned long now) {
telem.leftClicks = statLeftClicks;
telem.rightClicks = statRightClicks;
#endif
#ifdef FEATURE_BATTERY_MONITOR
telem.battVoltage = readBatteryVoltage();
#endif
cfgTelemetry.write ((uint8_t*)&telem, sizeof(telem));
cfgTelemetry.notify((uint8_t*)&telem, sizeof(telem));
}

View File

@@ -114,8 +114,9 @@ struct __attribute__((packed)) TelemetryPacket {
uint16_t recalCount; // [20]
uint8_t chargeStatus; // [22]
uint8_t _pad; // [23]
float battVoltage; // [24] raw battery voltage (V)
};
static_assert(sizeof(TelemetryPacket) == 24, "TelemetryPacket must be 24 bytes");
static_assert(sizeof(TelemetryPacket) == 28, "TelemetryPacket must be 28 bytes");
extern TelemetryPacket telem;
#endif