Skip to content

Commit b0917af

Browse files
committed
Changed from Switch/case to if/elseif to save some runtime ram
1 parent 519d652 commit b0917af

File tree

2 files changed

+38
-56
lines changed

2 files changed

+38
-56
lines changed

libraries/MySensors/MyMessage.cpp

100644100755
+33-46
Original file line numberDiff line numberDiff line change
@@ -64,28 +64,21 @@ char* MyMessage::getString(char *buffer) const {
6464
buffer[miGetLength()] = 0;
6565
return buffer;
6666
} else if (buffer != NULL) {
67-
switch(payloadType) {
68-
case P_BYTE:
69-
itoa(bValue, buffer, 10);
70-
break;
71-
case P_INT16:
72-
itoa(iValue, buffer, 10);
73-
break;
74-
case P_UINT16:
75-
utoa(uiValue, buffer, 10);
76-
break;
77-
case P_LONG32:
78-
ltoa(lValue, buffer, 10);
79-
break;
80-
case P_ULONG32:
81-
ultoa(ulValue, buffer, 10);
82-
break;
83-
case P_FLOAT32:
84-
dtostrf(fValue,2,fPrecision,buffer);
85-
break;
86-
case P_CUSTOM:
87-
return getStream(buffer);
88-
break;
67+
if (payloadType == P_BYTE) {
68+
itoa(bValue, buffer, 10);
69+
} else if (payloadType == P_INT16) {
70+
itoa(iValue, buffer, 10);
71+
} else if (payloadType == P_UINT16) {
72+
utoa(uiValue, buffer, 10);
73+
} else if (payloadType == P_LONG32) {
74+
ltoa(lValue, buffer, 10);
75+
} else if (payloadType == P_ULONG32) {
76+
77+
ultoa(ulValue, buffer, 10);
78+
} else if (payloadType == P_FLOAT32) {
79+
dtostrf(fValue,2,fPrecision,buffer);
80+
} else if (payloadType == P_CUSTOM) {
81+
return getStream(buffer);
8982
}
9083
return buffer;
9184
} else {
@@ -94,12 +87,11 @@ char* MyMessage::getString(char *buffer) const {
9487
}
9588

9689
uint8_t MyMessage::getByte() const {
97-
switch(miGetPayloadType()) {
98-
case P_BYTE:
90+
if (miGetPayloadType() == P_BYTE) {
9991
return data[0];
100-
case P_STRING:
92+
} else if (miGetPayloadType() == P_STRING) {
10193
return atoi(data);
102-
default:
94+
} else {
10395
return 0;
10496
}
10597
}
@@ -109,56 +101,51 @@ bool MyMessage::getBool() const {
109101
}
110102

111103
float MyMessage::getFloat() const {
112-
switch(miGetPayloadType()) {
113-
case P_FLOAT32:
104+
if (miGetPayloadType() == P_FLOAT32) {
114105
return fValue;
115-
case P_STRING:
106+
} else if (miGetPayloadType() == P_STRING) {
116107
return atof(data);
117-
default:
108+
} else {
118109
return 0;
119110
}
120111
}
121112

122113
long MyMessage::getLong() const {
123-
switch(miGetPayloadType()) {
124-
case P_LONG32:
114+
if (miGetPayloadType() == P_LONG32) {
125115
return lValue;
126-
case P_STRING:
116+
} else if (miGetPayloadType() == P_STRING) {
127117
return atol(data);
128-
default:
118+
} else {
129119
return 0;
130120
}
131121
}
132122

133123
unsigned long MyMessage::getULong() const {
134-
switch(miGetPayloadType()) {
135-
case P_ULONG32:
124+
if (miGetPayloadType() == P_ULONG32) {
136125
return ulValue;
137-
case P_STRING:
126+
} else if (miGetPayloadType() == P_STRING) {
138127
return atol(data);
139-
default:
128+
} else {
140129
return 0;
141130
}
142131
}
143132

144133
int MyMessage::getInt() const {
145-
switch(miGetPayloadType()) {
146-
case P_INT16:
134+
if (miGetPayloadType() == P_INT16) {
147135
return iValue;
148-
case P_STRING:
136+
} else if (miGetPayloadType() == P_STRING) {
149137
return atoi(data);
150-
default:
138+
} else {
151139
return 0;
152140
}
153141
}
154142

155143
unsigned int MyMessage::getUInt() const {
156-
switch(miGetPayloadType()) {
157-
case P_UINT16:
144+
if (miGetPayloadType() == P_UINT16) {
158145
return uiValue;
159-
case P_STRING:
146+
} else if (miGetPayloadType() == P_STRING) {
160147
return atoi(data);
161-
default:
148+
} else {
162149
return 0;
163150
}
164151

libraries/MySensors/MySensor.cpp

100644100755
+5-10
Original file line numberDiff line numberDiff line change
@@ -327,12 +327,10 @@ boolean MySensor::process() {
327327
} else if (sender == GATEWAY_ADDRESS) {
328328
bool isMetric;
329329

330-
switch (type) {
331-
case I_REBOOT:
330+
if (type == I_REBOOT) {
332331
wdt_enable(WDTO_15MS);
333332
for (;;);
334-
break;
335-
case I_ID_RESPONSE:
333+
} else if (type == I_ID_RESPONSE) {
336334
if (nc.nodeId == AUTO) {
337335
nc.nodeId = msg.getByte();
338336
// Write id to EEPROM
@@ -346,8 +344,7 @@ boolean MySensor::process() {
346344
}
347345
debug(PSTR("id=%d\n"), nc.nodeId);
348346
}
349-
break;
350-
case I_CONFIG:
347+
} else if (type == I_CONFIG) {
351348
// Pick up configuration from controller (currently only metric/imperial)
352349
// and store it in eeprom if changed
353350
isMetric = msg.getByte() == 'M' ;
@@ -356,8 +353,7 @@ boolean MySensor::process() {
356353
eeprom_write_byte((uint8_t*)EEPROM_CONTROLLER_CONFIG_ADDRESS, isMetric);
357354
//eeprom_write_block((const void*)&cc, (uint8_t*)EEPROM_CONTROLLER_CONFIG_ADDRESS, sizeof(ControllerConfig));
358355
}
359-
break;
360-
case I_CHILDREN:
356+
} else if (type == I_CHILDREN) {
361357
if (repeaterMode && msg.getByte() == 'C') {
362358
// Clears child relay data for this node
363359
debug(PSTR("rd=clear\n"));
@@ -366,8 +362,7 @@ boolean MySensor::process() {
366362
}
367363
sendRoute(build(msg, nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_CHILDREN,false).set(""));
368364
}
369-
break;
370-
case I_TIME:
365+
} else if (type == I_TIME) {
371366
if (timeCallback != NULL) {
372367
// Deliver time to callback
373368
timeCallback(msg.getULong());

0 commit comments

Comments
 (0)