pour olivier
Fork of RTC-DS1307 by
Revision 4:d2cc690aaac7, committed 2013-06-23
- Comitter:
- leihen
- Date:
- Sun Jun 23 16:29:35 2013 +0000
- Parent:
- 3:e89d63f3342e
- Child:
- 5:30531f2121a2
- Commit message:
- Added a RTC wrapper class.
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RtcCls.c Sun Jun 23 16:29:35 2013 +0000 @@ -0,0 +1,68 @@ +#include "RtcCls.h" +#define DEBUG +#include "debug.h" + + + + +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 t; + // start the wave + setSquareWaveOutput(true, RS_1Hz); + // query time + getTime(t); + struct tm now = {t.sec, t.min, t.hour, t.date, t.mon, t.year}; + m_time = mktime(&tm); + // register callback from now on the time will be maintained by the square wave input + m_sqw.rise(_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 t; + getTime(t); + struct tm now = {t.sec, t.min, t.hour, t.date, t.mon, t.year}; + m_time = mktime(&tm); + } + return m_time; +} + +void RtcCls::setTime(time_t t) +{ + Time 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); +} + + + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RtcCls.h Sun Jun 23 16:29:35 2013 +0000 @@ -0,0 +1,38 @@ +#ifndef __RTC_H__ +#define __RTC_H__ + +#include "Rtc_Ds1307.h" +#include "mbed.h" + +typedef void (*callback_t)(void); + +class RtcCls : public Rtc_Ds1307 +{ + protected: + InterruptIn m_sqw; + bool m_bUseSqw; + time_t m_time; // Only used in case of SQW use + + bool m_bAlarmEnabled; + callback_t m_alarmfunc; + time_t m_alarmTime; + + public: + RtcCls(PinName sda, PinName scl, PinName sqw, bool bUseSqw); + + protected: + static void _callback(void); + + public: + time_t getTime(); + void setTime(time_t t); + public: + void setAlarm(int nSeconds, callback_t alarmfunc) + { + m_alarmfunc = alarmfunc; + m_alarmTime = m_time + nSeconds; + m_bAlarmEnabled = (alarmfunc == NULL) ? false : true; + } +}; + +#endif // __RTC_H__ \ No newline at end of file
--- a/Rtc_Ds1307.cpp Sun Jun 23 11:26:26 2013 +0000 +++ b/Rtc_Ds1307.cpp Sun Jun 23 16:29:35 2013 +0000 @@ -1,7 +1,8 @@ /* Rtc_Ds1307.cpp */ #include "Rtc_Ds1307.h" -#define DEBUG +//#define DEBUG +#undef DEBUG #include "debug.h" const char *Rtc_Ds1307::m_weekDays[] = { "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };