From 4f6079f418ab9d8cedaa586b735d42f9fb19e05c Mon Sep 17 00:00:00 2001 From: Thomas Hooge Date: Fri, 22 Aug 2025 10:14:38 +0200 Subject: [PATCH] Improve and speedup undervoltage detection code --- lib/obp60task/obp60task.cpp | 122 ++++++++++++++++++------------------ 1 file changed, 61 insertions(+), 61 deletions(-) diff --git a/lib/obp60task/obp60task.cpp b/lib/obp60task/obp60task.cpp index 0ca3532..62e2332 100644 --- a/lib/obp60task/obp60task.cpp +++ b/lib/obp60task/obp60task.cpp @@ -21,9 +21,6 @@ #include #endif -// True type character sets includes -// See OBP60ExtensionPort.cpp - // Pictures //#include GxEPD_BitmapExamples // Example picture #include "MFD_OBP60_400x300_sw.h" // MFD with logo @@ -159,7 +156,7 @@ class BoatValueList{ //additionally we put the necessary values into the paga data - see below GwApi::BoatValue *allBoatValues[MAXVALUES]; int numValues=0; - + bool addValueToList(GwApi::BoatValue *v){ for (int i=0;i Pages; -//the page list class class PageList{ public: Pages pages; @@ -273,64 +269,64 @@ void registerAllPages(PageList &list){ } // Undervoltage detection for shutdown display -void underVoltageDetection(GwApi *api, CommonData &common){ - // Read settings - double voffset = (api->getConfig()->getConfigItem(api->getConfig()->vOffset,true)->asString()).toFloat(); - double vslope = (api->getConfig()->getConfigItem(api->getConfig()->vSlope,true)->asString()).toFloat(); +void underVoltageError(CommonData &common) { +#if defined VOLTAGE_SENSOR && defined LIPO_ACCU_1200 + // Switch off all power lines + setPortPin(OBP_BACKLIGHT_LED, false); // Backlight Off + setFlashLED(false); // Flash LED Off + buzzer(TONE4, 20); // Buzzer tone 4kHz 20ms + // Shutdown EInk display + getdisplay().setFullWindow(); // Set full Refresh + //getdisplay().setPartialWindow(0, 0, getdisplay().width(), getdisplay().height()); // Set partial update + getdisplay().fillScreen(common.bgcolor);// Clear screen + getdisplay().setTextColor(common.fgcolor); + getdisplay().setFont(&Ubuntu_Bold20pt8b); + getdisplay().setCursor(65, 150); + getdisplay().print("Undervoltage"); + getdisplay().setFont(&Ubuntu_Bold8pt8b); + getdisplay().setCursor(65, 175); + getdisplay().print("Charge battery and restart system"); + getdisplay().nextPage(); // Partial update + getdisplay().powerOff(); // Display power off + setPortPin(OBP_POWER_EPD, false); // Power off ePaper display + setPortPin(OBP_POWER_SD, false); // Power off SD card +#else + // Switch off all power lines + setPortPin(OBP_BACKLIGHT_LED, false); // Backlight Off + setFlashLED(false); // Flash LED Off + buzzer(TONE4, 20); // Buzzer tone 4kHz 20ms + setPortPin(OBP_POWER_50, false); // Power rail 5.0V Off + // Shutdown EInk display + getdisplay().setPartialWindow(0, 0, getdisplay().width(), getdisplay().height()); // Set partial update + getdisplay().fillScreen(common.bgcolor);// Clear screen + getdisplay().setTextColor(common.fgcolor); + getdisplay().setFont(&Ubuntu_Bold20pt8b); + getdisplay().setCursor(65, 150); + getdisplay().print("Undervoltage"); + getdisplay().setFont(&Ubuntu_Bold8pt8b); + getdisplay().setCursor(65, 175); + getdisplay().print("To wake up repower system"); + getdisplay().nextPage(); // Partial update + getdisplay().powerOff(); // Display power off +#endif + // Stop system + while (true) { + esp_deep_sleep_start(); // Deep Sleep without wakeup. Wakeup only after power cycle (restart). + } +} + +inline bool underVoltageDetection(float voffset, float vslope) { // Read supply voltage - #if defined VOLTAGE_SENSOR && defined LIPO_ACCU_1200 +#if defined VOLTAGE_SENSOR && defined LIPO_ACCU_1200 float actVoltage = (float(analogRead(OBP_ANALOG0)) * 3.3 / 4096 + 0.53) * 2; // Vin = 1/2 for OBP40 float minVoltage = 3.65; // Absolut minimum volatge for 3,7V LiPo accu - #else +#else float actVoltage = (float(analogRead(OBP_ANALOG0)) * 3.3 / 4096 + 0.17) * 20; // Vin = 1/20 for OBP60 float minVoltage = MIN_VOLTAGE; - #endif - double calVoltage = actVoltage * vslope + voffset; // Calibration - if(calVoltage < minVoltage){ - #if defined VOLTAGE_SENSOR && defined LIPO_ACCU_1200 - // Switch off all power lines - setPortPin(OBP_BACKLIGHT_LED, false); // Backlight Off - setFlashLED(false); // Flash LED Off - buzzer(TONE4, 20); // Buzzer tone 4kHz 20ms - // Shutdown EInk display - getdisplay().setFullWindow(); // Set full Refresh - //getdisplay().setPartialWindow(0, 0, getdisplay().width(), getdisplay().height()); // Set partial update - getdisplay().fillScreen(common.bgcolor);// Clear screen - getdisplay().setTextColor(common.fgcolor); - getdisplay().setFont(&Ubuntu_Bold20pt8b); - getdisplay().setCursor(65, 150); - getdisplay().print("Undervoltage"); - getdisplay().setFont(&Ubuntu_Bold8pt8b); - getdisplay().setCursor(65, 175); - getdisplay().print("Charge battery and restart system"); - getdisplay().nextPage(); // Partial update - getdisplay().powerOff(); // Display power off - setPortPin(OBP_POWER_EPD, false); // Power off ePaper display - setPortPin(OBP_POWER_SD, false); // Power off SD card - #else - // Switch off all power lines - setPortPin(OBP_BACKLIGHT_LED, false); // Backlight Off - setFlashLED(false); // Flash LED Off - buzzer(TONE4, 20); // Buzzer tone 4kHz 20ms - setPortPin(OBP_POWER_50, false); // Power rail 5.0V Off - // Shutdown EInk display - getdisplay().setPartialWindow(0, 0, getdisplay().width(), getdisplay().height()); // Set partial update - getdisplay().fillScreen(common.bgcolor);// Clear screen - getdisplay().setTextColor(common.fgcolor); - getdisplay().setFont(&Ubuntu_Bold20pt8b); - getdisplay().setCursor(65, 150); - getdisplay().print("Undervoltage"); - getdisplay().setFont(&Ubuntu_Bold8pt8b); - getdisplay().setCursor(65, 175); - getdisplay().print("To wake up repower system"); - getdisplay().nextPage(); // Partial update - getdisplay().powerOff(); // Display power off - #endif - // Stop system - while(true){ - esp_deep_sleep_start(); // Deep Sleep without weakup. Weakup only after power cycle (restart). - } - } +#endif + // TODO Why double here? + float calVoltage = actVoltage * vslope + voffset; // Calibration + return (calVoltage < minVoltage); } // Calculate true wind data and add to obp60task boat data list @@ -738,7 +734,9 @@ void OBP60Task(GwApi *api){ commonData.backlight.brightness = 2.55 * uint(config->getConfigItem(config->blBrightness,true)->asInt()); commonData.powermode = api->getConfig()->getConfigItem(api->getConfig()->powerMode,true)->asString(); - bool uvoltage = api->getConfig()->getConfigItem(api->getConfig()->underVoltage,true)->asBoolean(); + bool uvoltage = config->getConfigItem(config->underVoltage, true)->asBoolean(); + float voffset = (config->getConfigItem(config->vOffset,true)->asString()).toFloat(); + float vslope = (config->getConfigItem(config->vSlope,true)->asString()).toFloat(); String cpuspeed = api->getConfig()->getConfigItem(api->getConfig()->cpuSpeed,true)->asString(); uint hdopAccuracy = uint(api->getConfig()->getConfigItem(api->getConfig()->hdopAccuracy,true)->asInt()); @@ -787,8 +785,10 @@ void OBP60Task(GwApi *api){ bool keypressed = false; // Undervoltage detection - if(uvoltage == true){ - underVoltageDetection(api, commonData); + if (uvoltage == true) { + if (underVoltageDetection(voffset, vslope)) { + underVoltageError(commonData); + } } // Set CPU speed after boot after 1min