Fork of RTC-DS1307 by
Diff: Rtc_Ds1307.cpp
- Revision:
- 7:dca20be3ef38
- Parent:
- 5:30531f2121a2
- Child:
- 8:d0e66fa78e79
diff -r bba89618ee63 -r dca20be3ef38 Rtc_Ds1307.cpp --- a/Rtc_Ds1307.cpp Sun Jun 23 18:26:47 2013 +0000 +++ b/Rtc_Ds1307.cpp Sun Jun 23 18:48:59 2013 +0000 @@ -1,6 +1,6 @@ -/* Rtc_Ds1307.cpp */ #include "mbed.h" #include "Rtc_Ds1307.h" + #ifndef DEBUG //#define DEBUG #endif @@ -197,4 +197,65 @@ } m_rtc->stop(); return true; -} \ No newline at end of file +} + + + + +RtcCls::RtcCls(PinName sda, PinName scl, PinName sqw, bool bUseSqw) + : Rtc_Ds1307(sda, scl), m_sqw(sqw), m_bUseSqw(bUseSqw), m_bAlarmEnabled(false), m_alarmfunc(NULL) +{ + // Only register the callback and start the SQW if requested to do so. Otherwise the system + // will use the MBED built-in RTC. + if (m_bUseSqw) { + Time_rtc t; + // start the wave + setSquareWaveOutput(true, RS1Hz); + // query time + getTime(t); + struct tm now = {t.sec, t.min, t.hour, t.date, t.mon, t.year}; + m_time = mktime(&now); + // register callback from now on the time will be maintained by the square wave input + m_sqw.rise(this, &RtcCls::_callback); + } +} + +void RtcCls::_callback(void) +{ + // Simply increase the number of seconds + m_time++; + if (m_bAlarmEnabled && (m_time == m_alarmTime)) { + if (m_alarmfunc != NULL) + m_alarmfunc(); + m_bAlarmEnabled = false; + } +} + +time_t RtcCls::getTime() +{ + // when not using the HW support, we have to query the RTC chip. Other wise we can just return out stored value + if (!m_bUseSqw) { + Time_rtc t; + getTime(t); + struct tm now = {t.sec, t.min, t.hour, t.date, t.mon, t.year}; + m_time = mktime(&now); + } + return m_time; +} + +void RtcCls::setTime(time_t t) +{ + Time_rtc tim; + struct tm *now; + now = localtime(&t); + + tim.sec = now->tm_sec; + tim.min = now->tm_min; + tim.hour = now->tm_hour; + tim.date = now->tm_mday; + tim.mon = now->tm_mon; + tim.year = now->tm_year + 1900; + tim.wday = now->tm_wday +1; + + setTime( tim, true, true); +}