From 6d26997fa4d6cefa591ba9f9273e3a096ad966cd Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Sat, 7 Oct 2023 19:45:55 +0200 Subject: [PATCH] Added battery state reporting over USB/Serial/Bluetooth. Implements #39. --- Config.h | 2 +- Framing.h | 1 + Power.h | 12 ++++++++++++ Utilities.h | 10 ++++++++++ 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/Config.h b/Config.h index dd3c8b3..448a1ca 100644 --- a/Config.h +++ b/Config.h @@ -19,7 +19,7 @@ #define CONFIG_H #define MAJ_VERS 0x01 - #define MIN_VERS 0x40 + #define MIN_VERS 0x41 #define PLATFORM_AVR 0x90 #define PLATFORM_ESP32 0x80 diff --git a/Framing.h b/Framing.h index d95bf93..0236aa9 100644 --- a/Framing.h +++ b/Framing.h @@ -44,6 +44,7 @@ #define CMD_STAT_SNR 0x24 #define CMD_STAT_CHTM 0x25 #define CMD_STAT_PHYPRM 0x26 + #define CMD_STAT_BAT 0x27 #define CMD_BLINK 0x30 #define CMD_RANDOM 0x40 diff --git a/Power.h b/Power.h index 0822aaf..3fda5af 100644 --- a/Power.h +++ b/Power.h @@ -32,6 +32,9 @@ uint32_t last_pmu_update = 0; uint8_t pmu_target_pps = 1; int pmu_update_interval = 1000/pmu_target_pps; +uint8_t pmu_rc = 0; +#define PMU_R_INTERVAL 5 +void kiss_indicate_battery(); void measure_battery() { #if BOARD_MODEL == BOARD_RNODE_NG_21 || BOARD_MODEL == BOARD_LORA32_V2_1 @@ -82,6 +85,8 @@ void measure_battery() { #endif } + + // if (bt_state == BT_STATE_CONNECTED) { // SerialBT.printf("Bus voltage %.3fv. Unfiltered %.3fv.", battery_voltage, bat_v_samples[BAT_SAMPLES-1]); // if (bat_voltage_dropping) { @@ -154,6 +159,13 @@ void measure_battery() { // SerialBT.println(""); // } #endif + + if (battery_ready) { + pmu_rc++; + if (pmu_rc%PMU_R_INTERVAL == 0) { + kiss_indicate_battery(); + } + } } void update_pmu() { diff --git a/Utilities.h b/Utilities.h index 0c7f741..e76f91c 100644 --- a/Utilities.h +++ b/Utilities.h @@ -755,6 +755,16 @@ void kiss_indicate_phy_stats() { #endif } +void kiss_indicate_battery() { + #if MCU_VARIANT == MCU_ESP32 + serial_write(FEND); + serial_write(CMD_STAT_BAT); + escaped_serial_write(battery_state); + escaped_serial_write((uint8_t)int(battery_percent)); + serial_write(FEND); + #endif +} + void kiss_indicate_btpin() { #if HAS_BLUETOOTH serial_write(FEND);