forked from sieren/Homepoint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Girgitt/1-re-enable-screen-saver
1 re enable screen saver
- Loading branch information
Showing
12 changed files
with
262 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.