From 9518c5f2e4440f93384c5d706c28e99e7f12eb84 Mon Sep 17 00:00:00 2001 From: cschwinne Date: Sat, 30 Jan 2021 20:51:36 +0100 Subject: [PATCH] might work --- wled00/FX.h | 2 +- wled00/FX_fcn.cpp | 13 +++++---- wled00/bus_manager.h | 51 +++++++++++++++++++++----------- wled00/cfg.cpp | 8 ++--- wled00/set.cpp | 69 +++++++++++++++++--------------------------- wled00/wled.cpp | 14 ++++++--- wled00/wled.h | 2 +- 7 files changed, 83 insertions(+), 76 deletions(-) diff --git a/wled00/FX.h b/wled00/FX.h index a55bcc7a..e6c7cbd8 100644 --- a/wled00/FX.h +++ b/wled00/FX.h @@ -584,7 +584,7 @@ class WS2812FX { } void - init(bool supportWhite, uint16_t countPixels, bool skipFirst), + finalizeInit(bool supportWhite, uint16_t countPixels, bool skipFirst), service(void), blur(uint8_t), fill(uint32_t), diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index 9bdd2b92..3ce21c74 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -49,7 +49,7 @@ const uint16_t customMappingSize = sizeof(customMappingTable)/sizeof(uint16_t); #endif //do not call this method from system context (network callback) -void WS2812FX::init(bool supportWhite, uint16_t countPixels, bool skipFirst) +void WS2812FX::finalizeInit(bool supportWhite, uint16_t countPixels, bool skipFirst) { if (supportWhite == _useRgbw && countPixels == _length && _skipFirstMode == skipFirst) return; RESET_RUNTIME; @@ -62,11 +62,12 @@ void WS2812FX::init(bool supportWhite, uint16_t countPixels, bool skipFirst) _lengthRaw += LED_SKIP_AMOUNT; } - uint8_t pins[] = {2}; - - while (!busses.canAllShow()) yield(); - busses.removeAll(); - busses.add(supportWhite? TYPE_SK6812_RGBW : TYPE_WS2812_RGB, pins, 0, countPixels, COL_ORDER_GRB); + //if busses failed to load (FS issue...) + if (busses.getNumBusses() == 0) { + uint8_t defPin[] = {LEDPIN}; + BusConfig defCfg = BusConfig(TYPE_WS2812_RGB, defPin, 0, _lengthRaw, COL_ORDER_GRB); + busses.add(defCfg); + } _segments[0].start = 0; _segments[0].stop = _length; diff --git a/wled00/bus_manager.h b/wled00/bus_manager.h index 20898b8a..265da36c 100644 --- a/wled00/bus_manager.h +++ b/wled00/bus_manager.h @@ -10,6 +10,23 @@ #include "bus_wrapper.h" #include +//temporary struct for passing bus configuration to bus +struct BusConfig { + uint8_t type = TYPE_WS2812_RGB; + uint16_t count = 1; + uint16_t start = 0; + uint8_t colorOrder = COL_ORDER_GRB; + bool reversed = false; + uint8_t pins[5] = {LEDPIN, 255, 255, 255, 255}; + BusConfig(uint8_t busType, uint8_t* ppins, uint16_t pstart, uint16_t len = 1, uint8_t pcolorOrder = COL_ORDER_GRB, bool rev = false) { + type = busType; count = len; start = pstart; colorOrder = pcolorOrder; reversed = rev; + uint8_t nPins = 1; + if (type > 47) nPins = 2; + else if (type > 41 && type < 46) nPins = NUM_PWM_PINS(type); + for (uint8_t i = 0; i < nPins; i++) pins[i] = ppins[i]; + } +}; + //parent class of BusDigital and BusPwm class Bus { public: @@ -73,23 +90,23 @@ class Bus { class BusDigital : public Bus { public: - BusDigital(uint8_t type, uint8_t* pins, uint16_t start, uint16_t len, uint8_t colorOrder, uint8_t nr, bool rev) : Bus(type, start) { - if (!IS_DIGITAL(type) || !len) return; - _pins[0] = pins[0]; + BusDigital(BusConfig &bc, uint8_t nr) : Bus(bc.type, bc.start) { + if (!IS_DIGITAL(bc.type) || !bc.count) return; + _pins[0] = bc.pins[0]; if (!pinManager.allocatePin(_pins[0])) return; - if (IS_2PIN(type)) { - _pins[1] = pins[1]; + if (IS_2PIN(bc.type)) { + _pins[1] = bc.pins[1]; if (!pinManager.allocatePin(_pins[1])) { cleanup(); return; } } - _len = len; - reversed = rev; - _iType = PolyBus::getI(type, _pins, nr); + _len = bc.count; + reversed = bc.reversed; + _iType = PolyBus::getI(bc.type, _pins, nr); if (_iType == I_NONE) return; _busPtr = PolyBus::create(_iType, _pins, _len); _valid = (_busPtr != nullptr); - _colorOrder = colorOrder; + _colorOrder = bc.colorOrder; //Serial.printf("Successfully inited strip %u (len %u) with type %u and pins %u,%u (itype %u)\n",nr, len, type, pins[0],pins[1],_iType); }; @@ -170,9 +187,9 @@ class BusDigital : public Bus { class BusPwm : public Bus { public: - BusPwm(uint8_t type, uint8_t* pins, uint16_t start) : Bus(type, start) { - if (!IS_PWM(type)) return; - uint8_t numPins = NUM_PWM_PINS(type); + BusPwm(BusConfig &bc) : Bus(bc.type, bc.start) { + if (!IS_PWM(bc.type)) return; + uint8_t numPins = NUM_PWM_PINS(bc.type); #ifdef ESP8266 analogWriteRange(255); //same range as one RGB channel @@ -185,7 +202,7 @@ class BusPwm : public Bus { #endif for (uint8_t i = 0; i < numPins; i++) { - _pins[i] = pins[i]; + _pins[i] = bc.pins[i]; if (!pinManager.allocatePin(_pins[i])) { deallocatePins(); return; } @@ -281,12 +298,12 @@ class BusManager { }; - int add(uint8_t busType, uint8_t* pins, uint16_t start, uint16_t len = 1, uint8_t colorOrder = COL_ORDER_GRB, bool rev = false) { + int add(BusConfig &bc) { if (numBusses >= WLED_MAX_BUSSES) return -1; - if (IS_DIGITAL(busType)) { - busses[numBusses] = new BusDigital(busType, pins, start, len, colorOrder, numBusses, rev); + if (IS_DIGITAL(bc.type)) { + busses[numBusses] = new BusDigital(bc, numBusses); } else { - busses[numBusses] = new BusPwm(busType, pins, start); + busses[numBusses] = new BusPwm(bc); } numBusses++; return numBusses -1; diff --git a/wled00/cfg.cpp b/wled00/cfg.cpp index 9e8a2912..24195854 100644 --- a/wled00/cfg.cpp +++ b/wled00/cfg.cpp @@ -44,12 +44,14 @@ bool initBusInstances(JsonArray ins) { //RGBW mode is enabled if at least one of the strips is RGBW useRGBW = (useRGBW || BusManager::isRgbw(ledType)); s++; - busses.add(ledType, pins, start, ledCount, colorOrder, reversed); + BusConfig bc = BusConfig(ledType, pins, start, ledCount, colorOrder, reversed); + busses.add(bc); } //if no bus inited successfully (empty cfg or invalid), init default if (s==0) { uint8_t defPin[] = {LEDPIN}; - busses.add(TYPE_WS2812_RGB, defPin, 0, ledCount, COL_ORDER_GRB); + BusConfig defCfg = BusConfig(TYPE_WS2812_RGB, defPin, 0, ledCount, COL_ORDER_GRB); + busses.add(defCfg); } return s; } @@ -72,8 +74,6 @@ void deserializeConfig() { return; } - //deserializeJson(doc, json); - //int rev_major = doc[F("rev")][0]; // 1 //int rev_minor = doc[F("rev")][1]; // 0 diff --git a/wled00/set.cpp b/wled00/set.cpp index 61b8c0e0..1eda9d44 100644 --- a/wled00/set.cpp +++ b/wled00/set.cpp @@ -89,50 +89,33 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage) uint16_t length, start; uint8_t pins[2] = {255, 255}; - if (ledDoc != nullptr) delete ledDoc; - ledDoc = new DynamicJsonDocument(1024); - - if (ledDoc) { - JsonArray ledO = ledDoc->to(); - //[{"en":true,"start":0,"len":120,"pin":[2],"order":0,"rev":false,"skip":0,"type":22}] - for (uint8_t s = 0; s < WLED_MAX_BUSSES; s++) { - char lp[4] = "L0"; lp[2] = 48+s; lp[3] = 0; //ascii 0-9 //strip data pin - char lk[4] = "L1"; lk[2] = 48+s; lk[3] = 0; //strip clock pin. 255 for none - char lc[4] = "LC"; lc[2] = 48+s; lc[3] = 0; //strip length - char co[4] = "CO"; co[2] = 48+s; co[3] = 0; //strip color order - char lt[4] = "LT"; lt[2] = 48+s; lt[3] = 0; //strip type - char ls[4] = "LS"; ls[2] = 48+s; ls[3] = 0; //strip start LED - char cv[4] = "CV"; cv[2] = 48+s; cv[3] = 0; //strip reverse - if (!request->hasArg(lp)) { - DEBUG_PRINTLN("No data."); break; - } - pins[0] = request->arg(lp).toInt(); - if (request->hasArg(lk)) { - pins[1] = (request->arg(lk).length() > 0) ? request->arg(lk).toInt() : 255; - } - type = request->arg(lt).toInt(); - - if (request->hasArg(lc) && request->arg(lc).toInt() > 0) { - length = request->arg(lc).toInt(); - } else { - break; // no parameter - } - colorOrder = request->arg(co).toInt(); - start = (request->hasArg(ls)) ? request->arg(ls).toInt() : 0; - - JsonObject ins = ledO.createNestedObject(); - ins[F("en")] = true; - ins[F("start")] = start; - ins[F("len")] = length; - JsonArray ins_pin = ins.createNestedArray("pin"); - uint8_t nPins = pins[1] < 100 ? 2 : 1; //TODO 3,4,5 pin types - for (uint8_t i = 0; i < nPins; i++) ins_pin.add(pins[i]); - ins[F("order")] = colorOrder; - ins[F("rev")] = request->hasArg(cv); - ins[F("skip")] = (skipFirstLed && s == 0) ? 1 : 0; - ins[F("type")] = type; - //temporary: add this info to a JSON object and re-init the strips after network callback + for (uint8_t s = 0; s < WLED_MAX_BUSSES; s++) { + char lp[4] = "L0"; lp[2] = 48+s; lp[3] = 0; //ascii 0-9 //strip data pin + char lk[4] = "L1"; lk[2] = 48+s; lk[3] = 0; //strip clock pin. 255 for none + char lc[4] = "LC"; lc[2] = 48+s; lc[3] = 0; //strip length + char co[4] = "CO"; co[2] = 48+s; co[3] = 0; //strip color order + char lt[4] = "LT"; lt[2] = 48+s; lt[3] = 0; //strip type + char ls[4] = "LS"; ls[2] = 48+s; ls[3] = 0; //strip start LED + char cv[4] = "CV"; cv[2] = 48+s; cv[3] = 0; //strip reverse + if (!request->hasArg(lp)) { + DEBUG_PRINTLN("No data."); break; } + pins[0] = request->arg(lp).toInt(); + if (request->hasArg(lk)) { + pins[1] = (request->arg(lk).length() > 0) ? request->arg(lk).toInt() : 255; + } + type = request->arg(lt).toInt(); + + if (request->hasArg(lc) && request->arg(lc).toInt() > 0) { + length = request->arg(lc).toInt(); + } else { + break; // no parameter + } + colorOrder = request->arg(co).toInt(); + start = (request->hasArg(ls)) ? request->arg(ls).toInt() : 0; + + if (busConfigs[s] != nullptr) delete busConfigs[s]; + busConfigs[s] = new BusConfig(type, pins, start, length, colorOrder, request->hasArg(cv)); } ledCount = request->arg(F("LC")).toInt(); diff --git a/wled00/wled.cpp b/wled00/wled.cpp index 3c20c90d..37c2ebfa 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -195,9 +195,15 @@ void WLED::loop() handleHue(); handleBlynk(); - if (ledDoc != nullptr) { - initBusInstances(ledDoc->as()); - delete ledDoc; ledDoc = nullptr; + //LED settings have been saved, re-init busses + if (busConfigs[0] != nullptr) { + busses.removeAll(); + for (uint8_t i = 0; i < WLED_MAX_BUSSES; i++) { + if (busConfigs[i] == nullptr) break; + busses.add(*busConfigs[i]); + delete busConfigs[i]; busConfigs[i] = nullptr; + } + strip.finalizeInit(useRGBW, ledCount, skipFirstLed); } yield(); @@ -356,7 +362,7 @@ void WLED::beginStrip() if (ledCount > MAX_LEDS || ledCount == 0) ledCount = 30; - strip.init(useRGBW, ledCount, skipFirstLed); + strip.finalizeInit(useRGBW, ledCount, skipFirstLed); strip.setBrightness(0); strip.setShowCallback(handleOverlayDraw); diff --git a/wled00/wled.h b/wled00/wled.h index a1499f26..f99cce5e 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -552,7 +552,7 @@ WLED_GLOBAL bool e131NewData _INIT(false); // led fx library object WLED_GLOBAL BusManager busses _INIT(BusManager()); WLED_GLOBAL WS2812FX strip _INIT(WS2812FX()); -WLED_GLOBAL JsonDocument* ledDoc; //temporary, to remember values from network callback until after +WLED_GLOBAL BusConfig* busConfigs[WLED_MAX_BUSSES]; //temporary, to remember values from network callback until after // Usermod manager WLED_GLOBAL UsermodManager usermods _INIT(UsermodManager());