From c4f659243e07293dc25379d41156f30ae36d75e5 Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Sun, 1 Aug 2021 15:07:06 +0200 Subject: [PATCH] optimize for loops for size (and speed) --- Outputs.cpp | 9 ++++++--- Sensors.cpp | 3 ++- Turnouts.cpp | 3 ++- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Outputs.cpp b/Outputs.cpp index 52cafb8..a332a84 100644 --- a/Outputs.cpp +++ b/Outputs.cpp @@ -138,15 +138,18 @@ void Output::load(){ // id. If someone uses only pins 0 to 7 of their arduino, they // loose. This is (if you look at an arduino) however unlikely. - for(uint16_t i=0;idata.nOutputs;i++){ + uint16_t i=EEStore::eeStore->data.nOutputs; + while(i--){ EEPROM.get(EEStore::pointer()+ i*sizeof(struct BrokenOutputData),bdata); if (bdata.iFlag > 7) { // it's a pin and not an iFlag! isBroken=0; break; } } + + i=EEStore::eeStore->data.nOutputs; if ( isBroken ) { - for(uint16_t i=0;idata.nOutputs;i++){ + while(i--){ EEPROM.get(EEStore::pointer(),bdata); tt=create(bdata.id,bdata.pin,bdata.iFlag); tt->data.oStatus=bitRead(tt->data.iFlag,1)?bitRead(tt->data.iFlag,2):bdata.oStatus; // restore status to EEPROM value is bit 1 of iFlag=0, otherwise set to value of bit 2 of iFlag @@ -158,7 +161,7 @@ void Output::load(){ } else { struct OutputData data; - for(uint16_t i=0;idata.nOutputs;i++){ + while(i--){ EEPROM.get(EEStore::pointer(),data); tt=create(data.id,data.pin,data.iFlag); tt->data.oStatus=bitRead(tt->data.iFlag,1)?bitRead(tt->data.iFlag,2):data.oStatus; // restore status to EEPROM value is bit 1 of iFlag=0, otherwise set to value of bit 2 of iFlag diff --git a/Sensors.cpp b/Sensors.cpp index 33c3fd6..689584e 100644 --- a/Sensors.cpp +++ b/Sensors.cpp @@ -184,7 +184,8 @@ void Sensor::load(){ struct SensorData data; Sensor *tt; - for(uint16_t i=0;idata.nSensors;i++){ + uint16_t i=EEStore::eeStore->data.nSensors; + while(i--){ EEPROM.get(EEStore::pointer(),data); tt=create(data.snum,data.pin,data.pullUp); EEStore::advance(sizeof(tt->data)); diff --git a/Turnouts.cpp b/Turnouts.cpp index 4c46a68..7da2480 100644 --- a/Turnouts.cpp +++ b/Turnouts.cpp @@ -103,7 +103,8 @@ void Turnout::load(){ struct TurnoutData data; Turnout *tt; - for(uint16_t i=0;idata.nTurnouts;i++){ + uint16_t i=EEStore::eeStore->data.nTurnouts; + while(i--){ EEPROM.get(EEStore::pointer(),data); if (data.tStatus & STATUS_PWM) tt=create(data.id,data.tStatus & STATUS_PWMPIN, data.inactiveAngle,data.moveAngle); else tt=create(data.id,data.address,data.subAddress);