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@5:d65fc7060635, 2016-05-20 (annotated)
- Committer:
- hudakz
- Date:
- Fri May 20 18:14:34 2016 +0000
- Revision:
- 5:d65fc7060635
- Parent:
- 3:58f5afbd24cd
- Child:
- 6:7edabed68b0f
Added support for attaching member functions as interrupt handler.
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 | class Clock |
hudakz | 0:af43d5d263e7 | 30 | { |
hudakz | 5:d65fc7060635 | 31 | Ticker _ticker; |
hudakz | 5:d65fc7060635 | 32 | static time_t _time; |
hudakz | 5:d65fc7060635 | 33 | static struct tm _tm; |
hudakz | 0:af43d5d263e7 | 34 | |
hudakz | 5:d65fc7060635 | 35 | protected: |
hudakz | 5:d65fc7060635 | 36 | |
hudakz | 5:d65fc7060635 | 37 | static FunctionPointer _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 | static time_t asTime(int year, int mon, int mday, int hour, int min, int sec); |
hudakz | 5:d65fc7060635 | 56 | void attach(void (*fptr)(void)); |
hudakz | 5:d65fc7060635 | 57 | void detach(); |
hudakz | 5:d65fc7060635 | 58 | template<typename T> |
hudakz | 5:d65fc7060635 | 59 | void attach(T* tptr, void (T::*mptr)(void)) { |
hudakz | 5:d65fc7060635 | 60 | if((tptr != NULL) && (mptr != NULL)) |
hudakz | 5:d65fc7060635 | 61 | _onTick.attach(tptr, mptr); |
hudakz | 5:d65fc7060635 | 62 | } |
hudakz | 0:af43d5d263e7 | 63 | }; |
hudakz | 0:af43d5d263e7 | 64 | #endif /* Clock_H_ */ |