Restructure project

This commit is contained in:
2026-03-01 18:24:12 +01:00
parent 3263b12f73
commit 0ad07ac8ce
16 changed files with 1679 additions and 1524 deletions

46
source/battery.cpp Normal file
View File

@@ -0,0 +1,46 @@
#include "battery.h"
#ifdef FEATURE_BATTERY_MONITOR
#include <bluefruit.h>
extern BLEBas blebas;
// Battery ADC is kept permanently configured at 12-bit / AR_INTERNAL_3_0 to avoid
// the SAADC re-init cost of analogReference() changes, which blocks the CPU for
// several ms and causes BLE connection-interval violations (visible mouse freeze).
// PIN_VBAT_ENABLE is held LOW permanently once battery monitoring starts.
void initBatteryADC() {
pinMode(PIN_VBAT_ENABLE, OUTPUT); digitalWrite(PIN_VBAT_ENABLE, LOW);
pinMode(PIN_VBAT_READ, INPUT);
analogReference(AR_INTERNAL_3_0); analogReadResolution(12);
// Warm up with a few reads (no delay — just discard results)
for (int i=0; i<8; i++) analogRead(PIN_VBAT_READ);
}
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;
return (raw / 4096.0f) * 3.0f * 2.0f;
}
int batteryPercent(float v) {
return (int)constrain((v - BATT_EMPTY) / (BATT_FULL - BATT_EMPTY) * 100.f, 0, 100);
}
void updateBattery() {
float v = readBatteryVoltage(); int pct = batteryPercent(v);
bool chg = (digitalRead(PIN_CHG) == LOW);
ChargeStatus status = chg ? (pct >= 99 ? CHGSTAT_FULL : CHGSTAT_CHARGING) : CHGSTAT_DISCHARGING;
blebas.write(pct);
lastChargeStatus = status;
#ifdef FEATURE_TELEMETRY
telem.chargeStatus = (uint8_t)status;
#endif
const char* st[] = {"discharging","charging","full"};
Serial.print("[BATT] "); Serial.print(v,2); Serial.print("V ");
Serial.print(pct); Serial.print("% "); Serial.println(st[status]);
if (status == CHGSTAT_DISCHARGING && v < BATT_CRITICAL)
for (int i=0; i<6; i++) { digitalWrite(LED_RED,LOW); delay(80); digitalWrite(LED_RED,HIGH); delay(80); }
}
#endif // FEATURE_BATTERY_MONITOR