Add battery indicator, fix display

- Add battery icon with percentage display on main screen
- Implement battery voltage reading via ADC pin 4
- Remove network ID from main display to reduce clutter
- Reorganize UI layout for better space utilization:
  * Move battery icon to prevent text overflow
  * Compact header to single device info line
  * Gain 10 pixels vertical space for device list
- Battery updates every 30 seconds to conserve power
- Network ID still visible on 2-second startup screen

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
martin 2025-08-31 18:57:22 +02:00
commit 326d49a653
2 changed files with 41 additions and 2 deletions

View file

@ -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

View file

@ -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