Skip to content

Commit f75b13a

Browse files
CopilotDedeHai
andcommitted
Simplify bus creation logic as requested - replace complex binary search with simple default count fallback
Co-authored-by: DedeHai <6280424+DedeHai@users.noreply.github.com>
1 parent 1b19c35 commit f75b13a

File tree

1 file changed

+9
-81
lines changed

1 file changed

+9
-81
lines changed

wled00/FX_fcn.cpp

Lines changed: 9 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,94 +1175,22 @@ void WS2812FX::finalizeInit() {
11751175
digitalCount = 0;
11761176
#endif
11771177

1178-
// create buses/outputs with memory-aware truncation
1178+
// create buses/outputs
11791179
unsigned mem = 0;
1180-
std::vector<BusConfig> truncatedConfigs;
1181-
11821180
for (auto bus : busConfigs) {
11831181
// Calculate what this bus would use with its current configuration
11841182
unsigned busMemUsage = bus.memUsage(Bus::isDigital(bus.type) && !Bus::is2Pin(bus.type) ? digitalCount : 0);
11851183

1186-
if (mem + busMemUsage <= MAX_LED_MEMORY) {
1187-
// Bus fits as-is, add it
1188-
if (BusManager::add(bus) != -1) {
1189-
mem += busMemUsage;
1190-
if (Bus::isDigital(bus.type) && !Bus::is2Pin(bus.type)) digitalCount++;
1191-
truncatedConfigs.push_back(bus);
1192-
} else break;
1193-
} else {
1194-
// Bus doesn't fit, try to truncate LED count to fit available memory
1195-
unsigned remainingMem = MAX_LED_MEMORY - mem;
1196-
if (remainingMem > 0) {
1197-
// Binary search to find maximum LED count that fits in remaining memory
1198-
uint16_t maxCount = bus.count;
1199-
uint16_t minCount = 1;
1200-
uint16_t bestCount = 0;
1201-
1202-
while (minCount <= maxCount) {
1203-
uint16_t testCount = minCount + (maxCount - minCount) / 2;
1204-
BusConfig testBus = bus;
1205-
testBus.count = testCount;
1206-
1207-
unsigned testMem = testBus.memUsage(Bus::isDigital(testBus.type) && !Bus::is2Pin(testBus.type) ? digitalCount : 0);
1208-
1209-
if (testMem <= remainingMem) {
1210-
bestCount = testCount;
1211-
minCount = testCount + 1;
1212-
} else {
1213-
maxCount = testCount - 1;
1214-
}
1215-
}
1216-
1217-
if (bestCount > 0) {
1218-
// Found a truncated configuration that fits
1219-
bus.count = bestCount;
1220-
DEBUG_PRINTF_P(PSTR("Truncated bus %d from %u to %u LEDs to fit memory constraints.\n"), (int)bus.type, (int)maxCount, (int)bestCount);
1221-
1222-
if (BusManager::add(bus) != -1) {
1223-
mem += bus.memUsage(Bus::isDigital(bus.type) && !Bus::is2Pin(bus.type) ? digitalCount : 0);
1224-
if (Bus::isDigital(bus.type) && !Bus::is2Pin(bus.type)) digitalCount++;
1225-
truncatedConfigs.push_back(bus);
1226-
}
1227-
} else {
1228-
DEBUG_PRINTF_P(PSTR("Out of LED memory! Bus %d (%d) #%u not created - no room for even 1 LED."), (int)bus.type, (int)bus.count, digitalCount);
1229-
}
1230-
} else {
1231-
DEBUG_PRINTF_P(PSTR("Out of LED memory! Bus %d (%d) #%u not created."), (int)bus.type, (int)bus.count, digitalCount);
1232-
}
1233-
}
1234-
}
1235-
1236-
// If no buses were successfully created, create a minimal fallback using first user config or defaults
1237-
if (BusManager::getNumBusses() == 0) {
1238-
DEBUG_PRINTLN(F("No buses created due to memory limits! Creating fallback bus with user config or defaults."));
1239-
1240-
BusConfig fallbackCfg;
1241-
if (!busConfigs.empty()) {
1242-
// Use first user configuration but with minimal LED count
1243-
fallbackCfg = busConfigs[0];
1244-
fallbackCfg.count = 1; // Start with minimal count
1245-
fallbackCfg.start = 0; // Ensure it starts at 0
1246-
1247-
// Try to fit as many LEDs as possible within memory constraints
1248-
for (uint16_t testCount = 1; testCount <= DEFAULT_LED_COUNT; testCount++) {
1249-
fallbackCfg.count = testCount;
1250-
unsigned testMem = fallbackCfg.memUsage(Bus::isDigital(fallbackCfg.type) && !Bus::is2Pin(fallbackCfg.type) ? 0 : 0);
1251-
if (testMem > MAX_LED_MEMORY) {
1252-
fallbackCfg.count = (testCount > 1) ? testCount - 1 : 1;
1253-
break;
1254-
}
1255-
}
1256-
1257-
DEBUG_PRINTF_P(PSTR("Using user config: type=%d, pin=%d, count=%d\n"),
1258-
(int)fallbackCfg.type, (int)fallbackCfg.pins[0], (int)fallbackCfg.count);
1259-
} else {
1260-
// Use complete defaults
1261-
uint8_t defPin[1] = {DEFAULT_LED_PIN};
1262-
fallbackCfg = BusConfig(DEFAULT_LED_TYPE, defPin, 0, DEFAULT_LED_COUNT, DEFAULT_LED_COLOR_ORDER, false, 0, RGBW_MODE_MANUAL_ONLY, 0);
1184+
// If memory exceeds limit, set count to default length and add anyway
1185+
if (mem + busMemUsage > MAX_LED_MEMORY) {
1186+
bus.count = DEFAULT_LED_COUNT;
1187+
DEBUG_PRINTF_P(PSTR("Bus %d memory usage exceeds limit, setting count to default %d\n"), (int)bus.type, DEFAULT_LED_COUNT);
12631188
}
12641189

1265-
BusManager::add(fallbackCfg);
1190+
if (BusManager::add(bus) != -1) {
1191+
mem += bus.memUsage(Bus::isDigital(bus.type) && !Bus::is2Pin(bus.type) ? digitalCount : 0);
1192+
if (Bus::isDigital(bus.type) && !Bus::is2Pin(bus.type)) digitalCount++;
1193+
} else break;
12661194
}
12671195

12681196
busConfigs.clear();

0 commit comments

Comments
 (0)