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
+33 -46
View File
@@ -64,28 +64,21 @@ char* MyMessage::getString(char *buffer) const {
buffer[miGetLength()] = 0;
return buffer;
} else if (buffer != NULL) {
switch(payloadType) {
case P_BYTE:
itoa(bValue, buffer, 10);
break;
case P_INT16:
itoa(iValue, buffer, 10);
break;
case P_UINT16:
utoa(uiValue, buffer, 10);
break;
case P_LONG32:
ltoa(lValue, buffer, 10);
break;
case P_ULONG32:
ultoa(ulValue, buffer, 10);
break;
case P_FLOAT32:
dtostrf(fValue,2,fPrecision,buffer);
break;
case P_CUSTOM:
return getStream(buffer);
break;
if (payloadType == P_BYTE) {
itoa(bValue, buffer, 10);
} else if (payloadType == P_INT16) {
itoa(iValue, buffer, 10);
} else if (payloadType == P_UINT16) {
utoa(uiValue, buffer, 10);
} else if (payloadType == P_LONG32) {
ltoa(lValue, buffer, 10);
} else if (payloadType == P_ULONG32) {
ultoa(ulValue, buffer, 10);
} else if (payloadType == P_FLOAT32) {
dtostrf(fValue,2,fPrecision,buffer);
} else if (payloadType == P_CUSTOM) {
return getStream(buffer);
}
return buffer;
} else {
@@ -94,12 +87,11 @@ char* MyMessage::getString(char *buffer) const {
}
uint8_t MyMessage::getByte() const {
switch(miGetPayloadType()) {
case P_BYTE:
if (miGetPayloadType() == P_BYTE) {
return data[0];
case P_STRING:
} else if (miGetPayloadType() == P_STRING) {
return atoi(data);
default:
} else {
return 0;
}
}
@@ -109,56 +101,51 @@ bool MyMessage::getBool() const {
}
float MyMessage::getFloat() const {
switch(miGetPayloadType()) {
case P_FLOAT32:
if (miGetPayloadType() == P_FLOAT32) {
return fValue;
case P_STRING:
} else if (miGetPayloadType() == P_STRING) {
return atof(data);
default:
} else {
return 0;
}
}
long MyMessage::getLong() const {
switch(miGetPayloadType()) {
case P_LONG32:
if (miGetPayloadType() == P_LONG32) {
return lValue;
case P_STRING:
} else if (miGetPayloadType() == P_STRING) {
return atol(data);
default:
} else {
return 0;
}
}
unsigned long MyMessage::getULong() const {
switch(miGetPayloadType()) {
case P_ULONG32:
if (miGetPayloadType() == P_ULONG32) {
return ulValue;
case P_STRING:
} else if (miGetPayloadType() == P_STRING) {
return atol(data);
default:
} else {
return 0;
}
}
int MyMessage::getInt() const {
switch(miGetPayloadType()) {
case P_INT16:
if (miGetPayloadType() == P_INT16) {
return iValue;
case P_STRING:
} else if (miGetPayloadType() == P_STRING) {
return atoi(data);
default:
} else {
return 0;
}
}
unsigned int MyMessage::getUInt() const {
switch(miGetPayloadType()) {
case P_UINT16:
if (miGetPayloadType() == P_UINT16) {
return uiValue;
case P_STRING:
} else if (miGetPayloadType() == P_STRING) {
return atoi(data);
default:
} else {
return 0;
}
Regular → Executable
+5 -10
View File
@@ -327,12 +327,10 @@ boolean MySensor::process() {
} else if (sender == GATEWAY_ADDRESS) {
bool isMetric;
switch (type) {
case I_REBOOT:
if (type == I_REBOOT) {
wdt_enable(WDTO_15MS);
for (;;);
break;
case I_ID_RESPONSE:
} else if (type == I_ID_RESPONSE) {
if (nc.nodeId == AUTO) {
nc.nodeId = msg.getByte();
// Write id to EEPROM
@@ -346,8 +344,7 @@ boolean MySensor::process() {
}
debug(PSTR("id=%d\n"), nc.nodeId);
}
break;
case I_CONFIG:
} else if (type == I_CONFIG) {
// Pick up configuration from controller (currently only metric/imperial)
// and store it in eeprom if changed
isMetric = msg.getByte() == 'M' ;
@@ -356,8 +353,7 @@ boolean MySensor::process() {
eeprom_write_byte((uint8_t*)EEPROM_CONTROLLER_CONFIG_ADDRESS, isMetric);
//eeprom_write_block((const void*)&cc, (uint8_t*)EEPROM_CONTROLLER_CONFIG_ADDRESS, sizeof(ControllerConfig));
}
break;
case I_CHILDREN:
} else if (type == I_CHILDREN) {
if (repeaterMode && msg.getByte() == 'C') {
// Clears child relay data for this node
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(""));
}
break;
case I_TIME:
} else if (type == I_TIME) {
if (timeCallback != NULL) {
// Deliver time to callback
timeCallback(msg.getULong());