From b7e32dad699119573c2ec06cd4b1daf724a023a9 Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Mon, 11 Jun 2018 15:09:31 +0200 Subject: [PATCH] move state into new struct --- src/main.cpp | 63 +++++++++++++++++++++++--------------------------- src/main.h | 28 ++++++++++++++++++++++ src/screen.cpp | 6 ++--- src/screen.h | 4 +--- 4 files changed, 61 insertions(+), 40 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 4f0b484..d836c0d 100644 --- a/src/main.cpp +++ b/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); - -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; -RTC_DATA_ATTR bool displaySleep = true; + +RTC_DATA_ATTR WEBRADIO_STATE_PERSISTANT state_persistant; +WEBRADIO_STATE state; //Create a new Basecamp instance called iot Basecamp iot; @@ -97,7 +92,7 @@ void setup() { pinMode(18, OUTPUT); 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}; // esp_wifi_set_country(&wifi_country); @@ -160,7 +155,7 @@ void setup() { if (timeinfo.tm_year < (2016 - 1900)) { // time not set } - strftime(timeStr, sizeof(timeStr), "%H:%M", &timeinfo); + strftime(state.timeStr, sizeof(state.timeStr), "%H:%M", &timeinfo); char strftime_buf[64]; @@ -185,12 +180,12 @@ void setup() { obtain_time(); } - double seconds = difftime(now, mktime(&alarmTime)); + double seconds = difftime(now, mktime(&(state_persistant.alarmTime))); 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); - if (!alarmArmed || -seconds > secondsToSleep) suspend(); + if (!state_persistant.alarmArmed || -seconds > secondsToSleep) suspend(); else if (-seconds > 5*60) suspend(5*60); } @@ -228,7 +223,7 @@ void setup() { time(&now); } 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) { Serial.println("night mode"); @@ -254,7 +249,7 @@ void setup() { if (wakeup_reason == ESP_SLEEP_WAKEUP_EXT1) { uint64_t wakeup_pinmask = esp_sleep_get_ext1_wakeup_status(); 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; } } @@ -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 - if (!displaySleep) { + if (!state.displaySleep) { u8g2.begin(); u8g2.setPowerSave(0); u8g2.clearBuffer(); u8g2.sendBuffer(); 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 :( esp_sleep_enable_timer_wakeup(1000000LL * 60); } @@ -370,27 +365,27 @@ void suspend(uint32_t secondsToSleep) { void suspend() { time_t now; time(&now); - double seconds = difftime(now, mktime(&alarmTime)); - if (-seconds > secondsToSleep || !alarmArmed) suspend(secondsToSleep); + double seconds = difftime(now, mktime(&(state_persistant.alarmTime))); + if (-seconds > secondsToSleep || !state_persistant.alarmArmed) suspend(secondsToSleep); else suspend(5*60); } void setAlarmTime(struct tm time) { - alarmTime = time; - alarmArmed = true; + state_persistant.alarmTime = time; + state_persistant.alarmArmed = true; 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); } struct tm getAlarmTime() { - return alarmTime; + return state_persistant.alarmTime; } bool isAlarmActive(){ time_t 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; else return false; } @@ -436,15 +431,15 @@ void loop() time(&now); localtime_r(&now, &timeinfo); - double seconds = difftime(now, mktime(&alarmTime)); + double seconds = difftime(now, mktime(&(state_persistant.alarmTime))); 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(); - Serial.printf("alarm in %f seconds (%d)\n", seconds, alarmArmed); - if (seconds >= 0 && alarmArmed) { - alarmArmed = false; + Serial.printf("alarm in %f seconds (%d)\n", seconds, state_persistant.alarmArmed); + if (seconds >= 0 && state_persistant.alarmArmed) { + state_persistant.alarmArmed = false; lastActive = millis(); led.wakeUpLight(0); Serial.println("WAKEUP TIME!!!!!"); @@ -462,16 +457,16 @@ void loop() mp3.start("http://radioessen.cast.addradio.de/radioessen/simulcast/high/stream.mp3"); 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); } int voltage = rom_phy_get_vdd33(); Serial.printf("voltage: %d\n", voltage); - strftime(timeStr, sizeof(timeStr), "%H:%M:%S", &timeinfo); - sprintf(weatherStr, "%.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":"_"); + strftime(state.timeStr, sizeof(state.timeStr), "%H:%M:%S", &timeinfo); + sprintf(state.string1, "%.1f°C %.1f%% %.0fhPa", bme280.readTemperature(), bme280.readHumidity(), bme280.readPressure()); + sprintf(state.string2, "%.1f %dmV %ddBm %ukB %s", temperatureRead(), voltage, WiFi.RSSI(), ESP.getFreeHeap()/1024, isAlarmActive()?"A":"_"); Serial.print("Free Heap: "); Serial.println(ESP.getFreeHeap()); @@ -491,7 +486,7 @@ void loop() /* if (digitalRead(sensorPin) == HIGH) { lastActive = millis(); 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; } } */ diff --git a/src/main.h b/src/main.h index 38b2dd5..678e31e 100644 --- a/src/main.h +++ b/src/main.h @@ -1,3 +1,6 @@ +#ifndef _MAIN_H +#define _MAIN_H + void resetToFactoryDefaults(); void setup(); void onMqttConnect(bool sessionPresent); @@ -11,3 +14,28 @@ bool isAlarmActive(); void loop(); void rotation(int i, int direction, int buttonPressed); 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 */ diff --git a/src/screen.cpp b/src/screen.cpp index 00479e4..eef6f73 100644 --- a/src/screen.cpp +++ b/src/screen.cpp @@ -283,12 +283,12 @@ void MainScreen::draw() { u8g2.setFont(u8g2_font_inb19_mf); u8g2.setFontMode(0); 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.drawUTF8(0, 30, weatherStr); + u8g2.drawUTF8(0, 30, state.string1); 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 diff --git a/src/screen.h b/src/screen.h index 92ea551..7a6df40 100644 --- a/src/screen.h +++ b/src/screen.h @@ -26,9 +26,7 @@ enum menuType { eNone=0, eWelcomeScreen, eMainScreen, eMainMenu, eStationMenu, eAlarmClockScreen, eSuspendScreen, eLightScreen }; extern U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2; -extern char timeStr[]; -extern char weatherStr[]; -extern char statusStr[]; +extern WEBRADIO_STATE state; extern BME280 bme280; extern MP3 mp3; extern menuType menuChange;