Skip to content

Commit cc011e3

Browse files
committed
Bus creation bugfix
- speed improvements in ABL - verbose debugging
1 parent adead9b commit cc011e3

File tree

3 files changed

+44
-19
lines changed

3 files changed

+44
-19
lines changed

wled00/FX_fcn.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,6 +1259,7 @@ void WS2812FX::finalizeInit() {
12591259
#endif
12601260
mem += BusManager::memUsage(*busConfigs[i]); // includes global buffer
12611261
if (mem <= MAX_LED_MEMORY) BusManager::add(*busConfigs[i]);
1262+
else DEBUG_PRINTF_P(PSTR("Out of LED memory! Bus #%u not created."), i);
12621263
delete busConfigs[i];
12631264
busConfigs[i] = nullptr;
12641265
}

wled00/bus_manager.cpp

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -130,30 +130,41 @@ BusDigital::BusDigital(const BusConfig &bc, uint8_t nr, const ColorOrderMap &com
130130
, _milliAmpsMax(bc.milliAmpsMax)
131131
, _colorOrderMap(com)
132132
{
133-
if (!isDigital(bc.type) || !bc.count) return;
134-
if (!PinManager::allocatePin(bc.pins[0], true, PinOwner::BusDigital)) return;
133+
DEBUG_PRINTLN(F("Bus: Creating digital bus."));
134+
if (!isDigital(bc.type) || !bc.count) { DEBUG_PRINTLN(F("Not digial or empty bus!")); return; }
135+
if (!PinManager::allocatePin(bc.pins[0], true, PinOwner::BusDigital)) { DEBUG_PRINTLN(F("Pin 0 allocated!")); return; }
135136
_frequencykHz = 0U;
136137
_pins[0] = bc.pins[0];
137138
if (is2Pin(bc.type)) {
138139
if (!PinManager::allocatePin(bc.pins[1], true, PinOwner::BusDigital)) {
139140
cleanup();
141+
DEBUG_PRINTLN(F("Pin 1 allocated!"));
140142
return;
141143
}
142144
_pins[1] = bc.pins[1];
143145
_frequencykHz = bc.frequency ? bc.frequency : 2000U; // 2MHz clock if undefined
144146
}
145147
_iType = PolyBus::getI(bc.type, _pins, nr);
146-
if (_iType == I_NONE) return;
148+
if (_iType == I_NONE) { DEBUG_PRINTLN(F("Incorrect iType!")); return; }
147149
_hasRgb = hasRGB(bc.type);
148150
_hasWhite = hasWhite(bc.type);
149151
_hasCCT = hasCCT(bc.type);
150-
if (bc.doubleBuffer && !allocateData(bc.count * Bus::getNumberOfChannels(bc.type))) return;
152+
if (bc.doubleBuffer && !allocateData(bc.count * Bus::getNumberOfChannels(bc.type))) { DEBUG_PRINTLN(F("Buffer allocation failed!")); return; }
151153
//_buffering = bc.doubleBuffer;
152154
uint16_t lenToCreate = bc.count;
153155
if (bc.type == TYPE_WS2812_1CH_X3) lenToCreate = NUM_ICS_WS2812_1CH_3X(bc.count); // only needs a third of "RGB" LEDs for NeoPixelBus
154156
_busPtr = PolyBus::create(_iType, _pins, lenToCreate + _skip, nr);
155157
_valid = (_busPtr != nullptr);
156-
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);
158+
DEBUG_PRINTF_P(PSTR("Bus: %successfully inited #%u (len:%u, type:%u (RGB:%d, W:%d, CCT:%d), pins:%u,%u [itype:%u] mA=%d/%d)\n"),
159+
_valid?"S":"Uns",
160+
(int)nr,
161+
(int)bc.count,
162+
(int)bc.type,
163+
(int)_hasRgb, (int)_hasWhite, (int)_hasCCT,
164+
(unsigned)_pins[0], is2Pin(bc.type)?(unsigned)_pins[1]:255U,
165+
(unsigned)_iType,
166+
(int)_milliAmpsPerLed, (int)_milliAmpsMax
167+
);
157168
}
158169

159170
//DISCLAIMER
@@ -177,7 +188,7 @@ uint8_t BusDigital::estimateCurrentAndLimitBri() {
177188
actualMilliampsPerLed = 12; // from testing an actual strip
178189
}
179190

