Changed from Switch/case to if/elseif to save some runtime ram

This commit is contained in:
Daniel Wiegert
2014-08-17 20:51:52 +02:00
parent 519d652557
commit b0917af27e
2 changed files with 38 additions and 56 deletions
+26 -39
View File
@@ -64,28 +64,21 @@ char* MyMessage::getString(char *buffer) const {
buffer[miGetLength()] = 0; buffer[miGetLength()] = 0;
return buffer; return buffer;
} else if (buffer != NULL) { } else if (buffer != NULL) {
switch(payloadType) { if (payloadType == P_BYTE) {
case P_BYTE:
itoa(bValue, buffer, 10); itoa(bValue, buffer, 10);
break; } else if (payloadType == P_INT16) {
case P_INT16:
itoa(iValue, buffer, 10); itoa(iValue, buffer, 10);
break; } else if (payloadType == P_UINT16) {
case P_UINT16:
utoa(uiValue, buffer, 10); utoa(uiValue, buffer, 10);
break; } else if (payloadType == P_LONG32) {
case P_LONG32:
ltoa(lValue, buffer, 10); ltoa(lValue, buffer, 10);
break; } else if (payloadType == P_ULONG32) {
case P_ULONG32:
ultoa(ulValue, buffer, 10); ultoa(ulValue, buffer, 10);
break; } else if (payloadType == P_FLOAT32) {
case P_FLOAT32:
dtostrf(fValue,2,fPrecision,buffer); dtostrf(fValue,2,fPrecision,buffer);
break; } else if (payloadType == P_CUSTOM) {
case P_CUSTOM:
return getStream(buffer); return getStream(buffer);
break;
} }
return buffer; return buffer;
} else { } else {
@@ -94,12 +87,11 @@ char* MyMessage::getString(char *buffer) const {
} }
uint8_t MyMessage::getByte() const { uint8_t MyMessage::getByte() const {
switch(miGetPayloadType()) { if (miGetPayloadType() == P_BYTE) {
case P_BYTE:
return data[0]; return data[0];
case P_STRING: } else if (miGetPayloadType() == P_STRING) {
return atoi(data); return atoi(data);
default: } else {
return 0; return 0;
} }
} }
@@ -109,56 +101,51 @@ bool MyMessage::getBool() const {
} }
float MyMessage::getFloat() const { float MyMessage::getFloat() const {
switch(miGetPayloadType()) { if (miGetPayloadType() == P_FLOAT32) {
case P_FLOAT32:
return fValue; return fValue;
case P_STRING: } else if (miGetPayloadType() == P_STRING) {
return atof(data); return atof(data);
default: } else {
return 0; return 0;
} }
} }
long MyMessage::getLong() const { long MyMessage::getLong() const {
switch(miGetPayloadType()) { if (miGetPayloadType() == P_LONG32) {
case P_LONG32:
return lValue; return lValue;
case P_STRING: } else if (miGetPayloadType() == P_STRING) {
return atol(data); return atol(data);
default: } else {
return 0; return 0;
} }
} }
unsigned long MyMessage::getULong() const { unsigned long MyMessage::getULong() const {
switch(miGetPayloadType()) { if (miGetPayloadType() == P_ULONG32) {
case P_ULONG32:
return ulValue; return ulValue;
case P_STRING: } else if (miGetPayloadType() == P_STRING) {
return atol(data); return atol(data);
default: } else {
return 0; return 0;
} }
} }
int MyMessage::getInt() const { int MyMessage::getInt() const {
switch(miGetPayloadType()) { if (miGetPayloadType() == P_INT16) {
case P_INT16:
return iValue; return iValue;
case P_STRING: } else if (miGetPayloadType() == P_STRING) {
return atoi(data); return atoi(data);
default: } else {
return 0; return 0;
} }
} }
unsigned int MyMessage::getUInt() const { unsigned int MyMessage::getUInt() const {
switch(miGetPayloadType()) { if (miGetPayloadType() == P_UINT16) {
case P_UINT16:
return uiValue; return uiValue;
case P_STRING: } else if (miGetPayloadType() == P_STRING) {
return atoi(data); return atoi(data);
default: } else {
return 0; return 0;
} }
Regular → Executable
+5 -10
View File
@@ -327,12 +327,10 @@ boolean MySensor::process() {
} else if (sender == GATEWAY_ADDRESS) { } else if (sender == GATEWAY_ADDRESS) {
bool isMetric; bool isMetric;
switch (type) { if (type == I_REBOOT) {
case I_REBOOT:
wdt_enable(WDTO_15MS); wdt_enable(WDTO_15MS);
for (;;); for (;;);
break; } else if (type == I_ID_RESPONSE) {
case I_ID_RESPONSE:
if (nc.nodeId == AUTO) { if (nc.nodeId == AUTO) {
nc.nodeId = msg.getByte(); nc.nodeId = msg.getByte();
// Write id to EEPROM // Write id to EEPROM
@@ -346,8 +344,7 @@ boolean MySensor::process() {
} }
debug(PSTR("id=%d\n"), nc.nodeId); debug(PSTR("id=%d\n"), nc.nodeId);
} }
break; } else if (type == I_CONFIG) {
case I_CONFIG:
// Pick up configuration from controller (currently only metric/imperial) // Pick up configuration from controller (currently only metric/imperial)
// and store it in eeprom if changed // and store it in eeprom if changed
isMetric = msg.getByte() == 'M' ; isMetric = msg.getByte() == 'M' ;
@@ -356,8 +353,7 @@ boolean MySensor::process() {
eeprom_write_byte((uint8_t*)EEPROM_CONTROLLER_CONFIG_ADDRESS, isMetric); eeprom_write_byte((uint8_t*)EEPROM_CONTROLLER_CONFIG_ADDRESS, isMetric);
//eeprom_write_block((const void*)&cc, (uint8_t*)EEPROM_CONTROLLER_CONFIG_ADDRESS, sizeof(ControllerConfig)); //eeprom_write_block((const void*)&cc, (uint8_t*)EEPROM_CONTROLLER_CONFIG_ADDRESS, sizeof(ControllerConfig));
} }
break; } else if (type == I_CHILDREN) {
case I_CHILDREN:
if (repeaterMode && msg.getByte() == 'C') { if (repeaterMode && msg.getByte() == 'C') {
// Clears child relay data for this node // Clears child relay data for this node
debug(PSTR("rd=clear\n")); debug(PSTR("rd=clear\n"));
@@ -366,8 +362,7 @@ boolean MySensor::process() {
} }
sendRoute(build(msg, nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_CHILDREN,false).set("")); sendRoute(build(msg, nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_CHILDREN,false).set(""));
} }
break; } else if (type == I_TIME) {
case I_TIME:
if (timeCallback != NULL) { if (timeCallback != NULL) {
// Deliver time to callback // Deliver time to callback
timeCallback(msg.getULong()); timeCallback(msg.getULong());