|
16 | 16 | bool colonsDone = false;
|
17 | 17 | bool lightDone = false;
|
18 | 18 | _setAllFalse();
|
19 |
| - |
| 19 | + |
20 | 20 | for (int index = 0; index < displayMaskLen; index++) {
|
21 | 21 | int timeVar = 0;
|
22 |
| - switch (umSSDRDisplayMask[index]) { |
| 22 | + char c = umSSDRDisplayMask[index]; |
| 23 | + switch (c) { |
23 | 24 | case 'h':
|
24 | 25 | timeVar = hourFormat12(localTime);
|
25 | 26 | _showElements(&umSSDRHours, timeVar, 0, !umSSDRLeadingZero);
|
|
68 | 69 | lightDone = true;
|
69 | 70 | }
|
70 | 71 | break;
|
| 72 | + _logWithPrefix("Unknown mask char '%c'", c); |
71 | 73 | }
|
72 | 74 | }
|
73 | 75 | _setMaskToLeds();
|
|
205 | 207 | void UsermodSSDR::_publishMQTTint_P(const char *subTopic, int value)
|
206 | 208 | {
|
207 | 209 | #ifndef WLED_DISABLE_MQTT
|
| 210 | + _logWithPrefix("Entering _publishMQTTint_P topic='%s' value=%d", subTopic, value); |
208 | 211 | if (WLED_MQTT_CONNECTED) {
|
209 |
| - if(mqtt == NULL) return; |
| 212 | + if(mqtt == NULL){ |
| 213 | + _logWithPrefix("MQTT pointer NULL"); |
| 214 | + return; |
| 215 | + } |
210 | 216 |
|
211 |
| - char buffer[64]; |
212 |
| - char valBuffer[12]; |
213 |
| - int result = snprintf_P(buffer, sizeof(buffer), PSTR("%s/%S/%S"), mqttDeviceTopic, _str_name, subTopic); |
214 |
| - if (result < 0 || result >= sizeof(buffer)) return; // Buffer overflow check |
215 |
| - |
| 217 | + char buffer[64], nameBuffer[30], topicBuffer[30], valBuffer[12]; |
| 218 | + |
| 219 | + // Copy PROGMEM strings to local buffers |
| 220 | + strlcpy(nameBuffer, reinterpret_cast<const char *>(_str_name), sizeof(nameBuffer)); |
| 221 | + strlcpy(topicBuffer, reinterpret_cast<const char *>(subTopic), sizeof(topicBuffer)); |
| 222 | + |
| 223 | + int result = snprintf(buffer, sizeof(buffer), "%s/%s/%s", mqttDeviceTopic, nameBuffer, topicBuffer); |
| 224 | + if (result < 0 || result >= sizeof(buffer)) { |
| 225 | + _logWithPrefix("Buffer overflow in _publishMQTTint_P"); |
| 226 | + return; // Buffer overflow check |
| 227 | + } |
216 | 228 | snprintf_P(valBuffer, sizeof(valBuffer), PSTR("%d"), value);
|
| 229 | + |
| 230 | + _logWithPrefix("Publishing INT to: '%s', value: '%s'", buffer, valBuffer); |
217 | 231 | mqtt->publish(buffer, 2, true, valBuffer);
|
218 | 232 | }
|
219 | 233 | #endif
|
|
222 | 236 | void UsermodSSDR::_publishMQTTstr_P(const char *subTopic, String Value)
|
223 | 237 | {
|
224 | 238 | #ifndef WLED_DISABLE_MQTT
|
| 239 | + _logWithPrefix("Entering _publishMQTTstr_P topic='%s' value='%s'", subTopic, Value.c_str()); |
225 | 240 | if (WLED_MQTT_CONNECTED) {
|
226 |
| - if(mqtt == NULL) return; |
227 |
| - char buffer[64]; |
228 |
| - int result = snprintf_P(buffer, sizeof(buffer), PSTR("%s/%S/%S"), mqttDeviceTopic, _str_name, subTopic); |
229 |
| - if (result < 0 || result >= sizeof(buffer)) return; // Buffer overflow check |
230 |
| - |
| 241 | + if(mqtt == NULL){ |
| 242 | + _logWithPrefix("MQTT pointer NULL"); |
| 243 | + return; |
| 244 | + } |
| 245 | + |
| 246 | + char buffer[64], nameBuffer[30], topicBuffer[30]; |
| 247 | + |
| 248 | + strlcpy(nameBuffer, reinterpret_cast<const char *>(_str_name), sizeof(nameBuffer)); |
| 249 | + strlcpy(topicBuffer, reinterpret_cast<const char *>(subTopic), sizeof(topicBuffer)); |
| 250 | + |
| 251 | + int result = snprintf(buffer, sizeof(buffer), "%s/%s/%s", mqttDeviceTopic, nameBuffer, topicBuffer); |
| 252 | + if (result < 0 || result >= sizeof(buffer)) { |
| 253 | + _logWithPrefix("Buffer overflow in _publishMQTTstr_P"); |
| 254 | + return; // Buffer overflow check |
| 255 | + } |
| 256 | + |
| 257 | + _logWithPrefix("Publishing STR to: '%s', value: '%s'", buffer, Value.c_str()); |
231 | 258 | mqtt->publish(buffer, 2, true, Value.c_str(), Value.length());
|
232 | 259 | }
|
233 | 260 | #endif
|
234 | 261 | }
|
235 | 262 |
|
236 | 263 | bool UsermodSSDR::_cmpIntSetting_P(char *topic, char *payload, const char *setting, void *value)
|
237 | 264 | {
|
238 |
| - if (strcmp_P(topic, setting) == 0) |
| 265 | + _logWithPrefix("._cmpIntSetting topic='%s' payload='%s' setting='%s'", topic, payload, reinterpret_cast<const char*>(setting)); |
| 266 | + char settingBuffer[30]; |
| 267 | + strlcpy(settingBuffer, reinterpret_cast<const char *>(setting), sizeof(settingBuffer)); |
| 268 | + |
| 269 | + if (strcmp_P(topic, settingBuffer) == 0) |
239 | 270 | {
|
| 271 | + int oldValue = *((int *)value); |
240 | 272 | *((int *)value) = strtol(payload, nullptr, 10); // Changed NULL to nullptr
|
| 273 | + _logWithPrefix("Setting updated from %d to %d", oldValue, *((int *)value)); |
241 | 274 | _publishMQTTint_P(setting, *((int *)value));
|
242 | 275 | return true;
|
243 | 276 | }
|
244 | 277 | return false;
|
245 | 278 | }
|
246 | 279 |
|
247 | 280 | bool UsermodSSDR::_handleSetting(char *topic, char *payload) {
|
| 281 | + _logWithPrefix("Handling setting. Topic='%s', Payload='%s'", topic, payload); |
| 282 | + |
248 | 283 | if (_cmpIntSetting_P(topic, payload, _str_timeEnabled, &umSSDRDisplayTime)) {
|
| 284 | + _logWithPrefix("Updated timeEnabled"); |
249 | 285 | return true;
|
250 | 286 | }
|
251 | 287 | if (_cmpIntSetting_P(topic, payload, _str_ldrEnabled, &umSSDREnableLDR)) {
|
| 288 | + _logWithPrefix("Updated ldrEnabled"); |
252 | 289 | return true;
|
253 | 290 | }
|
254 | 291 | if (_cmpIntSetting_P(topic, payload, _str_inverted, &umSSDRInverted)) {
|
| 292 | + _logWithPrefix("Updated inverted"); |
255 | 293 | return true;
|
256 | 294 | }
|
257 | 295 | if (_cmpIntSetting_P(topic, payload, _str_colonblink, &umSSDRColonblink)) {
|
| 296 | + _logWithPrefix("Updated colonblink"); |
258 | 297 | return true;
|
259 | 298 | }
|
260 | 299 | if (_cmpIntSetting_P(topic, payload, _str_leadingZero, &umSSDRLeadingZero)) {
|
| 300 | + _logWithPrefix("Updated leadingZero"); |
261 | 301 | return true;
|
262 | 302 | }
|
| 303 | + |
| 304 | + char displayMaskBuffer[30]; |
| 305 | + strlcpy(displayMaskBuffer, reinterpret_cast<const char *>(_str_displayMask), sizeof(displayMaskBuffer)); |
| 306 | + |
| 307 | + _logWithPrefix("Comparing '%s' with '%s'", topic, displayMaskBuffer); |
| 308 | + |
263 | 309 | if (strcmp_P(topic, _str_displayMask) == 0) {
|
264 | 310 | umSSDRDisplayMask = String(payload);
|
| 311 | + |
| 312 | + _logWithPrefix("Updated displayMask to '%s'", umSSDRDisplayMask.c_str()); |
265 | 313 | _publishMQTTstr_P(_str_displayMask, umSSDRDisplayMask);
|
266 | 314 | return true;
|
267 | 315 | }
|
| 316 | + _logWithPrefix("No matching setting found"); |
268 | 317 | return false;
|
269 | 318 | }
|
270 | 319 |
|
|
329 | 378 | umSSDRMask = (bool*) calloc(umSSDRLength, sizeof(bool)); // Use calloc to initialize to 0
|
330 | 379 |
|
331 | 380 | if (umSSDRMask == nullptr) {
|
332 |
| - DEBUG_PRINTLN(F("Failed to allocate memory for SSDR mask")); |
| 381 | + _logWithPrefix("Failed to allocate memory for SSDR mask"); |
333 | 382 | return; // Early return on memory allocation failure
|
334 | 383 | }
|
335 | 384 | _setAllFalse();
|
|
340 | 389 | #ifdef USERMOD_BH1750
|
341 | 390 | bh1750 = (Usermod_BH1750*) UsermodManager::lookup(USERMOD_ID_BH1750);
|
342 | 391 | #endif
|
343 |
| - DEBUG_PRINTLN(F("SSDR setup done")); |
| 392 | + _logWithPrefix("Setup done"); |
344 | 393 | }
|
345 | 394 |
|
346 | 395 | // Clean up any allocated memory in the destructor
|
|
360 | 409 | }
|
361 | 410 |
|
362 | 411 | #if defined(USERMOD_SN_PHOTORESISTOR) || defined(USERMOD_BH1750)
|
363 |
| - if(bri != 0 && umSSDREnableLDR && (millis() - umSSDRLastRefresh > umSSDRRefreshTime) && !disableUmLedControl) { |
| 412 | + if(bri != 0 && umSSDREnableLDR && (millis() - umSSDRLastRefresh > umSSDRRefreshTime) && !externalLedOutputDisabled) { |
364 | 413 | float lux = -1; // Initialize lux with an invalid value
|
365 | 414 |
|
366 | 415 | #ifdef USERMOD_SN_PHOTORESISTOR
|
|
385 | 434 | } else {
|
386 | 435 | brightness = map(constrainedLux, umSSDRLuxMin, umSSDRLuxMax, umSSDRBrightnessMax, umSSDRBrightnessMin);
|
387 | 436 | }
|
| 437 | + _logWithPrefix("Lux=%.2f?brightness=%d",lux,br); |
388 | 438 |
|
389 | 439 | if (bri != brightness) {
|
390 |
| - DEBUG_PRINTF("Adjusting brightness based on lux value: %.2f lx, new brightness: %d\n", lux, brightness); |
| 440 | + _logWithPrefix("Adjusting brightness based on lux value: %.2f lx, new brightness: %d", lux, brightness); |
391 | 441 | bri = brightness;
|
392 | 442 | stateUpdated(1);
|
393 | 443 | }
|
|
398 | 448 | }
|
399 | 449 |
|
400 | 450 | void UsermodSSDR::handleOverlayDraw() {
|
401 |
| - if (umSSDRDisplayTime && !disableUmLedControl && umSSDRMask != nullptr) { |
| 451 | + if (umSSDRDisplayTime && !externalLedOutputDisabled && umSSDRMask != nullptr) { |
402 | 452 | _overlaySevenSegmentDraw();
|
403 | 453 | }
|
404 | 454 | }
|
|
482 | 532 | }
|
483 | 533 |
|
484 | 534 | void UsermodSSDR::onMqttConnect(bool sessionPresent) {
|
| 535 | + _logWithPrefix("onMqttConnect sessionPresent=%d", sessionPresent); |
485 | 536 | if (!umSSDRDisplayTime) {
|
| 537 | + _logWithPrefix("DisplayTime disabled, skipping subscribe"); |
486 | 538 | return;
|
487 | 539 | }
|
488 | 540 | #ifndef WLED_DISABLE_MQTT
|
489 | 541 | if (WLED_MQTT_CONNECTED) {
|
490 |
| - char subBuffer[48]; |
| 542 | + char subBuffer[48], nameBuffer[30]; |
| 543 | + |
| 544 | + // Copy PROGMEM string to local buffer |
| 545 | + strlcpy(nameBuffer, reinterpret_cast<const char *>(_str_name), sizeof(nameBuffer)); |
| 546 | + |
| 547 | + _logWithPrefix("Connected. DeviceTopic='%s', GroupTopic='%s'", mqttDeviceTopic, mqttGroupTopic); |
| 548 | + |
491 | 549 | if (mqttDeviceTopic[0] != 0)
|
492 | 550 | {
|
493 | 551 | _updateMQTT();
|
494 | 552 | //subscribe for sevenseg messages on the device topic
|
495 |
| - sprintf_P(subBuffer, PSTR("%s/%S/+/set"), mqttDeviceTopic, _str_name); |
| 553 | + sprintf(subBuffer, "%s/%s/+/set", mqttDeviceTopic, nameBuffer); |
| 554 | + _logWithPrefix("Subscribing to device topic: '%s'", subBuffer); |
496 | 555 | mqtt->subscribe(subBuffer, 2);
|
497 | 556 | }
|
498 | 557 |
|
499 | 558 | if (mqttGroupTopic[0] != 0)
|
500 | 559 | {
|
501 | 560 | //subscribe for sevenseg messages on the group topic
|
502 |
| - sprintf_P(subBuffer, PSTR("%s/%S/+/set"), mqttGroupTopic, _str_name); |
| 561 | + sprintf(subBuffer, "%s/%s/+/set", mqttGroupTopic, nameBuffer); |
| 562 | + _logWithPrefix("Subscribing to group topic: '%s'", subBuffer); |
503 | 563 | mqtt->subscribe(subBuffer, 2);
|
504 | 564 | }
|
505 | 565 | }
|
|
512 | 572 | //If topic begins with sevenSeg cut it off, otherwise not our message.
|
513 | 573 | size_t topicPrefixLen = strlen_P(PSTR("/wledSS/"));
|
514 | 574 | if (strncmp_P(topic, PSTR("/wledSS/"), topicPrefixLen) == 0) {
|
| 575 | + _logWithPrefix("onMqttMessage topic='%s' payload='%s'", topic, payload); |
515 | 576 | topic += topicPrefixLen;
|
| 577 | + _logWithPrefix("Topic starts with /wledSS/, modified topic: '%s'", topic); |
516 | 578 | } else {
|
517 | 579 | return false;
|
518 | 580 | }
|
|
526 | 588 | {
|
527 | 589 | //Trim /set and handle it
|
528 | 590 | topic[topicLen - 4] = '\0';
|
529 |
| - _handleSetting(topic, payload); |
| 591 | + _logWithPrefix("Processing setting. Setting='%s', Value='%s'", topic, payload); |
| 592 | + bool result = _handleSetting(topic, payload); |
| 593 | + _logWithPrefix("Handling result: %s", result ? "success" : "failure"); |
530 | 594 | }
|
531 | 595 | }
|
532 | 596 | return true;
|
|
543 | 607 | JsonObject top = root[FPSTR(_str_name)];
|
544 | 608 |
|
545 | 609 | if (top.isNull()) {
|
546 |
| - DEBUG_PRINT(FPSTR(_str_name)); |
547 |
| - DEBUG_PRINTLN(F(": No config found. (Using defaults.)")); |
| 610 | + _logWithPrefix("%s: No config found. (Using defaults.)", reinterpret_cast<const char*>(_str_name)); |
548 | 611 | return false;
|
549 | 612 | }
|
550 | 613 | umSSDRDisplayTime = (top[FPSTR(_str_timeEnabled)] | umSSDRDisplayTime);
|
|
567 | 630 | umSSDRLuxMax = top[FPSTR(_str_luxMax)] | umSSDRLuxMax;
|
568 | 631 | umSSDRInvertAutoBrightness = top[FPSTR(_str_invertAutoBrightness)] | umSSDRInvertAutoBrightness;
|
569 | 632 |
|
570 |
| - DEBUG_PRINT(FPSTR(_str_name)); |
571 |
| - DEBUG_PRINTLN(F(" config (re)loaded.")); |
| 633 | + _logWithPrefix("%s: config (re)loaded.)", reinterpret_cast<const char*>(_str_name)); |
572 | 634 |
|
573 | 635 | return true;
|
574 | 636 | }
|
|
0 commit comments