Skip to content

Commit 445b6ee

Browse files
author
Gregory Schmidt
committed
Fix MQTT Null publish
1 parent 0327f94 commit 445b6ee

File tree

1 file changed

+41
-35
lines changed

1 file changed

+41
-35
lines changed

usermods/seven_segment_display/usermod_v2_seven_segment_display.h

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ class SevenSegmentDisplay : public Usermod
2222
// set your config variables to their boot default value (this can also be done in readFromConfig() or a constructor if you prefer)
2323
int ssLEDPerSegment = 1; //The number of LEDs in each segment of the 7 seg (total per digit is 7 * ssLedPerSegment)
2424
int ssLEDPerPeriod = 1; //A Period will have 1x and a Colon will have 2x
25-
int ssStartLED = 0; //The pixel that the display starts at.
25+
int ssStartLED = 0; //The pixel that the display starts at.
2626
/* HH - 0-23. hh - 1-12, kk - 1-24 hours
2727
// MM or mm - 0-59 minutes
2828
// SS or ss = 0-59 seconds
2929
// : for a colon
3030
// All others for alpha numeric, (will be blank when displaying time)
3131
*/
32-
String ssDisplayMask = "HHMMSS"; //Physical Display Mask, this should reflect physical equipment.
32+
String ssDisplayMask = "HHMMSS"; //Physical Display Mask, this should reflect physical equipment.
3333
/* ssDisplayConfig
3434
// -------
3535
// / A / 0 - EDCGFAB
@@ -44,7 +44,7 @@ class SevenSegmentDisplay : public Usermod
4444
*/
4545
int ssDisplayConfig = 5; //Physical configuration of the Seven segment display
4646
String ssDisplayMessage = "testing123";
47-
bool ssTimeEnabled = true; //If not, display message.
47+
bool ssTimeEnabled = true; //If not, display message.
4848
unsigned int ssScrollSpeed = 1000; //Time between advancement of extended message scrolling, in milliseconds.
4949

5050
//String to reduce flash memory usage
@@ -60,7 +60,6 @@ class SevenSegmentDisplay : public Usermod
6060
static const char _str_subFormat[];
6161
static const char _str_topicFormat[];
6262

