Skip to content

Commit b753069

Browse files
authored
fixed wrong topic, added more logging with identifier [SSDR]
1 parent aeb430d commit b753069

File tree

2 files changed

+101
-30
lines changed

2 files changed

+101
-30
lines changed

usermods/seven_segment_display_reloaded_v2/seven_segment_display_reloaded_v2.cpp

+89-27
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
bool colonsDone = false;
1717
bool lightDone = false;
1818
_setAllFalse();
19-
19+
2020
for (int index = 0; index < displayMaskLen; index++) {
2121
int timeVar = 0;
22-
switch (umSSDRDisplayMask[index]) {
22+
char c = umSSDRDisplayMask[index];
23+
switch (c) {
2324
case 'h':
2425
timeVar = hourFormat12(localTime);
2526
_showElements(&umSSDRHours, timeVar, 0, !umSSDRLeadingZero);
@@ -68,6 +69,7 @@
6869
lightDone = true;
6970
}
7071
break;
72+
_logWithPrefix("Unknown mask char '%c'", c);
7173
}
7274
}
7375
_setMaskToLeds();
@@ -205,15 +207,27 @@
205207
void UsermodSSDR::_publishMQTTint_P(const char *subTopic, int value)
206208
{
207209
#ifndef WLED_DISABLE_MQTT
210+
_logWithPrefix("Entering _publishMQTTint_P topic='%s' value=%d", subTopic, value);
208211
if (WLED_MQTT_CONNECTED) {
209-
if(mqtt == NULL) return;
212+
if(mqtt == NULL){
213+
_logWithPrefix("MQTT pointer NULL");
214+
return;
215+
}
210216

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+
}
216228
snprintf_P(valBuffer, sizeof(valBuffer), PSTR("%d"), value);
229+
230+
_logWithPrefix("Publishing INT to: '%s', value: '%s'", buffer, valBuffer);
217231
mqtt->publish(buffer, 2, true, valBuffer);
218232
}
219233
#endif
@@ -222,49 +236,84 @@
222236
void UsermodSSDR::_publishMQTTstr_P(const char *subTopic, String Value)
223237
{
224238
#ifndef WLED_DISABLE_MQTT
239+
_logWithPrefix("Entering _publishMQTTstr_P topic='%s' value='%s'", subTopic, Value.c_str());
225240
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());
231258
mqtt->publish(buffer, 2, true, Value.c_str(), Value.length());
232259
}
233260
#endif
234261
}
235262

236263
bool UsermodSSDR::_cmpIntSetting_P(char *topic, char *payload, const char *setting, void *value)
237264
{
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)
239270
{
271+
int oldValue = *((int *)value);
240272
*((int *)value) = strtol(payload, nullptr, 10); // Changed NULL to nullptr
273+
_logWithPrefix("Setting updated from %d to %d", oldValue, *((int *)value));
241274
_publishMQTTint_P(setting, *((int *)value));
242275
return true;
243276
}
244277
return false;
245278
}
246279

247280
bool UsermodSSDR::_handleSetting(char *topic, char *payload) {
281+
_logWithPrefix("Handling setting. Topic='%s', Payload='%s'", topic, payload);
282+
248283
if (_cmpIntSetting_P(topic, payload, _str_timeEnabled, &umSSDRDisplayTime)) {
284+
_logWithPrefix("Updated timeEnabled");
249285
return true;
250286
}
251287
if (_cmpIntSetting_P(topic, payload, _str_ldrEnabled, &umSSDREnableLDR)) {
288+
_logWithPrefix("Updated ldrEnabled");
252289
return true;
253290
}
254291
if (_cmpIntSetting_P(topic, payload, _str_inverted, &umSSDRInverted)) {
292+
_logWithPrefix("Updated inverted");
255293
return true;
256294
}
257295
if (_cmpIntSetting_P(topic, payload, _str_colonblink, &umSSDRColonblink)) {
296+
_logWithPrefix("Updated colonblink");
258297
return true;
259298
}
260299
if (_cmpIntSetting_P(topic, payload, _str_leadingZero, &umSSDRLeadingZero)) {
300+
_logWithPrefix("Updated leadingZero");
261301
return true;
262302
}
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+
263309
if (strcmp_P(topic, _str_displayMask) == 0) {
264310
umSSDRDisplayMask = String(payload);
311+
312+
_logWithPrefix("Updated displayMask to '%s'", umSSDRDisplayMask.c_str());
265313
_publishMQTTstr_P(_str_displayMask, umSSDRDisplayMask);
266314
return true;
267315
}
316+
_logWithPrefix("No matching setting found");
268317
return false;
269318
}
270319

@@ -329,7 +378,7 @@
329378
umSSDRMask = (bool*) calloc(umSSDRLength, sizeof(bool)); // Use calloc to initialize to 0
330379

331380
if (umSSDRMask == nullptr) {
332-
DEBUG_PRINTLN(F("Failed to allocate memory for SSDR mask"));
381+
_logWithPrefix("Failed to allocate memory for SSDR mask");
333382
return; // Early return on memory allocation failure
334383
}
335384
_setAllFalse();
@@ -340,7 +389,7 @@
340389
#ifdef USERMOD_BH1750
341390
bh1750 = (Usermod_BH1750*) UsermodManager::lookup(USERMOD_ID_BH1750);
342391
#endif
343-
DEBUG_PRINTLN(F("SSDR setup done"));
392+
_logWithPrefix("Setup done");
344393
}
345394

