diff --git a/src/main.cpp b/src/main.cpp index 683c5f2..51ae4b0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -38,6 +38,7 @@ U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ 16, /* clock=*/ 15 char timeStr[20]; char weatherStr[32]; RTC_DATA_ATTR static int boot_count = 0; +RTC_DATA_ATTR struct tm alarmTime; //Create a new Basecamp instance called iot Basecamp iot; @@ -66,17 +67,6 @@ void setup() { boot_count++; - time_t now; - struct tm timeinfo; - time(&now); - setenv("TZ", "Europe/Berlin", 1); - tzset(); - localtime_r(&now, &timeinfo); - - if (timeinfo.tm_year < (2016 - 1900)) { - // time not set - } - //Initialize Basecamp iot.begin(); @@ -102,11 +92,28 @@ void setup() { break; } -char strftime_buf[64]; -strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo); -Serial.println(strftime_buf); + time_t now; + struct tm timeinfo; + time(&now); + setenv("TZ", "Europe/Berlin", 1); + tzset(); + localtime_r(&now, &timeinfo); + if (timeinfo.tm_year < (2016 - 1900)) { + // time not set + } + char strftime_buf[64]; + strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo); + Serial.print("Current time: "); Serial.println(strftime_buf); + if (wakeup_reason == ESP_SLEEP_WAKEUP_TIMER) { + double seconds = difftime(now, mktime(&alarmTime)); + Serial.printf("seconds to alarm: %f\n", seconds); + strftime(strftime_buf, sizeof(strftime_buf), "%c", &alarmTime); + Serial.print(" Alarm time: "); Serial.println(strftime_buf); + + if (-seconds > secondsToSleep) suspend(); + } u8g2.begin(); // delay(50); @@ -213,7 +220,11 @@ void suspend() { suspend(secondsToSleep); } -void setWakeupTime(uint32_t time) { +void setAlarmTime(struct tm time) { + alarmTime = time; + char strftime_buf[64]; + strftime(strftime_buf, sizeof(strftime_buf), "%c", &alarmTime); + Serial.print("Setting Alarm time: "); Serial.println(strftime_buf); } diff --git a/src/main.h b/src/main.h index 7fba66c..492c55e 100644 --- a/src/main.h +++ b/src/main.h @@ -5,7 +5,7 @@ void transmitStatus(); void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total); void suspend(uint32_t secondsToSleep); void suspend(); -void setWakeupTime(uint32_t time); +void setAlarmTime(struct tm time); void loop(); void rotation(int i, int direction, int buttonPressed); void obtain_time(void); diff --git a/src/screen.cpp b/src/screen.cpp index 307130a..44c9821 100644 --- a/src/screen.cpp +++ b/src/screen.cpp @@ -280,6 +280,12 @@ void MainScreen::previous() { Serial.printf("volume: %d\n", volume); } +AlarmClockScreen::AlarmClockScreen() { + time_t now; + time(&now); + localtime_r(&now, &alarmTime); +} + void AlarmClockScreen::draw() { uint8_t header_height = 12; u8g2.clearBuffer(); @@ -290,7 +296,7 @@ void AlarmClockScreen::draw() { u8g2.drawLine(0,header_height,u8g2.getDisplayWidth(),header_height); char alarmStr[9]; - sprintf(alarmStr, "%02d:%02d", hours, minutes); + strftime(alarmStr, sizeof(alarmStr), "%H:%M", &alarmTime); u8g2.setFont(u8g2_font_inb19_mf); u8g2.drawStr(20, 55, alarmStr); @@ -298,33 +304,33 @@ void AlarmClockScreen::draw() { } void AlarmClockScreen::next() { - if (millis() - lastRotary <= 40) minutes+=20; - else if (millis() - lastRotary <= 80) minutes+=5; - else minutes++; + if (millis() - lastRotary <= 40) alarmTime.tm_min+=20; + else if (millis() - lastRotary <= 80) alarmTime.tm_min+=5; + else alarmTime.tm_min++; lastRotary = millis(); - if (minutes >= 60) { - hours += minutes/60; - hours %= 24; - minutes %= 60; + if (alarmTime.tm_min >= 60) { + alarmTime.tm_hour += alarmTime.tm_min/60; + alarmTime.tm_hour %= 24; + alarmTime.tm_min %= 60; } } void AlarmClockScreen::previous() { - if (millis() - lastRotary <= 40) minutes-=20; - else if (millis() - lastRotary <= 80) minutes-=5; - else minutes--; + if (millis() - lastRotary <= 40) alarmTime.tm_min-=20; + else if (millis() - lastRotary <= 80) alarmTime.tm_min-=5; + else alarmTime.tm_min--; lastRotary = millis(); - if (minutes < 0) { - hours += minutes/60 -1; - if (hours < 0) hours = hours%24 +24; - minutes = minutes % 60 + 60 ; + if (alarmTime.tm_min < 0) { + alarmTime.tm_hour += alarmTime.tm_min/60 -1; + if (alarmTime.tm_hour < 0) alarmTime.tm_hour = alarmTime.tm_hour%24 +24; + alarmTime.tm_min = alarmTime.tm_min % 60 + 60 ; } } uint8_t AlarmClockScreen::select() { - setWakeupTime(0); + setAlarmTime(alarmTime); menuChange = eMainScreen; } @@ -332,5 +338,6 @@ void SuspendScreen::draw() { mp3.stop(); led.stop(); delay(1000); + u8g2.setPowerSave(1); suspend(30); } diff --git a/src/screen.h b/src/screen.h index 355baf2..a292e1d 100644 --- a/src/screen.h +++ b/src/screen.h @@ -109,6 +109,7 @@ class StationMenu : public SelectionList { class AlarmClockScreen : public Screen { public: + AlarmClockScreen(); void draw(void) override; void next(void) override; void previous(void) override; @@ -116,8 +117,7 @@ class AlarmClockScreen : public Screen { const char* title = "Alarm Clock"; private: uint32_t lastRotary = 0; - int hours = 8; - int minutes = 0; + struct tm alarmTime; }; class SuspendScreen : public Screen {