From 5dc5f18de8f43fde2beeb3ac2020242d2a09db23 Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Wed, 7 Feb 2018 16:37:30 +0100 Subject: [PATCH] alarmClock --- src/led.cpp | 30 ++++++++++++++++++++++++------ src/led.h | 3 +++ src/main.cpp | 38 +++++++++++++++++++++++++++++--------- src/main.h | 1 + src/screen.cpp | 25 ++++++++++++++++++++++--- 5 files changed, 79 insertions(+), 18 deletions(-) diff --git a/src/led.cpp b/src/led.cpp index 0a50c3b..5d8dc99 100644 --- a/src/led.cpp +++ b/src/led.cpp @@ -10,6 +10,7 @@ using namespace std; Led::Led(void) { index = 0; duration = 0; + wakeupIndex = 0; } void Led::loop_rainbow(void) { @@ -35,6 +36,22 @@ void Led::loop_steady(void) { fill_solid(leds, NUM_LEDS, color); } +void Led::loop_off(void) { + if (wakeupIndex == 0) { + fill_solid(leds, NUM_LEDS, CRGB::Black); + } else { + fill_solid(leds, NUM_LEDS, CRGB::White); + FastLED.setBrightness(wakeupIndex); + } +} + +void Led::wakeUpLight(uint8_t i) { + fill_solid(leds, NUM_LEDS, CRGB::White); + FastLED.setBrightness(i); + Serial.printf("wakeuplight(%d)\n", i); + wakeupIndex = i; +} + void Led::changeAnimation(uint8_t num, uint16_t duration) { if (duration == 0) this->duration = UINT16_MAX; else this->duration = duration; @@ -42,6 +59,7 @@ void Led::changeAnimation(uint8_t num, uint16_t duration) { else if (num == 1) refresh = &Led::loop_sweep; else if (num == 2) refresh = &Led::loop_confetti; else if (num == 3) refresh = &Led::loop_rainbow; + else if (num == 255) refresh = &Led::loop_off; } void Led::changeColor(uint8_t r, uint8_t g, uint8_t b) { @@ -68,10 +86,10 @@ void Led::setup(void) { FastLED.addLeds(leds, NUM_LEDS); FastLED.setBrightness(96); FastLED.setCorrection(0xFF5050); - fill_solid(leds, NUM_LEDS, CRGB::White); + fill_solid(leds, NUM_LEDS, CRGB::Black); color = CRGB::Orange; - refresh = &Led::loop_rainbow; + refresh = &Led::loop_off; delay(100); xTaskCreate( &cTaskWrapper, /* Task function. */ @@ -91,16 +109,16 @@ void Led::animationTask(void* parameters) { // EVERY_N_MILLISECONDS ( 20) { index++; } index++; if (duration == 0) { - refresh = &Led::loop_rainbow; + refresh = &Led::loop_off; } else if (duration == UINT16_MAX) { } else { duration--; } - EVERY_N_SECONDS(30) { +// EVERY_N_SECONDS(30) { // FastLED.clear(); // delay(200); - changeAnimation(2, 2000); - } +// changeAnimation(2, 2000); +// } } vTaskDelay(50 / portTICK_PERIOD_MS); } diff --git a/src/led.h b/src/led.h index 5cd58ad..dfe5b5a 100644 --- a/src/led.h +++ b/src/led.h @@ -16,6 +16,7 @@ class Led { void changeColor(uint8_t, uint8_t, uint8_t); void setBrightness(uint8_t); void stop(void); + void wakeUpLight(uint8_t); private: CRGB leds[NUM_LEDS]; void animationTask(void*); @@ -26,8 +27,10 @@ class Led { void loop_rainbow(void); void loop_confetti(void); void loop_sweep(void); + void loop_off(void); uint16_t duration; uint8_t index; + uint8_t wakeupIndex; CRGB color; bool runTask; }; diff --git a/src/main.cpp b/src/main.cpp index e6ae2ef..9fa17b4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -41,6 +41,7 @@ char timeStr[20]; char weatherStr[32]; RTC_DATA_ATTR static int boot_count = 0; RTC_DATA_ATTR struct tm alarmTime; +RTC_DATA_ATTR bool alarmArmed = false; //Create a new Basecamp instance called iot Basecamp iot; @@ -112,7 +113,7 @@ void setup() { if (wakeup_reason == ESP_SLEEP_WAKEUP_TIMER) { double seconds = difftime(now, mktime(&alarmTime)); - Serial.printf("seconds to alarm: %f\n", seconds); + Serial.printf("alarm in %f seconds\n", seconds); strftime(strftime_buf, sizeof(strftime_buf), "%c", &alarmTime); Serial.print(" Alarm time: "); Serial.println(strftime_buf); @@ -226,11 +227,16 @@ void suspend() { void setAlarmTime(struct tm time) { alarmTime = time; + alarmArmed = true; char strftime_buf[64]; strftime(strftime_buf, sizeof(strftime_buf), "%c", &alarmTime); Serial.print("Setting Alarm time: "); Serial.println(strftime_buf); } +struct tm getAlarmTime() { + return alarmTime; +} + void obtain_time(void) { sntp_setoperatingmode(SNTP_OPMODE_POLL); @@ -266,16 +272,30 @@ void loop() if(millis() - lastUpdate >= 1000) { lastUpdate = millis(); - time_t now; - struct tm timeinfo; - time(&now); - localtime_r(&now, &timeinfo); + time_t now; + struct tm timeinfo; + time(&now); + localtime_r(&now, &timeinfo); + + double seconds = difftime(now, mktime(&alarmTime)); + Serial.printf("alarm in %f seconds (%d)\n", seconds, alarmArmed); + if (seconds >= 0 && alarmArmed) { + alarmArmed = false; + led.wakeUpLight(0); + Serial.println("WAKEUP TIME!!!!!"); + mp3.setVolume(2); + mp3.start(); + led.changeAnimation(2, 0); + } + if (seconds >= -5*60 && seconds <= 0) { + led.wakeUpLight(255*(seconds+300)/300); + } - strftime(timeStr, sizeof(timeStr), "%H:%M:%S", &timeinfo); - sprintf(weatherStr, "%.1f°C %.1f%% %.0fhPa", bme280.readTemperature(), bme280.readHumidity(), bme280.readPressure()); + strftime(timeStr, sizeof(timeStr), "%H:%M:%S", &timeinfo); + sprintf(weatherStr, "%.1f°C %.1f%% %.0fhPa", bme280.readTemperature(), bme280.readHumidity(), bme280.readPressure()); - Serial.print("Free Heap: "); - Serial.println(ESP.getFreeHeap()); + Serial.print("Free Heap: "); + Serial.println(ESP.getFreeHeap()); } diff --git a/src/main.h b/src/main.h index 492c55e..43cd262 100644 --- a/src/main.h +++ b/src/main.h @@ -6,6 +6,7 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties void suspend(uint32_t secondsToSleep); void suspend(); void setAlarmTime(struct tm time); +struct tm getAlarmTime(); 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 23a8121..5e87467 100644 --- a/src/screen.cpp +++ b/src/screen.cpp @@ -76,8 +76,10 @@ uint8_t MainMenu::select() { case 0: if (mp3.playing) { mp3.stop(); + led.changeAnimation(255, 0); } else { mp3.start(); + led.changeAnimation(2, 0); } break; case 1: @@ -131,39 +133,51 @@ uint8_t StationMenu::select() { switch (current_pos) { case 0: mp3.start("http://streaming.shoutcast.com/80sPlanet?lang=en-US"); + led.changeAnimation(2, 0); break; case 1: mp3.start("http://ice1.somafm.com/seventies-128-mp3"); + led.changeAnimation(2, 0); break; case 2: mp3.start("http://streaming.radionomy.com/laradiostrong?lang=de"); + led.changeAnimation(2, 0); break; case 3: mp3.start("http://184.171.163.20:8162/stream"); + led.changeAnimation(2, 0); break; case 4: mp3.start("http://212.83.129.92:8028/;?type=http&nocache=683"); + led.changeAnimation(2, 0); break; case 5: mp3.start("http://192.99.4.210:3574/stream"); + led.changeAnimation(2, 0); break; case 6: mp3.start("http://188.165.192.5:8279/stream?icy=http"); + led.changeAnimation(2, 0); break; case 7: mp3.start("http://69.167.190.234:8080/stream"); + led.changeAnimation(2, 0); break; case 8: mp3.start("http://192.240.102.133:11790/stream"); + led.changeAnimation(2, 0); break; case 9: mp3.start("http://192.99.41.102:5044/stream"); + led.changeAnimation(2, 0); break; case 10: mp3.start("http://radioessen.cast.addradio.de/radioessen/simulcast/high/stream.mp3"); + led.changeAnimation(2, 0); break; case 11: mp3.start("http://172.16.75.17:8000/mopidy"); + led.changeAnimation(2, 0); break; case 12: menuChange = eMainMenu; @@ -281,9 +295,13 @@ void MainScreen::previous() { } AlarmClockScreen::AlarmClockScreen() { - time_t now; - time(&now); - localtime_r(&now, &alarmTime); + if (getAlarmTime().tm_year < (2016 - 1900)) { + time_t now; + time(&now); + localtime_r(&now, &alarmTime); + } else { + alarmTime = getAlarmTime(); + } } void AlarmClockScreen::draw() { @@ -332,6 +350,7 @@ void AlarmClockScreen::previous() { uint8_t AlarmClockScreen::select() { time_t now; time(&now); + alarmTime.tm_sec = 0; while(difftime(now, mktime(&alarmTime)) >= 0) alarmTime.tm_mday++; setAlarmTime(alarmTime); menuChange = eMainScreen;