From 08a0bc25ff375d38bb4cf9534819b42fb86069ea Mon Sep 17 00:00:00 2001 From: tekka007 Date: Sun, 8 Feb 2015 01:00:39 +0100 Subject: [PATCH] simply wait() & adding watchdog resets Simplify wait(), added watchdog reset to wait() and waitForReply(). Substituted all waitForReply() for wait(2000) to reduce code size/complexity --- libraries/MySensors/MySensor.cpp | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/libraries/MySensors/MySensor.cpp b/libraries/MySensors/MySensor.cpp index b642313..a330fb6 100755 --- a/libraries/MySensors/MySensor.cpp +++ b/libraries/MySensors/MySensor.cpp @@ -82,7 +82,7 @@ void MySensor::begin(void (*_msgCallback)(const MyMessage &), uint8_t _nodeId, b if (nc.nodeId != AUTO) { setupNode(); // Wait configuration reply. - waitForReply(); + wait(2000); } } @@ -127,7 +127,7 @@ void MySensor::requestNodeId() { debug(PSTR("req node id\n")); RF24::openReadingPipe(CURRENT_NODE_PIPE, TO_ADDR(nc.nodeId)); sendRoute(build(msg, nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_ID_REQUEST, false).set("")); - waitForReply(); + wait(2000); } void MySensor::setupNode() { @@ -155,13 +155,15 @@ void MySensor::findParentNode() { sendWrite(BROADCAST_ADDRESS, msg, true); // Wait for ping response. - waitForReply(); + wait(2000); } void MySensor::waitForReply() { unsigned long enter = millis(); // Wait a couple of seconds for response while (millis() - enter < 2000) { + // reset watchdog + wdt_reset(); process(); } } @@ -498,19 +500,13 @@ void MySensor::sleep(unsigned long ms) { } void MySensor::wait(unsigned long ms) { - bool slept_enough = false; - unsigned long start = millis(); - unsigned long now; - // Let serial prints finish (debug, log etc) Serial.flush(); - - while (!slept_enough) { - MySensor::process(); - now = millis(); - if (now - start > ms) { - slept_enough = true; - } + unsigned long enter = millis(); + while (millis() - enter < ms) { + // reset watchdog + wdt_reset(); + process(); } }