From 84a03fc95dab1b26775af8b29d1b855e7579c6a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Farkas=20Szil=C3=A1rd?= Date: Sat, 4 Jul 2020 14:32:29 +0200 Subject: [PATCH] Charge Mode functionality for devices which need to be turned on during charge, like the TTGO. --- main/config.h | 2 + main/hal.cpp | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++ main/hal.h | 11 +++++ main/main.cpp | 4 ++ 4 files changed, 140 insertions(+) diff --git a/main/config.h b/main/config.h index b1c1029..ff69f40 100644 --- a/main/config.h +++ b/main/config.h @@ -26,6 +26,8 @@ // #define WITH_SLEEP // with software sleep mode controlled by the long-press on the button +// #define WITH_CHARGE_MODE // Charge mode for those device which can not be charge without turning them on, like the TTGO. On boot the U8G2_OLED will display a message and charge mode can be entered by pressing the PRG button. + #define WITH_AXP // with AXP192 power controller (T-BEAM V1.0) // #define WITH_BQ // with BQ24295 power controller (FollowMe) diff --git a/main/hal.cpp b/main/hal.cpp index ae113b6..31418bb 100644 --- a/main/hal.cpp +++ b/main/hal.cpp @@ -1972,3 +1972,126 @@ uint8_t I2C_Restart(uint8_t Bus) // ====================================================================================================== +// Charge Mode +#ifdef WITH_CHARGE_MODE +void Handle_ChargeMode(void) +{ + xSemaphoreTake(CONS_Mutex, portMAX_DELAY); + Format_String(CONS_UART_Write, "Start Pressing PRG Button if you want to Charge.\n"); + xSemaphoreGive(CONS_Mutex); + + displayChargeStartScreen(); + + vTaskDelay(2500); + + if(Button_isPressed()) { + clearChargeScreen(); + + xSemaphoreTake(CONS_Mutex, portMAX_DELAY); + Format_String(CONS_UART_Write, "PRG Button Pressed. Entering Charge Mode.\n"); + xSemaphoreGive(CONS_Mutex); + + // show the screen initialy for 10 sec, with update every sec, so the volts change on screen + for( int i=0;i<10;i++) { + displayChargeScreen(); + vTaskDelay(1000); + } + turnOffDisplay(); // turn off OLED + + while ( true ) { + if(Button_isPressed()) { // if button pressed: turn back OLED, show the screen for 10 sec with updates every second. + turnOnDisplay(); + for( int i=0;i<10;i++) { + displayChargeScreen(); + vTaskDelay(1000); + } + turnOffDisplay(); + } + vTaskDelay(100); + } + } + else { + clearChargeScreen(); + } +} + +void displayChargeStartScreen() { + #ifdef WITH_U8G2_OLED + u8g2_ClearBuffer(&U8G2_OLED); + u8g2_SetFont(&U8G2_OLED, u8g2_font_7x13_tf); + + uint8_t text_width = 0; + char Line[128]; + uint8_t Len=0; + + Len = Format_String(Line , "Press and Hold"); + Line[Len]=0; + text_width = u8g2_GetStrWidth(&U8G2_OLED, Line); + u8g2_DrawStr(&U8G2_OLED, ( u8g2_GetDisplayWidth(&U8G2_OLED) - text_width ) / 2, 16, Line); + + Len = Format_String(Line , "PRG Button"); + Line[Len]=0; + text_width = u8g2_GetStrWidth(&U8G2_OLED, Line); + u8g2_DrawStr(&U8G2_OLED, ( u8g2_GetDisplayWidth(&U8G2_OLED) - text_width ) / 2, 32, Line); + + Len = Format_String(Line , "for Charge Mode"); + Line[Len]=0; + text_width = u8g2_GetStrWidth(&U8G2_OLED, Line); + u8g2_DrawStr(&U8G2_OLED, ( u8g2_GetDisplayWidth(&U8G2_OLED) - text_width ) / 2, 48, Line); + + uint32_t Battery = BatterySense(10); // [mV] + + Len = Format_String(Line , "Volts: -.---"); + Line[Len]=0; + Format_SignDec(Line+7, Battery, 1, 3); // battery voltage or charge percentage + text_width = u8g2_GetStrWidth(&U8G2_OLED, Line); + u8g2_DrawStr(&U8G2_OLED,( u8g2_GetDisplayWidth(&U8G2_OLED) - text_width ) / 2,64, Line); + u8g2_SendBuffer(&U8G2_OLED); + #endif +} +void displayChargeScreen() { + #ifdef WITH_U8G2_OLED + u8g2_ClearBuffer(&U8G2_OLED); + u8g2_SetFont(&U8G2_OLED, u8g2_font_7x13_tf); + + uint8_t text_width = 0; + char Line[128]; + uint8_t Len=0; + + Len = Format_String(Line , "Charging mode"); + Line[Len]=0; + text_width = u8g2_GetStrWidth(&U8G2_OLED, Line); + u8g2_DrawStr(&U8G2_OLED, ( u8g2_GetDisplayWidth(&U8G2_OLED) - text_width ) / 2, 16, Line); + + Len = Format_String(Line , "ON"); + Line[Len]=0; + text_width = u8g2_GetStrWidth(&U8G2_OLED, Line); + u8g2_DrawStr(&U8G2_OLED, ( u8g2_GetDisplayWidth(&U8G2_OLED) - text_width ) / 2, 32, Line); + + uint32_t Battery = BatterySense(10); // [mV] + + Len = Format_String(Line , "Volts: -.---"); + Line[Len]=0; + Format_SignDec(Line+7, Battery, 1, 3); // battery voltage or charge percentage + text_width = u8g2_GetStrWidth(&U8G2_OLED, Line); + u8g2_DrawStr(&U8G2_OLED,( u8g2_GetDisplayWidth(&U8G2_OLED) - text_width ) / 2,64, Line); + u8g2_SendBuffer(&U8G2_OLED); + #endif +} +void clearChargeScreen() { + #ifdef WITH_U8G2_OLED + u8g2_ClearBuffer(&U8G2_OLED); + u8g2_SendBuffer(&U8G2_OLED); + #endif +} +void turnOnDisplay() { + #ifdef WITH_U8G2_OLED + u8g2_SetPowerSave(&U8G2_OLED,0); + #endif +} +void turnOffDisplay() { + #ifdef WITH_U8G2_OLED + u8g2_SetPowerSave(&U8G2_OLED,1); + #endif +} +#endif \ No newline at end of file diff --git a/main/hal.h b/main/hal.h index 1f6c1e9..8d80b05 100644 --- a/main/hal.h +++ b/main/hal.h @@ -161,6 +161,7 @@ void LED_TimerCheck(uint8_t Ticks=1); // extern bool Button_SleepRequest; int32_t Button_TimerCheck(uint8_t Ticks=1); +bool Button_isPressed(void); void IO_Configuration(void); // Configure I/O @@ -211,4 +212,14 @@ extern AXP192 AXP; void Sleep(void); #endif +#ifdef WITH_CHARGE_MODE +void Handle_ChargeMode(void); + +void displayChargeStartScreen(); +void displayChargeScreen(); +void clearChargeScreen(); +void turnOnDisplay(); +void turnOffDisplay(); +#endif + #endif // __HAL_H__ diff --git a/main/main.cpp b/main/main.cpp index e7918b9..fbce3f9 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -46,6 +46,10 @@ void app_main(void) #endif IO_Configuration(); // initialize the GPIO/UART/I2C/SPI for Radio, GPS, OLED, Baro +#ifdef WITH_CHARGE_MODE + Handle_ChargeMode(); +#endif + #ifdef WITH_SD if(SD_isMounted()) // if SD card succesfully mounted at startup { Parameters.SaveToFlash=0;