From edcb3afb9200bdbb65f6ccf326433ff23b4338cb Mon Sep 17 00:00:00 2001 From: mgtm98 <43015074+mgtm98@users.noreply.github.com> Date: Thu, 5 Mar 2020 17:17:37 +0400 Subject: [PATCH 1/2] Default date/time are compile date/time --- src/RTCZero.cpp | 34 ++++++++++++++++++++++++++++++++++ src/RTCZero.h | 1 + 2 files changed, 35 insertions(+) diff --git a/src/RTCZero.cpp b/src/RTCZero.cpp index 71bfd8d..e3077e9 100644 --- a/src/RTCZero.cpp +++ b/src/RTCZero.cpp @@ -103,6 +103,7 @@ void RTCZero::begin(bool resetTime) while (RTCisSyncing()) ; + RTCdefaultTime(); _configured = true; } @@ -520,3 +521,36 @@ void RTCZero::RTCresetRemove() while (RTCisSyncing()) ; } + +void RTCZero::RTCdefaultTime() +{ + String time = __DATE__; // get compile time + uint8_t hours = t.substring(0, 2).toInt(); + uint8_t minutes = t.substring(3, 5).toInt(); + uint8_t seconds = t.substring(6, 8).toInt(); + setTime(hours,minutes, seconds); // get compile date + String date = __DATE__; + String monStr = t.substring(0,3); + uint8_t monInt = 0; + // check for the last char + // in Mar & Apr - Jan & Jun the last char is identical so check the second one + switch(monStr[2]){ + case 'n': + monInt = (monStr[1] == 'a')? 1 : 6; + break; + case 'b': monInt = 2; break; + case 'r': + monInt = (monStr[1] == 'a')? 3 : 4; + break; + case 'y': monInt = 5; break; + case 'l': monInt = 7; break; + case 'g': monInt = 8; break; + case 'p': monInt = 9; break; + case 't': monInt = 10; break; + case 'v': monInt = 11; break; + case 'c': monInt = 12; break; + } + uint8_t day = t.substring(4,6).toInt() + uint8_t year = t.substring(7).toInt() + setDate(day, monInt, year); +} \ No newline at end of file diff --git a/src/RTCZero.h b/src/RTCZero.h index f74f091..f37c8dc 100644 --- a/src/RTCZero.h +++ b/src/RTCZero.h @@ -112,6 +112,7 @@ class RTCZero { void RTCenable(); void RTCreset(); void RTCresetRemove(); + void RTCdefaultTime(); }; #endif // RTC_ZERO_H From 2a6843166ef31d0ba79cae19abcf4eb960e339e3 Mon Sep 17 00:00:00 2001 From: mgtm98 <43015074+mgtm98@users.noreply.github.com> Date: Thu, 5 Mar 2020 17:34:01 +0400 Subject: [PATCH 2/2] refactored monString to nmonInt in a separate function --- src/RTCZero.cpp | 20 +++++++++++++------- src/RTCZero.h | 3 ++- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/RTCZero.cpp b/src/RTCZero.cpp index e3077e9..c6573ce 100644 --- a/src/RTCZero.cpp +++ b/src/RTCZero.cpp @@ -103,7 +103,7 @@ void RTCZero::begin(bool resetTime) while (RTCisSyncing()) ; - RTCdefaultTime(); + RTCsetCompileTime(); _configured = true; } @@ -522,15 +522,23 @@ void RTCZero::RTCresetRemove() ; } -void RTCZero::RTCdefaultTime() +void RTCZero::RTCsetCompileTime() { String time = __DATE__; // get compile time uint8_t hours = t.substring(0, 2).toInt(); uint8_t minutes = t.substring(3, 5).toInt(); uint8_t seconds = t.substring(6, 8).toInt(); - setTime(hours,minutes, seconds); // get compile date - String date = __DATE__; + setTime(hours,minutes, seconds); + + String date = __DATE__; // get compile date String monStr = t.substring(0,3); + uint8_t day = t.substring(4,6).toInt() + uint8_t year = t.substring(7).toInt() + setDate(day, monIndex(monStr), year); +} + +uint8_t RTCZero::monIndex(String monStr) +{ uint8_t monInt = 0; // check for the last char // in Mar & Apr - Jan & Jun the last char is identical so check the second one @@ -550,7 +558,5 @@ void RTCZero::RTCdefaultTime() case 'v': monInt = 11; break; case 'c': monInt = 12; break; } - uint8_t day = t.substring(4,6).toInt() - uint8_t year = t.substring(7).toInt() - setDate(day, monInt, year); + return monInt; } \ No newline at end of file diff --git a/src/RTCZero.h b/src/RTCZero.h index f37c8dc..eddc847 100644 --- a/src/RTCZero.h +++ b/src/RTCZero.h @@ -112,7 +112,8 @@ class RTCZero { void RTCenable(); void RTCreset(); void RTCresetRemove(); - void RTCdefaultTime(); + void RTCsetCompileTime(); + uint8_t monIndex(String monStr); }; #endif // RTC_ZERO_H