From 0d24ef07e33e4bf630287f089b9b5f805fb5c941 Mon Sep 17 00:00:00 2001
From: Bertrand Lemasle <blemasle@gmail.com>
Date: Mon, 29 Jan 2018 23:17:23 +0100
Subject: [PATCH] Using define instead of magic numbers. Started to implement
 speed vs sleep time logic

---
 GpsTracker/Config.h       | 26 ++++++++++++++++++++++++
 GpsTracker/Core.cpp       | 22 ++++++++++++++++++++
 GpsTracker/Core.h         |  2 ++
 GpsTracker/Gps.cpp        | 10 +++++++---
 GpsTracker/Gps.h          |  3 ++-
 GpsTracker/GpsTracker.h   |  1 +
 GpsTracker/GpsTracker.ino |  9 ++++++---
 GpsTracker/Time2.h        | 42 ++++++++++++++-------------------------
 8 files changed, 81 insertions(+), 34 deletions(-)
 create mode 100644 GpsTracker/Config.h

diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h
new file mode 100644
index 0000000..732b080
--- /dev/null
+++ b/GpsTracker/Config.h
@@ -0,0 +1,26 @@
+#pragma once
+
+struct sleepTimings_t {
+	uint8_t speed;
+	uint16_t seconds;
+};
+
+#define SLEEP_DEFAULT_TIME_SECONDS	1800
+
+#define GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS	10000
+#define GPS_DEFAULT_TOTAL_TIMEOUT_MS		30000
+
+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 },
+	};
+
+}
\ No newline at end of file
diff --git a/GpsTracker/Core.cpp b/GpsTracker/Core.cpp
index e6d1bf5..2929a62 100644
--- a/GpsTracker/Core.cpp
+++ b/GpsTracker/Core.cpp
@@ -1,7 +1,29 @@
 #include "Core.h"
+#include "Config.h"
 
 #define LOGGER_NAME "Core"
 
 namespace core {
+	uint16_t sleepTime = SLEEP_DEFAULT_TIME_SECONDS;;
 
+	namespace details {
+		template<typename T, size_t N> size_t getFlashArraySize(T(&)[N]) { return N; }
+		template<typename T> void readFromFlash(const T *source, T &dest) {
+			memcpy_P(&dest, source, sizeof(T));
+		}
+	}
+
+	void setSleepTime(uint8_t velocity) {
+		for (uint8_t i = 0; i < details::getFlashArraySize(config::defaultSleepTimings); i++) {
+			sleepTimings_t timing;
+			details::readFromFlash(&config::defaultSleepTimings[i], timing);
+
+			if (velocity > timing.speed) continue;
+
+			sleepTime = timing.seconds;
+			return;
+		}
+
+		sleepTime = SLEEP_DEFAULT_TIME_SECONDS;
+	}
 }
\ No newline at end of file
diff --git a/GpsTracker/Core.h b/GpsTracker/Core.h
index 7d733f4..16c3db7 100644
--- a/GpsTracker/Core.h
+++ b/GpsTracker/Core.h
@@ -5,5 +5,7 @@
 #include "Debug.h"
 
 namespace core {
+	extern uint16_t sleepTime;
 
+	void setSleepTime(uint8_t velocity);
 }
\ No newline at end of file
diff --git a/GpsTracker/Gps.cpp b/GpsTracker/Gps.cpp
index 1f611e0..7419312 100644
--- a/GpsTracker/Gps.cpp
+++ b/GpsTracker/Gps.cpp
@@ -1,10 +1,10 @@
 #include "Gps.h"
+#include "Config.h"
 #include "Debug.h"
 #include "Hardware.h"
 #include "MainUnit.h"
 
 #define LOGGER_NAME "Gps"
-#define WAIT_FOR_FIX_DELAY 10000
 
 #define TIME_YEAR_OFFSET	0
 #define TIME_MONTH_OFFSET	4
