Fixes maintainer requests

This commit is contained in:
Justin Mitchell
2026-02-13 19:04:58 -05:00
parent 4f0a39ab03
commit a78897478f
11 changed files with 46 additions and 102 deletions
+23 -2
View File
@@ -1,5 +1,6 @@
#include <HalGPIO.h>
#include <SPI.h>
#include <Wire.h>
#include <esp_sleep.h>
void HalGPIO::begin() {
@@ -14,6 +15,16 @@ void HalGPIO::begin() {
pinMode(_batteryPin, INPUT);
pinMode(UART0_RXD, INPUT);
// I2C init must come AFTER pinMode(UART0_RXD) because GPIO20 is shared
// between USB detection (digital read) and I2C SDA. Wire.begin()
// reconfigures the pin for I2C, so it must run last.
if (_deviceType == DeviceType::X3) {
Wire.begin(20, 0, 400000);
_useI2C = true;
_i2cAddr = 0x55;
_socRegister = 0x2C;
}
}
void HalGPIO::update() { inputMgr.update(); }
@@ -43,8 +54,18 @@ void HalGPIO::startDeepSleep() {
}
int HalGPIO::getBatteryPercentage() const {
if (_deviceType == DeviceType::X3) {
return 0;
if (_useI2C) {
// Read SOC directly from I2C fuel gauge (16-bit LE register).
// Returns 0 on I2C error so the UI shows 0% rather than crashing.
Wire.beginTransmission(_i2cAddr);
Wire.write(_socRegister);
if (Wire.endTransmission(false) != 0) return 0;
Wire.requestFrom(_i2cAddr, (uint8_t)2);
if (Wire.available() < 2) return 0;
const uint8_t lo = Wire.read();
const uint8_t hi = Wire.read();
const uint16_t soc = (hi << 8) | lo;
return soc > 100 ? 100 : soc;
}
static const BatteryMonitor bat(BAT_GPIO0);
return bat.readPercentage();