Skip to content

Commit

Permalink
Merge pull request #3 from Girgitt/1-re-enable-screen-saver
Browse files Browse the repository at this point in the history
1 re enable screen saver
  • Loading branch information
Girgitt authored Jan 28, 2024
2 parents 8353cd7 + 5d0b8ea commit 3a7e511
Show file tree
Hide file tree
Showing 12 changed files with 262 additions and 35 deletions.
1 change: 1 addition & 0 deletions main/AppContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ namespace ctx
}
if (mModel.mTimeZone != "")
{
delay(100); // trying to avoid ntp occasionally corrupting heap on startup
mNTPSync = std::make_shared<ntp::NTPSync>(mModel.mTimeZone);
mNTPSync->syncTime();
}
Expand Down
10 changes: 9 additions & 1 deletion main/AppScreen.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <touch/TouchDriver.h>
#include <util/varianthelper.hpp>

#include <SharedGlobalState.h>

#include "Arduino.h"
#include "SPI.h"

Expand All @@ -22,6 +24,8 @@ extern "C"

namespace gfx
{
//std::shared_ptr<sgs::SharedGlobalState> global_state = sgs::SharedGlobalState::getInstance();

static const int kStatusBarHeight = 20;

template<class ScreenDriver, class NavigationDriver>
Expand Down Expand Up @@ -162,7 +166,11 @@ namespace gfx
std::lock_guard<std::mutex> 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))
Expand Down
1 change: 1 addition & 0 deletions main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ set(PLATFORM_SRC
set(COMPONENT_SRCS
"AppContext.cpp"
"main.cpp"
"SharedGlobalState.cpp"
${FS_SRC}
${UI_SRC}
${MQTT_SRC}
Expand Down
32 changes: 32 additions & 0 deletions main/SharedGlobalState.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "SharedGlobalState.h"
#include <memory>

namespace sgs {
// when using with C-compatible functions version
SharedGlobalState& sharedGlobalState = SharedGlobalState::getInstance();

// std::shared_ptr<SharedGlobalState> SharedGlobalState::getInstance() {
// static std::shared_ptr<SharedGlobalState> instance = std::make_shared<SharedGlobalState>();
// return instance;
// }

}

// Implementation of C-compatible functions

extern "C" {

void* sharedGlobalStateGetInstance() {
return reinterpret_cast<void*>(&sgs::SharedGlobalState::getInstance());
}

int sharedGlobalStateGetUptime(void* instance) {
return static_cast<sgs::SharedGlobalState*>(instance)->getUptime();
}

void sharedGlobalStateTickUptimeMs(void* instance, int ms) {
static_cast<sgs::SharedGlobalState*>(instance)->tickUptimeMs(ms);
}


} // extern "C"
91 changes: 91 additions & 0 deletions main/SharedGlobalState.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#pragma once

#include <memory>
#include <mutex>
#include <cmath>

extern "C"
{
#include "esp_log.h"
}

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<SharedGlobalState> getInstance();

void tickUptimeMs(int ms){
std::lock_guard<std::mutex> lock(mutex);
uptime_ms += static_cast<long>(ms);
uptime_sec = std::floor(static_cast<double>(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<int>(std::floor((uptime_ms - last_tap_ms) / 1000.0));

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<std::mutex> lock(mutex);
uptime_sec = new_uptime;
}

int getUptime() const {
std::lock_guard<std::mutex> lock(mutex);
return uptime_sec;
}

int getIdleTimeSec() const {
std::lock_guard<std::mutex> lock(mutex);
return sec_since_last_tap;
}

void registerTap(){
std::lock_guard<std::mutex> 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;
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;

} // namespace sgs
3 changes: 3 additions & 0 deletions main/config/PlatformInject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
18 changes: 18 additions & 0 deletions main/fs/ConfigReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ extern "C"
#include "esp_log.h"
}


#define LOG_TAG "config reader"

namespace fs
{

Expand Down Expand Up @@ -302,9 +305,24 @@ namespace fs

read(document, "screenSaverMinutes", [&](int mins)
{
ESP_LOGI(LOG_TAG, "overriden mScreensaverMins to %d min", mins);
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)
{
Expand Down
19 changes: 17 additions & 2 deletions main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,36 @@
#include <config/Config.h>
#include <fs/ConfigReader.hpp>
#include "AppScreen.hpp"
#include "SharedGlobalState.h"

#include <memory>
#include "esp_log.h"

extern "C"
{
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_task_wdt.h"
#include "nvs_flash.h"

}

#define MAINLOOPCORE 0
TaskHandle_t runLoopHandle = NULL;
bool loopTaskWDTEnabled = false; // Enable if watchdog running
bool loopTaskWDTEnabled = true; // Enable if watchdog running

std::shared_ptr<ctx::AppContext> mpAppContext(new ctx::AppContext());
gfx::AppScreen<ScreenDriver, NavigationDriver> mScreen(mpAppContext);
//std::shared_ptr<sgs::SharedGlobalState> 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()
{
Expand Down Expand Up @@ -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);
}
}
}
3 changes: 3 additions & 0 deletions main/model/HardwareConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}
Loading

0 comments on commit 3a7e511

Please sign in to comment.