diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index bc5c8b051..a06d4f843 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -1249,12 +1249,12 @@ void WS2812FX::finalizeInit(void) { //RGBW mode is enabled if at least one of the strips is RGBW _hasWhiteChannel |= bus->hasWhite(); //refresh is required to remain off if at least one of the strips requires the refresh. - _isOffRefreshRequired |= bus->isOffRefreshRequired(); + _isOffRefreshRequired |= bus->isOffRefreshRequired() && !bus->isPWM(); // use refresh bit for phase shift with analog unsigned busEnd = bus->getStart() + bus->getLength(); if (busEnd > _length) _length = busEnd; #ifdef ESP8266 // why do we need to reinitialise GPIO3??? - //if ((!IS_DIGITAL(bus->getType()) || IS_2PIN(bus->getType()))) continue; + //if (!bus->isDigital() || bus->is2Pin()) continue; //uint8_t pins[5]; //if (!bus->getPins(pins)) continue; //BusDigital* bd = static_cast(bus); diff --git a/wled00/bus_manager.cpp b/wled00/bus_manager.cpp index aef6b2ee9..854de8fb7 100644 --- a/wled00/bus_manager.cpp +++ b/wled00/bus_manager.cpp @@ -4,6 +4,9 @@ #include #include +#ifdef ARDUINO_ARCH_ESP32 +#include "driver/ledc.h" +#endif #include "const.h" #include "pin_manager.h" #include "bus_wrapper.h" @@ -96,11 +99,11 @@ BusDigital::BusDigital(BusConfig &bc, uint8_t nr, const ColorOrderMap &com) , _milliAmpsMax(bc.milliAmpsMax) , _colorOrderMap(com) { - if (!IS_DIGITAL(bc.type) || !bc.count) return; + if (!isDigital(bc.type) || !bc.count) return; if (!pinManager.allocatePin(bc.pins[0], true, PinOwner::BusDigital)) return; _frequencykHz = 0U; _pins[0] = bc.pins[0]; - if (IS_2PIN(bc.type)) { + if (is2Pin(bc.type)) { if (!pinManager.allocatePin(bc.pins[1], true, PinOwner::BusDigital)) { cleanup(); return; @@ -110,13 +113,16 @@ BusDigital::BusDigital(BusConfig &bc, uint8_t nr, const ColorOrderMap &com) } _iType = PolyBus::getI(bc.type, _pins, nr); if (_iType == I_NONE) return; + _hasRgb = hasRGB(bc.type); + _hasWhite = hasWhite(bc.type); + _hasCCT = hasCCT(bc.type); if (bc.doubleBuffer && !allocateData(bc.count * Bus::getNumberOfChannels(bc.type))) return; //_buffering = bc.doubleBuffer; uint16_t lenToCreate = bc.count; if (bc.type == TYPE_WS2812_1CH_X3) lenToCreate = NUM_ICS_WS2812_1CH_3X(bc.count); // only needs a third of "RGB" LEDs for NeoPixelBus _busPtr = PolyBus::create(_iType, _pins, lenToCreate + _skip, nr, _frequencykHz); _valid = (_busPtr != nullptr); - DEBUG_PRINTF_P(PSTR("%successfully inited strip %u (len %u) with type %u and pins %u,%u (itype %u). mA=%d/%d\n"), _valid?"S":"Uns", nr, bc.count, bc.type, _pins[0], IS_2PIN(bc.type)?_pins[1]:255, _iType, _milliAmpsPerLed, _milliAmpsMax); + DEBUG_PRINTF_P(PSTR("%successfully inited strip %u (len %u) with type %u and pins %u,%u (itype %u). mA=%d/%d\n"), _valid?"S":"Uns", nr, bc.count, bc.type, _pins[0], is2Pin(bc.type)?_pins[1]:255, _iType, _milliAmpsPerLed, _milliAmpsMax); } //fine tune power estimation constants for your setup @@ -337,7 +343,7 @@ uint32_t IRAM_ATTR BusDigital::getPixelColor(uint16_t pix) const { } uint8_t BusDigital::getPins(uint8_t* pinArray) const { - unsigned numPins = IS_2PIN(_type) ? 2 : 1; + unsigned numPins = is2Pin(_type) + 1; if (pinArray) for (unsigned i = 0; i < numPins; i++) pinArray[i] = _pins[i]; return numPins; } @@ -391,10 +397,10 @@ void BusDigital::cleanup(void) { #endif BusPwm::BusPwm(BusConfig &bc) -: Bus(bc.type, bc.start, bc.autoWhite, 1, bc.reversed) +: Bus(bc.type, bc.start, bc.autoWhite, 1, bc.reversed, bc.refreshReq) // hijack Off refresh flag to indicate usage of phase shifting { - if (!IS_PWM(bc.type)) return; - unsigned numPins = NUM_PWM_PINS(bc.type); + if (!isPWM(bc.type)) return; + unsigned numPins = numPWMPins(bc.type); _frequency = bc.frequency ? bc.frequency : WLED_PWM_FREQ; // duty cycle resolution (_depth) can be extracted from this formula: CLOCK_FREQUENCY > _frequency * 2^_depth for (_depth = MAX_BIT_WIDTH; _depth > 8; _depth--) if (((CLOCK_FREQUENCY/_frequency) >> _depth) > 0) break; @@ -422,6 +428,9 @@ BusPwm::BusPwm(BusConfig &bc) ledcAttachPin(_pins[i], _ledcStart + i); #endif } + _hasRgb = hasRGB(bc.type); + _hasWhite = hasWhite(bc.type); + _hasCCT = hasCCT(bc.type); _data = _pwmdata; // avoid malloc() and use stack _valid = true; DEBUG_PRINTF_P(PSTR("%successfully inited PWM strip with type %u, frequency %u, bit depth %u and pins %u,%u,%u,%u,%u\n"), _valid?"S":"Uns", bc.type, _frequency, _depth, _pins[0], _pins[1], _pins[2], _pins[3], _pins[4]); @@ -484,6 +493,7 @@ uint32_t BusPwm::getPixelColor(uint16_t pix) const { return RGBW32(_data[0], _data[0], _data[0], _data[0]); } +/* #ifndef ESP8266 static const uint16_t cieLUT[256] = { 0, 2, 4, 5, 7, 9, 11, 13, 15, 16, @@ -514,11 +524,13 @@ static const uint16_t cieLUT[256] = { 3890, 3930, 3971, 4012, 4053, 4095 }; #endif +*/ void BusPwm::show(void) { if (!_valid) return; - unsigned numPins = NUM_PWM_PINS(_type); + unsigned numPins = getPins(); unsigned maxBri = (1<<_depth) - 1; +/* #ifdef ESP8266 unsigned pwmBri = (unsigned)(roundf(powf((float)_bri / 255.0f, 1.7f) * (float)maxBri)); // using gamma 1.7 to extrapolate PWM duty cycle #else @@ -533,17 +545,43 @@ void BusPwm::show(void) { ledcWrite(_ledcStart + i, scaled); #endif } +*/ + // use CIE brightness formula (credit @dedehai) + unsigned pwmBri = (unsigned)_bri * 100; + if (pwmBri < 2040) pwmBri = ((pwmBri << _depth) + 115043U) / 230087U; //adding '0.5' before division for correct rounding + else { + pwmBri += 4080; + float temp = (float)pwmBri / 29580.0f; + temp = temp * temp * temp * (1<<_depth) - 1; + pwmBri = (unsigned)temp; + } + // determine phase shift POC (credit @dedehai) + [[maybe_unused]] uint32_t phaseOffset = maxBri / numPins; + for (unsigned i = 0; i < numPins; i++) { + unsigned scaled = (_data[i] * pwmBri) / 255; + if (_reversed) scaled = maxBri - scaled; + #ifdef ESP8266 + analogWrite(_pins[i], scaled); + #else + if (_needsRefresh) { // hacked to determine if phase shifted PWM is requested + uint8_t group = ((_ledcStart + i) / 8), channel = ((_ledcStart + i) % 8); // _ledcStart + i is always less than MAX_LED_CHANNELS/LEDC_CHANNELS + ledc_set_duty_with_hpoint((ledc_mode_t)group, (ledc_channel_t)channel, scaled, phaseOffset*i); + ledc_update_duty((ledc_mode_t)group, (ledc_channel_t)channel); + } else + ledcWrite(_ledcStart + i, scaled); + #endif + } } uint8_t BusPwm::getPins(uint8_t* pinArray) const { if (!_valid) return 0; - unsigned numPins = NUM_PWM_PINS(_type); + unsigned numPins = numPWMPins(_type); if (pinArray) for (unsigned i = 0; i < numPins; i++) pinArray[i] = _pins[i]; return numPins; } void BusPwm::deallocatePins(void) { - unsigned numPins = NUM_PWM_PINS(_type); + unsigned numPins = getPins(); for (unsigned i = 0; i < numPins; i++) { pinManager.deallocatePin(_pins[i], PinOwner::BusPwm); if (!pinManager.isPinOk(_pins[i])) continue; @@ -571,6 +609,9 @@ BusOnOff::BusOnOff(BusConfig &bc) } _pin = currentPin; //store only after allocatePin() succeeds pinMode(_pin, OUTPUT); + _hasRgb = false; + _hasWhite = false; + _hasCCT = false; _data = &_onoffdata; // avoid malloc() and use stack _valid = true; DEBUG_PRINTF_P(PSTR("%successfully inited On/Off strip with pin %u\n"), _valid?"S":"Uns", _pin); @@ -609,23 +650,22 @@ BusNetwork::BusNetwork(BusConfig &bc) { switch (bc.type) { case TYPE_NET_ARTNET_RGB: - _rgbw = false; _UDPtype = 2; break; case TYPE_NET_ARTNET_RGBW: - _rgbw = true; _UDPtype = 2; break; case TYPE_NET_E131_RGB: - _rgbw = false; _UDPtype = 1; break; default: // TYPE_NET_DDP_RGB / TYPE_NET_DDP_RGBW - _rgbw = bc.type == TYPE_NET_DDP_RGBW; _UDPtype = 0; break; } - _UDPchannels = _rgbw ? 4 : 3; + _hasRgb = hasRGB(bc.type); + _hasWhite = hasWhite(bc.type); + _hasCCT = false; + _UDPchannels = _hasWhite + 3; _client = IPAddress(bc.pins[0],bc.pins[1],bc.pins[2],bc.pins[3]); _valid = (allocateData(_len * _UDPchannels) != nullptr); DEBUG_PRINTF_P(PSTR("%successfully inited virtual strip with type %u and IP %u.%u.%u.%u\n"), _valid?"S":"Uns", bc.type, bc.pins[0], bc.pins[1], bc.pins[2], bc.pins[3]); @@ -633,25 +673,25 @@ BusNetwork::BusNetwork(BusConfig &bc) void BusNetwork::setPixelColor(uint16_t pix, uint32_t c) { if (!_valid || pix >= _len) return; - if (_rgbw) c = autoWhiteCalc(c); + if (_hasWhite) c = autoWhiteCalc(c); if (Bus::_cct >= 1900) c = colorBalanceFromKelvin(Bus::_cct, c); //color correction from CCT unsigned offset = pix * _UDPchannels; _data[offset] = R(c); _data[offset+1] = G(c); _data[offset+2] = B(c); - if (_rgbw) _data[offset+3] = W(c); + if (_hasWhite) _data[offset+3] = W(c); } uint32_t BusNetwork::getPixelColor(uint16_t pix) const { if (!_valid || pix >= _len) return 0; unsigned offset = pix * _UDPchannels; - return RGBW32(_data[offset], _data[offset+1], _data[offset+2], (_rgbw ? _data[offset+3] : 0)); + return RGBW32(_data[offset], _data[offset+1], _data[offset+2], (hasWhite() ? _data[offset+3] : 0)); } void BusNetwork::show(void) { if (!_valid || !canShow()) return; _broadcastLock = true; - realtimeBroadcast(_UDPtype, _client, _len, _data, _bri, _rgbw); + realtimeBroadcast(_UDPtype, _client, _len, _data, _bri, hasWhite()); _broadcastLock = false; } @@ -669,13 +709,13 @@ void BusNetwork::cleanup(void) { //utility to get the approx. memory usage of a given BusConfig uint32_t BusManager::memUsage(BusConfig &bc) { - if (bc.type == TYPE_ONOFF || IS_PWM(bc.type)) return 5; + if (Bus::isOnOff(bc.type) || Bus::isPWM(bc.type)) return 5; unsigned len = bc.count + bc.skipAmount; unsigned channels = Bus::getNumberOfChannels(bc.type); unsigned multiplier = 1; - if (IS_DIGITAL(bc.type)) { // digital types - if (IS_16BIT(bc.type)) len *= 2; // 16-bit LEDs + if (Bus::isDigital(bc.type)) { // digital types + if (Bus::is16bit(bc.type)) len *= 2; // 16-bit LEDs #ifdef ESP8266 if (bc.pins[0] == 3) { //8266 DMA uses 5x the mem multiplier = 5; @@ -695,11 +735,11 @@ uint32_t BusManager::memUsage(unsigned maxChannels, unsigned maxCount, unsigned int BusManager::add(BusConfig &bc) { if (getNumBusses() - getNumVirtualBusses() >= WLED_MAX_BUSSES) return -1; - if (IS_VIRTUAL(bc.type)) { + if (Bus::isVirtual(bc.type)) { busses[numBusses] = new BusNetwork(bc); - } else if (IS_DIGITAL(bc.type)) { + } else if (Bus::isDigital(bc.type)) { busses[numBusses] = new BusDigital(bc, numBusses, colorOrderMap); - } else if (bc.type == TYPE_ONOFF) { + } else if (Bus::isOnOff(bc.type)) { busses[numBusses] = new BusOnOff(bc); } else { busses[numBusses] = new BusPwm(bc); @@ -749,10 +789,10 @@ String BusManager::getLEDTypesJSONString(void) { String json = "["; for (const auto &type : types) { String id = String(type.id); + // capabilities follows similar pattern as JSON API + int capabilities = Bus::hasRGB(type.id) | Bus::hasWhite(type.id)<<1 | Bus::hasCCT(type.id)<<2 | Bus::is16bit(type.id)<<4; json += "{i:" + id - + F(",w:") + String((int)Bus::hasWhite(type.id)) - + F(",c:") + String((int)Bus::hasCCT(type.id)) - + F(",s:") + String((int)Bus::is16bit(type.id)) + + F(",c:") + String(capabilities) + F(",t:\"") + FPSTR(type.type) + F("\",n:\"") + FPSTR(type.name) + F("\"},"); } @@ -799,7 +839,7 @@ void BusManager::esp32RMTInvertIdle(void) { if (u >= _parallelOutputs + 8) return; // only 8 RMT channels rmt = u - _parallelOutputs; #endif - if (busses[u]->getLength()==0 || !IS_DIGITAL(busses[u]->getType()) || IS_2PIN(busses[u]->getType())) continue; + if (busses[u]->getLength()==0 || !Bus::isDigital(busses[u]->getType()) || IS_2PIN(busses[u]->getType())) continue; //assumes that bus number to rmt channel mapping stays 1:1 rmt_channel_t ch = static_cast(rmt); rmt_idle_level_t lvl; @@ -818,7 +858,7 @@ void BusManager::on(void) { if (pinManager.getPinOwner(LED_BUILTIN) == PinOwner::BusDigital) { for (unsigned i = 0; i < numBusses; i++) { uint8_t pins[2] = {255,255}; - if (IS_DIGITAL(busses[i]->getType()) && busses[i]->getPins(pins)) { + if (Bus::isDigital(busses[i]->getType()) && busses[i]->getPins(pins)) { if (pins[0] == LED_BUILTIN || pins[1] == LED_BUILTIN) { BusDigital *bus = static_cast(busses[i]); bus->reinit(); diff --git a/wled00/bus_manager.h b/wled00/bus_manager.h index 8d23f1127..df96e5944 100644 --- a/wled00/bus_manager.h +++ b/wled00/bus_manager.h @@ -23,56 +23,7 @@ uint16_t approximateKelvinFromRGB(uint32_t rgb); #define IC_INDEX_WS2812_2CH_3X(i) ((i)*2/3) #define WS2812_2CH_3X_SPANS_2_ICS(i) ((i)&0x01) // every other LED zone is on two different ICs -//temporary struct for passing bus configuration to bus -struct BusConfig { - uint8_t type; - uint16_t count; - uint16_t start; - uint8_t colorOrder; - bool reversed; - uint8_t skipAmount; - bool refreshReq; - uint8_t autoWhite; - uint8_t pins[5] = {255, 255, 255, 255, 255}; - uint16_t frequency; - bool doubleBuffer; - uint8_t milliAmpsPerLed; - uint16_t milliAmpsMax; - - BusConfig(uint8_t busType, uint8_t* ppins, uint16_t pstart, uint16_t len = 1, uint8_t pcolorOrder = COL_ORDER_GRB, bool rev = false, uint8_t skip = 0, byte aw=RGBW_MODE_MANUAL_ONLY, uint16_t clock_kHz=0U, bool dblBfr=false, uint8_t maPerLed=LED_MILLIAMPS_DEFAULT, uint16_t maMax=ABL_MILLIAMPS_DEFAULT) - : count(len) - , start(pstart) - , colorOrder(pcolorOrder) - , reversed(rev) - , skipAmount(skip) - , autoWhite(aw) - , frequency(clock_kHz) - , doubleBuffer(dblBfr) - , milliAmpsPerLed(maPerLed) - , milliAmpsMax(maMax) - { - refreshReq = (bool) GET_BIT(busType,7); - type = busType & 0x7F; // bit 7 may be/is hacked to include refresh info (1=refresh in off state, 0=no refresh) - size_t nPins = 1; - if (IS_VIRTUAL(type)) nPins = 4; //virtual network bus. 4 "pins" store IP address - else if (IS_2PIN(type)) nPins = 2; - else if (IS_PWM(type)) nPins = NUM_PWM_PINS(type); - for (size_t i = 0; i < nPins; i++) pins[i] = ppins[i]; - } - - //validates start and length and extends total if needed - bool adjustBounds(uint16_t& total) { - if (!count) count = 1; - if (count > MAX_LEDS_PER_BUS) count = MAX_LEDS_PER_BUS; - if (start >= MAX_LEDS) return false; - //limit length of strip if it would exceed total permissible LEDs - if (start + count > MAX_LEDS) count = MAX_LEDS - start; - //extend total count accordingly - if (start + count > total) total = start + count; - return true; - } -}; - +struct BusConfig; // forward declaration // Defines an LED Strip and its color ordering. typedef struct { @@ -126,12 +77,7 @@ class Bus { virtual void setStatusPixel(uint32_t c) {} virtual void setPixelColor(uint16_t pix, uint32_t c) = 0; virtual void setBrightness(uint8_t b) { _bri = b; }; - inline void setStart(uint16_t start) { _start = start; } virtual void setColorOrder(uint8_t co) {} - virtual bool hasRGB(void) const { return Bus::hasRGB(_type); } - virtual bool hasWhite(void) const { return Bus::hasWhite(_type); } - virtual bool hasCCT(void) const { return Bus::hasCCT(_type); } - virtual bool is16bit(void) const { return Bus::is16bit(_type); } virtual uint32_t getPixelColor(uint16_t pix) const { return 0; } virtual uint8_t getPins(uint8_t* pinArray = nullptr) const { return 0; } virtual uint16_t getLength(void) const { return isOk() ? _len : 0; } @@ -141,11 +87,21 @@ class Bus { virtual uint16_t getLEDCurrent(void) const { return 0; } virtual uint16_t getUsedCurrent(void) const { return 0; } virtual uint16_t getMaxCurrent(void) const { return 0; } - virtual uint8_t getNumberOfChannels(void) const { return hasWhite(_type) + 3*hasRGB(_type) + hasCCT(_type); } + inline bool hasRGB(void) const { return _hasRgb; } + inline bool hasWhite(void) const { return _hasWhite; } + inline bool hasCCT(void) const { return _hasCCT; } + inline bool isDigital(void) const { return isDigital(_type); } + inline bool is2Pin(void) const { return is2Pin(_type); } + inline bool isOnOff(void) const { return isOnOff(_type); } + inline bool isPWM(void) const { return isPWM(_type); } + inline bool isVirtual(void) const { return isVirtual(_type); } + inline bool is16bit(void) const { return is16bit(_type); } inline void setReversed(bool reversed) { _reversed = reversed; } + inline void setStart(uint16_t start) { _start = start; } inline void setAutoWhiteMode(uint8_t m) { if (m < 5) _autoWhiteMode = m; } inline uint8_t getAutoWhiteMode(void) const { return _autoWhiteMode; } + inline uint8_t getNumberOfChannels(void) const { return hasWhite() + 3*hasRGB() + hasCCT(); } inline uint16_t getStart(void) const { return _start; } inline uint8_t getType(void) const { return _type; } inline bool isOk(void) const { return _valid; } @@ -153,7 +109,7 @@ class Bus { inline bool isOffRefreshRequired(void) const { return _needsRefresh; } inline bool containsPixel(uint16_t pix) const { return pix >= _start && pix < _start + _len; } - static inline uint8_t getNumberOfChannels(uint8_t type) { return hasWhite(type) + 3*hasRGB(type) + hasCCT(type); } + static constexpr uint8_t getNumberOfChannels(uint8_t type) { return hasWhite(type) + 3*hasRGB(type) + hasCCT(type); } static constexpr bool hasRGB(uint8_t type) { return !((type >= TYPE_WS2812_1CH && type <= TYPE_WS2812_WWA) || type == TYPE_ANALOG_1CH || type == TYPE_ANALOG_2CH || type == TYPE_ONOFF); } @@ -170,12 +126,20 @@ class Bus { type == TYPE_FW1906 || type == TYPE_WS2805 || type == TYPE_SM16825; } - static constexpr bool is16bit(uint8_t type) { return type == TYPE_UCS8903 || type == TYPE_UCS8904 || type == TYPE_SM16825; } - static inline int16_t getCCT(void) { return _cct; } - static inline void setGlobalAWMode(uint8_t m) { if (m < 5) _gAWM = m; else _gAWM = AW_GLOBAL_DISABLED; } - static inline uint8_t getGlobalAWMode(void) { return _gAWM; } - static void setCCT(int16_t cct) { _cct = cct; } - static inline uint8_t getCCTBlend(void) { return _cctBlend; } + static constexpr bool isTypeValid(uint8_t type) { return (type > 15 && type < 128); } + static constexpr bool isDigital(uint8_t type) { return (type >= TYPE_DIGITAL_MIN && type <= TYPE_DIGITAL_MAX) || is2Pin(type); } + static constexpr bool is2Pin(uint8_t type) { return (type >= TYPE_2PIN_MIN && type <= TYPE_2PIN_MAX); } + static constexpr bool isOnOff(uint8_t type) { return (type == TYPE_ONOFF); } + static constexpr bool isPWM(uint8_t type) { return (type >= TYPE_ANALOG_MIN && type <= TYPE_ANALOG_MAX); } + static constexpr bool isVirtual(uint8_t type) { return (type >= TYPE_VIRTUAL_MIN && type <= TYPE_VIRTUAL_MAX); } + static constexpr bool is16bit(uint8_t type) { return type == TYPE_UCS8903 || type == TYPE_UCS8904 || type == TYPE_SM16825; } + static constexpr int numPWMPins(uint8_t type) { return (type - 40); } + + static inline int16_t getCCT(void) { return _cct; } + static inline void setGlobalAWMode(uint8_t m) { if (m < 5) _gAWM = m; else _gAWM = AW_GLOBAL_DISABLED; } + static inline uint8_t getGlobalAWMode(void) { return _gAWM; } + static inline void setCCT(int16_t cct) { _cct = cct; } + static inline uint8_t getCCTBlend(void) { return _cctBlend; } static void setCCTBlend(uint8_t b) { if (b > 100) b = 100; _cctBlend = (b * 127) / 100; @@ -210,9 +174,14 @@ class Bus { uint8_t _bri; uint16_t _start; uint16_t _len; - bool _reversed; - bool _valid; - bool _needsRefresh; + //struct { //using bitfield struct adds abour 250 bytes to binary size + bool _reversed;// : 1; + bool _valid;// : 1; + bool _needsRefresh;// : 1; + bool _hasRgb;// : 1; + bool _hasWhite;// : 1; + bool _hasCCT;// : 1; + //} __attribute__ ((packed)); uint8_t _autoWhiteMode; uint8_t *_data; // global Auto White Calculation override @@ -247,7 +216,7 @@ class BusDigital : public Bus { void setColorOrder(uint8_t colorOrder) override; uint32_t getPixelColor(uint16_t pix) const override; uint8_t getColorOrder(void) const override { return _colorOrder; } - uint8_t getPins(uint8_t* pinArray) const override; + uint8_t getPins(uint8_t* pinArray = nullptr) const override; uint8_t skippedLeds(void) const override { return _skip; } uint16_t getFrequency(void) const override { return _frequencykHz; } uint16_t getLEDCurrent(void) const override { return _milliAmpsPerLed; } @@ -291,7 +260,7 @@ class BusPwm : public Bus { void setPixelColor(uint16_t pix, uint32_t c) override; uint32_t getPixelColor(uint16_t pix) const override; //does no index check - uint8_t getPins(uint8_t* pinArray) const override; + uint8_t getPins(uint8_t* pinArray = nullptr) const override; uint16_t getFrequency(void) const override { return _frequency; } void show(void) override; void cleanup(void) { deallocatePins(); } @@ -331,12 +300,10 @@ class BusNetwork : public Bus { BusNetwork(BusConfig &bc); ~BusNetwork() { cleanup(); } - bool hasRGB(void) const override { return true; } - bool hasWhite(void) const override { return _rgbw; } bool canShow(void) const override { return !_broadcastLock; } // this should be a return value from UDP routine if it is still sending data out void setPixelColor(uint16_t pix, uint32_t c) override; uint32_t getPixelColor(uint16_t pix) const override; - uint8_t getPins(uint8_t* pinArray) const override; + uint8_t getPins(uint8_t* pinArray = nullptr) const override; void show(void) override; void cleanup(void); @@ -344,11 +311,62 @@ class BusNetwork : public Bus { IPAddress _client; uint8_t _UDPtype; uint8_t _UDPchannels; - bool _rgbw; bool _broadcastLock; }; +//temporary struct for passing bus configuration to bus +struct BusConfig { + uint8_t type; + uint16_t count; + uint16_t start; + uint8_t colorOrder; + bool reversed; + uint8_t skipAmount; + bool refreshReq; + uint8_t autoWhite; + uint8_t pins[5] = {255, 255, 255, 255, 255}; + uint16_t frequency; + bool doubleBuffer; + uint8_t milliAmpsPerLed; + uint16_t milliAmpsMax; + + BusConfig(uint8_t busType, uint8_t* ppins, uint16_t pstart, uint16_t len = 1, uint8_t pcolorOrder = COL_ORDER_GRB, bool rev = false, uint8_t skip = 0, byte aw=RGBW_MODE_MANUAL_ONLY, uint16_t clock_kHz=0U, bool dblBfr=false, uint8_t maPerLed=LED_MILLIAMPS_DEFAULT, uint16_t maMax=ABL_MILLIAMPS_DEFAULT) + : count(len) + , start(pstart) + , colorOrder(pcolorOrder) + , reversed(rev) + , skipAmount(skip) + , autoWhite(aw) + , frequency(clock_kHz) + , doubleBuffer(dblBfr) + , milliAmpsPerLed(maPerLed) + , milliAmpsMax(maMax) + { + refreshReq = (bool) GET_BIT(busType,7); + type = busType & 0x7F; // bit 7 may be/is hacked to include refresh info (1=refresh in off state, 0=no refresh) + size_t nPins = 1; + if (Bus::isVirtual(type)) nPins = 4; //virtual network bus. 4 "pins" store IP address + else if (Bus::is2Pin(type)) nPins = 2; + else if (Bus::isPWM(type)) nPins = Bus::numPWMPins(type); + for (size_t i = 0; i < nPins; i++) pins[i] = ppins[i]; + DEBUGBUS_PRINTF_P(PSTR("BusConfig type %d pins: %u\r\n"), (int)type, (int)Bus::isVirtual(type), nPins); + } + + //validates start and length and extends total if needed + bool adjustBounds(uint16_t& total) { + if (!count) count = 1; + if (count > MAX_LEDS_PER_BUS) count = MAX_LEDS_PER_BUS; + if (start >= MAX_LEDS) return false; + //limit length of strip if it would exceed total permissible LEDs + if (start + count > MAX_LEDS) count = MAX_LEDS - start; + //extend total count accordingly + if (start + count > total) total = start + count; + return true; + } +}; + + class BusManager { public: BusManager() {}; @@ -398,7 +416,7 @@ class BusManager { static uint8_t _parallelOutputs; #ifdef ESP32_DATA_IDLE_HIGH - static void esp32RMTInvertIdle(void); + static void esp32RMTInvertIdle(void) ; #endif static uint8_t getNumVirtualBusses(void) { int j = 0; diff --git a/wled00/bus_wrapper.h b/wled00/bus_wrapper.h index ae39adc14..bf2d30c0e 100644 --- a/wled00/bus_wrapper.h +++ b/wled00/bus_wrapper.h @@ -1314,8 +1314,8 @@ class PolyBus { //gives back the internal type index (I_XX_XXX_X above) for the input static uint8_t getI(uint8_t busType, uint8_t* pins, uint8_t num = 0) { - if (!IS_DIGITAL(busType)) return I_NONE; - if (IS_2PIN(busType)) { //SPI LED chips + if (!Bus::isDigital(busType)) return I_NONE; + if (Bus::is2Pin(busType)) { //SPI LED chips bool isHSPI = false; #ifdef ESP8266 if (pins[0] == P_8266_HS_MOSI && pins[1] == P_8266_HS_CLK) isHSPI = true; diff --git a/wled00/cfg.cpp b/wled00/cfg.cpp index 76ff4d20e..5f0c8593f 100644 --- a/wled00/cfg.cpp +++ b/wled00/cfg.cpp @@ -173,8 +173,8 @@ bool deserializeConfig(JsonObject doc, bool fromFS) { for (JsonObject elm : ins) { unsigned type = elm["type"] | TYPE_WS2812_RGB; unsigned len = elm["len"] | DEFAULT_LED_COUNT; - if (!IS_DIGITAL(type)) continue; - if (!IS_2PIN(type)) { + if (!Bus::isDigital(type)) continue; + if (!Bus::is2Pin(type)) { digitalCount++; unsigned channels = Bus::getNumberOfChannels(type); if (len > maxLedsOnBus) maxLedsOnBus = len; @@ -215,7 +215,7 @@ bool deserializeConfig(JsonObject doc, bool fromFS) { uint8_t maPerLed = elm[F("ledma")] | LED_MILLIAMPS_DEFAULT; uint16_t maMax = elm[F("maxpwr")] | (ablMilliampsMax * length) / total; // rough (incorrect?) per strip ABL calculation when no config exists // To disable brightness limiter we either set output max current to 0 or single LED current to 0 (we choose output max current) - if (IS_PWM(ledType) || IS_ONOFF(ledType) || IS_VIRTUAL(ledType)) { // analog and virtual + if (Bus::isPWM(ledType) || Bus::isOnOff(ledType) || Bus::isVirtual(ledType)) { // analog and virtual maPerLed = 0; maMax = 0; } diff --git a/wled00/const.h b/wled00/const.h index d5cd049d5..a87c6ff30 100644 --- a/wled00/const.h +++ b/wled00/const.h @@ -281,6 +281,7 @@ #define TYPE_NONE 0 //light is not configured #define TYPE_RESERVED 1 //unused. Might indicate a "virtual" light //Digital types (data pin only) (16-39) +#define TYPE_DIGITAL_MIN 16 // first usable digital type #define TYPE_WS2812_1CH 18 //white-only chips (1 channel per IC) (unused) #define TYPE_WS2812_1CH_X3 19 //white-only chips (3 channels per IC) #define TYPE_WS2812_2CH_X3 20 //CCT chips (1st IC controls WW + CW of 1st zone and CW of 2nd zone, 2nd IC controls WW of 2nd zone and WW + CW of 3rd zone) @@ -298,26 +299,36 @@ #define TYPE_WS2805 32 //RGB + WW + CW #define TYPE_TM1914 33 //RGB #define TYPE_SM16825 34 //RGB + WW + CW +#define TYPE_DIGITAL_MAX 39 // last usable digital type //"Analog" types (40-47) #define TYPE_ONOFF 40 //binary output (relays etc.; NOT PWM) +#define TYPE_ANALOG_MIN 41 // first usable analog type #define TYPE_ANALOG_1CH 41 //single channel PWM. Uses value of brightest RGBW channel #define TYPE_ANALOG_2CH 42 //analog WW + CW #define TYPE_ANALOG_3CH 43 //analog RGB #define TYPE_ANALOG_4CH 44 //analog RGBW #define TYPE_ANALOG_5CH 45 //analog RGB + WW + CW +#define TYPE_ANALOG_6CH 46 //analog RGB + A + WW + CW +#define TYPE_ANALOG_MAX 47 // last usable analog type //Digital types (data + clock / SPI) (48-63) +#define TYPE_2PIN_MIN 48 #define TYPE_WS2801 50 #define TYPE_APA102 51 #define TYPE_LPD8806 52 #define TYPE_P9813 53 #define TYPE_LPD6803 54 +#define TYPE_2PIN_MAX 63 //Network types (master broadcast) (80-95) +#define TYPE_VIRTUAL_MIN 80 #define TYPE_NET_DDP_RGB 80 //network DDP RGB bus (master broadcast bus) #define TYPE_NET_E131_RGB 81 //network E131 RGB bus (master broadcast bus, unused) #define TYPE_NET_ARTNET_RGB 82 //network ArtNet RGB bus (master broadcast bus, unused) #define TYPE_NET_DDP_RGBW 88 //network DDP RGBW bus (master broadcast bus) #define TYPE_NET_ARTNET_RGBW 89 //network ArtNet RGB bus (master broadcast bus, unused) +#define TYPE_VIRTUAL_MAX 95 +/* +// old macros that have been moved to Bus class #define IS_TYPE_VALID(t) ((t) > 15 && (t) < 128) #define IS_DIGITAL(t) (((t) > 15 && (t) < 40) || ((t) > 47 && (t) < 64)) //digital are 16-39 and 48-63 #define IS_2PIN(t) ((t) > 47 && (t) < 64) @@ -326,6 +337,7 @@ #define IS_PWM(t) ((t) > 40 && (t) < 46) //does not include on/Off type #define NUM_PWM_PINS(t) ((t) - 40) //for analog PWM 41-45 only #define IS_VIRTUAL(t) ((t) >= 80 && (t) < 96) //this was a poor choice a better would be 96-111 +*/ //Color orders #define COL_ORDER_GRB 0 //GRB(w),defaut diff --git a/wled00/data/settings_leds.htm b/wled00/data/settings_leds.htm index 54c16b9d9..57d94f31a 100644 --- a/wled00/data/settings_leds.htm +++ b/wled00/data/settings_leds.htm @@ -25,9 +25,10 @@ function isDig(t) { return gT(t).t === "D" || isD2P(t); } // is digital type function isD2P(t) { return gT(t).t === "2P"; } // is digital 2 pin type function isVir(t) { return gT(t).t === "V"; } // is virtual type - function hasW(t) { return gT(t).w == 1; } // has white channel - function hasCCT(t) { return gT(t).c == 1; } // is white CCT enabled - function is16b(t) { return gT(t).s == 1; } // is digital 16 bit type + function hasRGB(t) { return !!(gT(t).c & 0x01); } // has RGB + function hasW(t) { return !!(gT(t).c & 0x02); } // has white channel + function hasCCT(t) { return !!(gT(t).c & 0x04); } // is white CCT enabled + function is16b(t) { return !!(gT(t).c & 0x10); } // is digital 16 bit type // https://www.educative.io/edpresso/how-to-dynamically-load-a-js-file-in-javascript function loadJS(FILE_URL, async = true) { let scE = d.createElement("script"); @@ -229,24 +230,29 @@ let setPinConfig = (n,t) => { let p0d = "GPIO:"; let p1d = ""; + let off = "Off Refresh"; switch (gT(t).t.charAt(0)) { case '2': - p1d = "Clk "+p0d; + p1d = "Clock "+p0d; case 'D': p0d = "Data "+p0d; break; case 'A': - if (gT(t).t.length > 1) p0d = "GPIOs:"; + if (gT(t).t.length > 1) { + p0d = "GPIOs:"; + off = "Phase shift"; + } else gId(`dig${n}f`).style.display = "none"; break; case 'V': p0d = "IP address:"; break; } - gId("p0d"+n).innerHTML = p0d; - gId("p1d"+n).innerHTML = p1d; + gId("p0d"+n).innerText = p0d; + gId("p1d"+n).innerText = p1d; + gId("off"+n).innerText = off; // secondary pins show/hide (type string length is equivalent to number of pins used; except for virtual and on/off) let pins = gT(t).t.length + 3*isVir(t); // fixes virtual pins to 4 - if (pins == 0) pins = 1; // fixes on/off pin + if (pins == 0) pins = 1; // fixes on/off pin for (let p=1; p<5; p++) { var LK = d.Sf["L"+p+n]; if (!LK) continue; @@ -281,7 +287,7 @@ gId("dig"+n+"c").style.display = (isAna(t)) ? "none":"inline"; // hide count for analog gId("dig"+n+"r").style.display = (isVir(t)) ? "none":"inline"; // hide reversed for virtual gId("dig"+n+"s").style.display = (isVir(t) || isAna(t)) ? "none":"inline"; // hide skip 1st for virtual & analog - gId("dig"+n+"f").style.display = (isDig(t)) ? "inline":"none"; // hide refresh + gId("dig"+n+"f").style.display = (isDig(t) || isPWM(t)) ? "inline":"none"; // hide refresh (PWM hijacks reffresh for phase shifting) gId("dig"+n+"a").style.display = (hasW(t)) ? "inline":"none"; // auto calculate white gId("dig"+n+"l").style.display = (isD2P(t) || isPWM(t)) ? "inline":"none"; // bus clock speed / PWM speed (relative) (not On/Off) gId("rev"+n).innerHTML = isAna(t) ? "Inverted output":"Reversed"; // change reverse text for analog else (rotated 180°) @@ -454,8 +460,8 @@ mA/LED:

Reversed:

Skip first LEDs:
-

Off Refresh:
-

Auto-calculate white channel from RGB:
 
+

Off Refresh:
+

Auto-calculate W channel from RGB:
 
`; f.insertAdjacentHTML("beforeend", cn); // fill led types (credit @netmindz) diff --git a/wled00/set.cpp b/wled00/set.cpp index d295d36ee..7be27ed20 100644 --- a/wled00/set.cpp +++ b/wled00/set.cpp @@ -177,7 +177,7 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage) } awmode = request->arg(aw).toInt(); uint16_t freq = request->arg(sp).toInt(); - if (IS_PWM(type)) { + if (Bus::isPWM(type)) { switch (freq) { case 0 : freq = WLED_PWM_FREQ/2; break; case 1 : freq = WLED_PWM_FREQ*2/3; break; @@ -186,7 +186,7 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage) case 3 : freq = WLED_PWM_FREQ*2; break; case 4 : freq = WLED_PWM_FREQ*10/3; break; // uint16_t max (19531 * 3.333) } - } else if (IS_DIGITAL(type) && IS_2PIN(type)) { + } else if (Bus::is2Pin(type)) { switch (freq) { default: case 0 : freq = 1000; break; @@ -199,7 +199,7 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage) freq = 0; } channelSwap = Bus::hasWhite(type) ? request->arg(wo).toInt() : 0; - if (type == TYPE_ONOFF || IS_PWM(type) || IS_VIRTUAL(type)) { // analog and virtual + if (Bus::isOnOff(type) || Bus::isPWM(type) || Bus::isVirtual(type)) { // analog and virtual maPerLed = 0; maMax = 0; } else { diff --git a/wled00/wled.cpp b/wled00/wled.cpp index a6143eee6..90498f52b 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -186,8 +186,8 @@ void WLED::loop() unsigned maxChannels = 0; for (unsigned i = 0; i < WLED_MAX_BUSSES+WLED_MIN_VIRTUAL_BUSSES; i++) { if (busConfigs[i] == nullptr) break; - if (!IS_DIGITAL(busConfigs[i]->type)) continue; - if (!IS_2PIN(busConfigs[i]->type)) { + if (!Bus::isDigital(busConfigs[i]->type)) continue; + if (!Bus::is2Pin(busConfigs[i]->type)) { digitalCount++; unsigned channels = Bus::getNumberOfChannels(busConfigs[i]->type); if (busConfigs[i]->count > maxLedsOnBus) maxLedsOnBus = busConfigs[i]->count; diff --git a/wled00/xml.cpp b/wled00/xml.cpp index df1475249..c06b02768 100644 --- a/wled00/xml.cpp +++ b/wled00/xml.cpp @@ -395,7 +395,7 @@ void getSettingsJS(byte subPage, char* dest) int nPins = bus->getPins(pins); for (int i = 0; i < nPins; i++) { lp[1] = offset+i; - if (pinManager.isPinOk(pins[i]) || IS_VIRTUAL(bus->getType())) sappend('v',lp,pins[i]); + if (pinManager.isPinOk(pins[i]) || bus->isVirtual()) sappend('v',lp,pins[i]); } sappend('v',lc,bus->getLength()); sappend('v',lt,bus->getType()); @@ -407,7 +407,7 @@ void getSettingsJS(byte subPage, char* dest) sappend('v',aw,bus->getAutoWhiteMode()); sappend('v',wo,bus->getColorOrder() >> 4); unsigned speed = bus->getFrequency(); - if (IS_PWM(bus->getType())) { + if (bus->isPWM()) { switch (speed) { case WLED_PWM_FREQ/2 : speed = 0; break; case WLED_PWM_FREQ*2/3 : speed = 1; break; @@ -416,7 +416,7 @@ void getSettingsJS(byte subPage, char* dest) case WLED_PWM_FREQ*2 : speed = 3; break; case WLED_PWM_FREQ*10/3 : speed = 4; break; // uint16_t max (19531 * 3.333) } - } else if (IS_DIGITAL(bus->getType()) && IS_2PIN(bus->getType())) { + } else if (bus->is2Pin()) { switch (speed) { case 1000 : speed = 0; break; case 2000 : speed = 1; break;