Skip to content

Commit ae35b9a

Browse files
committed
Minor refactoring of delay variables
This is a preparation for handling multiple interrupts in delay(). It also reduces the size of the interrupt-related variables and arguments to int8_t as that should be ample enough for any Arduino implementation.
1 parent 4ffb53b commit ae35b9a

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

libraries/MySensors/MySensor.cpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -465,43 +465,45 @@ int MySensor::getInternalTemp(void)
465465
return result/10000;
466466
}
467467

468-
int continueTimer = true;
468+
int8_t pinIntTrigger = 0;
469469
void wakeUp() //place to send the interrupts
470470
{
471-
continueTimer = false;
471+
pinIntTrigger = 1;
472472
}
473473

474474
void MySensor::internalSleep(unsigned long ms) {
475-
while (continueTimer && ms >= 8000) { LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF); ms -= 8000; }
476-
if (continueTimer && ms >= 4000) { LowPower.powerDown(SLEEP_4S, ADC_OFF, BOD_OFF); ms -= 4000; }
477-
if (continueTimer && ms >= 2000) { LowPower.powerDown(SLEEP_2S, ADC_OFF, BOD_OFF); ms -= 2000; }
478-
if (continueTimer && ms >= 1000) { LowPower.powerDown(SLEEP_1S, ADC_OFF, BOD_OFF); ms -= 1000; }
479-
if (continueTimer && ms >= 500) { LowPower.powerDown(SLEEP_500MS, ADC_OFF, BOD_OFF); ms -= 500; }
480-
if (continueTimer && ms >= 250) { LowPower.powerDown(SLEEP_250MS, ADC_OFF, BOD_OFF); ms -= 250; }
481-
if (continueTimer && ms >= 125) { LowPower.powerDown(SLEEP_120MS, ADC_OFF, BOD_OFF); ms -= 120; }
482-
if (continueTimer && ms >= 64) { LowPower.powerDown(SLEEP_60MS, ADC_OFF, BOD_OFF); ms -= 60; }
483-
if (continueTimer && ms >= 32) { LowPower.powerDown(SLEEP_30MS, ADC_OFF, BOD_OFF); ms -= 30; }
484-
if (continueTimer && ms >= 16) { LowPower.powerDown(SLEEP_15Ms, ADC_OFF, BOD_OFF); ms -= 15; }
475+
while (!pinIntTrigger && ms >= 8000) { LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF); ms -= 8000; }
476+
if (!pinIntTrigger && ms >= 4000) { LowPower.powerDown(SLEEP_4S, ADC_OFF, BOD_OFF); ms -= 4000; }
477+
if (!pinIntTrigger && ms >= 2000) { LowPower.powerDown(SLEEP_2S, ADC_OFF, BOD_OFF); ms -= 2000; }
478+
if (!pinIntTrigger && ms >= 1000) { LowPower.powerDown(SLEEP_1S, ADC_OFF, BOD_OFF); ms -= 1000; }
479+
if (!pinIntTrigger && ms >= 500) { LowPower.powerDown(SLEEP_500MS, ADC_OFF, BOD_OFF); ms -= 500; }
480+
if (!pinIntTrigger && ms >= 250) { LowPower.powerDown(SLEEP_250MS, ADC_OFF, BOD_OFF); ms -= 250; }
481+
if (!pinIntTrigger && ms >= 125) { LowPower.powerDown(SLEEP_120MS, ADC_OFF, BOD_OFF); ms -= 120; }
482+
if (!pinIntTrigger && ms >= 64) { LowPower.powerDown(SLEEP_60MS, ADC_OFF, BOD_OFF); ms -= 60; }
483+
if (!pinIntTrigger && ms >= 32) { LowPower.powerDown(SLEEP_30MS, ADC_OFF, BOD_OFF); ms -= 30; }
484+
if (!pinIntTrigger && ms >= 16) { LowPower.powerDown(SLEEP_15Ms, ADC_OFF, BOD_OFF); ms -= 15; }
485485
}
486486

487487
void MySensor::sleep(unsigned long ms) {
488488
// Let serial prints finish (debug, log etc)
489489
Serial.flush();
490490
RF24::powerDown();
491-
continueTimer = true;
491+
pinIntTrigger = 0;
492492
internalSleep(ms);
493493
}
494494

495-
bool MySensor::sleep(int interrupt, int mode, unsigned long ms) {
495+
bool MySensor::sleep(uint8_t interrupt, uint8_t mode, unsigned long ms) {
496496
// Let serial prints finish (debug, log etc)
497497
bool pinTriggeredWakeup = true;
498498
Serial.flush();
499499
RF24::powerDown();
500-
attachInterrupt(interrupt, wakeUp, mode); //Interrupt on pin 3 for any change in solar power
500+
attachInterrupt(interrupt, wakeUp, mode);
501501
if (ms>0) {
502-
continueTimer = true;
502+
pinIntTrigger = 0;
503503
sleep(ms);
504-
pinTriggeredWakeup = !continueTimer;
504+
if (0 == pinIntTrigger) {
505+
pinTriggeredWakeup = false;
506+
}
505507
} else {
506508
Serial.flush();
507509
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);

libraries/MySensors/MySensor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ class MySensor : public RF24
219219
* @param ms Number of milliseconds to sleep or 0 to sleep forever
220220
* @return true if wake up was triggered by pin change and false means timer woke it up.
221221
*/
222-
bool sleep(int interrupt, int mode, unsigned long ms=0);
222+
bool sleep(uint8_t interrupt, uint8_t mode, unsigned long ms=0);
223223

224224
/**
225225
* getInternalTemp

0 commit comments

Comments
 (0)