From 35788d6d94b3184343e7995da6ca1162625ad328 Mon Sep 17 00:00:00 2001 From: Patrick Fallberg Date: Sun, 7 Sep 2014 15:53:57 +0200 Subject: [PATCH] Added constructor to sleep() for monitoring two interrupts The new constructor allows two separate interrupts with two separate trigger policies to wake up device. --- libraries/MySensors/MySensor.cpp | 29 +++++++++++++++++++++++++++++ libraries/MySensors/MySensor.h | 13 +++++++++++++ 2 files changed, 42 insertions(+) diff --git a/libraries/MySensors/MySensor.cpp b/libraries/MySensors/MySensor.cpp index ae4e519..e7002d5 100755 --- a/libraries/MySensors/MySensor.cpp +++ b/libraries/MySensors/MySensor.cpp @@ -470,6 +470,10 @@ void wakeUp() //place to send the interrupts { pinIntTrigger = 1; } +void wakeUp2() //place to send the second interrupts +{ + pinIntTrigger = 2; +} void MySensor::internalSleep(unsigned long ms) { while (!pinIntTrigger && ms >= 8000) { LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF); ms -= 8000; } @@ -512,7 +516,32 @@ bool MySensor::sleep(uint8_t interrupt, uint8_t mode, unsigned long ms) { return pinTriggeredWakeup; } +int8_t MySensor::sleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mode2, unsigned long ms) { + int8_t retVal = 1; + Serial.flush(); // Let serial prints finish (debug, log etc) + RF24::powerDown(); + attachInterrupt(interrupt1, wakeUp, mode1); + attachInterrupt(interrupt2, wakeUp2, mode2); + if (ms>0) { + pinIntTrigger = 0; + sleep(ms); + if (0 == pinIntTrigger) { + retVal = -1; + } + } else { + Serial.flush(); + LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); + } + detachInterrupt(interrupt1); + detachInterrupt(interrupt2); + if (1 == pinIntTrigger) { + retVal = (int8_t)interrupt1; + } else if (2 == pinIntTrigger) { + retVal = (int8_t)interrupt2; + } + return retVal; +} #ifdef DEBUG void MySensor::debugPrint(const char *fmt, ... ) { diff --git a/libraries/MySensors/MySensor.h b/libraries/MySensors/MySensor.h index d8f0adf..a69e48c 100644 --- a/libraries/MySensors/MySensor.h +++ b/libraries/MySensors/MySensor.h @@ -221,6 +221,19 @@ class MySensor : public RF24 */ bool sleep(uint8_t interrupt, uint8_t mode, unsigned long ms=0); + /** + * Sleep (PowerDownMode) the Arduino and radio. Wake up on timer or pin change for two separate interrupts. + * See: http://arduino.cc/en/Reference/attachInterrupt for details on modes and which pin + * is assigned to what interrupt. On Nano/Pro Mini: 0=Pin2, 1=Pin3 + * @param interrupt1 First interrupt that should trigger the wakeup + * @param mode1 Mode for first interrupt (RISING, FALLING, CHANGE) + * @param interrupt2 Second interrupt that should trigger the wakeup + * @param mode2 Mode for second interrupt (RISING, FALLING, CHANGE) + * @param ms Number of milliseconds to sleep or 0 to sleep forever + * @return Interrupt number wake up was triggered by pin change and negative if timer woke it up. + */ + int8_t sleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mode2, unsigned long ms=0); + /** * getInternalTemp *