Browse Source

alarm and deepsleep

main
Hendrik Langer 7 years ago
parent
commit
0d7fe67c86
  1. 2
      src/hardware.h
  2. 11
      src/led.cpp
  3. 2
      src/led.h
  4. 27
      src/main.cpp
  5. 4
      src/main.h
  6. 65
      src/screen.cpp
  7. 24
      src/screen.h

2
src/hardware.h

@ -8,7 +8,7 @@ static constexpr uint8_t buttonPin = 0;
static constexpr uint8_t sensorPin = 0; static constexpr uint8_t sensorPin = 0;
static constexpr uint8_t rotaryPinA = 39; static constexpr uint8_t rotaryPinA = 39;
static constexpr uint8_t rotaryPinB = 38; 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 LED_PIN = 21;
static constexpr uint8_t PROGMEM NUM_LEDS = 10; static constexpr uint8_t PROGMEM NUM_LEDS = 10;
#define LED_TYPE WS2812B #define LED_TYPE WS2812B

11
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) { void Led::setup(void) {
pinMode(LED_PIN, OUTPUT); pinMode(LED_PIN, OUTPUT);
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS); FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
@ -75,7 +83,8 @@ void Led::setup(void) {
} }
void Led::animationTask(void* parameters) { void Led::animationTask(void* parameters) {
while(true) { runTask = true;
while(runTask) {
if (refresh != nullptr) { if (refresh != nullptr) {
(*this.*refresh)(); (*this.*refresh)();
FastLED.show(); FastLED.show();

2
src/led.h

@ -15,6 +15,7 @@ class Led {
void changeAnimation(uint8_t num, uint16_t duration); void changeAnimation(uint8_t num, uint16_t duration);
void changeColor(uint8_t, uint8_t, uint8_t); void changeColor(uint8_t, uint8_t, uint8_t);
void setBrightness(uint8_t); void setBrightness(uint8_t);
void stop(void);
private: private:
CRGB leds[NUM_LEDS]; CRGB leds[NUM_LEDS];
void animationTask(void*); void animationTask(void*);
@ -28,6 +29,7 @@ class Led {
uint16_t duration; uint16_t duration;
uint8_t index; uint8_t index;
CRGB color; CRGB color;
bool runTask;
}; };
#endif /* _LED_H */ #endif /* _LED_H */

27
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"); DEBUG_PRINTLN("Entering deep sleep");
esp_sleep_enable_timer_wakeup(1000000LL * secondsToSleep); esp_sleep_enable_timer_wakeup(1000000LL * secondsToSleep);
esp_sleep_enable_ext0_wakeup((gpio_num_t)sensorPin, 0); const uint64_t ext_wakeup_rotarybtn_mask = 1ULL << rotaryPinButton;
esp_sleep_enable_ext1_wakeup(ext_wakeup_rotarybtn_mask, ESP_EXT1_WAKEUP_ANY_HIGH);
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_ext0_wakeup((gpio_num_t)sensorPin, 0);
// esp_sleep_enable_ext1_wakeup(ext_wakeup_pin_1_mask | ext_wakeup_pin_2_mask, ESP_EXT1_WAKEUP_ANY_HIGH); // 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_deep_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF); // power down all peripherals //// 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 //properly disconnect from the MQTT broker
iot.mqtt.disconnect(); iot.mqtt.disconnect();
//send the ESP into deep sleep //send the ESP into deep sleep
esp_deep_sleep_start(); esp_deep_sleep_start();
} }
void suspend() {
suspend(secondsToSleep);
}
void setWakeupTime(uint32_t time) {
}
void obtain_time(void) { void obtain_time(void) {
sntp_setoperatingmode(SNTP_OPMODE_POLL); sntp_setoperatingmode(SNTP_OPMODE_POLL);
@ -267,6 +276,8 @@ void loop()
if (menuChange == eMainScreen) screen = new MainScreen(); if (menuChange == eMainScreen) screen = new MainScreen();
else if (menuChange == eMainMenu) screen = new MainMenu(); else if (menuChange == eMainMenu) screen = new MainMenu();
else if (menuChange == eStationMenu) screen = new StationMenu(); 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(); else screen = new MainScreen();
menuChange = eNone; menuChange = eNone;
} }

4
src/main.h

@ -1,11 +1,11 @@
#include "driver/i2s.h"
void resetToFactoryDefaults(); void resetToFactoryDefaults();
void setup(); void setup();
void onMqttConnect(bool sessionPresent); void onMqttConnect(bool sessionPresent);
void transmitStatus(); void transmitStatus();
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total); 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 suspend();
void setWakeupTime(uint32_t time);
void loop(); void loop();
void rotation(int i, int direction, int buttonPressed); void rotation(int i, int direction, int buttonPressed);
void obtain_time(void); void obtain_time(void);

65
src/screen.cpp

@ -62,6 +62,8 @@ MainMenu::MainMenu() {
"Radio Stations", "Radio Stations",
"Toggle Light", "Toggle Light",
"Test2", "Test2",
"AlarmClock",
"SLEEP",
"\xAB Return"}; "\xAB Return"};
if (mp3.playing) { if (mp3.playing) {
string_list[0] = "STOP"; string_list[0] = "STOP";
@ -89,6 +91,14 @@ uint8_t MainMenu::select() {
iot.mqtt.publish(topic.c_str(), 0, false, "test2" ); iot.mqtt.publish(topic.c_str(), 0, false, "test2" );
break; break;
case 4: case 4:
menuChange = eAlarmClockScreen;
return current_pos;
break;
case 5:
menuChange = eSuspendScreen;
return current_pos;
break;
case 6:
menuChange = eMainScreen; menuChange = eMainScreen;
break; break;
default: default:
@ -269,3 +279,58 @@ void MainScreen::previous() {
led.setBrightness((255*volume)/100); led.setBrightness((255*volume)/100);
Serial.printf("volume: %d\n", volume); 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);
}

24
src/screen.h

@ -16,12 +16,13 @@
#include <Wire.h> #include <Wire.h>
#endif #endif
#include "main.h"
#include "mp3.h" #include "mp3.h"
#include "BME280.h" #include "BME280.h"
#include "image.h" #include "image.h"
#include "led.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 U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2;
extern char timeStr[]; extern char timeStr[];
@ -106,4 +107,25 @@ class StationMenu : public SelectionList {
private: 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 */ #endif /* _SCREEN_H */

Loading…
Cancel
Save