From 0d7fe67c86a506d23e54ab6d3040557c5b3ad916 Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Wed, 7 Feb 2018 02:15:51 +0100 Subject: [PATCH] alarm and deepsleep --- src/hardware.h | 2 +- src/led.cpp | 11 ++++++++- src/led.h | 2 ++ src/main.cpp | 27 ++++++++++++++------- src/main.h | 4 ++-- src/screen.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/screen.h | 24 ++++++++++++++++++- 7 files changed, 122 insertions(+), 13 deletions(-) diff --git a/src/hardware.h b/src/hardware.h index dc5b951..387df83 100644 --- a/src/hardware.h +++ b/src/hardware.h @@ -8,7 +8,7 @@ static constexpr uint8_t buttonPin = 0; static constexpr uint8_t sensorPin = 0; static constexpr uint8_t rotaryPinA = 39; static constexpr uint8_t rotaryPinB = 38; -static constexpr uint8_t rotaryPinButton = 17; +static constexpr uint8_t rotaryPinButton = 2; static constexpr uint8_t PROGMEM LED_PIN = 21; static constexpr uint8_t PROGMEM NUM_LEDS = 10; #define LED_TYPE WS2812B diff --git a/src/led.cpp b/src/led.cpp index 825ed8d..0a50c3b 100644 --- a/src/led.cpp +++ b/src/led.cpp @@ -55,6 +55,14 @@ void Led::setBrightness(uint8_t brightness) { } } +void Led::stop(void) { + runTask = false; + delay(50); +// FastLED.clear(); + fill_solid(leds, NUM_LEDS, CRGB::Black); + FastLED.show(); +} + void Led::setup(void) { pinMode(LED_PIN, OUTPUT); FastLED.addLeds(leds, NUM_LEDS); @@ -75,7 +83,8 @@ void Led::setup(void) { } void Led::animationTask(void* parameters) { - while(true) { + runTask = true; + while(runTask) { if (refresh != nullptr) { (*this.*refresh)(); FastLED.show(); diff --git a/src/led.h b/src/led.h index 5ec0d5e..5cd58ad 100644 --- a/src/led.h +++ b/src/led.h @@ -15,6 +15,7 @@ class Led { void changeAnimation(uint8_t num, uint16_t duration); void changeColor(uint8_t, uint8_t, uint8_t); void setBrightness(uint8_t); + void stop(void); private: CRGB leds[NUM_LEDS]; void animationTask(void*); @@ -28,6 +29,7 @@ class Led { uint16_t duration; uint8_t index; CRGB color; + bool runTask; }; #endif /* _LED_H */ diff --git a/src/main.cpp b/src/main.cpp index 2224fdd..683c5f2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -191,22 +191,31 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties } } -void suspend() { +void suspend(uint32_t secondsToSleep) { DEBUG_PRINTLN("Entering deep sleep"); esp_sleep_enable_timer_wakeup(1000000LL * secondsToSleep); - esp_sleep_enable_ext0_wakeup((gpio_num_t)sensorPin, 0); - - const uint64_t ext_wakeup_pin_1_mask = 1ULL << ext_wakeup_pin_1; - const uint64_t ext_wakeup_pin_2_mask = 1ULL << ext_wakeup_pin_2; -// esp_sleep_enable_ext1_wakeup(ext_wakeup_pin_1_mask | ext_wakeup_pin_2_mask, ESP_EXT1_WAKEUP_ANY_HIGH); - - //esp_deep_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF); // power down all peripherals + const uint64_t ext_wakeup_rotarybtn_mask = 1ULL << rotaryPinButton; + esp_sleep_enable_ext1_wakeup(ext_wakeup_rotarybtn_mask, ESP_EXT1_WAKEUP_ANY_HIGH); + +////esp_sleep_enable_ext0_wakeup((gpio_num_t)sensorPin, 0); +// const uint64_t ext_wakeup_pin_1_mask = 1ULL << ext_wakeup_pin_1; +// const uint64_t ext_wakeup_pin_2_mask = 1ULL << ext_wakeup_pin_2; +//// esp_sleep_enable_ext1_wakeup(ext_wakeup_pin_1_mask | ext_wakeup_pin_2_mask, ESP_EXT1_WAKEUP_ANY_HIGH); +// +////esp_deep_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF); // power down all peripherals //properly disconnect from the MQTT broker iot.mqtt.disconnect(); //send the ESP into deep sleep esp_deep_sleep_start(); } +void suspend() { + suspend(secondsToSleep); +} + +void setWakeupTime(uint32_t time) { +} + void obtain_time(void) { sntp_setoperatingmode(SNTP_OPMODE_POLL); @@ -267,6 +276,8 @@ void loop() if (menuChange == eMainScreen) screen = new MainScreen(); else if (menuChange == eMainMenu) screen = new MainMenu(); else if (menuChange == eStationMenu) screen = new StationMenu(); + else if (menuChange == eAlarmClockScreen) screen = new AlarmClockScreen(); + else if (menuChange == eSuspendScreen) screen = new SuspendScreen(); else screen = new MainScreen(); menuChange = eNone; } diff --git a/src/main.h b/src/main.h index 0e7feb7..7fba66c 100644 --- a/src/main.h +++ b/src/main.h @@ -1,11 +1,11 @@ -#include "driver/i2s.h" - void resetToFactoryDefaults(); void setup(); void onMqttConnect(bool sessionPresent); 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 loop(); void rotation(int i, int direction, int buttonPressed); void obtain_time(void); diff --git a/src/screen.cpp b/src/screen.cpp index 180b8b3..307130a 100644 --- a/src/screen.cpp +++ b/src/screen.cpp @@ -62,6 +62,8 @@ MainMenu::MainMenu() { "Radio Stations", "Toggle Light", "Test2", + "AlarmClock", + "SLEEP", "\xAB Return"}; if (mp3.playing) { string_list[0] = "STOP"; @@ -89,6 +91,14 @@ uint8_t MainMenu::select() { iot.mqtt.publish(topic.c_str(), 0, false, "test2" ); break; case 4: + menuChange = eAlarmClockScreen; + return current_pos; + break; + case 5: + menuChange = eSuspendScreen; + return current_pos; + break; + case 6: menuChange = eMainScreen; break; default: @@ -269,3 +279,58 @@ void MainScreen::previous() { led.setBrightness((255*volume)/100); Serial.printf("volume: %d\n", volume); } + +void AlarmClockScreen::draw() { + uint8_t header_height = 12; + u8g2.clearBuffer(); + + /* draw Title */ + u8g2.setFont(u8g2_font_guildenstern_nbp_tr); + u8g2.drawUTF8(2,header_height-2,title); + u8g2.drawLine(0,header_height,u8g2.getDisplayWidth(),header_height); + + char alarmStr[9]; + sprintf(alarmStr, "%02d:%02d", hours, minutes); + u8g2.setFont(u8g2_font_inb19_mf); + u8g2.drawStr(20, 55, alarmStr); + + u8g2.sendBuffer(); // transfer internal memory to the display +} + +void AlarmClockScreen::next() { + if (millis() - lastRotary <= 40) minutes+=20; + else if (millis() - lastRotary <= 80) minutes+=5; + else minutes++; + lastRotary = millis(); + + if (minutes >= 60) { + hours += minutes/60; + hours %= 24; + minutes %= 60; + } +} + +void AlarmClockScreen::previous() { + if (millis() - lastRotary <= 40) minutes-=20; + else if (millis() - lastRotary <= 80) minutes-=5; + else minutes--; + lastRotary = millis(); + + if (minutes < 0) { + hours += minutes/60 -1; + if (hours < 0) hours = hours%24 +24; + minutes = minutes % 60 + 60 ; + } +} + +uint8_t AlarmClockScreen::select() { + setWakeupTime(0); + menuChange = eMainScreen; +} + +void SuspendScreen::draw() { + mp3.stop(); + led.stop(); + delay(1000); + suspend(30); +} diff --git a/src/screen.h b/src/screen.h index f765ff5..355baf2 100644 --- a/src/screen.h +++ b/src/screen.h @@ -16,12 +16,13 @@ #include #endif +#include "main.h" #include "mp3.h" #include "BME280.h" #include "image.h" #include "led.h" -enum menuType { eNone=0, eWelcomeScreen, eMainScreen, eMainMenu, eStationMenu }; +enum menuType { eNone=0, eWelcomeScreen, eMainScreen, eMainMenu, eStationMenu, eAlarmClockScreen, eSuspendScreen }; extern U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2; extern char timeStr[]; @@ -106,4 +107,25 @@ class StationMenu : public SelectionList { private: }; +class AlarmClockScreen : public Screen { + public: + void draw(void) override; + void next(void) override; + void previous(void) override; + uint8_t select(void) override; + const char* title = "Alarm Clock"; + private: + uint32_t lastRotary = 0; + int hours = 8; + int minutes = 0; +}; + +class SuspendScreen : public Screen { + public: + void draw(void) override; + const char* title = "zzzzzZZZZZ"; + private: + int progress = 0; +}; + #endif /* _SCREEN_H */