From 3fdb6e590791abcafdcdbf04c320f1c80ddd04af Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Wed, 24 Jan 2018 23:29:17 +0100 Subject: [PATCH] Implemented wait for gps position and use it to set rtc time --- GpsTracker/Gps.cpp | 37 ++++++++++++++++++++++++++---- GpsTracker/Gps.h | 4 ++-- GpsTracker/GpsTracker.ino | 7 +++--- GpsTracker/Rtc.cpp | 47 +++++++++++++++++---------------------- GpsTracker/Rtc.h | 10 +++++---- GpsTracker/Time2.cpp | 2 ++ 6 files changed, 66 insertions(+), 41 deletions(-) diff --git a/GpsTracker/Gps.cpp b/GpsTracker/Gps.cpp index 25375b9..1f611e0 100644 --- a/GpsTracker/Gps.cpp +++ b/GpsTracker/Gps.cpp @@ -1,26 +1,55 @@ #include "Gps.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 +#define TIME_DAY_OFFSET 6 +#define TIME_HOUR_OFFSET 8 +#define TIME_MINUTE_OFFSET 10 +#define TIME_SECOND_OFFSET 12 + +#define STRTOUL_SUBSTRING(dst, src, size) strtoul(strncpy(dst, src , size), NULL, 2); namespace gps { char lastPosition[GPS_POSITION_SIZE]; SIM808_GPS_STATUS lastStatus; - SIM808_GPS_STATUS acquireCurrentPosition() { + SIM808_GPS_STATUS acquireCurrentPosition(uint16_t timeout) { SIM808_GPS_STATUS currentStatus = SIM808_GPS_STATUS::OFF; - //TODO : do while (!timeout && < accurate_fix) + do { + currentStatus = hardware::sim808::device.getGpsStatus(); + if (currentStatus > SIM808_GPS_STATUS::NO_FIX) break; + + mainunit::deepSleep(WAIT_FOR_FIX_DELAY); + timeout -= WAIT_FOR_FIX_DELAY; + } while (timeout > 1); + if (currentStatus > SIM808_GPS_STATUS::NO_FIX) { lastStatus = currentStatus; + hardware::sim808::device.getGpsPosition(lastPosition); } return currentStatus; } - timestamp_t getTime() { - + void getTime(tmElements_t &time) { + char *timeStr; + char buffer[4]; + + hardware::sim808::device.getGpsField(lastPosition, SIM808_GPS_FIELD::UTC, timeStr); + + time.Year = STRTOUL_SUBSTRING(buffer, timeStr + TIME_YEAR_OFFSET, 4); + time.Month = STRTOUL_SUBSTRING(buffer, timeStr + TIME_MONTH_OFFSET, 2); + time.Day = STRTOUL_SUBSTRING(buffer, timeStr + TIME_DAY_OFFSET, 2); + time.Hour = STRTOUL_SUBSTRING(buffer, timeStr + TIME_HOUR_OFFSET, 2); + time.Minute = STRTOUL_SUBSTRING(buffer, timeStr + TIME_MINUTE_OFFSET, 2); + time.Second = STRTOUL_SUBSTRING(buffer, timeStr + TIME_SECOND_OFFSET, 2); } } \ No newline at end of file diff --git a/GpsTracker/Gps.h b/GpsTracker/Gps.h index c8d1743..ddc9e0e 100644 --- a/GpsTracker/Gps.h +++ b/GpsTracker/Gps.h @@ -15,7 +15,7 @@ namespace gps { inline void powerOn() { hardware::sim808::gpsPowerOn(); } inline void powerOff() { hardware::sim808::gpsPowerOff(); } - SIM808_GPS_STATUS acquireCurrentPosition(); - timestamp_t getTime(); + SIM808_GPS_STATUS acquireCurrentPosition(uint16_t timeout); + void getTime(tmElements_t &time); } \ No newline at end of file diff --git a/GpsTracker/GpsTracker.ino b/GpsTracker/GpsTracker.ino index 08953a6..e9fd6a6 100644 --- a/GpsTracker/GpsTracker.ino +++ b/GpsTracker/GpsTracker.ino @@ -17,13 +17,12 @@ void setup() { void loop() { gps::powerOn(); - SIM808_GPS_STATUS gpsStatus = gps::acquireCurrentPosition(); + SIM808_GPS_STATUS gpsStatus = gps::acquireCurrentPosition(30000); gps::powerOff(); if (gpsStatus > SIM808_GPS_STATUS::NO_FIX) { - timestamp_t time = gps::getTime(); - tmElements_t t; - breakTime(time, t); + tmElements_t time; + gps::getTime(time); rtc::powerOn(); rtc::setTime(time); rtc::powerOff(); diff --git a/GpsTracker/Rtc.cpp b/GpsTracker/Rtc.cpp index 17b549a..053916d 100644 --- a/GpsTracker/Rtc.cpp +++ b/GpsTracker/Rtc.cpp @@ -10,31 +10,24 @@ namespace rtc { namespace details { - timestamp_t readTimeFromRegisters() { - tmElements_t tmElements = { - RTC.s, - RTC.m, - RTC.h, - RTC.dow, - RTC.dd, - RTC.mm, - CalendarYrToTm(RTC.yyyy) - }; - - return makeTime(tmElements); + void readTimeFromRegisters(tmElements_t &time) { + time.Second = RTC.s; + time.Minute = RTC.m; + time.Hour = RTC.h; + time.Wday = RTC.dow; + time.Day = RTC.dd; + time.Month = RTC.mm; + time.Year = CalendarYrToTm(RTC.yyyy); } - void writeTimeToRegisters(timestamp_t &time) { - tmElements_t tmElements; - breakTime(time, tmElements); - - RTC.s = tmElements.Second; - RTC.m = tmElements.Minute; - RTC.h = tmElements.Hour; - RTC.dow = tmElements.Wday; - RTC.dd = tmElements.Day; - RTC.mm = tmElements.Month; - RTC.yyyy = tmYearToCalendar(tmElements.Year); + void writeTimeToRegisters(tmElements_t &time) { + RTC.s = time.Second; + RTC.m = time.Minute; + RTC.h = time.Hour; + RTC.dow = time.Wday; + RTC.dd = time.Day; + RTC.mm = time.Month; + RTC.yyyy = tmYearToCalendar(time.Year); } } @@ -44,17 +37,17 @@ namespace rtc { RTC.control(DS3231_INT_ENABLE, DS3231_OFF); //INTCN OFF } - timestamp_t getTime() { + void getTime(tmElements_t &time) { RTC.readTime(); - return details::readTimeFromRegisters(); + details::readTimeFromRegisters(time); } - void setTime(timestamp_t &time) { + void setTime(tmElements_t &time) { details::writeTimeToRegisters(time); RTC.writeTime(); } - void setAlarm(timestamp_t &time) { + void setAlarm(tmElements_t &time) { details::writeTimeToRegisters(time); RTC.writeAlarm1(DS3231_ALM_S); diff --git a/GpsTracker/Rtc.h b/GpsTracker/Rtc.h index 706c04f..74fe803 100644 --- a/GpsTracker/Rtc.h +++ b/GpsTracker/Rtc.h @@ -14,12 +14,14 @@ namespace rtc { void setup(); - timestamp_t getTime(); - void setTime(timestamp_t &time); + void getTime(tmElements_t &time); + void setTime(tmElements_t &time); inline void setAlarm(uint16_t seconds) { - setAlarm(getTime() + seconds); + tmElements_t time; + getTime(time); + setAlarm(makeTime(time) + seconds); //TODO : use operator } - void setAlarm(timestamp_t &time); + void setAlarm(tmElements_t &time); } \ No newline at end of file diff --git a/GpsTracker/Time2.cpp b/GpsTracker/Time2.cpp index 02208ca..374a805 100644 --- a/GpsTracker/Time2.cpp +++ b/GpsTracker/Time2.cpp @@ -128,3 +128,5 @@ timestamp_t makeTime(tmElements_t &tm) { } /*=====================================================*/ /* Low level system time functions */ + +//TODO : + and - operator between tmElements_t and timestamp_t \ No newline at end of file