Skip to content

Commit d45a48d

Browse files
author
Roy Hooper
committed
Add wait() helper that doesn't sleep. Useful for repeaters and sensors that read from the network.
1 parent dfd38ff commit d45a48d

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

libraries/MySensors/MySensor.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,23 @@ void MySensor::sleep(unsigned long ms) {
486486
internalSleep(ms);
487487
}
488488

489+
void MySensor::wait(unsigned long ms) {
490+
bool slept_enough = false;
491+
unsigned long start = millis();
492+
unsigned long now;
493+
494+
// Let serial prints finish (debug, log etc)
495+
Serial.flush();
496+
497+
while (!slept_enough) {
498+
MySensor::process();
499+
now = millis();
500+
if (now - start > ms) {
501+
slept_enough = true;
502+
}
503+
}
504+
}
505+
489506
bool MySensor::sleep(uint8_t interrupt, uint8_t mode, unsigned long ms) {
490507
// Let serial prints finish (debug, log etc)
491508
bool pinTriggeredWakeup = true;

libraries/MySensors/MySensor.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,15 @@ class MySensor : public RF24
210210
*/
211211
void sleep(unsigned long ms);
212212

213+
/**
214+
* Wait for a specified amount of time to pass. Keeps process()ing.
215+
* This does not power-down the radio nor the Arduino.
216+
* Because this calls process() in a loop, it is a good way to wait
217+
* in your loop() on a repeater node or sensor that listens to messages.
218+
* @param ms Number of milliseconds to sleep.
219+
*/
220+
void wait(unsigned long ms);
221+
213222
/**
214223
* Sleep (PowerDownMode) the Arduino and radio. Wake up on timer or pin change.
215224
* See: http://arduino.cc/en/Reference/attachInterrupt for details on modes and which pin

0 commit comments

Comments
 (0)