From 4649c796b9238ac7429ca3f2ce6f3eb4acfed5eb Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sun, 22 Jul 2018 08:41:41 +0200 Subject: [PATCH] battery monitor --- src/display.cpp | 6 ++---- src/display.h | 2 +- src/globals.h | 4 ++-- src/main.cpp | 48 +++++++++++++++++++++++++++++++++++++++++------- 4 files changed, 46 insertions(+), 14 deletions(-) diff --git a/src/display.cpp b/src/display.cpp index a5116041..52decae1 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -89,7 +89,7 @@ void init_display(const char *Productname, const char *Version) { } // init_display -void refreshDisplay() { +void refreshtheDisplay() { // set display on/off according to current device configuration if (DisplayState != cfg.screenon) { @@ -109,13 +109,11 @@ void refreshDisplay() { u8x8.draw2x2String(0, 0, buff); // display number on unique macs total Wifi + BLE -/* // update Battery status (line 2) #ifdef HAS_BATTERY_PROBE u8x8.setCursor(0, 2); - u8x8.printf("B:%.1fV", read_voltage() / 1000.0); + u8x8.printf("B:%.1fV", batt_voltage / 1000.0); #endif -*/ // update GPS status (line 2) #ifdef HAS_GPS diff --git a/src/display.h b/src/display.h index a444de7b..c88d9315 100644 --- a/src/display.h +++ b/src/display.h @@ -6,7 +6,7 @@ extern uint8_t DisplayState; void init_display(const char *Productname, const char *Version); -void refreshDisplay(void); +void refreshtheDisplay(void); void DisplayKey(const uint8_t *key, uint8_t len, bool lsb); #endif \ No newline at end of file diff --git a/src/globals.h b/src/globals.h index b02bc37a..10324461 100644 --- a/src/globals.h +++ b/src/globals.h @@ -40,8 +40,8 @@ typedef struct { extern configData_t cfg; // current device configuration extern char display_line6[], display_line7[]; // screen buffers extern uint8_t channel; // wifi channel rotation counter -extern uint16_t macs_total, macs_wifi, macs_ble; // display values -extern std::set macs; // temp storage for MACs +extern uint16_t macs_total, macs_wifi, macs_ble, batt_voltage; // display values +extern std::set macs; // temp storage for MACs extern hw_timer_t *channelSwitch, *sendCycle; extern portMUX_TYPE timerMux; diff --git a/src/main.cpp b/src/main.cpp index 55ba10d2..8e124d8b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -30,13 +30,16 @@ licenses. Refer to LICENSE.txt file in repository for more details. configData_t cfg; // struct holds current device configuration char display_line6[16], display_line7[16]; // display buffers uint8_t channel = 0; // channel rotation counter -uint16_t macs_total = 0, macs_wifi = 0, macs_ble = 0; // globals for display -hw_timer_t *channelSwitch = NULL, *displaytimer = NULL, - *sendCycle = NULL; // configure hardware timer for cyclic tasks +uint16_t macs_total = 0, macs_wifi = 0, macs_ble = 0, + batt_voltage = 0; // globals for display + +// hardware timer for cyclic tasks +hw_timer_t *channelSwitch = NULL, *displaytimer = NULL, *sendCycle = NULL, + *battCycle = NULL; // this variables will be changed in the ISR, and read in main loop static volatile int ButtonPressedIRQ = 0, ChannelTimerIRQ = 0, - SendCycleTimerIRQ = 0, DisplayTimerIRQ = 0; + SendCycleTimerIRQ = 0, DisplayTimerIRQ = 0, BattReadIRQ = 0; portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED; // sync main loop and ISR when modifying IRQ @@ -116,17 +119,30 @@ void IRAM_ATTR DisplayIRQ() { DisplayTimerIRQ++; portEXIT_CRITICAL_ISR(&timerMux); } - void updateDisplay() { - // refresh display according to refresh cycle setting if (DisplayTimerIRQ) { portENTER_CRITICAL(&timerMux); DisplayTimerIRQ = 0; portEXIT_CRITICAL(&timerMux); - refreshDisplay(); + refreshtheDisplay(); } } +#endif +#ifdef HAS_BATTERY_PROBE +void IRAM_ATTR BattCycleIRQ() { + portENTER_CRITICAL(&timerMux); + BattReadIRQ++; + portEXIT_CRITICAL(&timerMux); +} +void readBattery() { + if (BattReadIRQ) { + portENTER_CRITICAL(&timerMux); + BattReadIRQ = 0; + portEXIT_CRITICAL(&timerMux); + batt_voltage = read_voltage(); + } +} #endif #ifdef HAS_BUTTON @@ -313,6 +329,12 @@ void setup() { strcat_P(features, " GPS"); #endif +// initialize battery status if present +#ifdef HAS_BATTERY_PROBE + strcat_P(features, " BATT"); + batt_voltage = read_voltage(); +#endif + // initialize display if present #ifdef HAS_DISPLAY strcat_P(features, " OLED"); @@ -344,6 +366,14 @@ void setup() { timerAlarmWrite(sendCycle, cfg.sendcycle * 2 * 10000, true); timerAlarmEnable(sendCycle); + // setup battery read cycle trigger IRQ using esp32 hardware timer 3 +#ifdef HAS_BATTERY_PROBE + battCycle = timerBegin(3, 8000, true); + timerAttachInterrupt(battCycle, &BattCycleIRQ, true); + timerAlarmWrite(battCycle, 60 * 100, true); + timerAlarmEnable(battCycle); +#endif + // show payload encoder #if PAYLOAD_ENCODER == 1 strcat_P(features, " PAYLOAD_PLAIN"); @@ -431,6 +461,10 @@ void loop() { readButton(); #endif +#ifdef HAS_BATTERY_PROBE + readBattery(); +#endif + #ifdef HAS_DISPLAY updateDisplay(); #endif