346395
// Clean up any allocated memory in the destructor
@@ -360,7 +409,7 @@
360409
}
361410

362411
#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) {
364413
float lux = -1; // Initialize lux with an invalid value
365414

366415
#ifdef USERMOD_SN_PHOTORESISTOR
@@ -385,9 +434,10 @@
385434
} else {
386435
brightness = map(constrainedLux, umSSDRLuxMin, umSSDRLuxMax, umSSDRBrightnessMax, umSSDRBrightnessMin);
387436
}
437+
_logWithPrefix("Lux=%.2f?brightness=%d",lux,br);
388438

389439
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);
391441
bri = brightness;
392442
stateUpdated(1);
393443
}
@@ -398,7 +448,7 @@
398448
}
399449

400450
void UsermodSSDR::handleOverlayDraw() {
401-
if (umSSDRDisplayTime && !disableUmLedControl && umSSDRMask != nullptr) {
451+
if (umSSDRDisplayTime && !externalLedOutputDisabled && umSSDRMask != nullptr) {
402452
_overlaySevenSegmentDraw();
403453
}
404454
}
@@ -482,24 +532,34 @@
482532
}
483533

484534
void UsermodSSDR::onMqttConnect(bool sessionPresent) {
535+
_logWithPrefix("onMqttConnect sessionPresent=%d", sessionPresent);
485536
if (!umSSDRDisplayTime) {
537+
_logWithPrefix("DisplayTime disabled, skipping subscribe");
486538
return;
487539
}
488540
#ifndef WLED_DISABLE_MQTT
489541
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+
491549
if (mqttDeviceTopic[0] != 0)
492550
{
493551
_updateMQTT();
494552
//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);
496555
mqtt->subscribe(subBuffer, 2);
497556
}
498557

499558
if (mqttGroupTopic[0] != 0)
500559
{
501560
//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);
503563
mqtt->subscribe(subBuffer, 2);
504564
}
505565
}
@@ -512,7 +572,9 @@
512572
//If topic begins with sevenSeg cut it off, otherwise not our message.
513573
size_t topicPrefixLen = strlen_P(PSTR("/wledSS/"));
514574
if (strncmp_P(topic, PSTR("/wledSS/"), topicPrefixLen) == 0) {
575+
_logWithPrefix("onMqttMessage topic='%s' payload='%s'", topic, payload);
515576
topic += topicPrefixLen;
577+
_logWithPrefix("Topic starts with /wledSS/, modified topic: '%s'", topic);
516578
} else {
517579
return false;
518580
}
@@ -526,7 +588,9 @@
526588
{
527589
//Trim /set and handle it
528590
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");
530594
}
531595
}
532596
return true;
@@ -543,8 +607,7 @@
543607
JsonObject top = root[FPSTR(_str_name)];
544608

545609
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));
548611
return false;
549612
}
550613
umSSDRDisplayTime = (top[FPSTR(_str_timeEnabled)] | umSSDRDisplayTime);
@@ -567,8 +630,7 @@
567630
umSSDRLuxMax = top[FPSTR(_str_luxMax)] | umSSDRLuxMax;
568631
umSSDRInvertAutoBrightness = top[FPSTR(_str_invertAutoBrightness)] | umSSDRInvertAutoBrightness;
569632

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));
572634

573635
return true;
574636
}

usermods/seven_segment_display_reloaded_v2/seven_segment_display_reloaded_v2.h

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#pragma once
22
#include "wled.h"
33

4+
// logging macro:
5+
#define _logWithPrefix(fmt, ...) \
6+
DEBUG_PRINTF("[SSDR] " fmt "\n", ##__VA_ARGS__)
7+
48
//#define REFRESHTIME 497
59

610
class UsermodSSDR : public Usermod {
@@ -157,7 +161,7 @@ class UsermodSSDR : public Usermod {
157161
String umSSDRYears = umSSDR_YEARS;
158162

159163
bool* umSSDRMask = nullptr;
160-
bool disableUmLedControl = false;
164+
bool externalLedOutputDisabled = false;
161165

162166
// Forward declarations of private methods
163167
void _overlaySevenSegmentDraw();
@@ -224,10 +228,15 @@ class UsermodSSDR : public Usermod {
224228
uint16_t getId() override;
225229

226230
// Public function that can be called from other usermods
231+
/**
232+
* Allows external usermods to temporarily disable the LED output of this usermod.
233+
* This is useful when multiple usermods might be trying to control the same LEDs.
234+
* @param state true to disable LED output, false to enable
235+
*/
227236
void disableOutputFunction(bool state) {
228-
disableUmLedControl = state;
237+
externalLedOutputDisabled = state;
229238
#ifdef DEBUG_PRINTF
230-
DEBUG_PRINTF("disableOutputFunction was triggered by an external Usermod: %s\n", disableUmLedControl ? "true" : "false");
239+
_logWithPrefix("disableOutputFunction was triggered by an external Usermod: %s\n", externalLedOutputDisabled ? "true" : "false");
231240
#endif
232241
}
233242

0 commit comments

Comments
 (0)