63-
6463
unsigned long _overlaySevenSegmentProcess()
6564
{
6665
//Do time for now.
@@ -120,7 +119,7 @@ class SevenSegmentDisplay : public Usermod
120119
int len = static_cast<int>(ssDisplayMessage.length());
121120
if (ssDisplayMessageIdx > len)
122121
{
123-
//If it has scrolled the whole message, reset it.
122+
//If it has scrolled the whole message, reset it.
124123
setSevenSegmentMessage(ssDisplayMessage);
125124
return REFRESHTIME;
126125
}
@@ -186,7 +185,7 @@ class SevenSegmentDisplay : public Usermod
186185
{
187186
for (int numPerSeg = 0; numPerSeg < ssLEDPerSegment; numPerSeg++)
188187
{
189-
strip.setPixelColor(indexLED+numPerSeg, 0x000000);
188+
strip.setPixelColor(indexLED + numPerSeg, 0x000000);
190189
}
191190
}
192191
indexLED += ssLEDPerSegment;
@@ -267,22 +266,26 @@ class SevenSegmentDisplay : public Usermod
267266

268267
return result;
269268
}
270-
271-
void _publishMQTTint_P(const char* subTopic, int value)
269+
270+
void _publishMQTTint_P(const char *subTopic, int value)
272271
{
272+
if(mqtt == NULL) return;
273+
273274
char buffer[64];
274275
char valBuffer[12];
275276
sprintf_P(buffer, PSTR("%s/sevenSeg/%S"), mqttDeviceTopic, subTopic);
276-
Serial.println(buffer);
277277
sprintf_P(valBuffer, PSTR("%d"), value);
278278
mqtt->publish(buffer, 2, true, valBuffer);
279279
}
280-
void _publishMQTTstr_P(const char* subTopic, String Value)
280+
281+
void _publishMQTTstr_P(const char *subTopic, String Value)
281282
{
283+
if(mqtt == NULL) return;
282284
char buffer[64];
283285
sprintf_P(buffer, PSTR("%s/sevenSeg/%S"), mqttDeviceTopic, subTopic);
284286
mqtt->publish(buffer, 2, true, Value.c_str(), Value.length());
285287
}
288+
286289
void _updateMQTT()
287290
{
288291
_publishMQTTint_P(_str_perSegment, ssLEDPerSegment);
@@ -295,15 +298,18 @@ class SevenSegmentDisplay : public Usermod
295298
_publishMQTTstr_P(_str_displayMask, ssDisplayMask);
296299
_publishMQTTstr_P(_str_displayMsg, ssDisplayMessage);
297300
}
298-
bool _cmpIntSetting_P(char* topic, char* payload, const char* setting, void* value){
299-
if(strcmp_P(topic, setting) == 0)
301+
302+
bool _cmpIntSetting_P(char *topic, char *payload, const char *setting, void *value)
303+
{
304+
if (strcmp_P(topic, setting) == 0)
300305
{
301-
*((int*)value) = strtol(payload, NULL, 10);
302-
_publishMQTTint_P(setting, *((int*)value));
306+
*((int *)value) = strtol(payload, NULL, 10);
307+
_publishMQTTint_P(setting, *((int *)value));
303308
return true;
304309
}
305310
return false;
306311
}
312+
307313
bool _handleSetting(char *topic, char *payload)
308314
{
309315
if (_cmpIntSetting_P(topic, payload, _str_perSegment, &ssLEDPerSegment))
@@ -318,14 +324,14 @@ class SevenSegmentDisplay : public Usermod
318324
return true;
319325
if (_cmpIntSetting_P(topic, payload, _str_scrollSpd, &ssScrollSpeed))
320326
return true;
321-
if(strcmp_P(topic, _str_displayMask)==0)
327+
if (strcmp_P(topic, _str_displayMask) == 0)
322328
{
323329
ssDisplayMask = String(payload);
324330
ssDisplayBuffer = ssDisplayMask;
325331
_publishMQTTstr_P(_str_displayMask, ssDisplayMask);
326332
return true;
327333
}
328-
if(strcmp_P(topic, _str_displayMsg)==0)
334+
if (strcmp_P(topic, _str_displayMsg) == 0)
329335
{
330336
setSevenSegmentMessage(String(payload));
331337
return true;
@@ -347,9 +353,9 @@ class SevenSegmentDisplay : public Usermod
347353
ssDisplayMessageIdx = -ssDisplayMask.length();
348354
else
349355
ssDisplayMessageIdx = 0;
350-
356+
351357
//If the message isn't the same, update runtime/mqtt (most calls will be resetting message scroll)
352-
if(!ssDisplayMessage.equals(message))
358+
if (!ssDisplayMessage.equals(message))
353359
{
354360
_publishMQTTstr_P(_str_displayMsg, message);
355361
ssDisplayMessage = message;
@@ -424,17 +430,17 @@ class SevenSegmentDisplay : public Usermod
424430
//Trim /set and handle it
425431
topic[topicLen - 4] = '\0';
426432
_handleSetting(topic, payload);
427-
428433
}
429434
return true;
430435
}
431436

432-
void addToConfig(JsonObject& root)
437+
void addToConfig(JsonObject &root)
433438
{
434439
JsonObject top = root[FPSTR(_str_sevenSeg)];
435-
if (top.isNull()) {
436-
top = root.createNestedObject(FPSTR(_str_sevenSeg));
437-
}
440+
if (top.isNull())
441+
{
442+
top = root.createNestedObject(FPSTR(_str_sevenSeg));
443+
}
438444
top[FPSTR(_str_perSegment)] = ssLEDPerSegment;
439445
top[FPSTR(_str_perPeriod)] = ssLEDPerPeriod;
440446
top[FPSTR(_str_startIdx)] = ssStartLED;
@@ -445,14 +451,15 @@ class SevenSegmentDisplay : public Usermod
445451
top[FPSTR(_str_scrollSpd)] = ssScrollSpeed;
446452
}
447453

448-
bool readFromConfig(JsonObject& root)
454+
bool readFromConfig(JsonObject &root)
449455
{
450456
JsonObject top = root[FPSTR(_str_sevenSeg)];
451457

452458
bool configComplete = !top.isNull();
453459

454460
//if sevenseg section doesn't exist return
455-
if(!configComplete) return configComplete;
461+
if (!configComplete)
462+
return configComplete;
456463

457464
configComplete &= getJsonValue(top[FPSTR(_str_perSegment)], ssLEDPerSegment);
458465
configComplete &= getJsonValue(top[FPSTR(_str_perPeriod)], ssLEDPerPeriod);
@@ -466,7 +473,6 @@ class SevenSegmentDisplay : public Usermod
466473

467474
configComplete &= getJsonValue(top[FPSTR(_str_timeEnabled)], ssTimeEnabled);
468475
configComplete &= getJsonValue(top[FPSTR(_str_scrollSpd)], ssScrollSpeed);
469-
470476
return configComplete;
471477
}
472478

@@ -480,12 +486,12 @@ class SevenSegmentDisplay : public Usermod
480486
}
481487
};
482488

483-
const char SevenSegmentDisplay::_str_perSegment[] PROGMEM = "perSegment";
484-
const char SevenSegmentDisplay::_str_perPeriod[] PROGMEM = "perPeriod";
485-
const char SevenSegmentDisplay::_str_startIdx[] PROGMEM = "startIdx";
486-
const char SevenSegmentDisplay::_str_displayCfg[] PROGMEM = "displayCfg";
487-
const char SevenSegmentDisplay::_str_timeEnabled[] PROGMEM = "timeEnabled";
488-
const char SevenSegmentDisplay::_str_scrollSpd[] PROGMEM = "scrollSpd";
489-
const char SevenSegmentDisplay::_str_displayMask[] PROGMEM = "displayMask";
490-
const char SevenSegmentDisplay::_str_displayMsg[] PROGMEM = "displayMsg";
491-
const char SevenSegmentDisplay::_str_sevenSeg[] PROGMEM = "sevenSeg";
489+
const char SevenSegmentDisplay::_str_perSegment[] PROGMEM = "perSegment";
490+
const char SevenSegmentDisplay::_str_perPeriod[] PROGMEM = "perPeriod";
491+
const char SevenSegmentDisplay::_str_startIdx[] PROGMEM = "startIdx";
492+
const char SevenSegmentDisplay::_str_displayCfg[] PROGMEM = "displayCfg";
493+
const char SevenSegmentDisplay::_str_timeEnabled[] PROGMEM = "timeEnabled";
494+
const char SevenSegmentDisplay::_str_scrollSpd[] PROGMEM = "scrollSpd";
495+
const char SevenSegmentDisplay::_str_displayMask[] PROGMEM = "displayMask";
496+
const char SevenSegmentDisplay::_str_displayMsg[] PROGMEM = "displayMsg";
497+
const char SevenSegmentDisplay::_str_sevenSeg[] PROGMEM = "sevenSeg";

0 commit comments

Comments
 (0)