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).
Clock.h@3:58f5afbd24cd, 2015-11-30 (annotated)
- Committer:
- hudakz
- Date:
- Mon Nov 30 11:36:09 2015 +0000
- Revision:
- 3:58f5afbd24cd
- Parent:
- 1:0668893c1c6f
- Child:
- 5:d65fc7060635
Day of week added.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 0:af43d5d263e7 | 1 | /* |
hudakz | 0:af43d5d263e7 | 2 | Clock.h |
hudakz | 0:af43d5d263e7 | 3 | |
hudakz | 0:af43d5d263e7 | 4 | Created on: Mar 24, 2015 |
hudakz | 0:af43d5d263e7 | 5 | Author: Zoltan Hudak |
hudakz | 0:af43d5d263e7 | 6 | |
hudakz | 0:af43d5d263e7 | 7 | Copyright (c) 2015 Zoltan Hudak <hudakz@inbox.com> |
hudakz | 0:af43d5d263e7 | 8 | All rights reserved. |
hudakz | 0:af43d5d263e7 | 9 | |
hudakz | 0:af43d5d263e7 | 10 | This program is free software: you can redistribute it and/or modify |
hudakz | 0:af43d5d263e7 | 11 | it under the terms of the GNU General Public License as published by |
hudakz | 0:af43d5d263e7 | 12 | the Free Software Foundation, either version 3 of the License, or |
hudakz | 0:af43d5d263e7 | 13 | (at your option) any later version. |
hudakz | 0:af43d5d263e7 | 14 | |
hudakz | 0:af43d5d263e7 | 15 | This program is distributed in the hope that it will be useful, |
hudakz | 0:af43d5d263e7 | 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
hudakz | 0:af43d5d263e7 | 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
hudakz | 0:af43d5d263e7 | 18 | GNU General Public License for more details. |
hudakz | 0:af43d5d263e7 | 19 | |
hudakz | 0:af43d5d263e7 | 20 | You should have received a copy of the GNU General Public License |
hudakz | 0:af43d5d263e7 | 21 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
hudakz | 0:af43d5d263e7 | 22 | */ |
hudakz | 0:af43d5d263e7 | 23 | |
hudakz | 0:af43d5d263e7 | 24 | #ifndef Clock_H_ |
hudakz | 0:af43d5d263e7 | 25 | #define Clock_H_ |
hudakz | 0:af43d5d263e7 | 26 | |
hudakz | 0:af43d5d263e7 | 27 | #include "mbed.h" |
hudakz | 0:af43d5d263e7 | 28 | |
hudakz | 0:af43d5d263e7 | 29 | typedef void (*ClockFnc_t) (void); |
hudakz | 0:af43d5d263e7 | 30 | |
hudakz | 0:af43d5d263e7 | 31 | class Clock |
hudakz | 0:af43d5d263e7 | 32 | { |
hudakz | 0:af43d5d263e7 | 33 | Ticker _ticker; |
hudakz | 0:af43d5d263e7 | 34 | static time_t _time; |
hudakz | 0:af43d5d263e7 | 35 | static struct tm _tm; |
hudakz | 0:af43d5d263e7 | 36 | |
hudakz | 0:af43d5d263e7 | 37 | static ClockFnc_t _onTick; |
hudakz | 0:af43d5d263e7 | 38 | |
hudakz | 0:af43d5d263e7 | 39 | public: |
hudakz | 0:af43d5d263e7 | 40 | |
hudakz | 0:af43d5d263e7 | 41 | Clock(int year, int mon, int mday, int hour, int min, int sec); |
hudakz | 0:af43d5d263e7 | 42 | Clock(); |
hudakz | 0:af43d5d263e7 | 43 | void set(int year, int mon, int mday, int hour, int min, int sec); |
hudakz | 0:af43d5d263e7 | 44 | void set(tm& val); |
hudakz | 1:0668893c1c6f | 45 | void set(time_t time); |
hudakz | 0:af43d5d263e7 | 46 | static time_t time(); |
hudakz | 0:af43d5d263e7 | 47 | int year(void); |
hudakz | 0:af43d5d263e7 | 48 | int mon(void); |
hudakz | 0:af43d5d263e7 | 49 | int mday(void); |
hudakz | 3:58f5afbd24cd | 50 | int wday(void); |
hudakz | 0:af43d5d263e7 | 51 | int hour(void); |
hudakz | 0:af43d5d263e7 | 52 | int min(void); |
hudakz | 0:af43d5d263e7 | 53 | int sec(void); |
hudakz | 0:af43d5d263e7 | 54 | static void tick(void); |
hudakz | 0:af43d5d263e7 | 55 | void attach(ClockFnc_t fnc); |
hudakz | 0:af43d5d263e7 | 56 | void detach(); |
hudakz | 0:af43d5d263e7 | 57 | static time_t asTime(int year, int mon, int mday, int hour, int min, int sec); |
hudakz | 0:af43d5d263e7 | 58 | }; |
hudakz | 0:af43d5d263e7 | 59 | #endif /* Clock_H_ */ |