This is a library for managing the mbed real time clock, including functions for setting and getting the rtc in text format, getting and setting the timezone offset, and getting and setting the calibration register, both directly, and by using an adjustment API to automatically set the calibration value.

Dependents:   LAB4WEEK1 Motor

Files at this revision

API Documentation at this revision

Comitter:
WiredHome
Date:
Wed Nov 02 01:32:07 2011 +0000
Parent:
5:fbbdf57675c3
Commit message:
v1.04 Minor changes to the adjustment logic for complexity reduction

Changed in this revision

TimeUtilities.cpp Show annotated file Show diff for this revision Revisions of this file
TimeUtilities.h Show annotated file Show diff for this revision Revisions of this file
diff -r fbbdf57675c3 -r a517fee06e2e TimeUtilities.cpp
--- a/TimeUtilities.cpp	Sun Oct 30 23:42:43 2011 +0000
+++ b/TimeUtilities.cpp	Wed Nov 02 01:32:07 2011 +0000
@@ -20,7 +20,7 @@
 #include "mbed.h"
 #endif
 
-#define VERSION "1.03"
+#define VERSION "1.04"
 
 #include "TimeUtilities.h"
 #include <time.h>
@@ -43,7 +43,6 @@
 
 static int tzOffsetHr = 0;       ///!< time zone offset hours to print time in local time
 static int tzOffsetMin = 0;      ///!< time zone offset minutes to print time in local time
-static time_t timeWhenLastSet = 0;
 
 
 static const char ver[] = VERSION;
@@ -55,7 +54,6 @@
 
 RealTimeClock::RealTimeClock() {
     GetTimeOffsetStore();
-    GetTimeLastSetStore();
 }
 
 
@@ -70,10 +68,6 @@
     return time(NULL);
 }
 
-time_t RealTimeClock::GetTimeValueWhenSet() {
-    return timeWhenLastSet;
-}
-
 
 void RealTimeClock::GetTimeString(char *buf, time_t tValue) {
     GetTimeOffsetStore();       // Load the time zone offset values from the battery ram
@@ -138,12 +132,12 @@
     tzOffsetMin = (LPC_RTC->GPREG0 & 0xFF);
 }
 
-void RealTimeClock::SetTimeLastSetStore() {
-    LPC_RTC->GPREG1 = timeWhenLastSet;
+void RealTimeClock::SetTimeLastSet(time_t t) {
+    LPC_RTC->GPREG1 = t;
 }
 
-void RealTimeClock::GetTimeLastSetStore() {
-    timeWhenLastSet = LPC_RTC->GPREG1;
+time_t RealTimeClock::GetTimeLastSet() {
+    return LPC_RTC->GPREG1;
 }
 
 // MM/DD/YYYY HH:MM:SS [(+/-hh:mm)]
@@ -187,8 +181,7 @@
                             seconds = mktime(t);
                             seconds = seconds - (time_t)(tzOffsetHr * 3600 + tzOffsetMin * 60);
                             set_time(seconds);
-                            timeWhenLastSet = seconds;
-                            SetTimeLastSetStore();
+                            SetTimeLastSet(seconds);
                         }
                     }
                 }
@@ -199,18 +192,16 @@
 }
 
 bool RealTimeClock::AdjustBySeconds(int32_t adjustSeconds) {
-    if (timeWhenLastSet != 0) {
+    time_t lastSet = GetTimeLastSet();
+    
+    if (lastSet != 0) {
         time_t seconds = time(NULL);    // get "now" according to the rtc
-        int32_t delta = seconds - timeWhenLastSet;
+        int32_t delta = seconds - lastSet;
         int32_t curCal = GetTimeCalibration();
         int32_t calMAX = 131071;
         int32_t secPerDay = 86400;
         float errSecPerDay;
                 
-        // Make the clock correct
-        seconds = seconds + adjustSeconds;
-        set_time(seconds);
-        
         // Convert the current calibration and the adjustment into
         // the new calibration value
         // assume it is +10sec and it has been 2days, then the adjustment
@@ -219,6 +210,12 @@
         // delta = now - then (number of elapsed seconds)
         if (adjustSeconds != 0 && delta != 0) {
             int32_t calFactor;
+
+            // Make the clock correct
+            seconds = seconds + adjustSeconds;
+            set_time(seconds);
+            SetTimeLastSet(seconds);
+            // Compute the calibration factor
             errSecPerDay = (float)adjustSeconds / ((float)(delta)/secPerDay);
             calFactor = (int32_t)((float)secPerDay/errSecPerDay);
             if (abs(calFactor) < calMAX)
diff -r fbbdf57675c3 -r a517fee06e2e TimeUtilities.h
--- a/TimeUtilities.h	Sun Oct 30 23:42:43 2011 +0000
+++ b/TimeUtilities.h	Wed Nov 02 01:32:07 2011 +0000
@@ -19,6 +19,8 @@
 ///
 /// @note
 /// History
+/// v1.04 20111101
+/// \li Minor change to the APIs to reduce some of the complexity
 /// v1.03 20111030
 /// \li Adding an API to adjust time and calibration based on how far it is
 ///     off "now", based on when it was set.
@@ -95,16 +97,6 @@
     ///
     time_t GetTimeValue();
     
-    /// GetTimeValueWhenSet gets the time in time_t seconds when the clock was last set
-    ///
-    /// This value represents the time_t time when the clock was set.
-    /// This value is not preserved through power cycles, or resets, in which
-    /// case it will return 0.
-    ///
-    /// @returns time in time_t format when the clock was last set
-    ///
-    time_t GetTimeValueWhenSet();
-    
     /// GetTimeString gets the formatted time applying the internal time-zone offset of hours and minutes
     ///
     /// It places the formatted string into the buffer in the format of
@@ -145,6 +137,16 @@
     ///
     void GetTimeString(char * buffer, time_t tValue, int hOffset, int mOffset);
 
+    /// GetTimeLastSet will extract the time_t when last set from RTC ram
+    ///
+    /// When the clock is correctly set, the time value will have been stored
+    /// which permits later adjustments to the calibration based on 
+    /// elapsed time and knowing when it was set. This function retrieves it.
+    ///
+    /// @returns time_t value when the time was last set
+    ///
+    time_t GetTimeLastSet();
+
     /// SetTimeOffset will set the hour and minute timezone offset
     ///
     /// @param offsetHour is the timezone offset in hours
@@ -244,26 +246,17 @@
     ///
     void GetTimeOffsetStore();
     
-    /// SetTimeLastSetStore will store the time_t when last set in RTC ram
+    /// SetTimeLastSet will store the time_t when last set in RTC ram
     ///
     /// When the clock is correctly set, the time value will be stored
     /// which permits later adjustments to the calibration based on 
     /// elapsed time and knowing when it was set.
     ///
+    /// @param t is the time_t value that should be saved
     /// @returns nothing
     ///
-    void SetTimeLastSetStore();
+    void SetTimeLastSet(time_t);
     
-    /// GetTimeLastSetStore will extract the time_t when last set from RTC ram
-    ///
-    /// When the clock is correctly set, the time value will have been stored
-    /// which permits later adjustments to the calibration based on 
-    /// elapsed time and knowing when it was set. This function retrieves it.
-    ///
-    /// @returns nothing
-    ///
-    void GetTimeLastSetStore();
-
 };
 
 #endif