15static const char*
TAG =
"SleepMgr";
29 static SleepManager s_instance;
43 lockScreen_ = lockScreen;
44 lockScreenEnteredMs_ = 0;
45 inLightSleep_ =
false;
54 lockScreenEnteredMs_ = nowMs;
62 lockScreenEnteredMs_ = esp_timer_get_time() / 1000;
73 if (!lockScreen_)
return;
76 if (lockScreenEnteredMs_ == 0) {
77 lockScreenEnteredMs_ = nowMs;
82 if (power_ && power_->isUsbConnected()) {
83 lockScreenEnteredMs_ = nowMs;
93 lockScreenEnteredMs_ = nowMs;
98 uint32_t elapsed = nowMs - lockScreenEnteredMs_;
100 enterLockScreenSleep();
108void SleepManager::enterLockScreenSleep() {
109 if (!lockScreen_ || !sleep_)
return;
116 inLightSleep_ =
true;
127 }
while (handleWakeup());
143bool SleepManager::handleWakeup() {
144 if (!sleep_ || !lockScreen_)
return false;
155 bool usb = power_ && power_->isUsbConnected();
170 userWake = sleep_->wasKeypadWakeup();
178 lockScreenEnteredMs_ = esp_timer_get_time() / 1000;
179 inLightSleep_ =
false;
181 lockScreen_->markDirty();
187 time_t now = time(
nullptr);
188 struct tm* tm = localtime(&now);
191 snprintf(hhmm,
sizeof(hhmm),
"%02d:%02d", tm->tm_hour, tm->tm_min);
192 lockScreen_->setClock(hhmm);
193 snprintf(buf,
sizeof(buf),
"%02d.%02d.%04d", tm->tm_mday, tm->tm_mon + 1, tm->tm_year + 1900);
194 lockScreen_->setDate(buf);
202 if (strcmp(hhmm, lastRenderedHHMM_) != 0) {
203 strncpy(lastRenderedHHMM_, hhmm,
sizeof(lastRenderedHHMM_) - 1);
204 lastRenderedHHMM_[
sizeof(lastRenderedHHMM_) - 1] =
'\0';
220 if (!reason)
return false;
223 for (uint8_t i = 0; i < inhibitorCount_; i++) {
224 if (inhibitors_[i] && strcmp(inhibitors_[i], reason) == 0) {
231 LOG_W(
TAG,
"Sleep inhibitor list full, cannot add: %s", reason);
236 inhibitors_[inhibitorCount_++] = reason;
237 LOG_I(
TAG,
"Sleep inhibitor added: %s (count=%d)", reason, inhibitorCount_);
240 updateCaffeinatedIcon();
250 if (!reason)
return false;
253 for (uint8_t i = 0; i < inhibitorCount_; i++) {
254 if (inhibitors_[i] && strcmp(inhibitors_[i], reason) == 0) {
256 for (uint8_t j = i; j < inhibitorCount_ - 1; j++) {
257 inhibitors_[j] = inhibitors_[j + 1];
259 inhibitors_[--inhibitorCount_] =
nullptr;
261 LOG_I(
TAG,
"Sleep inhibitor removed: %s (count=%d)", reason, inhibitorCount_);
264 updateCaffeinatedIcon();
276void SleepManager::updateCaffeinatedIcon() {
277 if (!lockScreen_)
return;
279 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 bool isUsbConnected() const =0
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.
void usb_cdc_reenumerate(void)
Forces a USB re-enumeration by toggling the bus connection.