|
|
@ -30,6 +30,9 @@ |
|
|
|
|
|
|
|
#include "driver/rtc_io.h" |
|
|
|
|
|
|
|
#include <WiFi.h> |
|
|
|
#include <WiFiUdp.h> |
|
|
|
|
|
|
|
#include "main.h" |
|
|
|
#include "hardware.h" |
|
|
|
#include "mp3.h" |
|
|
@ -50,6 +53,7 @@ U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ 16, /* clock=*/ 15 |
|
|
|
char timeStr[20]; |
|
|
|
char weatherStr[32]; |
|
|
|
char statusStr[32]; |
|
|
|
unsigned char udprecvbuf[1460]; |
|
|
|
RTC_DATA_ATTR static int boot_count = 0; |
|
|
|
RTC_DATA_ATTR struct tm alarmTime; |
|
|
|
RTC_DATA_ATTR bool alarmArmed = false; |
|
|
@ -63,6 +67,7 @@ MP3 mp3; |
|
|
|
Rotary rotary; |
|
|
|
Screen* screen; |
|
|
|
Led led; |
|
|
|
WiFiUDP udp; |
|
|
|
|
|
|
|
menuType menuChange = eNone; |
|
|
|
uint32_t lastButtonPress = 0; |
|
|
@ -77,6 +82,9 @@ String weatherTopic; |
|
|
|
String statusTopic; |
|
|
|
String sensorTopic; |
|
|
|
|
|
|
|
const IPAddress udpMulticastAddress(239,255,255,245); |
|
|
|
const uint16_t udpMulticastPort = 5555; |
|
|
|
|
|
|
|
void setup() { |
|
|
|
// gpio configuration
|
|
|
|
// rtc_gpio_deinit((gpio_num_t)buttonPin);
|
|
|
@ -177,7 +185,8 @@ void setup() { |
|
|
|
screen->draw(); |
|
|
|
menuChange = eMainScreen; |
|
|
|
u8g2.setContrast(127); |
|
|
|
|
|
|
|
// U8X8_CA(0x08d, 0x010); /* [2] charge pump setting (p62): 0x014 enable, 0x010 disable */
|
|
|
|
// U8X8_CA(0xd9, 0x22); /* [2] pre-charge period 0x022/f1*/
|
|
|
|
|
|
|
|
//Set up the Callbacks for the MQTT instance. Refer to the Async MQTT Client documentation
|
|
|
|
// TODO: We should do this actually _before_ connecting the mqtt client...
|
|
|
@ -206,11 +215,22 @@ void setup() { |
|
|
|
localtime_r(&now, &timeinfo); |
|
|
|
strftime(timeStr, sizeof(timeStr), "%H:%M:%S", &timeinfo); |
|
|
|
|
|
|
|
if (timeinfo.tm_hour < 7 || timeinfo.tm_hour > 23) u8g2.setContrast(1); |
|
|
|
else u8g2.setContrast(127); |
|
|
|
if (timeinfo.tm_hour < 7 || timeinfo.tm_hour >= 22) { |
|
|
|
Serial.println("night mode"); |
|
|
|
u8g2.setContrast(1); |
|
|
|
u8x8_cad_StartTransfer(u8g2.getU8x8()); |
|
|
|
u8x8_cad_SendCmd( u8g2.getU8x8(), 0xD9); |
|
|
|
u8x8_cad_SendArg( u8g2.getU8x8(), 0x01); //max 34 /* [2] pre-charge period 0x022/f1*/
|
|
|
|
u8x8_cad_EndTransfer(u8g2.getU8x8()); |
|
|
|
// u8x8_cad_StartTransfer(u8g2.getU8x8());
|
|
|
|
// u8x8_cad_SendCmd( u8g2.getU8x8(), 0x8D);
|
|
|
|
// u8x8_cad_SendArg( u8g2.getU8x8(), 0x10); /* [2] charge pump setting (p62): 0x014 enable, 0x010 disable */
|
|
|
|
// u8x8_cad_EndTransfer(u8g2.getU8x8());
|
|
|
|
} else u8g2.setContrast(127); |
|
|
|
|
|
|
|
mp3.begin(); |
|
|
|
led.setup(); |
|
|
|
udp.beginMulticast(udpMulticastAddress, udpMulticastPort); |
|
|
|
|
|
|
|
lastButtonPress = millis(); |
|
|
|
rotary.registerCallback(rotation); |
|
|
@ -309,7 +329,8 @@ void suspend(uint32_t secondsToSleep) { |
|
|
|
|
|
|
|
esp_sleep_enable_timer_wakeup(1000000LL * secondsToSleep); |
|
|
|
const uint64_t ext_wakeup_rotarybtn_mask = 1ULL << rotaryPinButton; |
|
|
|
const uint64_t ext_wakeup_sensor_mask = 1ULL << sensorPin; |
|
|
|
// const uint64_t ext_wakeup_sensor_mask = 1ULL << sensorPin;
|
|
|
|
const uint64_t ext_wakeup_sensor_mask = 0; // disable
|
|
|
|
esp_sleep_enable_ext1_wakeup(ext_wakeup_rotarybtn_mask | ext_wakeup_sensor_mask, ESP_EXT1_WAKEUP_ANY_HIGH); |
|
|
|
|
|
|
|
////esp_sleep_enable_ext0_wakeup((gpio_num_t)sensorPin, 0);
|
|
|
@ -404,7 +425,7 @@ void loop() |
|
|
|
|
|
|
|
bool stayAwake = false; |
|
|
|
if (alarmArmed && seconds >= -2*5*60) stayAwake = true; |
|
|
|
if (!mp3.playing && millis() - lastActive >= 60*1000 && !stayAwake) suspend(); |
|
|
|
if (!mp3.playing && millis() - lastActive >= stayOnTime && !stayAwake) suspend(); |
|
|
|
|
|
|
|
Serial.printf("alarm in %f seconds (%d)\n", seconds, alarmArmed); |
|
|
|
if (seconds >= 0 && alarmArmed) { |
|
|
@ -438,6 +459,11 @@ void loop() |
|
|
|
sensorON = false; |
|
|
|
} |
|
|
|
|
|
|
|
// Send a packet
|
|
|
|
udp.beginMulticastPacket(); |
|
|
|
udp.printf("Seconds since boot: %u", millis()/1000); |
|
|
|
udp.endPacket(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (digitalRead(sensorPin) == HIGH) { |
|
|
@ -453,6 +479,13 @@ void loop() |
|
|
|
transmitStatus(); |
|
|
|
} |
|
|
|
|
|
|
|
int lenp = udp.parsePacket(); |
|
|
|
//Serial.println(lenp);
|
|
|
|
while(udp.available()) { |
|
|
|
int len = udp.read(udprecvbuf, sizeof(udprecvbuf)); |
|
|
|
Serial.printf("udp received %d bytes\n", len); |
|
|
|
} |
|
|
|
|
|
|
|
if (menuChange != eNone) { |
|
|
|
delete screen; |
|
|
|
if (menuChange == eMainScreen) screen = new MainScreen(); |
|
|
|