Pseudo real-time clock using Ticker interruption, also implements time() and set_time() for platforms that don't (such as mBuino)
Fork of PseudoRTC by
PseudoRTC.h@2:7d153bc7403f, 2015-08-03 (annotated)
- Committer:
- maxint
- Date:
- Mon Aug 03 09:04:48 2015 +0000
- Revision:
- 2:7d153bc7403f
- Parent:
- 1:fb8fd750e935
- Child:
- 3:c8bfeb8a2989
added unix timestamp functions for boards that don't implement them (suc as mBuino)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
s_inoue_mbed | 0:9ab044e24d20 | 1 | /* Copyright (c) 2014 Shigenori Inoue, MIT License |
s_inoue_mbed | 0:9ab044e24d20 | 2 | * |
s_inoue_mbed | 0:9ab044e24d20 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
s_inoue_mbed | 0:9ab044e24d20 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
s_inoue_mbed | 0:9ab044e24d20 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
s_inoue_mbed | 0:9ab044e24d20 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
s_inoue_mbed | 0:9ab044e24d20 | 7 | * furnished to do so, subject to the following conditions: |
s_inoue_mbed | 0:9ab044e24d20 | 8 | * |
s_inoue_mbed | 0:9ab044e24d20 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
s_inoue_mbed | 0:9ab044e24d20 | 10 | * substantial portions of the Software. |
s_inoue_mbed | 0:9ab044e24d20 | 11 | * |
s_inoue_mbed | 0:9ab044e24d20 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
s_inoue_mbed | 0:9ab044e24d20 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
s_inoue_mbed | 0:9ab044e24d20 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
s_inoue_mbed | 0:9ab044e24d20 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
s_inoue_mbed | 0:9ab044e24d20 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
s_inoue_mbed | 0:9ab044e24d20 | 17 | */ |
s_inoue_mbed | 0:9ab044e24d20 | 18 | |
s_inoue_mbed | 0:9ab044e24d20 | 19 | #ifndef __PseudoRTC__ |
s_inoue_mbed | 0:9ab044e24d20 | 20 | #define __PseudoRTC__ |
s_inoue_mbed | 0:9ab044e24d20 | 21 | |
s_inoue_mbed | 0:9ab044e24d20 | 22 | #include "mbed.h" |
s_inoue_mbed | 0:9ab044e24d20 | 23 | |
s_inoue_mbed | 1:fb8fd750e935 | 24 | /** Example: |
s_inoue_mbed | 1:fb8fd750e935 | 25 | * @code |
s_inoue_mbed | 1:fb8fd750e935 | 26 | * #include "mbed.h" |
s_inoue_mbed | 1:fb8fd750e935 | 27 | * #include "PseudoRTC.h" |
s_inoue_mbed | 1:fb8fd750e935 | 28 | * |
s_inoue_mbed | 1:fb8fd750e935 | 29 | * PseudoRTC c; |
s_inoue_mbed | 1:fb8fd750e935 | 30 | * |
s_inoue_mbed | 1:fb8fd750e935 | 31 | * main() |
s_inoue_mbed | 1:fb8fd750e935 | 32 | * { |
maxint | 2:7d153bc7403f | 33 | * // Example: September 20, 2014, 21:05:30 |
s_inoue_mbed | 1:fb8fd750e935 | 34 | * c.setTime(2014, 09, 20, 21, 05, 30); |
s_inoue_mbed | 1:fb8fd750e935 | 35 | * |
s_inoue_mbed | 1:fb8fd750e935 | 36 | * while(true) { |
s_inoue_mbed | 1:fb8fd750e935 | 37 | * printf("%04d/%02d/%02d %02d:%02d:%02d\r\m", c.getYear(), c.getMonth(), c.getDay(), c.getHour(), c.getMinute(), c.getSecond()); |
s_inoue_mbed | 1:fb8fd750e935 | 38 | * wait(1); |
s_inoue_mbed | 1:fb8fd750e935 | 39 | * } |
s_inoue_mbed | 1:fb8fd750e935 | 40 | * } |
s_inoue_mbed | 1:fb8fd750e935 | 41 | * @endcode |
s_inoue_mbed | 1:fb8fd750e935 | 42 | */ |
s_inoue_mbed | 1:fb8fd750e935 | 43 | |
s_inoue_mbed | 0:9ab044e24d20 | 44 | class PseudoRTC |
s_inoue_mbed | 0:9ab044e24d20 | 45 | { |
s_inoue_mbed | 0:9ab044e24d20 | 46 | public: |
s_inoue_mbed | 0:9ab044e24d20 | 47 | /** Create a pseudo real-time clock */ |
s_inoue_mbed | 0:9ab044e24d20 | 48 | PseudoRTC(void); |
s_inoue_mbed | 0:9ab044e24d20 | 49 | |
s_inoue_mbed | 0:9ab044e24d20 | 50 | ~PseudoRTC(void); |
s_inoue_mbed | 0:9ab044e24d20 | 51 | |
s_inoue_mbed | 0:9ab044e24d20 | 52 | /** Set time in the pseudo real-time clock |
s_inoue_mbed | 0:9ab044e24d20 | 53 | * @param y Year |
s_inoue_mbed | 0:9ab044e24d20 | 54 | * @param mo Month |
s_inoue_mbed | 0:9ab044e24d20 | 55 | * @param d Day |
s_inoue_mbed | 0:9ab044e24d20 | 56 | * @param h Hour |
s_inoue_mbed | 0:9ab044e24d20 | 57 | * @param m Minute |
s_inoue_mbed | 0:9ab044e24d20 | 58 | * @param s Second |
s_inoue_mbed | 0:9ab044e24d20 | 59 | */ |
s_inoue_mbed | 0:9ab044e24d20 | 60 | void setTime(int y, int mo, int d, int h, int mi, int s); |
s_inoue_mbed | 0:9ab044e24d20 | 61 | |
maxint | 2:7d153bc7403f | 62 | /** Set the time using a unix timestamp value (the number of seconds since January 1, 1970) |
maxint | 2:7d153bc7403f | 63 | * @param thetime unix time as time_t |
maxint | 2:7d153bc7403f | 64 | */ |
maxint | 2:7d153bc7403f | 65 | void set_time(time_t thetime); |
maxint | 2:7d153bc7403f | 66 | |
maxint | 2:7d153bc7403f | 67 | /** Get the unix timestamp value (the number of seconds since January 1, 1970) |
maxint | 2:7d153bc7403f | 68 | * @param timer Pointer to the time_t variable |
maxint | 2:7d153bc7403f | 69 | * @return time_t current timestamp |
maxint | 2:7d153bc7403f | 70 | */ |
maxint | 2:7d153bc7403f | 71 | time_t time(time_t *timer); |
maxint | 2:7d153bc7403f | 72 | |
maxint | 2:7d153bc7403f | 73 | /** add (or subtract) some seconds to the rtc to adjust time as needed |
maxint | 2:7d153bc7403f | 74 | * @param nSec number of seconds to add or subtract |
maxint | 2:7d153bc7403f | 75 | * @return time_t current timestamp |
maxint | 2:7d153bc7403f | 76 | */ |
maxint | 2:7d153bc7403f | 77 | time_t addSeconds(int nSec); |
maxint | 2:7d153bc7403f | 78 | |
s_inoue_mbed | 0:9ab044e24d20 | 79 | /** Get the year value */ |
s_inoue_mbed | 0:9ab044e24d20 | 80 | int getYear(void); |
s_inoue_mbed | 0:9ab044e24d20 | 81 | |
s_inoue_mbed | 0:9ab044e24d20 | 82 | /** Get the month value */ |
s_inoue_mbed | 0:9ab044e24d20 | 83 | int getMonth(void); |
s_inoue_mbed | 0:9ab044e24d20 | 84 | |
s_inoue_mbed | 0:9ab044e24d20 | 85 | /** Get the day value */ |
s_inoue_mbed | 0:9ab044e24d20 | 86 | int getDay(void); |
s_inoue_mbed | 0:9ab044e24d20 | 87 | |
s_inoue_mbed | 0:9ab044e24d20 | 88 | /** Get the hour value */ |
s_inoue_mbed | 0:9ab044e24d20 | 89 | int getHour(void); |
s_inoue_mbed | 0:9ab044e24d20 | 90 | |
s_inoue_mbed | 0:9ab044e24d20 | 91 | /** Get the minute value */ |
s_inoue_mbed | 0:9ab044e24d20 | 92 | int getMinute(void); |
s_inoue_mbed | 0:9ab044e24d20 | 93 | |
s_inoue_mbed | 0:9ab044e24d20 | 94 | /** Get the second value */ |
s_inoue_mbed | 0:9ab044e24d20 | 95 | int getSecond(void); |
s_inoue_mbed | 0:9ab044e24d20 | 96 | |
s_inoue_mbed | 0:9ab044e24d20 | 97 | private: |
s_inoue_mbed | 0:9ab044e24d20 | 98 | int year; |
s_inoue_mbed | 0:9ab044e24d20 | 99 | int month; |
s_inoue_mbed | 0:9ab044e24d20 | 100 | int day; |
s_inoue_mbed | 0:9ab044e24d20 | 101 | int hour; |
s_inoue_mbed | 0:9ab044e24d20 | 102 | int minute; |
s_inoue_mbed | 0:9ab044e24d20 | 103 | int second; |
maxint | 2:7d153bc7403f | 104 | time_t unixtime; |
s_inoue_mbed | 0:9ab044e24d20 | 105 | Ticker t; |
maxint | 2:7d153bc7403f | 106 | time_t toUnixTime(void); |
s_inoue_mbed | 0:9ab044e24d20 | 107 | void tictoc(void); |
s_inoue_mbed | 0:9ab044e24d20 | 108 | }; |
s_inoue_mbed | 0:9ab044e24d20 | 109 | |
s_inoue_mbed | 0:9ab044e24d20 | 110 | #endif |