@@ -27,8 +27,8 @@ namespace gps {
 			currentStatus = hardware::sim808::device.getGpsStatus();
 			if (currentStatus > SIM808_GPS_STATUS::NO_FIX) break;
 
-			mainunit::deepSleep(WAIT_FOR_FIX_DELAY);
-			timeout -= WAIT_FOR_FIX_DELAY;
+			mainunit::deepSleep(GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS);
+			timeout -= GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS;
 		} while (timeout > 1);
 
 		if (currentStatus > SIM808_GPS_STATUS::NO_FIX) {
@@ -39,6 +39,10 @@ namespace gps {
 		return currentStatus;
 	}
 
+	void getVelocity(uint8_t &velocity) {
+		hardware::sim808::device.getGpsField(lastPosition, SIM808_GPS_FIELD::SPEED, &velocity);
+	}
+
 	void getTime(tmElements_t &time) {
 		char *timeStr;
 		char buffer[4];
diff --git a/GpsTracker/Gps.h b/GpsTracker/Gps.h
index ddc9e0e..bb231ec 100644
--- a/GpsTracker/Gps.h
+++ b/GpsTracker/Gps.h
@@ -16,6 +16,7 @@ namespace gps {
 	inline void powerOff() { hardware::sim808::gpsPowerOff(); }
 
 	SIM808_GPS_STATUS acquireCurrentPosition(uint16_t timeout);
-	void getTime(tmElements_t &time);
 
+	void getVelocity(uint8_t &velocity);
+	void getTime(tmElements_t &time);
 }
\ No newline at end of file
diff --git a/GpsTracker/GpsTracker.h b/GpsTracker/GpsTracker.h
index 3723942..a1c5bd0 100644
--- a/GpsTracker/GpsTracker.h
+++ b/GpsTracker/GpsTracker.h
@@ -5,6 +5,7 @@
 #include <SIM808.h>
 
 #include "Debug.h"
+#include "Config.h"
 #include "Core.h"
 
 #include "Gps.h"
diff --git a/GpsTracker/GpsTracker.ino b/GpsTracker/GpsTracker.ino
index 9647e3e..931156d 100644
--- a/GpsTracker/GpsTracker.ino
+++ b/GpsTracker/GpsTracker.ino
@@ -13,9 +13,8 @@ void setup() {
 }
 
 void loop() {
-
 	gps::powerOn();
-	SIM808_GPS_STATUS gpsStatus = gps::acquireCurrentPosition(30000);
+	SIM808_GPS_STATUS gpsStatus = gps::acquireCurrentPosition(GPS_DEFAULT_TOTAL_TIMEOUT_MS);
 	gps::powerOff();
 
 	if (gpsStatus > SIM808_GPS_STATUS::NO_FIX) {
@@ -26,11 +25,15 @@ void loop() {
 		rtc::powerOff();
 
 		positions::appendLast();
+
+		uint8_t velocity;
+		gps::getVelocity(velocity);
+		core::setSleepTime(velocity);
 	}
 
 	if (positions::needsToSend()) {
 		positions::send();
 	}
 
-	mainunit::deepSleep(10); //TODO : duration
+	mainunit::deepSleep(core::sleepTime);
 }
diff --git a/GpsTracker/Time2.h b/GpsTracker/Time2.h
index 3afa0c8..5a545aa 100644
--- a/GpsTracker/Time2.h
+++ b/GpsTracker/Time2.h
@@ -10,30 +10,18 @@
 
 /*============================================================================*/
 
-	typedef unsigned long timestamp_t;
-
-	typedef struct {
-		uint8_t Second;
-		uint8_t Minute;
-		uint8_t Hour;
-		uint8_t Wday;   // day of week, sunday is day 1
-		uint8_t Day;
-		uint8_t Month;
-		uint8_t Year;   // offset from 1970; 
-	} 	tmElements_t, TimeElements, *tmElementsPtr_t;
-
-	/* low level functions to convert to and from system time                     */
-	void breakTime(const timestamp_t time, tmElements_t &tm);  // break timestamp_t into elements
-	timestamp_t makeTime(const tmElements_t &tm);  // convert time elements into timestamp_t
-
-	tmElements_t operator += (const timestamp_t b) {
-
-	}
-
-	tmElements_t operator + (const tmElements_t a, const timestamp_t b) {
-		tmElements_t result;
-		timestamp_t aTimestamp = makeTime(a);
-		breakTime(aTimestamp + b, result);
-
-		return result;
-	}
+typedef unsigned long timestamp_t;
+
+typedef struct {
+	uint8_t Second;
+	uint8_t Minute;
+	uint8_t Hour;
+	uint8_t Wday;   // day of week, sunday is day 1
+	uint8_t Day;
+	uint8_t Month;
+	uint8_t Year;   // offset from 1970;
+} 	tmElements_t, TimeElements, *tmElementsPtr_t;
+
+/* low level functions to convert to and from system time                     */
+void breakTime(const timestamp_t time, tmElements_t &tm);  // break timestamp_t into elements
+timestamp_t makeTime(const tmElements_t &tm);  // convert time elements into timestamp_t