180-
size_t powerBudget = (_milliAmpsMax - MA_FOR_ESP/BusManager::getNumBusses()); //80/120mA for ESP power
191+
unsigned powerBudget = (_milliAmpsMax - MA_FOR_ESP/BusManager::getNumBusses()); //80/120mA for ESP power
181192
if (powerBudget > getLength()) { //each LED uses about 1mA in standby, exclude that from power budget
182193
powerBudget -= getLength();
183194
} else {
@@ -202,16 +213,15 @@ uint8_t BusDigital::estimateCurrentAndLimitBri() {
202213
}
203214

204215
// powerSum has all the values of channels summed (max would be getLength()*765 as white is excluded) so convert to milliAmps
205-
busPowerSum = (busPowerSum * actualMilliampsPerLed) / 765;
206-
_milliAmpsTotal = busPowerSum * _bri / 255;
216+
_milliAmpsTotal = (busPowerSum * actualMilliampsPerLed * _bri) / (765*255);
207217

208218
uint8_t newBri = _bri;
209-
if (busPowerSum * _bri / 255 > powerBudget) { //scale brightness down to stay in current limit
210-
float scale = (float)(powerBudget * 255) / (float)(busPowerSum * _bri);
211-
if (scale >= 1.0f) return _bri;
212-
_milliAmpsTotal = ceilf((float)_milliAmpsTotal * scale);
213-
uint8_t scaleB = min((int)(scale * 255), 255);
214-
newBri = unsigned(_bri * scaleB) / 256 + 1;
219+
if (_milliAmpsTotal > powerBudget) {
220+
//scale brightness down to stay in current limit
221+
unsigned scaleB = powerBudget * 255 / _milliAmpsTotal;
222+
newBri = (_bri * scaleB) / 256 + 1;
223+
_milliAmpsTotal = powerBudget;
224+
//_milliAmpsTotal = (busPowerSum * actualMilliampsPerLed * newBri) / (765*255);
215225
}
216226
return newBri;
217227
}
@@ -406,8 +416,8 @@ std::vector<LEDType> BusDigital::getLEDTypes() {
406416
{TYPE_WS2805, "D", PSTR("WS2805 RGBCW")},
407417
{TYPE_SM16825, "D", PSTR("SM16825 RGBCW")},
408418
{TYPE_WS2812_1CH_X3, "D", PSTR("WS2811 White")},
409-
//{TYPE_WS2812_2CH_X3, "D", PSTR("WS2811 CCT")}, // not implemented
410-
{TYPE_WS2812_WWA, "D", PSTR("WS2811 WWA")}, // amber ignored
419+
//{TYPE_WS2812_2CH_X3, "D", PSTR("WS281x CCT")}, // not implemented
420+
{TYPE_WS2812_WWA, "D", PSTR("WS281x WWA")}, // amber ignored
411421
{TYPE_WS2801, "2P", PSTR("WS2801")},
412422
{TYPE_APA102, "2P", PSTR("APA102")},
413423
{TYPE_LPD8806, "2P", PSTR("LPD8806")},
@@ -428,9 +438,9 @@ void BusDigital::cleanup() {
428438
_valid = false;
429439
_busPtr = nullptr;
430440
if (_data != nullptr) freeData();
431-
PinManager::deallocateMultiplePins(_pins, 2, PinOwner::BusDigital);
432-
//PinManager::deallocatePin(_pins[1], PinOwner::BusDigital);
433-
//PinManager::deallocatePin(_pins[0], PinOwner::BusDigital);
441+
//PinManager::deallocateMultiplePins(_pins, 2, PinOwner::BusDigital);
442+
PinManager::deallocatePin(_pins[1], PinOwner::BusDigital);
443+
PinManager::deallocatePin(_pins[0], PinOwner::BusDigital);
434444
}
435445

436446

@@ -782,6 +792,7 @@ std::vector<LEDType> BusNetwork::getLEDTypes() {
782792
}
783793

784794
void BusNetwork::cleanup() {
795+
DEBUG_PRINTLN(F("Virtual Cleanup."));
785796
_type = I_NONE;
786797
_valid = false;
787798
freeData();
@@ -819,6 +830,7 @@ unsigned BusManager::getTotalBuffers() {
819830
}
820831

821832
int BusManager::add(const BusConfig &bc) {
833+
DEBUG_PRINTF_P(PSTR("Bus: Adding bus #%d (%d - %d >= %d)\n"), numBusses, getNumBusses(), getNumVirtualBusses(), WLED_MAX_BUSSES);
822834
if (getNumBusses() - getNumVirtualBusses() >= WLED_MAX_BUSSES) return -1;
823835
if (Bus::isVirtual(bc.type)) {
824836
busses[numBusses] = new BusNetwork(bc);
@@ -858,6 +870,7 @@ String BusManager::getLEDTypesJSONString() {
858870
}
859871

860872
void BusManager::useParallelOutput() {
873+
DEBUG_PRINTLN(F("Bus: Enabling parallel I2S."));
861874
PolyBus::setParallelI2S1Output();
862875
}
863876

wled00/cfg.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,8 +813,19 @@ void serializeConfig() {
813813
JsonArray hw_led_ins = hw_led.createNestedArray("ins");
814814

815815
for (size_t s = 0; s < BusManager::getNumBusses(); s++) {
816+
DEBUG_PRINTF_P(PSTR("Cfg: Saving bus #%u\n"), s);
816817
Bus *bus = BusManager::getBus(s);
817818
if (!bus || bus->getLength()==0) break;
819+
DEBUG_PRINTF_P(PSTR(" (%d-%d, type:%d, CO:%d, rev:%d, skip:%d, AW:%d kHz:%d, mA:%d/%d)\n"),
820+
(int)bus->getStart(), (int)(bus->getStart()+bus->getLength()),
821+
(int)(bus->getType() & 0x7F),
822+
(int)bus->getColorOrder(),
823+
(int)bus->isReversed(),
824+
(int)bus->skippedLeds(),
825+
(int)bus->getAutoWhiteMode(),
826+
(int)bus->getFrequency(),
827+
(int)bus->getLEDCurrent(), (int)bus->getMaxCurrent()
828+
);
818829
JsonObject ins = hw_led_ins.createNestedObject();
819830
ins["start"] = bus->getStart();
820831
ins["len"] = bus->getLength();

0 commit comments

Comments
 (0)