Software implemented real time clock driven by a Ticker. No external hardware (like DS1307 or DS3231 or etc.) is needed. Should work on any mbed platform where Ticker works.
See demo:
Import programClock_Hello
Demo for the Clock library (real time clock driven by a Ticker).
Diff: Clock.cpp
- Revision:
- 5:d65fc7060635
- Parent:
- 4:bbe7d7474acd
- Child:
- 6:7edabed68b0f
--- a/Clock.cpp Fri Apr 01 07:23:02 2016 +0000 +++ b/Clock.cpp Fri May 20 18:14:34 2016 +0000 @@ -29,10 +29,10 @@ #include "mbed.h" #include "Clock.h" -// static member initialization +// Static member initialization time_t Clock::_time = 0; tm Clock::_tm = {0, 0, 0, 0, 0, 0, -1}; -ClockFnc_t Clock::_onTick = NULL; +FunctionPointer Clock::_onTick = NULL; /** * @brief Constructs a Clock. @@ -48,7 +48,7 @@ * @retval */ Clock::Clock(int year, int mon, int mday, int hour, int min, int sec) { - _ticker.attach(&tick, 1.0); // a Ticker ticking at 1s rate + _ticker.attach_us(&tick, 1000000); // a Ticker ticking at 1s rate set(year, mon, mday, hour, min, sec); // set date and time as requested attach_rtc(time, NULL, NULL, NULL); // attach for C time functions } @@ -63,7 +63,7 @@ * @retval */ Clock::Clock() { - _ticker.attach(&tick, 1.0); // a Ticker ticking at 1s rate + _ticker.attach_us(&tick, 1000000); // a Ticker ticking at 1s rate set(1970, 1, 1, 0, 0, 0); // set date and time to the begin of Epoch attach_rtc(time, NULL, NULL, NULL); // attach for C time functions } @@ -80,20 +80,17 @@ * @retval */ void Clock::set(int year, int mon, int mday, int hour, int min, int sec) { - _ticker.detach(); // suspend ticks - + _ticker.detach(); // suspend ticks _tm.tm_year = year - 1900; - _tm.tm_mon = mon - 1; // convert to 0 based month + _tm.tm_mon = mon - 1; // convert to 0 based month _tm.tm_mday = mday; _tm.tm_hour = hour; _tm.tm_min = min; _tm.tm_sec = sec; - _tm.tm_isdst = -1; // Is DST on? 1 = yes, 0 = no, -1 = unknown - - _time = mktime(&_tm); // convert to seconds elapsed since January 1, 1970 - set_time(_time); // set time - - _ticker.attach(&tick, 1.0); // resume ticks + _tm.tm_isdst = -1; // Is DST on? 1 = yes, 0 = no, -1 = unknown + _time = mktime(&_tm); // convert to seconds elapsed since January 1, 1970 + set_time(_time); // set time + _ticker.attach_us(&tick, 1000000); // resume ticks } /** @@ -103,9 +100,9 @@ * @retval */ void Clock::set(tm& val) { - _ticker.detach(); // suspend ticks + _ticker.detach(); // suspend ticks _tm = val; - _ticker.attach(&tick, 1.0); // resume ticks + _ticker.attach_us(&tick, 1000000); // resume ticks } /** @@ -115,10 +112,10 @@ * @retval */ void Clock::set(time_t val) { - _ticker.detach(); // suspend ticks + _ticker.detach(); // suspend ticks _time = val; _tm = *::localtime(&_time); - _ticker.attach(&tick, 1.0); // resume ticks + _ticker.attach_us(&tick, 1000000); // resume ticks } /** @@ -210,9 +207,7 @@ */ void Clock::tick(void) { _tm = *::localtime(&(++_time)); - - if(_onTick != NULL) // if a handler was attached by the user - _onTick(); // call it + _onTick.call(); } @@ -222,10 +217,12 @@ * @param fnc: User defined handler function of type void fnc(void) * @retval */ -void Clock::attach(ClockFnc_t fnc) { - _onTick = fnc; +void Clock::attach(void (*fptr)(void)) { + if(fptr) + _onTick.attach(fptr); } + /** * @brief Detaches handler function from the onTick event * @note @@ -233,7 +230,7 @@ * @retval */ void Clock::detach() { - _onTick = NULL; + _onTick.attach(NULL); } /**