diff --git a/Arduino/McLighting/McLighting.ino b/Arduino/McLighting/McLighting.ino index e00a4da..a51f1db 100644 --- a/Arduino/McLighting/McLighting.ino +++ b/Arduino/McLighting/McLighting.ino @@ -200,6 +200,7 @@ void tick() { #endif Ticker settings_save_state; +Ticker settings_save_conf; // *************************************************************************** // Saved state handling in WifiManager @@ -564,6 +565,7 @@ void setup() { last_conf[sizeof(last_conf)]=0; writeEEPROM(0, 222, last_conf); EEPROM.commit(); + shouldSaveConfig = false; } #endif #endif @@ -887,8 +889,26 @@ void loop() { updateState = false; settings_save_state.detach(); #endif + } + if (shouldSaveConfig) { + #if ENABLE_STATE_SAVE == 1 + (writeConfigFS(true)) ? DBG_OUTPUT_PORT.println("Config FS Save success!"): DBG_OUTPUT_PORT.println("Config FS Save failure!"); + #endif + #if ENABLE_STATE_SAVE == 0 + char last_conf[223]; + #if defined(ENABLE_MQTT) + snprintf(last_conf, sizeof(last_conf), "CNF|%64s|%64s|%5d|%32s|%32s|%4d|%2d|%4s|%3d", HOSTNAME, mqtt_host, mqtt_port, mqtt_user, mqtt_pass, WS2812FXStripSettings.stripSize, WS2812FXStripSettings.pin, WS2812FXStripSettings.RGBOrder, WS2812FXStripSettings.fxoptions); + #else + snprintf(last_conf, sizeof(last_conf), "CNF|%64s|%64s|%5d|%32s|%32s|%4d|%2d|%4s|%3d", HOSTNAME, "", "", "", "", WS2812FXStripSettings.stripSize, WS2812FXStripSettings.pin, WS2812FXStripSettings.RGBOrder, WS2812FXStripSettings.fxoptions); + #endif + last_conf[sizeof(last_conf)-1]= 0x00; + writeEEPROM(0, 222, last_conf); + EEPROM.commit(); + shouldSaveConfig = false; + settings_save_conf.detach(); + #endif + } #endif - } prevmode = mode; diff --git a/Arduino/McLighting/definitions.h b/Arduino/McLighting/definitions.h index 1cdc604..dbb4f10 100644 --- a/Arduino/McLighting/definitions.h +++ b/Arduino/McLighting/definitions.h @@ -8,7 +8,7 @@ #define LED_PIN 3 // PIN (15 / D8) where neopixel / WS2811 strip is attached; is configurable, if USE_WS2812FX_DMA is not defined. Just for the start #define NUMLEDS 50 // Number of leds in the; is configurable just for the start #define RGBORDER "GRBW" // RGBOrder; is configurable just for the start -#define FX_OPTIONS 56 // ws2812fx Options 56 = SIZE_SMALL + FADE_MEDIUM + GAMMA is configurable just for the start; for WS2812FX setSegment OPTIONS, see: https://github.com/kitesurfer1404/WS2812FX/blob/master/extras/WS2812FX%20Users%20Guide.md +#define FX_OPTIONS 48 // ws2812fx Options 56 = SIZE_SMALL + FADE_MEDIUM is configurable just for the start; for WS2812FX setSegment OPTIONS, see: https://github.com/kitesurfer1404/WS2812FX/blob/master/extras/WS2812FX%20Users%20Guide.md //#define LED_TYPE_WS2811 // Uncomment, if LED type uses 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) #define LED_BUILTIN 2 // ESP-12F has the built in LED on GPIO2, see https://github.com/esp8266/Arduino/issues/2192 @@ -26,15 +26,15 @@ char HOSTNAME[65] = "McLightingRGBW"; // Friedly hostname is configurable just #define ENABLE_STATE_SAVE 1 // If defined, load saved state on reboot and save state. If set to 0 from EEPROM, if set to 1 from SPIFFS #define ENABLE_LEGACY_ANIMATIONS // Enable Legacy Animations -#define CUSTOM_WS2812FX_ANIMATIONS //uncomment and put animations in "custom_ws2812fx_animations.h" +#define CUSTOM_WS2812FX_ANIMATIONS // uncomment and put animations in "custom_ws2812fx_animations.h" #define ENABLE_E131 // E1.31 implementation You have to uncomment #define USE_WS2812FX_DMA and set it to 0 #define ENABLE_TV // Enable TV Animation -bool USE_HTML_MIN_GZ = true; //uncomment for using index.htm & edit.htm from PROGMEM instead of SPIFFs -> next version i will make it customizable +#define USE_HTML_MIN_GZ // uncomment for using index.htm & edit.htm from PROGMEM instead of SPIFFs #if defined(ENABLE_E131) #define MULTICAST false #define START_UNIVERSE 1 // First DMX Universe to listen for - uint8_t END_UNIVERSE = 1; // Total number of Universes to listen for, starting at UNIVERSE + uint8_t END_UNIVERSE = 1; // Total number of Universes to listen for, starting at UNIVERSE #endif @@ -137,12 +137,9 @@ uint32_t autoParams[][6] = { // main_color, back_color, xtra_color, speed, mod MODE mode = SET_ALL; // Standard mode that is active when software starts MODE prevmode = mode; -uint8_t ws2812fx_speed = 196; // Global variable for storing the delay between color changes --> smaller == faster -uint8_t brightness = 196; // Global variable for storing the brightness (255 == 100%) - -uint8_t ws2812fx_mode = 0; // Global variable for storing the WS2812FX modes - -bool shouldSaveConfig = false; // For WiFiManger custom config +uint8_t ws2812fx_speed = 196; // Global variable for storing the delay between color changes --> smaller == faster +uint8_t brightness = 196; // Global variable for storing the brightness (255 == 100%) +uint8_t ws2812fx_mode = 0; // Global variable for storing the WS2812FX modes uint32_t hex_colors[3] = {}; // Color array for setting WS2812FX struct ledstate // Data structure to store a state of a single led @@ -159,6 +156,7 @@ LEDState main_color = { 255, 0, 0, 0 }; // Store the "main color" of the strip LEDState back_color = { 0, 0, 0, 0 }; // Store the "2nd color" of the strip used in single color modes LEDState xtra_color = { 0, 0, 0, 0 }; // Store the "3rd color" of the strip used in single color modes +bool shouldSaveConfig = false; // For WiFiManger custom config and config char last_state[67]; // Keeps the state representation before auto or off mode bool updateState = false; diff --git a/Arduino/McLighting/request_handlers.h b/Arduino/McLighting/request_handlers.h index 0f37666..41a0508 100644 --- a/Arduino/McLighting/request_handlers.h +++ b/Arduino/McLighting/request_handlers.h @@ -5,7 +5,10 @@ // Prototypes void handleAutoStart(); char * listStatusJSON(); -bool writeConfigFS(bool); +#if defined(ENABLE_STATE_SAVE) + bool writeConfigFS(bool); + void tickerSaveConfig(); +#endif #if defined(ENABLE_E131) void handleE131(){ @@ -291,7 +294,16 @@ void handleRangeDifferentColors(uint8_t * mypayload) { } } -bool checkPin(uint8_t pin) { +bool checkPin(uint8_t pin) { + #if USE_WS2812FX_DMA == 0 + pin = 3; + #endif + #if USE_WS2812FX_DMA == 1 + pin = 2; + #endif + #if USE_WS2812FX_DMA == 2 + pin = 1; + #endif if (((pin >= 0 && pin <= 5) || (pin >= 12 && pin <= 16)) && (pin != WS2812FXStripSettings.pin)) { WS2812FXStripSettings.pin = pin; return true; @@ -567,6 +579,13 @@ char * listStatusJSON() { return buffer; } +void getStatusJSON() { + char * buffer = listStatusJSON(); + server.sendHeader("Access-Control-Allow-Origin", "*"); + server.send ( 200, "application/json", buffer); + free (buffer); +} + char * listConfigJSON() { //uint8_t tmp_mode = (mode == SET_MODE) ? (uint8_t) ws2812fx_mode : strip->getMode(); #if defined(ENABLE_MQTT) @@ -594,14 +613,6 @@ char * listConfigJSON() { return buffer; } - -void getStatusJSON() { - char * buffer = listStatusJSON(); - server.sendHeader("Access-Control-Allow-Origin", "*"); - server.send ( 200, "application/json", buffer); - free (buffer); -} - void getConfigJSON() { char * buffer = listConfigJSON(); server.sendHeader("Access-Control-Allow-Origin", "*"); @@ -609,7 +620,6 @@ void getConfigJSON() { free (buffer); } - char * listModesJSON() { const size_t bufferSize = JSON_ARRAY_SIZE(strip->getModeCount() + 3) + (strip->getModeCount() + 3)*JSON_OBJECT_SIZE(2) + 2000; DynamicJsonDocument jsonBuffer(bufferSize); @@ -909,25 +919,7 @@ void checkpayload(uint8_t * payload, bool mqtt = false, uint8_t num = 0) { initMqtt(); } #endif - - #if defined(ENABLE_STATE_SAVE) - #if ENABLE_STATE_SAVE == 1 - (writeConfigFS(updateConf || updateStrip)) ? DBG_OUTPUT_PORT.println("Config FS Save success!"): DBG_OUTPUT_PORT.println("Config FS Save failure!"); - #endif - #if ENABLE_STATE_SAVE == 0 - if (updateConf || updateStrip) { - char last_conf[223]; - #if defined(ENABLE_MQTT) - snprintf(last_conf, sizeof(last_conf), "CNF|%64s|%64s|%5d|%32s|%32s|%4d|%2d|%4s|%3d", HOSTNAME, mqtt_host, mqtt_port, mqtt_user, mqtt_pass, WS2812FXStripSettings.stripSize, WS2812FXStripSettings.pin, WS2812FXStripSettings.RGBOrder, WS2812FXStripSettings.fxoptions); - #else - snprintf(last_conf, sizeof(last_conf), "CNF|%64s|%64s|%5d|%32s|%32s|%4d|%2d|%4s|%3d", HOSTNAME, "", "", "", "", WS2812FXStripSettings.stripSize, WS2812FXStripSettings.pin, WS2812FXStripSettings.RGBOrder, WS2812FXStripSettings.fxoptions); - #endif - last_conf[sizeof(last_conf)-1]= 0x00; - writeEEPROM(0, 222, last_conf); - EEPROM.commit(); - } - #endif - #endif + char * buffer = listConfigJSON(); if (mqtt == true) { DBG_OUTPUT_PORT.print("MQTT: "); @@ -944,6 +936,11 @@ void checkpayload(uint8_t * payload, bool mqtt = false, uint8_t num = 0) { webSocket.sendTXT(num, "OK"); webSocket.sendTXT(num, buffer); } +#if defined(ENABLE_STATE_SAVE) + if (updateStrip || updateConf) { + if(!settings_save_conf.active()) settings_save_conf.once(3, tickerSaveConfig); + } +#endif updateStrip = false; updateConf = false; DBG_OUTPUT_PORT.printf("Get status info: %s\r\n", buffer); @@ -1620,6 +1617,37 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght updateState = true; } + void tickerSaveConfig(){ + shouldSaveConfig = true; + } + + #if ENABLE_STATE_SAVE == 0 + // *************************************************************************** + // EEPROM helper + // *************************************************************************** + String readEEPROM(uint16_t offset, uint16_t len) { + String res = ""; + for (uint16_t i = 0; i < len; ++i) + { + res += char(EEPROM.read(i + offset)); + //DBG_OUTPUT_PORT.println(char(EEPROM.read(i + offset))); + } + DBG_OUTPUT_PORT.printf("readEEPROM(): %s\r\n", res.c_str()); + return res; + } + + void writeEEPROM(uint16_t offset, uint16_t len, String value) { + DBG_OUTPUT_PORT.printf("writeEEPROM(): %s\r\n", value.c_str()); + for (uint16_t i = 0; i < len; ++i) + { + if (i < value.length()) { + EEPROM.write(i + offset, value[i]); + } else { + EEPROM.write(i + offset, 0); + } + } + } + #endif #if ENABLE_STATE_SAVE == 1 // Write configuration to FS JSON bool writeConfigFS(bool saveConfig){ @@ -1631,6 +1659,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght DBG_OUTPUT_PORT.println(listConfigJSON()); configFile.print(listConfigJSON()); configFile.close(); + shouldSaveConfig = false; return true; //end save } else { @@ -1780,37 +1809,6 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght return false; } #endif - - #if ENABLE_STATE_SAVE == 0 - // *************************************************************************** - // EEPROM helper - // *************************************************************************** - String readEEPROM(uint16_t offset, uint16_t len) { - String res = ""; - for (uint16_t i = 0; i < len; ++i) - { - res += char(EEPROM.read(i + offset)); - //DBG_OUTPUT_PORT.println(char(EEPROM.read(i + offset))); - } - DBG_OUTPUT_PORT.printf("readEEPROM(): %s\r\n", res.c_str()); - return res; - } - - void writeEEPROM(uint16_t offset, uint16_t len, String value) { - DBG_OUTPUT_PORT.printf("writeEEPROM(): %s\r\n", value.c_str()); - for (uint16_t i = 0; i < len; ++i) - { - if (i < value.length()) { - EEPROM.write(i + offset, value[i]); - } else { - EEPROM.write(i + offset, 0); - } - } - } - - - - #endif #endif #if defined(ENABLE_REMOTE) diff --git a/Arduino/McLighting/rest_api.h b/Arduino/McLighting/rest_api.h index 3dccac4..f946880 100644 --- a/Arduino/McLighting/rest_api.h +++ b/Arduino/McLighting/rest_api.h @@ -19,23 +19,23 @@ // *************************************************************************** server.on("/", HTTP_GET, [&](){ - if (USE_HTML_MIN_GZ) { + #if defined(USE_HTML_MIN_GZ) server.sendHeader("Content-Encoding", "gzip", true); server.send_P(200, PSTR("text/html"), index_htm_gz, index_htm_gz_len); - } else { + #else if (!handleFileRead(server.uri())) handleNotFound(); - } + #endif }); server.on("/edit", HTTP_GET, [&](){ - if (USE_HTML_MIN_GZ) { + #if defined(USE_HTML_MIN_GZ) server.sendHeader("Content-Encoding", "gzip", true); server.send_P(200, PSTR("text/html"), edit_htm_gz, edit_htm_gz_len); - } else { + #else if (!handleFileRead("/edit.htm")) handleNotFound(); - } + #endif }); @@ -340,26 +340,14 @@ #endif #if defined(ENABLE_STATE_SAVE) - #if ENABLE_STATE_SAVE == 1 - (writeConfigFS(updateConf || updateStrip)) ? DBG_OUTPUT_PORT.println("Config FS Save success!"): DBG_OUTPUT_PORT.println("Config FS Save failure!"); - #endif - #if ENABLE_STATE_SAVE == 0 - if (updateConf || updateStrip) { - char last_conf[223]; - #if defined(ENABLE_MQTT) - snprintf(last_conf, sizeof(last_conf), "CNF|%64s|%64s|%5d|%32s|%32s|%4d|%2d|%4s|%3d", HOSTNAME, mqtt_host, mqtt_port, mqtt_user, mqtt_pass, WS2812FXStripSettings.stripSize, WS2812FXStripSettings.pin, WS2812FXStripSettings.RGBOrder, WS2812FXStripSettings.fxoptions); - #else - snprintf(last_conf, sizeof(last_conf), "CNF|%64s|%64s|%5d|%32s|%32s|%4d|%2d|%4s|%3d", HOSTNAME, "", "", "", "", WS2812FXStripSettings.stripSize, WS2812FXStripSettings.pin, WS2812FXStripSettings.RGBOrder, WS2812FXStripSettings.fxoptions); - #endif - writeEEPROM(0, 222, last_conf); - EEPROM.commit(); + if (updateStrip || updateConf) { + if(!settings_save_conf.active()) settings_save_conf.once(3, tickerSaveConfig); } - #endif #endif - getConfigJSON(); - delay(500); updateStrip = false; updateConf = false; + getConfigJSON(); + delay(500); }); server.on("/off", []() { diff --git a/Arduino/McLighting/version_info.ino b/Arduino/McLighting/version_info.ino index 9a67183..16c9aa6 100644 --- a/Arduino/McLighting/version_info.ino +++ b/Arduino/McLighting/version_info.ino @@ -145,4 +145,5 @@ * Bugfixes * added pointer for MQTT again * Removed some String datatype and replaced with char array + * better responsiveness in ui for sliders */