From 7cc36c6258ae8fee1a6a23cd645501b69e81fa9a Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Mon, 6 Aug 2018 21:16:23 +1200 Subject: [PATCH] Avoid backup while the tracker is moving --- src/Core.cpp | 21 +++++++++++++-------- src/Core.h | 9 ++++++++- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/Core.cpp b/src/Core.cpp index afbe9fa..f366a48 100644 --- a/src/Core.cpp +++ b/src/Core.cpp @@ -13,6 +13,7 @@ namespace core { uint16_t sleepTime = SLEEP_DEFAULT_TIME_SECONDS; uint8_t stoppedInARow = SLEEP_DEFAULT_STOPPED_THRESHOLD - 1; + TRACKER_MOVING_STATE movingState = TRACKER_MOVING_STATE::MOVING; namespace details { @@ -30,23 +31,25 @@ namespace core { } void main() { - bool forceBackup = false; bool acquired = false; PositionEntryMetadata metadata; - positions::prepareBackup(); + if(movingState >= TRACKER_MOVING_STATE::STOPPED) positions::prepareBackup(); acquired = positions::acquire(metadata); if (acquired) { positions::appendLast(metadata); - forceBackup = updateSleepTime(); + movingState = updateSleepTime(); gps::preserveCurrentCoordinates(); } alerts::clear(metadata); alerts::add(notifyFailures(metadata)); - positions::doBackup(forceBackup); + + if(movingState >= TRACKER_MOVING_STATE::STOPPED) { + positions::doBackup(movingState == TRACKER_MOVING_STATE::STOPPED); //do not force on STATIC + } if (acquired) updateRtcTime(); mainunit::deepSleep(sleepTime); @@ -104,8 +107,8 @@ namespace core { rtc::setTime(time); } - bool updateSleepTime() { - bool goingLongSleep = false; + TRACKER_MOVING_STATE updateSleepTime() { + TRACKER_MOVING_STATE state = TRACKER_MOVING_STATE::MOVING; uint8_t velocity = gps::getVelocity(); sleepTime = mapSleepTime(velocity); @@ -117,13 +120,15 @@ namespace core { if (stoppedInARow < SLEEP_DEFAULT_STOPPED_THRESHOLD) { sleepTime = SLEEP_DEFAULT_PAUSING_TIME_SECONDS; + state = TRACKER_MOVING_STATE::PAUSED; } - else if (stoppedInARow == SLEEP_DEFAULT_STOPPED_THRESHOLD) goingLongSleep = true; + else if (stoppedInARow == SLEEP_DEFAULT_STOPPED_THRESHOLD) state = TRACKER_MOVING_STATE::STOPPED; + else state = TRACKER_MOVING_STATE::STATIC; } else stoppedInARow = 0; NOTICE_FORMAT("updateSleepTime", "%dkmh => %d seconds", velocity, sleepTime); - return goingLongSleep; + return state; } uint16_t mapSleepTime(uint8_t velocity) { diff --git a/src/Core.h b/src/Core.h index 16ba5c7..7afbe4e 100644 --- a/src/Core.h +++ b/src/Core.h @@ -10,12 +10,19 @@ #include "Pins.h" #include "Positions.h" +enum class TRACKER_MOVING_STATE : uint8_t { + MOVING = 0, + PAUSED = 1, + STOPPED = 2, + STATIC = 3 +}; + namespace core { extern uint16_t sleepTime; void main(); void updateRtcTime(); - bool updateSleepTime(); + TRACKER_MOVING_STATE updateSleepTime(); uint16_t mapSleepTime(uint8_t velocity); uint8_t notifyFailures(PositionEntryMetadata &metadata);