Simple RTC class based on DS1307. Emphasis on simple. Allows you to run at 100k or 400k Hz (for newer DS1307 capable devices). MapTime() allows you to set the time() service to the same as the RTC. Uses struct tm throughout so you can use traditional time functions for manipulation.

Dependents:   AdaFruit_RGBLCD

Revision:
7:3621025e7949
Parent:
5:d71d6e5a7eee
Child:
9:3a0ba8364ef2
--- a/RTclock.cpp	Sat Aug 09 17:05:44 2014 +0000
+++ b/RTclock.cpp	Sun Aug 10 12:34:32 2014 +0000
@@ -1,8 +1,6 @@
 #include "mbed.h"
 #include "RTclock.h"
 
-const char * RTclock::m_aWeekDays[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
-
 RTclock::RTclock(PinName in_nSDA, PinName in_nSCL, bool in_bHiSpeed)
     : RTclock_parent(in_nSDA,in_nSCL)
     , m_bTwelveHour(false)
@@ -15,17 +13,17 @@
 {
 }
 
-int RTclock::BcdToDecimal(int in_nBCD)
+int RTclock::bcdToDecimal(int in_nBCD)
 {
     return ((in_nBCD & 0xF0) >> 4) * 10 + (in_nBCD & 0x0F);
 }
 
-int RTclock::DecimalToBcd(int in_nDecimal)
+int RTclock::decimalToBcd(int in_nDecimal)
 {
     return (in_nDecimal % 10) + ((in_nDecimal / 10) << 4);
 }
 
-bool RTclock::GetTime(tm & out_sTM)
+bool RTclock::getTime(tm & out_sTM)
 {
     char aBuffer[7];
     
@@ -33,45 +31,40 @@
 
     m_bTwelveHour = ((aBuffer[2] & 0x40) == 0x40);
     
-    out_sTM.tm_sec = BcdToDecimal(aBuffer[0] & 0x7f);
-    out_sTM.tm_min = BcdToDecimal(aBuffer[1]);
+    out_sTM.tm_sec = bcdToDecimal(aBuffer[0] & 0x7f);
+    out_sTM.tm_min = bcdToDecimal(aBuffer[1]);
     
     if (m_bTwelveHour)
     {
         // add 12 hours if PM bit is set and past midday
-        out_sTM.tm_hour = BcdToDecimal(aBuffer[2] & 31);
+        out_sTM.tm_hour = bcdToDecimal(aBuffer[2] & 31);
         
         bool bPM = (0 != (aBuffer[2] & 0x20));
         if (bPM && 12 != out_sTM.tm_hour) out_sTM.tm_hour += 12;
     }
     else
     {
-        out_sTM.tm_hour = BcdToDecimal(aBuffer[2] & 0x3f);
+        out_sTM.tm_hour = bcdToDecimal(aBuffer[2] & 0x3f);
     }
     
-    out_sTM.tm_wday = aBuffer[3]; 
-    out_sTM.tm_mday = BcdToDecimal(aBuffer[4]);
-    out_sTM.tm_mon  = BcdToDecimal(aBuffer[5]);
-    out_sTM.tm_year = (BcdToDecimal(aBuffer[6]) + 2000) - 1900;   //  Returns from 2000, need form 1900 for time function
+    out_sTM.tm_wday = aBuffer[3] % 7;
+    out_sTM.tm_mday = bcdToDecimal(aBuffer[4]);
+    out_sTM.tm_mon  = bcdToDecimal(aBuffer[5]);
+    out_sTM.tm_year = (bcdToDecimal(aBuffer[6]) + 2000) - 1900;   //  Returns from 2000, need form 1900 for time function
     out_sTM.tm_isdst = 0;
     
     return true;
 }
 
-const char * RTclock::GetWeekday(int in_nWeekDay)
-{
-    return m_aWeekDays[in_nWeekDay];
-}
-
- bool RTclock::IsTwelveHour()
+bool RTclock::isTwelveHour()
 {
     return m_bTwelveHour;
 }
 
-bool RTclock::MapTime()
+bool RTclock::mapTime()
 {
     tm sTM;
-    if (!GetTime(sTM)) return false;
+    if (!getTime(sTM)) return false;
     
     // Convert and set internal time
     time_t nTime = ::mktime(&sTM);
@@ -90,7 +83,7 @@
     return true;
 }
 
-bool RTclock::SetTime(const tm  & in_sTM, bool in_bTwelveHour)
+bool RTclock::setTime(const tm  & in_sTM, bool in_bTwelveHour)
 {
     char aBuffer[7];
 
@@ -104,13 +97,13 @@
     if (in_bTwelveHour) nHour %= 12;
     
     aBuffer[0] &= 0x7f;
-    aBuffer[0] = (aBuffer[0] & 0x80) | (DecimalToBcd(in_sTM.tm_sec)& 0x7f);
-    aBuffer[1] = DecimalToBcd(in_sTM.tm_min);    
-    aBuffer[2] = (aBuffer[2] & 0xc4) | (DecimalToBcd(nHour) & 0x3f);
+    aBuffer[0] = (aBuffer[0] & 0x80) | (decimalToBcd(in_sTM.tm_sec)& 0x7f);
+    aBuffer[1] = decimalToBcd(in_sTM.tm_min);    
+    aBuffer[2] = (aBuffer[2] & 0xc4) | (decimalToBcd(nHour) & 0x3f);
     aBuffer[3] = in_sTM.tm_wday;
-    aBuffer[4] = DecimalToBcd(in_sTM.tm_mday);
-    aBuffer[5] = DecimalToBcd(in_sTM.tm_mon);
-    aBuffer[6] = DecimalToBcd(in_sTM.tm_year + 1900 - 2000);
+    aBuffer[4] = decimalToBcd(in_sTM.tm_mday);
+    aBuffer[5] = decimalToBcd(in_sTM.tm_mon);
+    aBuffer[6] = decimalToBcd(in_sTM.tm_year + 1900 - 2000);
 
     // Handle the 12hr clock bits
     if (in_bTwelveHour)
@@ -127,16 +120,16 @@
     }
     
     // Write new date and time
-    SetRunning(false);
+    setRunning(false);
     
     bool bSuccess = write(0, aBuffer, 7);
     
-    if (bSuccess) SetRunning(true);
+    if (bSuccess) setRunning(true);
 
     return bSuccess;
 }
 
-bool RTclock::SetRunning(bool in_bEnable)
+bool RTclock::setRunning(bool in_bEnable)
 {
     char nRunning;
     
@@ -155,7 +148,7 @@
     return write(0, &nRunning, 1);
 }
 
-bool RTclock::SetSquareWaveOutput
+bool RTclock::setSquareWaveOutput
 (
     bool                in_bEnable,
     ESquareWaveRates    in_nRateSelect