Skip to content

Commit 897f50e

Browse files
committed
Fix entering/exiting sleep mode
1 parent 01bfbbe commit 897f50e

2 files changed

Lines changed: 19 additions & 16 deletions

File tree

firmware/main/include/nixie_clock.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,6 @@ class NixieClock : public IClock {
5454
TaskHandle_t mShowCurrentTimeTaskHandle;
5555
I2cBus mI2c;
5656
Ds3231 mRtc;
57+
bool mLastSleepModeStatus;
5758
};
5859
#endif // nixie_clock_h

firmware/main/nixie_clock.cpp

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ NixieClock::NixieClock()
5353
: mLedController(kLedPin),
5454
mNixieTube(kBcdPinA, kBcdPinB, kBcdPinC, kBcdPinD), mWebServer(*this),
5555
mShowCurrentTimeTaskHandle(nullptr), mI2c(kI2cPort, kI2cSda, kI2cScl),
56-
mRtc(mI2c) {
56+
mRtc(mI2c), mLastSleepModeStatus(false) {
5757
gRtcPtr = &mRtc;
5858
}
5959

@@ -163,9 +163,9 @@ void NixieClock::initialize() {
163163
handleSleepMode();
164164

165165
// Show current time after the current time is synced
166-
if (isTimeSynced) {
166+
if (isTimeSynced && !isInSleepMode()) {
167167
startShowCurrentTimeTask();
168-
xTaskCreate(loopTask, "loopTask", 2048, this, 2, nullptr);
168+
xTaskCreate(loopTask, "loopTask", 4096, this, 2, nullptr);
169169
}
170170
}
171171

@@ -309,7 +309,8 @@ void NixieClock::loopTask(void* param) {
309309
struct tm timeInfo;
310310
time(&now);
311311
localtime_r(&now, &timeInfo);
312-
if (timeInfo.tm_sec == 0 && lastSecond != 0) {
312+
self->handleSleepMode();
313+
if (timeInfo.tm_sec == 0 && lastSecond != 0 && !self->isInSleepMode()) {
313314
self->startShowCurrentTimeTask();
314315
}
315316
lastSecond = timeInfo.tm_sec;
@@ -335,12 +336,6 @@ void NixieClock::showCurrentTimeTask(void* param) {
335336
char buf[32];
336337
strftime(buf, sizeof(buf), "%H:%M:%S", &nowTm);
337338
ESP_LOGI(kTag, "Current time: %s", buf);
338-
if (self->isInSleepMode()) {
339-
ESP_LOGI(kTag, "Sleep mode on");
340-
self->mShowCurrentTimeTaskHandle = nullptr;
341-
vTaskDelete(nullptr);
342-
return;
343-
}
344339
LedInfo ledInfo = self->mLedController.getLedInfo();
345340
if (ledInfo.getState() != LedState::Off) {
346341
ledInfo.setState(LedState::On);
@@ -387,13 +382,20 @@ void NixieClock::showCurrentTimeTask(void* param) {
387382
}
388383

389384
void NixieClock::handleSleepMode() {
390-
LedInfo ledInfo = ConfigStore::loadLedInfo().value();
391-
if (isInSleepMode()) {
392-
ESP_LOGI(kTag, "In sleep mode, turning off LED.");
393-
// turn off the led
394-
ledInfo.setState(LedState::Off);
385+
bool currentSleepModeStatus = isInSleepMode();
386+
if (mLastSleepModeStatus != currentSleepModeStatus) {
387+
mLastSleepModeStatus = currentSleepModeStatus;
388+
if (mLastSleepModeStatus) {
389+
ESP_LOGI(kTag, "Entering sleep mode.");
390+
auto ledInfo = mLedController.getLedInfo();
391+
ledInfo.setState(LedState::Off);
392+
mLedController.setLedInfo(ledInfo);
393+
} else {
394+
ESP_LOGI(kTag, "Exiting sleep mode.");
395+
LedInfo ledInfo = ConfigStore::loadLedInfo().value_or(LedInfo());
396+
mLedController.setLedInfo(ledInfo);
397+
}
395398
}
396-
mLedController.setLedInfo(ledInfo);
397399
}
398400

399401
time_t NixieClock::timegmRtc(struct tm* tm) {

0 commit comments

Comments
 (0)