From 7e112df110de6316549e5bc6806446be954dfdcd Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 10 Mar 2018 12:09:36 +1300 Subject: [PATCH] Fixed RTC time get from GPS --- GpsTracker/Gps.cpp | 29 +++++++++++++++++++---------- GpsTracker/Rtc.cpp | 3 +++ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/GpsTracker/Gps.cpp b/GpsTracker/Gps.cpp index e06b473..ea6802f 100644 --- a/GpsTracker/Gps.cpp +++ b/GpsTracker/Gps.cpp @@ -13,10 +13,15 @@ #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 { + namespace details { + uint8_t getTmCompoment(char *buffer, char *start, uint8_t size) { + strlcpy(buffer, start, size + 1); + return static_cast(strtoul(buffer, NULL, 10)); + } + + } char lastPosition[GPS_POSITION_SIZE]; SIM808_GPS_STATUS lastStatus; @@ -45,15 +50,19 @@ namespace gps { void getTime(tmElements_t &time) { char *timeStr; - char buffer[4]; + char buffer[5]; + char* end; + hardware::sim808::device.getGpsField(lastPosition, SIM808_GPS_FIELD::UTC, &timeStr); + + VERBOSE_FORMAT("getTime", "%s", timeStr); - hardware::sim808::device.getGpsField(lastPosition, SIM808_GPS_FIELD::UTC, timeStr); + time.Year = CalendarYrToTm(details::getTmCompoment(buffer, timeStr + TIME_YEAR_OFFSET, 4)); + time.Month = details::getTmCompoment(buffer, timeStr + TIME_MONTH_OFFSET, 2); + time.Day = details::getTmCompoment(buffer, timeStr + TIME_DAY_OFFSET, 2); + time.Hour = details::getTmCompoment(buffer, timeStr + TIME_HOUR_OFFSET, 2); + time.Minute = details::getTmCompoment(buffer, timeStr + TIME_MINUTE_OFFSET, 2); + time.Second = details::getTmCompoment(buffer, timeStr + TIME_SECOND_OFFSET, 2); - 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); + VERBOSE_FORMAT("getTime", "%d/%d/%d %d:%d:%d", tmYearToCalendar(time.Year), time.Month, time.Day, time.Hour, time.Minute, time.Second); } } \ No newline at end of file diff --git a/GpsTracker/Rtc.cpp b/GpsTracker/Rtc.cpp index ec38d68..0b74d7b 100644 --- a/GpsTracker/Rtc.cpp +++ b/GpsTracker/Rtc.cpp @@ -1,3 +1,5 @@ +#include "Debug.h" + #include "Rtc.h" #include "Pins.h" @@ -43,6 +45,7 @@ namespace rtc { } void setTime(tmElements_t &time) { + VERBOSE_FORMAT("setTime", "%d/%d/%d %d:%d:%d", tmYearToCalendar(time.Year), time.Month, time.Day, time.Hour, time.Minute, time.Second); details::writeTimeToRegisters(time); RTC.writeTime(); }