Browse Source

move state into new struct

main
Hendrik Langer 7 years ago
parent
commit
b7e32dad69
  1. 63
      src/main.cpp
  2. 28
      src/main.h
  3. 6
      src/screen.cpp
  4. 4
      src/screen.h

63
src/main.cpp

@ -50,15 +50,10 @@ uint8_t temprature_sens_read();
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ 16, /* clock=*/ 15, /* data=*/ 4); U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ 16, /* clock=*/ 15, /* data=*/ 4);
char timeStr[20];
char weatherStr[32];
char statusStr[32];
unsigned char udprecvbuf[1460]; unsigned char udprecvbuf[1460];
RTC_DATA_ATTR static int boot_count = 0;
RTC_DATA_ATTR struct tm alarmTime; RTC_DATA_ATTR WEBRADIO_STATE_PERSISTANT state_persistant;
RTC_DATA_ATTR bool alarmArmed = false; WEBRADIO_STATE state;
RTC_DATA_ATTR bool displaySleep = true;
//Create a new Basecamp instance called iot //Create a new Basecamp instance called iot
Basecamp iot; Basecamp iot;
@ -97,7 +92,7 @@ void setup() {
pinMode(18, OUTPUT); pinMode(18, OUTPUT);
digitalWrite(18, HIGH); // disable LoRa_CS digitalWrite(18, HIGH); // disable LoRa_CS
boot_count++; state_persistant.boot_count++;
// static wifi_country_t wifi_country = {.cc="EU", .schan=1, .nchan=13, .policy=WIFI_COUNTRY_POLICY_AUTO}; // static wifi_country_t wifi_country = {.cc="EU", .schan=1, .nchan=13, .policy=WIFI_COUNTRY_POLICY_AUTO};
// esp_wifi_set_country(&wifi_country); // esp_wifi_set_country(&wifi_country);
@ -160,7 +155,7 @@ void setup() {
if (timeinfo.tm_year < (2016 - 1900)) { if (timeinfo.tm_year < (2016 - 1900)) {
// time not set // time not set
} }
strftime(timeStr, sizeof(timeStr), "%H:%M", &timeinfo); strftime(state.timeStr, sizeof(state.timeStr), "%H:%M", &timeinfo);
char strftime_buf[64]; char strftime_buf[64];
@ -185,12 +180,12 @@ void setup() {
obtain_time(); obtain_time();
} }
double seconds = difftime(now, mktime(&alarmTime)); double seconds = difftime(now, mktime(&(state_persistant.alarmTime)));
Serial.printf("alarm in %f seconds\n", seconds); Serial.printf("alarm in %f seconds\n", seconds);
strftime(strftime_buf, sizeof(strftime_buf), "%c", &alarmTime); strftime(strftime_buf, sizeof(strftime_buf), "%c", &(state_persistant.alarmTime));
Serial.print(" Alarm time: "); Serial.println(strftime_buf); Serial.print(" Alarm time: "); Serial.println(strftime_buf);
if (!alarmArmed || -seconds > secondsToSleep) suspend(); if (!state_persistant.alarmArmed || -seconds > secondsToSleep) suspend();
else if (-seconds > 5*60) suspend(5*60); else if (-seconds > 5*60) suspend(5*60);
} }
@ -228,7 +223,7 @@ void setup() {
time(&now); time(&now);
} }
localtime_r(&now, &timeinfo); localtime_r(&now, &timeinfo);
strftime(timeStr, sizeof(timeStr), "%H:%M:%S", &timeinfo); strftime(state.timeStr, sizeof(state.timeStr), "%H:%M:%S", &timeinfo);
if (timeinfo.tm_hour < 7 || timeinfo.tm_hour >= 22) { if (timeinfo.tm_hour < 7 || timeinfo.tm_hour >= 22) {
Serial.println("night mode"); Serial.println("night mode");
@ -254,7 +249,7 @@ void setup() {
if (wakeup_reason == ESP_SLEEP_WAKEUP_EXT1) { if (wakeup_reason == ESP_SLEEP_WAKEUP_EXT1) {
uint64_t wakeup_pinmask = esp_sleep_get_ext1_wakeup_status(); uint64_t wakeup_pinmask = esp_sleep_get_ext1_wakeup_status();
if (wakeup_pinmask & (1ULL << ext_wakeup_pin_1)) { if (wakeup_pinmask & (1ULL << ext_wakeup_pin_1)) {
uint16_t statusPacketIdSub = iot.mqtt.publish(sensorTopic.c_str(), 0, false, timeStr); uint16_t statusPacketIdSub = iot.mqtt.publish(sensorTopic.c_str(), 0, false, state.timeStr);
sensorON = true; sensorON = true;
} }
} }
@ -352,13 +347,13 @@ void suspend(uint32_t secondsToSleep) {
// //
////esp_deep_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF); // power down all peripherals ////esp_deep_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF); // power down all peripherals
if (!displaySleep) { if (!state.displaySleep) {
u8g2.begin(); u8g2.begin();
u8g2.setPowerSave(0); u8g2.setPowerSave(0);
u8g2.clearBuffer(); u8g2.clearBuffer();
u8g2.sendBuffer(); u8g2.sendBuffer();
u8g2.setFont(u8g2_font_inb19_mf); u8g2.setFont(u8g2_font_inb19_mf);
u8g2.drawStr(0, 20, timeStr); u8g2.drawStr(0, 20, state.timeStr);
rtc_gpio_hold_en((gpio_num_t)16); // 16 not connected to ulp processor :( rtc_gpio_hold_en((gpio_num_t)16); // 16 not connected to ulp processor :(
esp_sleep_enable_timer_wakeup(1000000LL * 60); esp_sleep_enable_timer_wakeup(1000000LL * 60);
} }
@ -370,27 +365,27 @@ void suspend(uint32_t secondsToSleep) {
void suspend() { void suspend() {
time_t now; time_t now;
time(&now); time(&now);
double seconds = difftime(now, mktime(&alarmTime)); double seconds = difftime(now, mktime(&(state_persistant.alarmTime)));
if (-seconds > secondsToSleep || !alarmArmed) suspend(secondsToSleep); if (-seconds > secondsToSleep || !state_persistant.alarmArmed) suspend(secondsToSleep);
else suspend(5*60); else suspend(5*60);
} }
void setAlarmTime(struct tm time) { void setAlarmTime(struct tm time) {
alarmTime = time; state_persistant.alarmTime = time;
alarmArmed = true; state_persistant.alarmArmed = true;
char strftime_buf[64]; char strftime_buf[64];
strftime(strftime_buf, sizeof(strftime_buf), "%c", &alarmTime); strftime(strftime_buf, sizeof(strftime_buf), "%c", &(state_persistant.alarmTime));
Serial.print("Setting Alarm time: "); Serial.println(strftime_buf); Serial.print("Setting Alarm time: "); Serial.println(strftime_buf);
} }
struct tm getAlarmTime() { struct tm getAlarmTime() {
return alarmTime; return state_persistant.alarmTime;
} }
bool isAlarmActive(){ bool isAlarmActive(){
time_t now; time_t now;
time(&now); time(&now);
double seconds = difftime(now, mktime(&alarmTime)); double seconds = difftime(now, mktime(&(state_persistant.alarmTime)));
if (seconds > -24*60*60 && seconds <=0) return true; if (seconds > -24*60*60 && seconds <=0) return true;
else return false; else return false;
} }
@ -436,15 +431,15 @@ void loop()
time(&now); time(&now);
localtime_r(&now, &timeinfo); localtime_r(&now, &timeinfo);
double seconds = difftime(now, mktime(&alarmTime)); double seconds = difftime(now, mktime(&(state_persistant.alarmTime)));
bool stayAwake = false; bool stayAwake = false;
if (alarmArmed && seconds >= -2*5*60) stayAwake = true; if (state_persistant.alarmArmed && seconds >= -2*5*60) stayAwake = true;
if (!mp3.playing && millis() - lastActive >= stayOnTime && !stayAwake) suspend(); if (!mp3.playing && millis() - lastActive >= stayOnTime && !stayAwake) suspend();
Serial.printf("alarm in %f seconds (%d)\n", seconds, alarmArmed); Serial.printf("alarm in %f seconds (%d)\n", seconds, state_persistant.alarmArmed);
if (seconds >= 0 && alarmArmed) { if (seconds >= 0 && state_persistant.alarmArmed) {
alarmArmed = false; state_persistant.alarmArmed = false;
lastActive = millis(); lastActive = millis();
led.wakeUpLight(0); led.wakeUpLight(0);
Serial.println("WAKEUP TIME!!!!!"); Serial.println("WAKEUP TIME!!!!!");
@ -462,16 +457,16 @@ void loop()
mp3.start("http://radioessen.cast.addradio.de/radioessen/simulcast/high/stream.mp3"); mp3.start("http://radioessen.cast.addradio.de/radioessen/simulcast/high/stream.mp3");
led.changeAnimation(2, 0); led.changeAnimation(2, 0);
} }
if (alarmArmed && seconds >= -5*60 && seconds <= 0) { if (state_persistant.alarmArmed && seconds >= -5*60 && seconds <= 0) {
led.wakeUpLight(255*(seconds+300)/300); led.wakeUpLight(255*(seconds+300)/300);
} }
int voltage = rom_phy_get_vdd33(); int voltage = rom_phy_get_vdd33();
Serial.printf("voltage: %d\n", voltage); Serial.printf("voltage: %d\n", voltage);
strftime(timeStr, sizeof(timeStr), "%H:%M:%S", &timeinfo); strftime(state.timeStr, sizeof(state.timeStr), "%H:%M:%S", &timeinfo);
sprintf(weatherStr, "%.1f°C %.1f%% %.0fhPa", bme280.readTemperature(), bme280.readHumidity(), bme280.readPressure()); sprintf(state.string1, "%.1f°C %.1f%% %.0fhPa", bme280.readTemperature(), bme280.readHumidity(), bme280.readPressure());
sprintf(statusStr, "%.1f %dmV %ddBm %ukB %s", temperatureRead(), voltage, WiFi.RSSI(), ESP.getFreeHeap()/1024, isAlarmActive()?"A":"_"); sprintf(state.string2, "%.1f %dmV %ddBm %ukB %s", temperatureRead(), voltage, WiFi.RSSI(), ESP.getFreeHeap()/1024, isAlarmActive()?"A":"_");
Serial.print("Free Heap: "); Serial.print("Free Heap: ");
Serial.println(ESP.getFreeHeap()); Serial.println(ESP.getFreeHeap());
@ -491,7 +486,7 @@ void loop()
/* if (digitalRead(sensorPin) == HIGH) { /* if (digitalRead(sensorPin) == HIGH) {
lastActive = millis(); lastActive = millis();
if (sensorON == false && digitalRead(sensorPin) == HIGH) { if (sensorON == false && digitalRead(sensorPin) == HIGH) {
uint16_t statusPacketIdSub = iot.mqtt.publish(sensorTopic.c_str(), 0, false, timeStr); uint16_t statusPacketIdSub = iot.mqtt.publish(sensorTopic.c_str(), 0, false, state.timeStr);
sensorON = true; sensorON = true;
} }
} */ } */

28
src/main.h

@ -1,3 +1,6 @@
#ifndef _MAIN_H
#define _MAIN_H
void resetToFactoryDefaults(); void resetToFactoryDefaults();
void setup(); void setup();
void onMqttConnect(bool sessionPresent); void onMqttConnect(bool sessionPresent);
@ -11,3 +14,28 @@ bool isAlarmActive();
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);
struct __attribute__((packed)) WEBRADIO_STATE_PERSISTANT {
int boot_count = 0;
struct tm alarmTime;
bool alarmArmed;
// uint8_t volume;
};
struct __attribute__((packed)) WEBRADIO_STATE {
bool displaySleep = true;
char timeStr[20];
char string1[32];
char string2[32];
// currentScreen
// bool playing;
// bool wifiConnected;
/* float temperature;
float humidity;
float pressure;
float temp2;
float voltage;
float rssi;*/
};
#endif /* _MAIN_H */

6
src/screen.cpp

@ -283,12 +283,12 @@ void MainScreen::draw() {
u8g2.setFont(u8g2_font_inb19_mf); u8g2.setFont(u8g2_font_inb19_mf);
u8g2.setFontMode(0); u8g2.setFontMode(0);
u8g2.setDrawColor(1); u8g2.setDrawColor(1);
u8g2.drawStr(0, 20, timeStr); u8g2.drawStr(0, 20, state.timeStr);
u8g2.setFont(u8g2_font_profont12_mf); // choose a suitable font u8g2.setFont(u8g2_font_profont12_mf); // choose a suitable font
u8g2.drawUTF8(0, 30, weatherStr); u8g2.drawUTF8(0, 30, state.string1);
u8g2.setFont(u8g2_font_profont10_mf); // choose a suitable font u8g2.setFont(u8g2_font_profont10_mf); // choose a suitable font
u8g2.drawUTF8(0,42, statusStr); u8g2.drawUTF8(0,42, state.string2);
u8g2.setFont(u8g2_font_prospero_bold_nbp_tf); // choose a suitable font u8g2.setFont(u8g2_font_prospero_bold_nbp_tf); // choose a suitable font

4
src/screen.h

@ -26,9 +26,7 @@
enum menuType { eNone=0, eWelcomeScreen, eMainScreen, eMainMenu, eStationMenu, eAlarmClockScreen, eSuspendScreen, eLightScreen }; enum menuType { eNone=0, eWelcomeScreen, eMainScreen, eMainMenu, eStationMenu, eAlarmClockScreen, eSuspendScreen, eLightScreen };
extern U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2; extern U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2;
extern char timeStr[]; extern WEBRADIO_STATE state;
extern char weatherStr[];
extern char statusStr[];
extern BME280 bme280; extern BME280 bme280;
extern MP3 mp3; extern MP3 mp3;
extern menuType menuChange; extern menuType menuChange;

Loading…
Cancel
Save