14static const char*
TAG =
"SleepMgr";
28 static SleepManager s_instance;
42 lockScreen_ = lockScreen;
43 lockScreenEnteredMs_ = 0;
44 inLightSleep_ =
false;
53 lockScreenEnteredMs_ = nowMs;
61 lockScreenEnteredMs_ = esp_timer_get_time() / 1000;
72 if (!lockScreen_)
return;
75 if (lockScreenEnteredMs_ == 0) {
76 lockScreenEnteredMs_ = nowMs;
81 if (power_ && power_->isUsbConnected()) {
82 lockScreenEnteredMs_ = nowMs;
92 lockScreenEnteredMs_ = nowMs;
97 uint32_t elapsed = nowMs - lockScreenEnteredMs_;
99 enterLockScreenSleep();
107void SleepManager::enterLockScreenSleep() {
108 if (!lockScreen_ || !sleep_)
return;
115 inLightSleep_ =
true;
126 }
while (handleWakeup());
134bool SleepManager::handleWakeup() {
135 if (!sleep_ || !lockScreen_)
return false;
149 lockScreenEnteredMs_ = esp_timer_get_time() / 1000;
150 inLightSleep_ =
false;
156 lockScreen_->markDirty();
161 time_t now = time(
nullptr);
162 struct tm* tm = localtime(&now);
165 snprintf(buf,
sizeof(buf),
"%02d:%02d", tm->tm_hour, tm->tm_min);
166 lockScreen_->setClock(buf);
167 snprintf(buf,
sizeof(buf),
"%02d.%02d.%04d", tm->tm_mday, tm->tm_mon + 1, tm->tm_year + 1900);
168 lockScreen_->setDate(buf);
181 if (power_ && power_->isUsbConnected()) {
184 lockScreenEnteredMs_ = esp_timer_get_time() / 1000;
185 inLightSleep_ =
false;
193 lockScreenEnteredMs_ = esp_timer_get_time() / 1000;
194 inLightSleep_ =
false;
209 if (!reason)
return false;
212 for (uint8_t i = 0; i < inhibitorCount_; i++) {
213 if (inhibitors_[i] && strcmp(inhibitors_[i], reason) == 0) {
220 LOG_W(
TAG,
"Sleep inhibitor list full, cannot add: %s", reason);
225 inhibitors_[inhibitorCount_++] = reason;
226 LOG_I(
TAG,
"Sleep inhibitor added: %s (count=%d)", reason, inhibitorCount_);
229 updateCaffeinatedIcon();
239 if (!reason)
return false;
242 for (uint8_t i = 0; i < inhibitorCount_; i++) {
243 if (inhibitors_[i] && strcmp(inhibitors_[i], reason) == 0) {
245 for (uint8_t j = i; j < inhibitorCount_ - 1; j++) {
246 inhibitors_[j] = inhibitors_[j + 1];
248 inhibitors_[--inhibitorCount_] =
nullptr;
250 LOG_I(
TAG,
"Sleep inhibitor removed: %s (count=%d)", reason, inhibitorCount_);
253 updateCaffeinatedIcon();
265void SleepManager::updateCaffeinatedIcon() {
266 if (!lockScreen_)
return;
268 if (inhibitorCount_ > 0) {
CDC Log: logging over TinyUSB CDC and UART.
#define LOG_W(tag, fmt,...)
#define LOG_I(tag, fmt,...)
static EventBus & instance()
Returns singleton event-bus instance.
virtual void enterLightSleep()=0
void removeStatusIcon(StatusIcon icon)
Removes one status icon flag.
void addStatusIcon(StatusIcon icon)
Adds one status icon flag.
bool isSleepInhibited() const
void resetTimer()
Resets lock-screen sleep timer using current system tick.
void checkLockScreenSleep(uint32_t nowMs)
Evaluates whether lock-screen light sleep should be entered.
bool addSleepInhibitor(const char *reason)
Sleep inhibitor API implementation.
static SleepManager & instance()
Returns singleton sleep manager instance.
bool removeSleepInhibitor(const char *reason)
Removes a sleep inhibitor reason.
void init(hal::ISleepController *sleep, hal::IPowerManager *power, LockScreenView *lockScreen)
Initializes sleep-manager dependencies and state.
void render(bool synchronous=false)
Render current view (and modal if present) and flush to display.
static ViewStack & instance()
Returns singleton view-stack instance.
IDisplay * getDisplayInstance()
Returns lazily created singleton display instance.
Centralized key-code constants for cdc_views.
static constexpr uint8_t MAX_SLEEP_INHIBITORS
static constexpr uint32_t LIGHT_SLEEP_TIMEOUT_MS
void updatePowerStatusIcons()
Synchronizes lock-screen status icons with current hardware state.