From 4f0a191d0be077679fbda4af17f228b89e065f0e Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Tue, 25 Apr 2023 08:18:36 +0200 Subject: [PATCH] Rediced read access to EEPROM by preloading data. Refactorized the setup function into separate functions. --- firmware/config.h | 1 - firmware/firmware.ino | 263 ++++++++++++++++++++---------------------- 2 files changed, 126 insertions(+), 138 deletions(-) diff --git a/firmware/config.h b/firmware/config.h index e333e59..1a81adb 100644 --- a/firmware/config.h +++ b/firmware/config.h @@ -15,5 +15,4 @@ #define EEPROM_LAST_DEFAULT_BLOCK_ADDRESS 20 #define EEPROM_TIMING_DATA_ADDRESS (EEPROM_TIMING_CONTROL_ENABLED_ADDRESS + LIGHTS_COUNT) // Stored data date per light ELE_USED; HH; MM; CH1; CH2; CH3; CH4; - #define TC_TIME_CHECK_INTERVAL_MS 600000 diff --git a/firmware/firmware.ino b/firmware/firmware.ino index 8531b87..937cc3a 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -12,16 +12,9 @@ uint8_t pins[LIGHTS_COUNT] = {12, 15, 13, 14}; -#define use_hardware_switch false // To control on/off state and brightness using GPIO/Pushbutton, set this value to true. -//For GPIO based on/off and brightness control, it is mandatory to connect the following GPIO pins to ground using 10k resistor -#define button1_pin 4 // on and brightness up -#define button2_pin 5 // off and brightness down - -#ifdef USE_STATIC_IP IPAddress strip_ip ( 192, 168, 0, 26); // choose an unique IP Adress IPAddress gateway_ip ( 192, 168, 0, 1); // Router IP IPAddress subnet_mask( 255, 255, 255, 0); -#endif //********************************// @@ -47,6 +40,8 @@ IPAddress subnet_mask( 255, 255, 255, 0); #define PWM_MAX 1023 // 24V - maximum light amount (100%) #define PWM_INC 4 // 24V-15V = 9V range; 9V ≙ 1024/640 = 383 counts; 383/100% = 3,83 counts (1%) / % => round up 4 counts / % (~1%) +//********************************// + uint8_t scene; uint8_t tc_enabled; @@ -63,23 +58,7 @@ byte mac[6]; ESP8266WebServer server(80); ESP8266HTTPUpdateServer httpUpdateServer; -void handleNotFound() -{ - String message = "File Not Found\n\n"; - message += "URI: "; - message += server.uri(); - message += "\nMethod: "; - message += (server.method() == HTTP_GET) ? "GET" : "POST"; - message += "\nArguments: "; - message += server.args(); - message += "\n"; - - for (uint8_t i = 0; i < server.args(); i++) - { - message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; - } - server.send(404, "text/plain", message); -} +//********************************// void apply_scene(uint8_t new_scene, uint8_t light) { @@ -98,6 +77,8 @@ void apply_scene(uint8_t new_scene, uint8_t light) } } +//********************************// + void process_lightdata(uint8_t light, float transitiontime) { transitiontime *= 16; @@ -109,6 +90,8 @@ void process_lightdata(uint8_t light, float transitiontime) } } +//********************************// + void lightEngine() { for (int i = 0; i < LIGHTS_COUNT; i++) @@ -123,7 +106,7 @@ void lightEngine() { current_bri[i] = bri[i]; } - analogWrite(pins[i], (int)(current_bri[i] * 4)); + analogWrite(pins[i], (int)(current_bri[i] * 4)); // TODO warum * 4? } } else { @@ -135,7 +118,7 @@ void lightEngine() { current_bri[i] = 0; } - analogWrite(pins[i], (int)(current_bri[i] * 4)); + analogWrite(pins[i], (int)(current_bri[i] * 4)); // TODO warum * 4? } } } @@ -145,77 +128,13 @@ void lightEngine() delay(6); in_transition = false; - } else if (use_hardware_switch == true) - { - if (digitalRead(button1_pin) == HIGH) - { - int i = 0; - while (digitalRead(button1_pin) == HIGH && i < 30) - { - delay(20); - i++; - } - - for (int light = 0; light < LIGHTS_COUNT; light++) - { - if (i < 30) - { - // there was a short press - light_state[light] = true; - } - else { - // there was a long press - bri[light] += 56; - if (bri[light] > 254) - { - // don't increase the brightness more then maximum value - bri[light] = 254; - } - } - process_lightdata(light, 4); - } - - } else if (digitalRead(button2_pin) == HIGH) - { - int i = 0; - while (digitalRead(button2_pin) == HIGH && i < 30) - { - delay(20); - i++; - } - - for (int light = 0; light < LIGHTS_COUNT; light++) - { - if (i < 30) - { - // there was a short press - light_state[light] = false; - - } else { - // there was a long press - bri[light] -= 56; - if (bri[light] < 1) - { - // don't decrease the brightness less than minimum value. - bri[light] = 1; - } - } - process_lightdata(light, 4); - } - } } } -void setup() +//********************************// + +void read_eeprom_config() { - EEPROM.begin(512); - - Serial.begin(SERIAL_BAUD_RATE); - -#ifdef USE_STATIC_IP - WiFi.config(strip_ip, gateway_ip, subnet_mask); -#endif - for (uint8_t light = 0; light < LIGHTS_COUNT; light++) { apply_scene(EEPROM.read(EEPROM_SCENE_ADDRESS), light); @@ -229,11 +148,12 @@ void setup() } } - if (EEPROM.read(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS) == TIMING_CONTROL_DISABLED) + uint8_t tmp = EEPROM.read(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS); + if (tmp == TIMING_CONTROL_DISABLED) { tc_enabled = TIMING_CONTROL_DISABLED; - } else if (EEPROM.read(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS) == TIMING_CONTROL_ENABLED) + } else if (tmp == TIMING_CONTROL_ENABLED) { tc_enabled = TIMING_CONTROL_ENABLED; @@ -264,6 +184,22 @@ void setup() EEPROM.commit(); } #endif +} + +//********************************// + +void setup() +{ + EEPROM.begin(512); + + Serial.begin(SERIAL_BAUD_RATE); + + if (EEPROM.read(EEPROM_DYNAMIC_IP_ADDRESS) == 0) + { + WiFi.config(strip_ip, gateway_ip, subnet_mask); + } + + read_eeprom_config(); for (int j = 0; j < 200; j++) { @@ -294,13 +230,47 @@ void setup() httpUpdateServer.setup(&server); // start http server - if (use_hardware_switch == true) - { - pinMode(button1_pin, INPUT); - pinMode(button2_pin, INPUT); - } + init_webserver(); - server.on("/state", HTTP_PUT, []() + + + tc_init(); + + server.begin(); +} // end of setup + +void loop() +{ + server.handleClient(); + lightEngine(); + + if (tc_enabled == TIMING_CONTROL_ENABLED) + { + tc_update(); + } +} + +void handleNotFound() +{ + String message = "File Not Found\n\n"; + message += "URI: "; + message += server.uri(); + message += "\nMethod: "; + message += (server.method() == HTTP_GET) ? "GET" : "POST"; + message += "\nArguments: "; + message += server.args(); + message += "\n"; + + for (uint8_t i = 0; i < server.args(); i++) + { + message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; + } + server.send(404, "text/plain", message); +} + +void init_webserver() +{ + server.on("/state", HTTP_PUT, []() { // HTTP PUT request used to set a new light state DynamicJsonDocument root(1024); DeserializationError error = deserializeJson(root, server.arg("plain")); @@ -317,18 +287,21 @@ void setup() JsonObject values = state.value(); int transitiontime = 4; + uint8_t tmp = EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS); if (values.containsKey("on")) { if (values["on"]) { light_state[light] = true; - if (EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS) == LAST_STATE_STARTUP_LIGHT_LAST_STATE && EEPROM.read(EEPROM_LAST_STATE_ADDRESS + light) == LIGHT_STATE_OFF) + if (tmp == LAST_STATE_STARTUP_LIGHT_LAST_STATE && + EEPROM.read(EEPROM_LAST_STATE_ADDRESS + light) == LIGHT_STATE_OFF) { EEPROM.write(EEPROM_LAST_STATE_ADDRESS + light, LIGHT_STATE_ON); } } else { light_state[light] = false; - if (EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS) == LAST_STATE_STARTUP_LIGHT_LAST_STATE && EEPROM.read(EEPROM_LAST_STATE_ADDRESS + light) == LIGHT_STATE_ON) + if (tmp == LAST_STATE_STARTUP_LIGHT_LAST_STATE && + EEPROM.read(EEPROM_LAST_STATE_ADDRESS + light) == LIGHT_STATE_ON) { EEPROM.write(EEPROM_LAST_STATE_ADDRESS + light, LIGHT_STATE_OFF); } @@ -389,26 +362,39 @@ void setup() server.on("/", []() { - float transitiontime = 4; + static float transitiontime = 4.0; + if (server.hasArg("transition")) + { + transitiontime = server.arg("transition").toFloat(); + } + + // startup behavior switch handling if (server.hasArg("startup")) { int startup = server.arg("startup").toInt(); - EEPROM.write(EEPROM_LAST_STATE_STARTUP_ADDRESS, startup); - EEPROM.commit(); - Serial.print("Startup behavior set to "); Serial.println(EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS)); + if (EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS) != startup) + { + EEPROM.write(EEPROM_LAST_STATE_STARTUP_ADDRESS, startup); + EEPROM.commit(); + Serial.print("Startup behavior set to "); Serial.println(EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS)); + } } + // timing controller switch handling if (server.hasArg("tc")) { if (server.arg("tc") == "true") { if (tc_enabled == TIMING_CONTROL_DISABLED) { - tc_enabled = TIMING_CONTROL_ENABLED; - EEPROM.write(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS, TIMING_CONTROL_ENABLED); - EEPROM.commit(); - Serial.print("Timing control = "); Serial.println(EEPROM.read(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS)); + if (EEPROM.read(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS) != TIMING_CONTROL_ENABLED) + { + tc_enabled = TIMING_CONTROL_ENABLED; + EEPROM.write(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS, TIMING_CONTROL_ENABLED); + EEPROM.commit(); + Serial.print("Timing control = "); Serial.println(EEPROM.read(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS)); + } } } else { // tc is set to false or something else @@ -416,22 +402,30 @@ void setup() if (tc_enabled == TIMING_CONTROL_ENABLED) { tc_enabled = TIMING_CONTROL_DISABLED; - EEPROM.write(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS, TIMING_CONTROL_DISABLED); - EEPROM.commit(); - Serial.print("Timing control = "); Serial.println(EEPROM.read(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS)); + if (EEPROM.read(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS) != TIMING_CONTROL_DISABLED) + { + EEPROM.write(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS, TIMING_CONTROL_DISABLED); + EEPROM.commit(); + Serial.print("Timing control = "); Serial.println(EEPROM.read(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS)); + } } } } + // scene switch handling if (server.hasArg("scene")) { scene = server.arg("scene").toInt(); - EEPROM.write(EEPROM_SCENE_ADDRESS, scene); - EEPROM.commit(); - Serial.print("Scene set to "); Serial.println(EEPROM.read(EEPROM_SCENE_ADDRESS)); + if (EEPROM.read(EEPROM_SCENE_ADDRESS) != scene) + { + EEPROM.write(EEPROM_SCENE_ADDRESS, scene); + EEPROM.commit(); + Serial.print("Scene set to "); Serial.println(EEPROM.read(EEPROM_SCENE_ADDRESS)); + } } + // process the received data for every light for (int light = 0; light < LIGHTS_COUNT; light++) { @@ -444,17 +438,18 @@ void setup() if (server.hasArg("on" + (String)light)) { + uint8_t tmp = EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS); if (server.arg("on" + (String)light) == "true") { light_state[light] = true; - if (EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS) == 0 && EEPROM.read(EEPROM_LAST_STATE_ADDRESS + light) == 0) + if (tmp == 0 && EEPROM.read(EEPROM_LAST_STATE_ADDRESS + light) == 0) { EEPROM.write(EEPROM_LAST_STATE_ADDRESS + light, LIGHT_STATE_ON); } } else { light_state[light] = false; - if (EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS) == 0 && EEPROM.read(EEPROM_LAST_STATE_ADDRESS + light) == 1) + if (tmp == 0 && EEPROM.read(EEPROM_LAST_STATE_ADDRESS + light) == 1) { EEPROM.write(EEPROM_LAST_STATE_ADDRESS + light, LIGHT_STATE_OFF); } @@ -466,7 +461,9 @@ void setup() } - if (server.hasArg("alert")) { + // start alerting for every light + if (server.hasArg("alert")) + { if (light_state[light]) { @@ -477,6 +474,7 @@ void setup() } + // switch the light if (light_state[light]) { step_level[light] = ((float)bri[light] - current_bri[light]) / transitiontime; @@ -487,7 +485,7 @@ void setup() } if (server.hasArg("resettc")) - { + { // reqrite the tc config and reboot tc_write_default(); ESP.reset(); } @@ -497,6 +495,7 @@ void setup() ESP.reset(); } + // Generate HTML page String http_content = ""; http_content += ""; http_content += ""; @@ -541,8 +540,12 @@ void setup() http_content += ""; http_content += "
"; - // Light control + http_content += "
"; + http_content += ""; + http_content += ""; + http_content += "
"; + // Light control for (uint8 light_num = 0; light_num < LIGHTS_COUNT; light_num++) { http_content += "

Light " + (String)(light_num+1) + "

"; @@ -551,6 +554,7 @@ void setup() http_content += "ON"; http_content += "OFF"; http_content += ""; + http_content += "
"; http_content += ""; http_content += "
"; @@ -631,19 +635,4 @@ void setup() }); server.onNotFound(handleNotFound); - - tc_init(); - - server.begin(); -} // end of setup - -void loop() -{ - server.handleClient(); - lightEngine(); - - if (tc_enabled == TIMING_CONTROL_ENABLED) - { - tc_update(); - } }