From 1b8e898a9bdd0061550e5daefc2bc36f89b7bb30 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sun, 18 Mar 2018 22:37:53 +1300 Subject: [PATCH] Introduced speed timing variations based on time of the day --- GpsTracker/Config.h | 36 ++++++++++++++++++------------------ GpsTracker/Core.cpp | 11 ++++++++++- GpsTracker/Debug.cpp | 2 +- GpsTracker/GpsTracker.ino | 2 +- GpsTracker/Rtc.cpp | 9 +++++++++ GpsTracker/Rtc.h | 1 + 6 files changed, 40 insertions(+), 21 deletions(-) diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h index 48ea242..c12cb1c 100644 --- a/GpsTracker/Config.h +++ b/GpsTracker/Config.h @@ -35,8 +35,14 @@ #pragma endregion +#define SLEEP_TIMING_TIME(hours, minutes) hours * 60 + minutes +#define SLEEP_TIMING_MIN SLEEP_TIMING_TIME(0, 0) +#define SLEEP_TIMING_MAX SLEEP_TIMING_TIME(23, 59) + struct sleepTimings_t { uint8_t speed; + uint16_t timeMin; + uint16_t timeMax; uint16_t seconds; }; @@ -53,25 +59,19 @@ struct config_t { namespace config { static const sleepTimings_t defaultSleepTimings[] PROGMEM = { - /*{ 5, SLEEP_DEFAULT_TIME_SECONDS }, - { 10, 1200 }, - { 20, 600 }, - { 30, 540 }, - { 50, 480 }, - { 80, 240 }, - { 100, 210 }, - { 180, 180 }, - };*/ - { 3, 1800 }, - { 5, 900 }, - { 10, 600 }, - { 20, 600 }, - { 30, 540 }, - { 50, 480 }, - { 80, 240 }, - { 100, 210 }, - { 180, 180 }, + { 3, SLEEP_TIMING_TIME(16, 00), SLEEP_TIMING_TIME(8, 30), SLEEP_DEFAULT_TIME_SECONDS }, + { 3, SLEEP_TIMING_MIN, SLEEP_TIMING_TIME(15, 59), 10800 }, + { 3, SLEEP_TIMING_TIME(8, 29), SLEEP_TIMING_MAX, 10800 }, + + { 5, SLEEP_TIMING_MIN, SLEEP_TIMING_MAX, 900 }, + { 20, SLEEP_TIMING_MIN, SLEEP_TIMING_MAX, 600 }, + { 30, SLEEP_TIMING_MIN, SLEEP_TIMING_MAX, 540 }, + { 50, SLEEP_TIMING_MIN, SLEEP_TIMING_MAX, 480 }, + { 80, SLEEP_TIMING_MIN, SLEEP_TIMING_MAX, 240 }, + { 100, SLEEP_TIMING_MIN, SLEEP_TIMING_MAX, 210 }, + { 180, SLEEP_TIMING_MIN, SLEEP_TIMING_MAX, 180 }, }; + namespace main { extern config_t value; diff --git a/GpsTracker/Core.cpp b/GpsTracker/Core.cpp index 492b278..74135e0 100644 --- a/GpsTracker/Core.cpp +++ b/GpsTracker/Core.cpp @@ -39,13 +39,22 @@ namespace core { uint16_t computeSleepTime(uint8_t velocity) { uint16_t result; + uint16_t currentTime = 0xFFFF; + + if (rtc::isAccurate()) { + tmElements_t time; + rtc::getTime(time); + currentTime = SLEEP_TIMING_TIME(time.Hour, time.Minute); + } + for (uint8_t i = 0; i < flash::getArraySize(config::defaultSleepTimings); i++) { sleepTimings_t timing; flash::read(&config::defaultSleepTimings[i], timing); if (velocity > timing.speed) continue; - + if (currentTime != 0xFFFF && (currentTime < timing.timeMin || currentTime > timing.timeMax)) continue; + result = timing.seconds; break; } diff --git a/GpsTracker/Debug.cpp b/GpsTracker/Debug.cpp index 42dcb41..c15821b 100644 --- a/GpsTracker/Debug.cpp +++ b/GpsTracker/Debug.cpp @@ -7,7 +7,7 @@ #define MENU_ENTRY(name, text) const char MENU_##name[] PROGMEM = text -const char FAKE_GPS_ENTRY[] PROGMEM = "1,1,20170924184842.000,49.454862,1.144537,71.900,67.99,172.6,1,,1.3,2.2,1.8,,11,7,,,37,,"; +const char FAKE_GPS_ENTRY[] PROGMEM = "1,1,20170924184842.000,49.454862,1.144537,71.900,2.70,172.6,1,,1.3,2.2,1.8,,11,7,,,37,,"; MENU_ENTRY(HEADER, "========================\n-- Menu --"); MENU_ENTRY(SEPARATOR, "----"); diff --git a/GpsTracker/GpsTracker.ino b/GpsTracker/GpsTracker.ino index 0b908c8..2a1ec23 100644 --- a/GpsTracker/GpsTracker.ino +++ b/GpsTracker/GpsTracker.ino @@ -4,7 +4,7 @@ #if _DEBUG #define MENU_DEFAULT_TIMEOUT 0 #else -#define MENU_DEFAULT_TIMEOUT 5000 +#define MENU_DEFAULT_TIMEOUT 10000 #endif bool bypassMenu = false; diff --git a/GpsTracker/Rtc.cpp b/GpsTracker/Rtc.cpp index 019d577..1f58059 100644 --- a/GpsTracker/Rtc.cpp +++ b/GpsTracker/Rtc.cpp @@ -31,6 +31,14 @@ namespace rtc { return temperature; } + bool isAccurate() { + hardware::i2c::powerOn(); + bool accurate = RTC.status(DS3231_HALTED_FLAG) == DS3231_OFF; + hardware::i2c::powerOff(); + + return accurate; + } + timestamp_t getTime() { tmElements_t time; getTime(time); @@ -50,6 +58,7 @@ namespace rtc { hardware::i2c::powerOn(); RTC.writeTime(time); + RTC.control(DS3231_HALTED_FLAG, DS3231_OFF); hardware::i2c::powerOff(); } diff --git a/GpsTracker/Rtc.h b/GpsTracker/Rtc.h index 1236f53..e752893 100644 --- a/GpsTracker/Rtc.h +++ b/GpsTracker/Rtc.h @@ -7,6 +7,7 @@ namespace rtc { float getTemperature(); + bool isAccurate(); timestamp_t getTime(); void getTime(tmElements_t &time); void setTime(const tmElements_t &time);