Skip to content

Commit d47157e

Browse files
author
Gregory Schmidt
committed
Refactor string usage
1 parent 6cd770b commit d47157e

File tree

1 file changed

+108
-91
lines changed

1 file changed

+108
-91
lines changed

usermods/seven_segment_display/usermod_v2_seven_segment_display.h

Lines changed: 108 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ class SevenSegmentDisplay : public Usermod
1111
//Runtime variables.
1212
unsigned long lastRefresh = 0;
1313
unsigned long lastCharacterStep = 0;
14-
//char ssDisplayBuffer[WLED_SS_BUFFLEN+1]; //Runtime buffer of what should be displayed.
1514
String ssDisplayBuffer = "";
1615
char ssCharacterMask[36] = {0x77, 0x11, 0x6B, 0x3B, 0x1D, 0x3E, 0x7E, 0x13, 0x7F, 0x1F, 0x5F, 0x7C, 0x66, 0x79, 0x6E, 0x4E, 0x76, 0x5D, 0x44, 0x71, 0x5E, 0x64, 0x27, 0x58, 0x77, 0x4F, 0x1F, 0x48, 0x3E, 0x6C, 0x75, 0x25, 0x7D, 0x2A, 0x3D, 0x6B};
1716
int ssDisplayMessageIdx = 0; //Position of the start of the message to be physically displayed.
@@ -30,8 +29,7 @@ class SevenSegmentDisplay : public Usermod
3029
// : for a colon
3130
// All others for alpha numeric, (will be blank when displaying time)
3231
*/
33-
//char ssDisplayMask[WLED_SS_BUFFLEN+1] = "HHMMSS"; //Physical Display Mask, this should reflect physical equipment.
34-
String ssDisplayMask = "HHMMSS";
32+
String ssDisplayMask = "HHMMSS"; //Physical Display Mask, this should reflect physical equipment.
3533
/* ssDisplayConfig
3634
// -------
3735
// / A / 0 - EDCGFAB
@@ -47,7 +45,21 @@ class SevenSegmentDisplay : public Usermod
4745
int ssDisplayConfig = 5; //Physical configuration of the Seven segment display
4846
String ssDisplayMessage = "testing123";
4947
bool ssTimeEnabled = true; //If not, display message.
50-
unsigned long ssScrollSpeed = 1000; //Time between advancement of extended message scrolling, in milliseconds.
48+
unsigned int ssScrollSpeed = 1000; //Time between advancement of extended message scrolling, in milliseconds.
49+
50+
//String to reduce flash memory usage
51+
static const char _str_perSegment[];
52+
static const char _str_perPeriod[];
53+
static const char _str_startIdx[];
54+
static const char _str_displayCfg[];
55+
static const char _str_timeEnabled[];
56+
static const char _str_scrollSpd[];
57+
static const char _str_displayMask[];
58+
static const char _str_displayMsg[];
59+
static const char _str_sevenSeg[];
60+
static const char _str_subFormat[];
61+
static const char _str_topicFormat[];
62+
5163

5264
unsigned long _overlaySevenSegmentProcess()
5365
{
@@ -134,7 +146,7 @@ class SevenSegmentDisplay : public Usermod
134146
{
135147

136148
//Start pixels at ssStartLED, Use ssLEDPerSegment, ssLEDPerPeriod, ssDisplayBuffer
137-
int indexLED = 0;
149+
int indexLED = ssStartLED;
138150
int displayMaskLen = static_cast<int>(ssDisplayMask.length());
139151
for (int indexBuffer = 0; indexBuffer < displayMaskLen; indexBuffer++)
140152
{
@@ -174,7 +186,7 @@ class SevenSegmentDisplay : public Usermod
174186
{
175187
for (int numPerSeg = 0; numPerSeg < ssLEDPerSegment; numPerSeg++)
176188
{
177-
strip.setPixelColor(indexLED, 0x000000);
189+
strip.setPixelColor(indexLED+numPerSeg, 0x000000);
178190
}
179191
}
180192
indexLED += ssLEDPerSegment;
@@ -183,13 +195,22 @@ class SevenSegmentDisplay : public Usermod
183195

184196
char _overlaySevenSegmentGetCharMask(char var)
185197
{
186-
//ssCharacterMask
187-
if (var > 0x60) //Essentially a "toLower" call.
188-
var -= 0x20;
189-
if (var > 0x39) //Meaning it is a non-numeric
190-
var -= 0x07;
191-
var -= 0x30; //Shift ascii down to start numeric 0 at index 0.
192-
198+
if (var >= 0x30 && var <= 0x39)
199+
{ /*If its a number, shift to index 0.*/
200+
var -= 0x30;
201+
}
202+
else if (var >= 0x41 && var <= 0x5a)
203+
{ /*If its an Upper case, shift to index 0xA.*/
204+
var -= 0x37;
205+
}
206+
else if (var >= 0x61 && var <= 0x7A)
207+
{ /*If its a lower case, shift to index 0xA.*/
208+
var -= 0x57;
209+
}
210+
else
211+
{ /* Else unsupported, return 0; */
212+
return 0;
213+
}
193214
char mask = ssCharacterMask[static_cast<int>(var)];
194215
/*
195216
0 - EDCGFAB
@@ -246,84 +267,70 @@ class SevenSegmentDisplay : public Usermod
246267

247268
return result;
248269
}
249-
void _publishMQTTint(const char* subTopic, int value)
270+
271+
void _publishMQTTint_P(const char* subTopic, int value)
250272
{
251273
char buffer[64];
252274
char valBuffer[12];
253-
sprintf_P(buffer, PSTR("%s/sevenSeg/%s"), mqttDeviceTopic, subTopic);
275+
sprintf_P(buffer, PSTR("%s/sevenSeg/%S"), mqttDeviceTopic, subTopic);
276+
Serial.println(buffer);
254277
sprintf_P(valBuffer, PSTR("%d"), value);
255278
mqtt->publish(buffer, 2, true, valBuffer);
256279
}
257-
void _publishMQTTstr(const char* subTopic, String Value)
280+
void _publishMQTTstr_P(const char* subTopic, String Value)
258281
{
259282
char buffer[64];
260-
sprintf_P(buffer, PSTR("%s/sevenSeg/%s"), mqttDeviceTopic, subTopic);
283+
sprintf_P(buffer, PSTR("%s/sevenSeg/%S"), mqttDeviceTopic, subTopic);
261284
mqtt->publish(buffer, 2, true, Value.c_str(), Value.length());
262285
}
263286
void _updateMQTT()
264287
{
265-
_publishMQTTint(PSTR("perSegment"), ssLEDPerSegment);
266-
_publishMQTTint(PSTR("perPeriod"), ssLEDPerPeriod);
267-
_publishMQTTint(PSTR("startIdx"), ssStartLED);
268-
_publishMQTTint(PSTR("displayCfg"), ssDisplayConfig);
269-
_publishMQTTint(PSTR("timeEnable"), ssTimeEnabled);
270-
_publishMQTTint(PSTR("scrollSpd"), ssScrollSpeed);
271-
272-
_publishMQTTstr(PSTR("displayMask"), ssDisplayMask);
273-
_publishMQTTstr(PSTR("displayMsg"), ssDisplayMessage);
288+
_publishMQTTint_P(_str_perSegment, ssLEDPerSegment);
289+
_publishMQTTint_P(_str_perPeriod, ssLEDPerPeriod);
290+
_publishMQTTint_P(_str_startIdx, ssStartLED);
291+
_publishMQTTint_P(_str_displayCfg, ssDisplayConfig);
292+
_publishMQTTint_P(_str_timeEnabled, ssTimeEnabled);
293+
_publishMQTTint_P(_str_scrollSpd, ssScrollSpeed);
294+
295+
_publishMQTTstr_P(_str_displayMask, ssDisplayMask);
296+
_publishMQTTstr_P(_str_displayMsg, ssDisplayMessage);
274297
}
275-
276-
void _handleMQTT(char *topic, char *payload)
277-
{
278-
if(strcmp_P(topic, PSTR("perSegment"))==0)
279-
{
280-
ssLEDPerSegment = strtol(payload, NULL, 10);
281-
_publishMQTTint(topic, ssLEDPerSegment);
282-
return;
283-
}
284-
if(strcmp_P(topic, PSTR("perPeriod"))==0)
298+
bool _cmpIntSetting_P(char* topic, char* payload, const char* setting, void* value){
299+
if(strcmp_P(topic, setting) == 0)
285300
{
286-
ssLEDPerPeriod = strtol(payload, NULL, 10);
287-
_publishMQTTint(topic, ssLEDPerPeriod);
288-
return;
301+
*((int*)value) = strtol(payload, NULL, 10);
302+
_publishMQTTint_P(setting, *((int*)value));
303+
return true;
289304
}
290-
if(strcmp_P(topic, PSTR("startIdx"))==0)
291-
{
292-
ssStartLED = strtol(payload, NULL, 10);
293-
_publishMQTTint(topic, ssStartLED);
294-
return;
295-
}
296-
if(strcmp_P(topic, PSTR("displayCfg"))==0)
297-
{
298-
ssDisplayConfig = strtol(payload, NULL, 10);
299-
_publishMQTTint(topic, ssDisplayConfig);
300-
return;
301-
}
302-
if(strcmp_P(topic, PSTR("timeEnable"))==0)
303-
{
304-
ssTimeEnabled = strtol(payload, NULL, 10);
305-
ssDoDisplayTime = ssTimeEnabled;
306-
_publishMQTTint(topic, ssTimeEnabled);
307-
return;
308-
}
309-
if(strcmp_P(topic, PSTR("scrollSpd"))==0)
310-
{
311-
ssScrollSpeed = strtol(payload, NULL, 10);
312-
_publishMQTTint(topic, ssScrollSpeed);
313-
return;
314-
}
315-
if(strcmp_P(topic, PSTR("displayMask"))==0)
305+
return false;
306+
}
307+
bool _handleSetting(char *topic, char *payload)
308+
{
309+
if (_cmpIntSetting_P(topic, payload, _str_perSegment, &ssLEDPerSegment))
310+
return true;
311+
if (_cmpIntSetting_P(topic, payload, _str_perPeriod, &ssLEDPerPeriod))
312+
return true;
313+
if (_cmpIntSetting_P(topic, payload, _str_startIdx, &ssStartLED))
314+
return true;
315+
if (_cmpIntSetting_P(topic, payload, _str_displayCfg, &ssDisplayConfig))
316+
return true;
317+
if (_cmpIntSetting_P(topic, payload, _str_timeEnabled, &ssTimeEnabled))
318+
return true;
319+
if (_cmpIntSetting_P(topic, payload, _str_scrollSpd, &ssScrollSpeed))
320+
return true;
321+
if(strcmp_P(topic, _str_displayMask)==0)
316322
{
317323
ssDisplayMask = String(payload);
318324
ssDisplayBuffer = ssDisplayMask;
319-
_publishMQTTstr(topic, ssDisplayMask);
320-
return;
325+
_publishMQTTstr_P(_str_displayMask, ssDisplayMask);
326+
return true;
321327
}
322-
if(strcmp_P(topic, PSTR("displayMsg"))==0)
328+
if(strcmp_P(topic, _str_displayMsg)==0)
323329
{
324330
setSevenSegmentMessage(String(payload));
325-
return;
331+
return true;
326332
}
333+
return false;
327334
}
328335

329336
public:
@@ -344,7 +351,7 @@ class SevenSegmentDisplay : public Usermod
344351
//If the message isn't the same, update runtime/mqtt (most calls will be resetting message scroll)
345352
if(!ssDisplayMessage.equals(message))
346353
{
347-
_publishMQTTstr(PSTR("displayMsg"), message);
354+
_publishMQTTstr_P(_str_displayMsg, message);
348355
ssDisplayMessage = message;
349356
}
350357
}
@@ -416,49 +423,49 @@ class SevenSegmentDisplay : public Usermod
416423
{
417424
//Trim /set and handle it
418425
topic[topicLen - 4] = '\0';
419-
_handleMQTT(topic, payload);
426+
_handleSetting(topic, payload);
420427

421428
}
422429
return true;
423430
}
424431

425432
void addToConfig(JsonObject& root)
426433
{
427-
JsonObject top = root[FPSTR("sevenseg")];
434+
JsonObject top = root[FPSTR(_str_sevenSeg)];
428435
if (top.isNull()) {
429-
top = root.createNestedObject(FPSTR("sevenseg"));
436+
top = root.createNestedObject(FPSTR(_str_sevenSeg));
430437
}
431-
top[FPSTR("perSegment")] = ssLEDPerSegment;
432-
top[FPSTR("perPeriod")] = ssLEDPerPeriod;
433-
top[FPSTR("startIdx")] = ssStartLED;
434-
top[FPSTR("displayMask")] = ssDisplayMask;
435-
top[FPSTR("displayCfg")] = ssDisplayConfig;
436-
top[FPSTR("displayMsg")] = ssDisplayMessage;
437-
top[FPSTR("timeEnable")] = ssTimeEnabled;
438-
top[FPSTR("scrollSpd")] = ssScrollSpeed;
438+
top[FPSTR(_str_perSegment)] = ssLEDPerSegment;
439+
top[FPSTR(_str_perPeriod)] = ssLEDPerPeriod;
440+
top[FPSTR(_str_startIdx)] = ssStartLED;
441+
top[FPSTR(_str_displayMask)] = ssDisplayMask;
442+
top[FPSTR(_str_displayCfg)] = ssDisplayConfig;
443+
top[FPSTR(_str_displayMsg)] = ssDisplayMessage;
444+
top[FPSTR(_str_timeEnabled)] = ssTimeEnabled;
445+
top[FPSTR(_str_scrollSpd)] = ssScrollSpeed;
439446
}
440447

441448
bool readFromConfig(JsonObject& root)
442449
{
443-
JsonObject top = root[FPSTR("sevenseg")];
450+
JsonObject top = root[FPSTR(_str_sevenSeg)];
444451

445452
bool configComplete = !top.isNull();
446453

447454
//if sevenseg section doesn't exist return
448455
if(!configComplete) return configComplete;
449456

450-
configComplete &= getJsonValue(top[FPSTR("perSegment")], ssLEDPerSegment);
451-
configComplete &= getJsonValue(top[FPSTR("perPeriod")], ssLEDPerPeriod);
452-
configComplete &= getJsonValue(top[FPSTR("startIdx")], ssStartLED);
453-
configComplete &= getJsonValue(top[FPSTR("displayMask")], ssDisplayMask);
454-
configComplete &= getJsonValue(top[FPSTR("displayCfg")], ssDisplayConfig);
457+
configComplete &= getJsonValue(top[FPSTR(_str_perSegment)], ssLEDPerSegment);
458+
configComplete &= getJsonValue(top[FPSTR(_str_perPeriod)], ssLEDPerPeriod);
459+
configComplete &= getJsonValue(top[FPSTR(_str_startIdx)], ssStartLED);
460+
configComplete &= getJsonValue(top[FPSTR(_str_displayMask)], ssDisplayMask);
461+
configComplete &= getJsonValue(top[FPSTR(_str_displayCfg)], ssDisplayConfig);
455462

456463
String newDisplayMessage;
457-
configComplete &= getJsonValue(top[FPSTR("displayMsg")], newDisplayMessage);
464+
configComplete &= getJsonValue(top[FPSTR(_str_displayMsg)], newDisplayMessage);
458465
setSevenSegmentMessage(newDisplayMessage);
459466

460-
configComplete &= getJsonValue(top[FPSTR("timeEnable")], ssTimeEnabled);
461-
configComplete &= getJsonValue(top[FPSTR("scrollSpd")], ssScrollSpeed);
467+
configComplete &= getJsonValue(top[FPSTR(_str_timeEnabled)], ssTimeEnabled);
468+
configComplete &= getJsonValue(top[FPSTR(_str_scrollSpd)], ssScrollSpeed);
462469

463470
return configComplete;
464471
}
@@ -471,4 +478,14 @@ class SevenSegmentDisplay : public Usermod
471478
{
472479
return USERMOD_ID_SEVEN_SEGMENT_DISPLAY;
473480
}
474-
};
481+
};
482+
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";

0 commit comments

Comments
 (0)