diff --git a/src/config/config.h b/src/config/config.h index 657e396..f29d37b 100644 --- a/src/config/config.h +++ b/src/config/config.h @@ -7,6 +7,7 @@ #define IO_TXD2 17 #define I2C_SDA 39 #define I2C_SCL 38 +#define BAT_PIN 4 // UWB Configuration #ifndef UWB_INDEX @@ -26,6 +27,7 @@ #define DISPLAY_UPDATE_INTERVAL 500 #define DEVICE_TIMEOUT 5000 #define SLEEP_DURATION 5000 +#define BATTERY_UPDATE_INTERVAL 30000 // Display Configuration #define SCREEN_WIDTH 128 diff --git a/src/main.cpp b/src/main.cpp index b2e8370..c3d88cd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,6 +15,8 @@ void checkTimeouts(); void processAnchorBehavior(); void processTagBehavior(); void handleSerialPassthrough(); +void updateBatteryLevel(); +void drawBatteryIcon(int x, int y, int percent); // Hardware setup HardwareSerial uwbSerial(2); @@ -35,6 +37,9 @@ DeviceData anchors[MAX_ANCHORS]; String response = ""; unsigned long lastDisplayUpdate = 0; unsigned long lastPositioningUpdate = 0; +unsigned long lastBatteryUpdate = 0; +float batteryVoltage = 0.0; +int batteryPercent = 0; void setup() { Serial.begin(SERIAL_BAUD); @@ -122,6 +127,9 @@ void setup() { } #endif + // Initial battery reading + updateBatteryLevel(); + Serial.printf("%s ready! Network ID: %d\n", deviceConfig.deviceName, NETWORK_ID); #if DEVICE_TYPE == DEVICE_ANCHOR @@ -155,6 +163,12 @@ void loop() { checkTimeouts(); handleSerialPassthrough(); + // Update battery level periodically + if (millis() - lastBatteryUpdate > BATTERY_UPDATE_INTERVAL) { + updateBatteryLevel(); + lastBatteryUpdate = millis(); + } + // Execute device-specific behavior if (DEVICE_IS_ANCHOR) { processAnchorBehavior(); @@ -209,10 +223,14 @@ void updateDisplay() { display.setTextSize(1); display.setTextColor(SSD1306_WHITE); - // Header + // Header - Device info with battery display.setCursor(0, 0); - display.printf("%s %d (Net:%d)", deviceConfig.deviceName, deviceConfig.deviceId, NETWORK_ID); + display.printf("%s %d", deviceConfig.deviceName, deviceConfig.deviceId); + // Battery indicator in top right + drawBatteryIcon(80, 0, batteryPercent); + + // Status line display.setCursor(0, 10); display.println(deviceConfig.statusDisplayPrefix); @@ -375,4 +393,23 @@ void showStartupScreen() { delay(2000); } +void updateBatteryLevel() { + batteryVoltage = 3.3 * analogRead(BAT_PIN) / 4096.0 * 2; + batteryPercent = constrain(map((int)(batteryVoltage * 100), 300, 420, 0, 100), 0, 100); +} + +void drawBatteryIcon(int x, int y, int percent) { + display.drawRect(x, y, 18, 10, SSD1306_WHITE); + display.fillRect(x + 18, y + 3, 2, 4, SSD1306_WHITE); + + int fillWidth = (14 * percent) / 100; + if (fillWidth > 0) { + display.fillRect(x + 2, y + 2, fillWidth, 6, SSD1306_WHITE); + } + + display.setCursor(x + 22, y + 1); + display.setTextSize(1); + display.printf("%d%%", percent); +} + // Cleanup - no dynamic allocation needed \ No newline at end of file