From bbe511dd15141a2ecc1501b28cbf5d2bf4769b57 Mon Sep 17 00:00:00 2001 From: cschwinne Date: Sat, 30 Nov 2019 11:17:37 +0100 Subject: [PATCH] Attempting to fix disconnect on Adalight (ESP32, #194) --- wled00/NpbWrapper.h | 12 ++++++------ wled00/wled00.ino | 6 +++++- wled00/wled01_eeprom.ino | 30 ++++++++++++++++++++++++++---- wled00/wled04_file.ino | 21 ++++++++------------- wled00/wled05_init.ino | 34 ++++++++++++++++++---------------- wled00/wled19_json.ino | 2 ++ 6 files changed, 65 insertions(+), 40 deletions(-) diff --git a/wled00/NpbWrapper.h b/wled00/NpbWrapper.h index 8e04a71e1..fb8ae1b53 100644 --- a/wled00/NpbWrapper.h +++ b/wled00/NpbWrapper.h @@ -3,14 +3,14 @@ #define NpbWrapper_h //PIN CONFIGURATION -#define LEDPIN 2 //strip pin. Any for ESP32, gpio2 or 3 is recommended for ESP8266 (gpio2/3 are labeled D4/RX on NodeMCU and Wemos) +#define LEDPIN 2 //strip pin. Any for ESP32, gpio2 or 3 is recommended for ESP8266 (gpio2/3 are labeled D4/RX on NodeMCU and Wemos) //#define USE_APA102 // Uncomment for using APA102 LEDs. -#define BTNPIN 0 //button pin. Needs to have pullup (gpio0 recommended) -#define IR_PIN 4 //infrared pin (-1 to disable) -#define RLYPIN 12 //pin for relay, will be set HIGH if LEDs are on (-1 to disable). Also usable for standby leds, triggers,... -#define AUXPIN -1 //debug auxiliary output pin (-1 to disable) +#define BTNPIN 0 //button pin. Needs to have pullup (gpio0 recommended) +#define IR_PIN 4 //infrared pin (-1 to disable) +#define RLYPIN 12 //pin for relay, will be set HIGH if LEDs are on (-1 to disable). Also usable for standby leds, triggers,... +#define AUXPIN -1 //debug auxiliary output pin (-1 to disable) -#define RLYMDE 1 //mode for relay, 0: LOW if LEDs are on 1: HIGH if LEDs are on +#define RLYMDE 1 //mode for relay, 0: LOW if LEDs are on 1: HIGH if LEDs are on #ifdef USE_APA102 #define CLKPIN 0 diff --git a/wled00/wled00.ino b/wled00/wled00.ino index a46cb6a53..f9aeb40b5 100644 --- a/wled00/wled00.ino +++ b/wled00/wled00.ino @@ -99,7 +99,7 @@ //version code in format yymmddb (b = daily build) -#define VERSION 1911261 +#define VERSION 1911301 char versionString[] = "0.8.7-dev"; @@ -420,6 +420,10 @@ uint16_t ntpLocalPort = 2390; char* obuf; uint16_t olen = 0; +uint16_t savedPresets = 0; + +byte errorFlag = 0; + String messageHead, messageSub; byte optionType; diff --git a/wled00/wled01_eeprom.ino b/wled00/wled01_eeprom.ino index 486b3f1fc..e0817f063 100644 --- a/wled00/wled01_eeprom.ino +++ b/wled00/wled01_eeprom.ino @@ -21,6 +21,11 @@ //11-> 0.8.5-dev #mqttauth @TimothyBrown //12-> 0.8.7 +void commit() +{ + if (!EEPROM.commit()) errorFlag = 2; +} + /* * Erase all configuration data */ @@ -30,7 +35,7 @@ void clearEEPROM() { EEPROM.write(i, 0); } - EEPROM.commit(); + commit(); } @@ -263,7 +268,7 @@ void saveSettingsToEEPROM() EEPROM.write(2522, mqttPort & 0xFF); EEPROM.write(2523, (mqttPort >> 8) & 0xFF); - EEPROM.commit(); + commit(); } @@ -541,12 +546,29 @@ void loadSettingsFromEEPROM(bool first) useHSB = useHSBDefault; overlayCurrent = overlayDefault; + + savedToPresets(); } //PRESET PROTOCOL 20 bytes //0: preset purpose byte 0:invalid 1:valid preset 1.0 //1:a 2:r 3:g 4:b 5:w 6:er 7:eg 8:eb 9:ew 10:fx 11:sx | custom chase 12:numP 13:numS 14:(0:fs 1:both 2:fe) 15:step 16:ix 17: fp 18-19:Zeros +//determines which presets already contain save data +void savedToPresets() +{ + for (byte index = 1; index <= 16; index++) + { + uint16_t i = 380 + index*20; + + if (EEPROM.read(i) == 1) { + savedPresets |= 0x01 << (index-1); + } else + { + savedPresets &= ~(0x01 << (index-1)); + } + } +} bool applyPreset(byte index, bool loadBri = true, bool loadCol = true, bool loadFX = true) { @@ -594,7 +616,7 @@ void savePreset(byte index) EEPROM.write(i+16, effectIntensity); EEPROM.write(i+17, effectPalette); - EEPROM.commit(); + commit(); } @@ -636,5 +658,5 @@ void saveMacro(byte index, String mc, bool sing=true) //only commit on single sa { EEPROM.write(i, mc.charAt(i-s)); } - if (sing) EEPROM.commit(); + if (sing) commit(); } diff --git a/wled00/wled04_file.ino b/wled00/wled04_file.ino index f1cc5e442..155337ba0 100644 --- a/wled00/wled04_file.ino +++ b/wled00/wled04_file.ino @@ -22,10 +22,10 @@ void handleSerial() static byte check = 0x00; static byte red = 0x00; static byte green = 0x00; - static bool changed = false; while (Serial.available() > 0) { + yield(); byte next = Serial.read(); switch (state) { case AdaState::Header_A: @@ -64,23 +64,18 @@ void handleSerial() break; case AdaState::Data_Blue: byte blue = next; - changed = true; setRealtimePixel(pixel++, red, green, blue, 0); if (--count > 0) state = AdaState::Data_Red; - else state = AdaState::Header_A; + else { + if (!realtimeActive && bri == 0) strip.setBrightness(briLast); + arlsLock(realtimeTimeoutMs); + + strip.show(); + state = AdaState::Header_A; + } break; } } - - if (changed) - { - if (!realtimeActive && bri == 0) strip.setBrightness(briLast); - arlsLock(realtimeTimeoutMs); - - yield(); - strip.show(); - changed = false; - } #endif } diff --git a/wled00/wled05_init.ino b/wled00/wled05_init.ino index 6ed76aec1..81ddda13a 100644 --- a/wled00/wled05_init.ino +++ b/wled00/wled05_init.ino @@ -241,21 +241,23 @@ void handleConnection() { if (lastReconnectAttempt == 0) initConnection(); byte stac = 0; - #ifdef ESP8266 - stac = wifi_softap_get_station_num(); - #else - wifi_sta_list_t stationList; - esp_wifi_ap_get_sta_list(&stationList); - stac = stationList.num; - #endif - if (stac != stacO) - { - stacO = stac; - DEBUG_PRINT("Connected AP clients: "); - DEBUG_PRINTLN(stac); - if (!WLED_CONNECTED && WLED_WIFI_CONFIGURED) { //trying to connect, but not connected - if (stac) WiFi.disconnect(); //disable search so that AP can work - else initConnection(); //restart search + if (apActive) { + #ifdef ESP8266 + stac = wifi_softap_get_station_num(); + #else + wifi_sta_list_t stationList; + esp_wifi_ap_get_sta_list(&stationList); + stac = stationList.num; + #endif + if (stac != stacO) + { + stacO = stac; + DEBUG_PRINT("Connected AP clients: "); + DEBUG_PRINTLN(stac); + if (!WLED_CONNECTED && WLED_WIFI_CONFIGURED) { //trying to connect, but not connected + if (stac) WiFi.disconnect(); //disable search so that AP can work + else initConnection(); //restart search + } } } if (forceReconnect) { @@ -272,7 +274,7 @@ void handleConnection() { interfacesInited = false; initConnection(); } - if (millis() - lastReconnectAttempt > 300000 && WLED_WIFI_CONFIGURED) initConnection(); + if (millis() - lastReconnectAttempt > ((stac) ? 300000 : 20000) && WLED_WIFI_CONFIGURED) initConnection(); if (!apActive && millis() - lastReconnectAttempt > 12000 && (!wasConnected || apBehavior == 1)) initAP(); } else if (!interfacesInited) { //newly connected DEBUG_PRINTLN(""); diff --git a/wled00/wled19_json.ino b/wled00/wled19_json.ino index f21c2c7ac..a9cb18ea0 100644 --- a/wled00/wled19_json.ino +++ b/wled00/wled19_json.ino @@ -142,6 +142,8 @@ void serializeSegment(JsonObject& root, WS2812FX::Segment& seg, byte id) void serializeState(JsonObject root) { + if (errorFlag) root["error"] = errorFlag; + root["on"] = (bri > 0); root["bri"] = briLast; root["transition"] = transitionDelay/100; //in 100ms