From 00db71eefbc1c10b34e8ee81cda426b4cc78e6fb Mon Sep 17 00:00:00 2001 From: Zbigniew Zasieczny Date: Thu, 25 Jan 2024 01:25:55 +0100 Subject: [PATCH 1/5] added SharedGlobalState singleton atoring uptime and time since last tap/press events --- main/AppScreen.ipp | 4 +++ main/CMakeLists.txt | 1 + main/SharedGlobalState.cpp | 32 ++++++++++++++++++ main/SharedGlobalState.h | 68 ++++++++++++++++++++++++++++++++++++++ main/main.cpp | 17 +++++++++- main/touch/TouchDriver.ipp | 6 ++++ 6 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 main/SharedGlobalState.cpp create mode 100644 main/SharedGlobalState.h diff --git a/main/AppScreen.ipp b/main/AppScreen.ipp index e400a6f..03925d6 100644 --- a/main/AppScreen.ipp +++ b/main/AppScreen.ipp @@ -8,6 +8,8 @@ #include #include +#include + #include "Arduino.h" #include "SPI.h" @@ -22,6 +24,8 @@ extern "C" namespace gfx { + //std::shared_ptr global_state = sgs::SharedGlobalState::getInstance(); + static const int kStatusBarHeight = 20; template diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index bd43f17..717a70e 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -87,6 +87,7 @@ set(PLATFORM_SRC set(COMPONENT_SRCS "AppContext.cpp" "main.cpp" + "SharedGlobalState.cpp" ${FS_SRC} ${UI_SRC} ${MQTT_SRC} diff --git a/main/SharedGlobalState.cpp b/main/SharedGlobalState.cpp new file mode 100644 index 0000000..b1cb008 --- /dev/null +++ b/main/SharedGlobalState.cpp @@ -0,0 +1,32 @@ +#include "SharedGlobalState.h" +#include + +namespace sgs { +// when using with C-compatible functions version +SharedGlobalState& sharedGlobalState = SharedGlobalState::getInstance(); + +// std::shared_ptr SharedGlobalState::getInstance() { +// static std::shared_ptr instance = std::make_shared(); +// return instance; +// } + +} + +// Implementation of C-compatible functions + +extern "C" { + +void* sharedGlobalStateGetInstance() { + return reinterpret_cast(&sgs::SharedGlobalState::getInstance()); +} + +int sharedGlobalStateGetUptime(void* instance) { + return static_cast(instance)->getUptime(); +} + +void sharedGlobalStateTickUptimeMs(void* instance, int ms) { + static_cast(instance)->tickUptimeMs(ms); +} + + +} // extern "C" \ No newline at end of file diff --git a/main/SharedGlobalState.h b/main/SharedGlobalState.h new file mode 100644 index 0000000..2cb8f93 --- /dev/null +++ b/main/SharedGlobalState.h @@ -0,0 +1,68 @@ +#pragma once + +#include +#include +#include + +extern "C" +{ + #include "esp_log.h" +} + +namespace sgs { + +class SharedGlobalState { +public: + SharedGlobalState() : uptime_sec(0), last_log_print_sec(0), uptime_ms(0), last_tap_ms(0) {} + + // when using with C-compatible functions version + static SharedGlobalState& getInstance() { + static SharedGlobalState instance; + return instance; + } + //static std::shared_ptr getInstance(); + + void tickUptimeMs(int ms){ + std::lock_guard lock(mutex); + uptime_ms += static_cast(ms); + uptime_sec = std::floor(static_cast(uptime_ms)/1000.0); + if (uptime_sec % 5 == 0){ + if(uptime_sec != last_log_print_sec){ + ESP_LOGI("SharedGlobalState", "uptime %d sec", uptime_sec); + long sec_since_last_tap = std::floor((uptime_ms - last_tap_ms) / 1000.0); + ESP_LOGI("SharedGlobalState", "sec_since_last_tap %d sec", static_cast(sec_since_last_tap)); + last_log_print_sec = uptime_sec; + } + } + + } + void setUptime(int new_uptime) { + std::lock_guard lock(mutex); + uptime_sec = new_uptime; + } + + int getUptime() const { + std::lock_guard lock(mutex); + return uptime_sec; + } + + void registerTap(){ + std::lock_guard lock(mutex); + + ESP_LOGI("SharedGlobalState", "registering tap"); + + last_tap_ms = uptime_ms; + } + +private: + int uptime_sec; + int last_log_print_sec; + long uptime_ms; + long last_tap_ms; + mutable std::mutex mutex; // Note: mutable is used to allow locking in const member functions +}; + +// Define the shared instance +extern SharedGlobalState& sharedGlobalState; + +} // namespace sgs \ No newline at end of file diff --git a/main/main.cpp b/main/main.cpp index ad102f2..a0fba52 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -5,8 +5,10 @@ #include #include #include "AppScreen.hpp" +#include "SharedGlobalState.h" #include +#include "esp_log.h" extern "C" { @@ -14,6 +16,7 @@ extern "C" #include "freertos/task.h" #include "esp_task_wdt.h" #include "nvs_flash.h" + } #define MAINLOOPCORE 0 @@ -22,11 +25,16 @@ bool loopTaskWDTEnabled = false; // Enable if watchdog running std::shared_ptr mpAppContext(new ctx::AppContext()); gfx::AppScreen mScreen(mpAppContext); +//std::shared_ptr global_state = sgs::SharedGlobalState::getInstance(); extern "C" { void runLoop(void *pvParameters); void setupApp(); + + void* sharedGlobalStateGetInstance(); + void sharedGlobalStateTickUptimeMs(void* instance, int ms); + void* sgs_instance = sharedGlobalStateGetInstance(); void app_main() { @@ -69,14 +77,21 @@ extern "C" void runLoop(void *pvParameters) { + //esp_log_level_set("*", ESP_LOG_INFO); + int loop_delay_ms = 50; + ESP_LOGI("main", "starting main loop"); + for(;;) { if (loopTaskWDTEnabled) { esp_task_wdt_reset(); } + mScreen.draw(); - delay(50); + sharedGlobalStateTickUptimeMs(sgs_instance, loop_delay_ms); + //global_state->tickUptimeMs(loop_delay_ms); + delay(loop_delay_ms); } } } diff --git a/main/touch/TouchDriver.ipp b/main/touch/TouchDriver.ipp index 22418f4..1dd263d 100644 --- a/main/touch/TouchDriver.ipp +++ b/main/touch/TouchDriver.ipp @@ -5,6 +5,7 @@ #include "Arduino.h" #include #include +#include namespace gfx { @@ -41,8 +42,10 @@ namespace gfx break; case TouchState::TouchStart: newState = TouchState::TouchRunning; + //sgs::sharedGlobalState.registerTap(); break; case TouchState::TouchEnded: + //sgs::sharedGlobalState.registerTap(); break; case TouchState::TouchRunning: newState = TouchState::TouchRunning; @@ -63,6 +66,7 @@ namespace gfx case TouchState::TouchRunning: case TouchState::TouchStart: newState = TouchState::TouchEnded; + //sgs::sharedGlobalState.registerTap(); break; case TouchState::TouchEnded: newState = TouchState::NoTouch; @@ -110,11 +114,13 @@ namespace gfx if (isShortPress) { tapEvent.state = PressEvent::Tap; + sgs::sharedGlobalState.registerTap(); return std::make_optional(tapEvent); } if (isLongPress) { tapEvent.state = PressEvent::LongPress; + sgs::sharedGlobalState.registerTap(); return std::make_optional(tapEvent); } } From eb4c3e389c416ca7b2cc4da54ed0fcfa307f32e0 Mon Sep 17 00:00:00 2001 From: Zbigniew Zasieczny Date: Fri, 26 Jan 2024 17:12:12 +0100 Subject: [PATCH 2/5] fixed screen saver based on idle time from last touch --- main/AppScreen.ipp | 6 ++++- main/SharedGlobalState.h | 15 +++++++++---- main/fs/ConfigReader.cpp | 4 ++++ main/tft/ScreenSaver.hpp | 47 +++++++++++++++++++++++++++++++--------- 4 files changed, 57 insertions(+), 15 deletions(-) diff --git a/main/AppScreen.ipp b/main/AppScreen.ipp index 03925d6..d0b7918 100644 --- a/main/AppScreen.ipp +++ b/main/AppScreen.ipp @@ -166,7 +166,11 @@ namespace gfx std::lock_guard guard(viewMutex); auto tapEvent = mNavigation.tapEvent(); // FIXME: screen saver is broken - activates on touch and not on timeout (automatically) and does not deactivate - // mScreenSaver(); + if(mScreenSaver()){ + // screen saver returned IGNORE_TOUCH_ON_WAKEUP + return; + } + // // Abort when Screensaver is on and no touch event happened. // if (!mScreenSaver.tapped(tapEvent)) diff --git a/main/SharedGlobalState.h b/main/SharedGlobalState.h index 2cb8f93..ae2d8d8 100644 --- a/main/SharedGlobalState.h +++ b/main/SharedGlobalState.h @@ -13,7 +13,7 @@ namespace sgs { class SharedGlobalState { public: - SharedGlobalState() : uptime_sec(0), last_log_print_sec(0), uptime_ms(0), last_tap_ms(0) {} + SharedGlobalState() : uptime_sec(0), last_log_print_sec(0), uptime_ms(0), last_tap_ms(0), sec_since_last_tap(0) {} // when using with C-compatible functions version static SharedGlobalState& getInstance() { @@ -26,11 +26,12 @@ class SharedGlobalState { std::lock_guard lock(mutex); uptime_ms += static_cast(ms); uptime_sec = std::floor(static_cast(uptime_ms)/1000.0); + sec_since_last_tap = static_cast(std::floor((uptime_ms - last_tap_ms) / 1000.0)); + if (uptime_sec % 5 == 0){ if(uptime_sec != last_log_print_sec){ - ESP_LOGI("SharedGlobalState", "uptime %d sec", uptime_sec); - long sec_since_last_tap = std::floor((uptime_ms - last_tap_ms) / 1000.0); - ESP_LOGI("SharedGlobalState", "sec_since_last_tap %d sec", static_cast(sec_since_last_tap)); + ESP_LOGI("SharedGlobalState", "uptime %d sec", uptime_sec); + ESP_LOGI("SharedGlobalState", "sec_since_last_tap %d sec", sec_since_last_tap); last_log_print_sec = uptime_sec; } } @@ -45,6 +46,11 @@ class SharedGlobalState { std::lock_guard lock(mutex); return uptime_sec; } + + int getIdleTimeSec() const { + std::lock_guard lock(mutex); + return sec_since_last_tap; + } void registerTap(){ std::lock_guard lock(mutex); @@ -59,6 +65,7 @@ class SharedGlobalState { int last_log_print_sec; long uptime_ms; long last_tap_ms; + int sec_since_last_tap; mutable std::mutex mutex; // Note: mutable is used to allow locking in const member functions }; diff --git a/main/fs/ConfigReader.cpp b/main/fs/ConfigReader.cpp index 5187400..8cda8ba 100644 --- a/main/fs/ConfigReader.cpp +++ b/main/fs/ConfigReader.cpp @@ -6,6 +6,9 @@ extern "C" #include "esp_log.h" } + +#define LOG_TAG "config reader" + namespace fs { @@ -302,6 +305,7 @@ namespace fs read(document, "screenSaverMinutes", [&](int mins) { + ESP_LOGI(LOG_TAG, "overriden mScreensaverMins to %d min", mins); hwConfig.mScreensaverMins = mins; } ); diff --git a/main/tft/ScreenSaver.hpp b/main/tft/ScreenSaver.hpp index b426887..272ad32 100644 --- a/main/tft/ScreenSaver.hpp +++ b/main/tft/ScreenSaver.hpp @@ -8,6 +8,10 @@ #include #include +#include + +#define LOG_TAG "screen saver" + namespace gfx { template @@ -18,20 +22,31 @@ namespace gfx mpDriver(driver), mpCtx(ctx) { - mLastTouch = std::chrono::system_clock::now(); + // //initial touch time + // mLastTouch = std::chrono::system_clock::now(); } - void operator()() + bool operator()() { - auto now = std::chrono::system_clock::now(); - const auto timeOut = mpCtx->getModel().mHardwareConfig.mScreensaverMins; - if (std::chrono::duration_cast(now - mLastTouch).count() > timeOut) + //auto now = std::chrono::system_clock::now(); + const auto timeOutMin = mpCtx->getModel().mHardwareConfig.mScreensaverMins; + const auto idleSec = sgs::sharedGlobalState.getIdleTimeSec(); + const auto uptime_sec = sgs::sharedGlobalState.getUptime(); + if (uptime_sec % 5 == 0){ + if(uptime_sec != last_log_print_sec){ + ESP_LOGI(LOG_TAG, "idleSec %d sec", idleSec); + ESP_LOGI(LOG_TAG, "timeOutMin %d min", timeOutMin); + last_log_print_sec = uptime_sec; + } + } + + if (static_cast(sgs::sharedGlobalState.getIdleTimeSec()) < static_cast(timeOutMin) * 60 ) { - switchScreen(false); + return switchScreen(false); } else { - switchScreen(true); + return switchScreen(true); } } @@ -57,18 +72,30 @@ namespace gfx private: - void switchScreen(bool on) + bool switchScreen(bool on) { if (on == mCurrentState) { - return; + return mCurrentState; } + + if (on) + { + ESP_LOGI(LOG_TAG, "switching screen OFF, new state: %d", on); + } + else{ + ESP_LOGI(LOG_TAG, "switching screen ON, new state: %d", on); + } + ScreenOnOffSwitch(mpDriver, on, mpCtx->getModel().mHardwareConfig.mIsLEDPinInverted); mCurrentState = on; + + return mCurrentState; } std::chrono::system_clock::time_point mLastTouch; - bool mCurrentState = true; + bool mCurrentState = false; + int last_log_print_sec = 0; ScreenDriver* mpDriver; std::shared_ptr mpCtx; From e52f73e13370be275f05759fce138790cde650d5 Mon Sep 17 00:00:00 2001 From: Zbigniew Zasieczny Date: Sat, 27 Jan 2024 13:18:29 +0100 Subject: [PATCH 3/5] added power save cpu freq when screen saver is on (configurable in config.json with screenSaverPowerSaveEnabled), enabled wdt reset in the main loop --- main/fs/ConfigReader.cpp | 14 +++++++ main/main.cpp | 5 ++- main/model/HardwareConfig.hpp | 3 ++ main/tft/ScreenSaver.hpp | 74 ++++++++++++++++++++++------------- main/touch/TouchDriver.ipp | 3 -- main/ui/UIWidgetBuilder.hpp | 2 +- 6 files changed, 68 insertions(+), 33 deletions(-) diff --git a/main/fs/ConfigReader.cpp b/main/fs/ConfigReader.cpp index 8cda8ba..a7a8969 100644 --- a/main/fs/ConfigReader.cpp +++ b/main/fs/ConfigReader.cpp @@ -309,6 +309,20 @@ namespace fs hwConfig.mScreensaverMins = mins; } ); + + read(document, "screenSaverPowerSaveEnabled", [&](bool screenSaverPowerSaveEnabled) + { + ESP_LOGI(LOG_TAG, "overriden screenSaverPowerSaveEnabled to %d", screenSaverPowerSaveEnabled); + hwConfig.mIsScreenSaverPowerSaveEnabled = screenSaverPowerSaveEnabled; + } + ); + + read(document, "powerSaveMHz", [&](int powerSaveMHz) + { + ESP_LOGI(LOG_TAG, "overriden powerSaveMHz to %d MHz", powerSaveMHz); + hwConfig.mPowerSaveFreq = powerSaveMHz; + } + ); read(document, "screenRotationAngle", [&](int angle) { diff --git a/main/main.cpp b/main/main.cpp index a0fba52..b8730e8 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -21,7 +21,7 @@ extern "C" #define MAINLOOPCORE 0 TaskHandle_t runLoopHandle = NULL; -bool loopTaskWDTEnabled = false; // Enable if watchdog running +bool loopTaskWDTEnabled = true; // Enable if watchdog running std::shared_ptr mpAppContext(new ctx::AppContext()); gfx::AppScreen mScreen(mpAppContext); @@ -46,6 +46,9 @@ extern "C" ESP_ERROR_CHECK(ret); initArduino(); + + setCpuFrequencyMhz(240); + InitializePlatform(); Serial.begin(115200); setupApp(); diff --git a/main/model/HardwareConfig.hpp b/main/model/HardwareConfig.hpp index a8bfd4b..49a5593 100644 --- a/main/model/HardwareConfig.hpp +++ b/main/model/HardwareConfig.hpp @@ -13,5 +13,8 @@ namespace config bool mIsTouchXAxisInverted = kTOUCH_X_AXIS_INVERTED; bool mIsTouchYAxisInverted = kTOUCH_Y_AXIS_INVERTED; bool mIsDisplayColorInverted = kDISPLAY_INVERTED; + bool mIsScreenSaverPowerSaveEnabled = true; + int mPowerSaveFreq = 80; + int mPerformanceFreq = 240; }; } \ No newline at end of file diff --git a/main/tft/ScreenSaver.hpp b/main/tft/ScreenSaver.hpp index 272ad32..941371e 100644 --- a/main/tft/ScreenSaver.hpp +++ b/main/tft/ScreenSaver.hpp @@ -24,6 +24,9 @@ namespace gfx { // //initial touch time // mLastTouch = std::chrono::system_clock::now(); + power_save_freq_mhz = mpCtx->getModel().mHardwareConfig.mPowerSaveFreq; + performance_freq_mhz = mpCtx->getModel().mHardwareConfig.mPerformanceFreq; + power_save_enabled = mpCtx->getModel().mHardwareConfig.mIsScreenSaverPowerSaveEnabled; } bool operator()() @@ -42,60 +45,75 @@ namespace gfx if (static_cast(sgs::sharedGlobalState.getIdleTimeSec()) < static_cast(timeOutMin) * 60 ) { - return switchScreen(false); + return switchScreen(screen_on); } else { - return switchScreen(true); + return switchScreen(screen_off); } } - template - bool tapped(const T& tapEvt) - { - if (!tapEvt) - { - return mCurrentState; - } - auto tapEvent = *tapEvt; - if (tapEvent.state == decltype(tapEvent.state)::Tap) - { - mLastTouch = std::chrono::system_clock::now(); - } - return mCurrentState; - } + // template + // bool tapped(const T& tapEvt) + // { + // if (!tapEvt) + // { + // return mCurrentState; + // } + // auto tapEvent = *tapEvt; + // if (tapEvent.state == decltype(tapEvent.state)::Tap) + // { + // mLastTouch = std::chrono::system_clock::now(); + // } + // return mCurrentState; + // } - void activate() - { - mLastTouch = std::chrono::system_clock::now(); - } + // void activate() + // { + // mLastTouch = std::chrono::system_clock::now(); + // } private: - bool switchScreen(bool on) + bool switchScreen(bool new_screen_state) { - if (on == mCurrentState) + if (new_screen_state == mCurrentState) { return mCurrentState; } - if (on) + if (new_screen_state) { - ESP_LOGI(LOG_TAG, "switching screen OFF, new state: %d", on); + ESP_LOGI(LOG_TAG, "switching screen OFF, new mCurrentState: %d", new_screen_state); + if (power_save_enabled) + { + ESP_LOGI(LOG_TAG, "setting CPU freq to: %d", power_save_freq_mhz); + setCpuFrequencyMhz(power_save_freq_mhz); + } } else{ - ESP_LOGI(LOG_TAG, "switching screen ON, new state: %d", on); + ESP_LOGI(LOG_TAG, "switching screen ON, new mCurrentState: %d", new_screen_state); + if (power_save_enabled) + { + ESP_LOGI(LOG_TAG, "setting CPU freq to: %d", performance_freq_mhz); + setCpuFrequencyMhz(performance_freq_mhz); + } } - ScreenOnOffSwitch(mpDriver, on, mpCtx->getModel().mHardwareConfig.mIsLEDPinInverted); - mCurrentState = on; + ScreenOnOffSwitch(mpDriver, new_screen_state, mpCtx->getModel().mHardwareConfig.mIsLEDPinInverted); + mCurrentState = new_screen_state; return mCurrentState; } std::chrono::system_clock::time_point mLastTouch; - bool mCurrentState = false; + const bool screen_on = false; + const bool screen_off = true; + bool mCurrentState = screen_on; int last_log_print_sec = 0; + int power_save_freq_mhz = 80; + int performance_freq_mhz = 240; + bool power_save_enabled = true; ScreenDriver* mpDriver; std::shared_ptr mpCtx; diff --git a/main/touch/TouchDriver.ipp b/main/touch/TouchDriver.ipp index 1dd263d..f56bc43 100644 --- a/main/touch/TouchDriver.ipp +++ b/main/touch/TouchDriver.ipp @@ -42,10 +42,8 @@ namespace gfx break; case TouchState::TouchStart: newState = TouchState::TouchRunning; - //sgs::sharedGlobalState.registerTap(); break; case TouchState::TouchEnded: - //sgs::sharedGlobalState.registerTap(); break; case TouchState::TouchRunning: newState = TouchState::TouchRunning; @@ -66,7 +64,6 @@ namespace gfx case TouchState::TouchRunning: case TouchState::TouchStart: newState = TouchState::TouchEnded; - //sgs::sharedGlobalState.registerTap(); break; case TouchState::TouchEnded: newState = TouchState::NoTouch; diff --git a/main/ui/UIWidgetBuilder.hpp b/main/ui/UIWidgetBuilder.hpp index 30cd0e2..0d714d2 100644 --- a/main/ui/UIWidgetBuilder.hpp +++ b/main/ui/UIWidgetBuilder.hpp @@ -54,7 +54,7 @@ namespace util } button->setTextColor(textColor); button->setImage(imagePath); - screenSaver.activate(); + //screenSaver.activate(); // no longer needed as screen saver uses now idle timeout from last touch event }; return button; }; From 4fd4a4a7be4fc5ba3630c504db1efcb40e51f0e4 Mon Sep 17 00:00:00 2001 From: Zbigniew Zasieczny Date: Sat, 27 Jan 2024 23:40:02 +0100 Subject: [PATCH 4/5] power save changed from cpu freq to wifi.setSleep due to wifi instability on CPU freq increase. hacky workaround for uptime ms overflow --- main/AppContext.cpp | 1 + main/SharedGlobalState.h | 118 ++++++++++++++++++++++----------------- main/main.cpp | 3 - main/tft/ScreenSaver.hpp | 19 +++++-- 4 files changed, 81 insertions(+), 60 deletions(-) diff --git a/main/AppContext.cpp b/main/AppContext.cpp index 9483736..1673ee7 100644 --- a/main/AppContext.cpp +++ b/main/AppContext.cpp @@ -93,6 +93,7 @@ namespace ctx } if (mModel.mTimeZone != "") { + delay(100); // trying to avoid ntp occasionally corrupting heap on startup mNTPSync = std::make_shared(mModel.mTimeZone); mNTPSync->syncTime(); } diff --git a/main/SharedGlobalState.h b/main/SharedGlobalState.h index ae2d8d8..1714bf2 100644 --- a/main/SharedGlobalState.h +++ b/main/SharedGlobalState.h @@ -9,67 +9,83 @@ extern "C" #include "esp_log.h" } -namespace sgs { +namespace sgs +{ + static const char *LOG_TAG = "SharedGlobalState"; -class SharedGlobalState { -public: - SharedGlobalState() : uptime_sec(0), last_log_print_sec(0), uptime_ms(0), last_tap_ms(0), sec_since_last_tap(0) {} - - // when using with C-compatible functions version - static SharedGlobalState& getInstance() { - static SharedGlobalState instance; - return instance; - } - //static std::shared_ptr getInstance(); + class SharedGlobalState { + public: + SharedGlobalState() : uptime_sec(0), last_log_print_sec(0), uptime_ms(0), last_tap_ms(0), sec_since_last_tap(0) {} + + // when using with C-compatible functions version + static SharedGlobalState& getInstance() { + static SharedGlobalState instance; + return instance; + } + //static std::shared_ptr getInstance(); - void tickUptimeMs(int ms){ - std::lock_guard lock(mutex); - uptime_ms += static_cast(ms); - uptime_sec = std::floor(static_cast(uptime_ms)/1000.0); - sec_since_last_tap = static_cast(std::floor((uptime_ms - last_tap_ms) / 1000.0)); + void tickUptimeMs(int ms){ + std::lock_guard lock(mutex); + uptime_ms += static_cast(ms); + uptime_sec = std::floor(static_cast(uptime_ms)/1000.0); + + // in case of uptime_ms overflow every 49.7 days the idle time is truncated to max 24h + if (last_tap_ms > uptime_ms) + { + ESP_LOGI(LOG_TAG, "uptime_ms overflow"); + last_tap_ms = 0; + if (sec_since_last_tap < 24 * 3600) + { + uptime_ms = sec_since_last_tap * 1000; + } + else{ + uptime_ms = 24 * 3600 * 1000; + } + } + sec_since_last_tap = static_cast(std::floor((uptime_ms - last_tap_ms) / 1000.0)); - if (uptime_sec % 5 == 0){ - if(uptime_sec != last_log_print_sec){ - ESP_LOGI("SharedGlobalState", "uptime %d sec", uptime_sec); - ESP_LOGI("SharedGlobalState", "sec_since_last_tap %d sec", sec_since_last_tap); - last_log_print_sec = uptime_sec; + if (uptime_sec % 5 == 0){ + if(uptime_sec != last_log_print_sec){ + ESP_LOGI(LOG_TAG, "uptime %d sec", uptime_sec); + ESP_LOGI(LOG_TAG, "sec_since_last_tap %d sec", sec_since_last_tap); + last_log_print_sec = uptime_sec; + } } - } - } - void setUptime(int new_uptime) { - std::lock_guard lock(mutex); - uptime_sec = new_uptime; - } + } + void setUptime(int new_uptime) { + std::lock_guard lock(mutex); + uptime_sec = new_uptime; + } - int getUptime() const { - std::lock_guard lock(mutex); - return uptime_sec; - } + int getUptime() const { + std::lock_guard lock(mutex); + return uptime_sec; + } - int getIdleTimeSec() const { - std::lock_guard lock(mutex); - return sec_since_last_tap; - } - - void registerTap(){ - std::lock_guard lock(mutex); + int getIdleTimeSec() const { + std::lock_guard lock(mutex); + return sec_since_last_tap; + } - ESP_LOGI("SharedGlobalState", "registering tap"); + void registerTap(){ + std::lock_guard lock(mutex); + + ESP_LOGI("SharedGlobalState", "registering tap"); - last_tap_ms = uptime_ms; - } + last_tap_ms = uptime_ms; + } -private: - int uptime_sec; - int last_log_print_sec; - long uptime_ms; - long last_tap_ms; - int sec_since_last_tap; - mutable std::mutex mutex; // Note: mutable is used to allow locking in const member functions -}; + private: + int uptime_sec; + int last_log_print_sec; + long uptime_ms; + long last_tap_ms; + int sec_since_last_tap; + mutable std::mutex mutex; // Note: mutable is used to allow locking in const member functions + }; -// Define the shared instance -extern SharedGlobalState& sharedGlobalState; + // Define the shared instance + extern SharedGlobalState& sharedGlobalState; } // namespace sgs \ No newline at end of file diff --git a/main/main.cpp b/main/main.cpp index b8730e8..e37b70b 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -46,9 +46,6 @@ extern "C" ESP_ERROR_CHECK(ret); initArduino(); - - setCpuFrequencyMhz(240); - InitializePlatform(); Serial.begin(115200); setupApp(); diff --git a/main/tft/ScreenSaver.hpp b/main/tft/ScreenSaver.hpp index 941371e..5082460 100644 --- a/main/tft/ScreenSaver.hpp +++ b/main/tft/ScreenSaver.hpp @@ -10,10 +10,10 @@ #include -#define LOG_TAG "screen saver" - namespace gfx { + static const char *LOG_TAG = "screen saver"; + template struct ScreenSaver { @@ -87,16 +87,23 @@ namespace gfx ESP_LOGI(LOG_TAG, "switching screen OFF, new mCurrentState: %d", new_screen_state); if (power_save_enabled) { - ESP_LOGI(LOG_TAG, "setting CPU freq to: %d", power_save_freq_mhz); - setCpuFrequencyMhz(power_save_freq_mhz); + ESP_LOGI(LOG_TAG, "enabling power save features"); + WiFi.setSleep(true); + //delay(100); + //ESP_LOGI(LOG_TAG, "setting CPU freq to: %d", power_save_freq_mhz); + //setCpuFrequencyMhz(power_save_freq_mhz); } } else{ ESP_LOGI(LOG_TAG, "switching screen ON, new mCurrentState: %d", new_screen_state); if (power_save_enabled) { - ESP_LOGI(LOG_TAG, "setting CPU freq to: %d", performance_freq_mhz); - setCpuFrequencyMhz(performance_freq_mhz); + ESP_LOGI(LOG_TAG, "disabling power save features"); + // increasing CPU frequency back to 240MHz is breaking wifi connection + //ESP_LOGI(LOG_TAG, "setting CPU freq to: %d", performance_freq_mhz); + //setCpuFrequencyMhz(performance_freq_mhz); + //delay(100); + WiFi.setSleep(false); } } From 5d0b8eafefc6b069574cab87a925b10cf16a58a1 Mon Sep 17 00:00:00 2001 From: Zbigniew Zasieczny Date: Sun, 28 Jan 2024 21:52:44 +0100 Subject: [PATCH 5/5] added switching backlight and power led on/off by screen saver --- main/config/PlatformInject.hpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/main/config/PlatformInject.hpp b/main/config/PlatformInject.hpp index 323877f..aa83837 100644 --- a/main/config/PlatformInject.hpp +++ b/main/config/PlatformInject.hpp @@ -42,6 +42,9 @@ auto ScreenOnOffSwitch = [](ScreenDriver* driver, bool on, bool inverted = false) { driver->setSleep(!on); + auto axp = AXP192(); + axp.SetDCDC3(!on); // turn screen off / on + axp.SetLed(!on); }; auto InitializeScreen = [](ScreenDriver* driver) {