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@3:c8bfeb8a2989, 2015-08-03 (annotated)
- Committer:
- maxint
- Date:
- Mon Aug 03 10:29:25 2015 +0000
- Revision:
- 3:c8bfeb8a2989
- Parent:
- 2:7d153bc7403f
bugfix; month was one off (note: tm_mon in struct tm is 0 to 11, month in PseudoRTC is 1-12!)
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 | 3:c8bfeb8a2989 | 33 | * time_t seconds; |
maxint | 3:c8bfeb8a2989 | 34 | * char buffer[32]; |
maxint | 3:c8bfeb8a2989 | 35 | * c.set_time(1256729737); // Set RTC time to Wed, 28 Oct 2009 11:35:37 (NOW WORKING ON mBuino) |
maxint | 3:c8bfeb8a2989 | 36 | * |
maxint | 3:c8bfeb8a2989 | 37 | * seconds=c.time(NULL); |
maxint | 3:c8bfeb8a2989 | 38 | * printf("Time as seconds since January 1, 1970 = %d\r\n", seconds); |
maxint | 3:c8bfeb8a2989 | 39 | * printf("Time as a basic string = %s\r", ctime(&seconds)); // ctime includes \n |
maxint | 3:c8bfeb8a2989 | 40 | * strftime(buffer, 32, "%I:%M %p", localtime(&seconds)); |
maxint | 3:c8bfeb8a2989 | 41 | * printf("Time as a custom formatted string = %s\r\n", buffer); |
maxint | 3:c8bfeb8a2989 | 42 | * |
maxint | 3:c8bfeb8a2989 | 43 | * c.setTime(2014, 09, 20, 21, 05, 30); // Second example: September 20, 2014, 21:05:30 |
s_inoue_mbed | 1:fb8fd750e935 | 44 | * while(true) { |
s_inoue_mbed | 1:fb8fd750e935 | 45 | * 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 | 46 | * wait(1); |
s_inoue_mbed | 1:fb8fd750e935 | 47 | * } |
s_inoue_mbed | 1:fb8fd750e935 | 48 | * } |
s_inoue_mbed | 1:fb8fd750e935 | 49 | * @endcode |
s_inoue_mbed | 1:fb8fd750e935 | 50 | */ |
s_inoue_mbed | 1:fb8fd750e935 | 51 | |
s_inoue_mbed | 0:9ab044e24d20 | 52 | class PseudoRTC |
s_inoue_mbed | 0:9ab044e24d20 | 53 | { |
s_inoue_mbed | 0:9ab044e24d20 | 54 | public: |
s_inoue_mbed | 0:9ab044e24d20 | 55 | /** Create a pseudo real-time clock */ |
s_inoue_mbed | 0:9ab044e24d20 | 56 | PseudoRTC(void); |
s_inoue_mbed | 0:9ab044e24d20 | 57 | |
s_inoue_mbed | 0:9ab044e24d20 | 58 | ~PseudoRTC(void); |
s_inoue_mbed | 0:9ab044e24d20 | 59 | |
s_inoue_mbed | 0:9ab044e24d20 | 60 | /** Set time in the pseudo real-time clock |
s_inoue_mbed | 0:9ab044e24d20 | 61 | * @param y Year |
maxint | 3:c8bfeb8a2989 | 62 | * @param mo Month (1-12) |
maxint | 3:c8bfeb8a2989 | 63 | * @param d Day (1-31) |
maxint | 3:c8bfeb8a2989 | 64 | * @param h Hour (0-23) |
maxint | 3:c8bfeb8a2989 | 65 | * @param m Minute (0-59) |
maxint | 3:c8bfeb8a2989 | 66 | * @param s Second (0-59) |
s_inoue_mbed | 0:9ab044e24d20 | 67 | */ |
s_inoue_mbed | 0:9ab044e24d20 | 68 | void setTime(int y, int mo, int d, int h, int mi, int s); |
s_inoue_mbed | 0:9ab044e24d20 | 69 | |
maxint | 2:7d153bc7403f | 70 | /** Set the time using a unix timestamp value (the number of seconds since January 1, 1970) |
maxint | 2:7d153bc7403f | 71 | * @param thetime unix time as time_t |
maxint | 2:7d153bc7403f | 72 | */ |
maxint | 2:7d153bc7403f | 73 | void set_time(time_t thetime); |
maxint | 2:7d153bc7403f | 74 | |
maxint | 2:7d153bc7403f | 75 | /** Get the unix timestamp value (the number of seconds since January 1, 1970) |
maxint | 2:7d153bc7403f | 76 | * @param timer Pointer to the time_t variable |
maxint | 2:7d153bc7403f | 77 | * @return time_t current timestamp |
maxint | 2:7d153bc7403f | 78 | */ |
maxint | 2:7d153bc7403f | 79 | time_t time(time_t *timer); |
maxint | 2:7d153bc7403f | 80 | |
maxint | 2:7d153bc7403f | 81 | /** add (or subtract) some seconds to the rtc to adjust time as needed |
maxint | 2:7d153bc7403f | 82 | * @param nSec number of seconds to add or subtract |
maxint | 2:7d153bc7403f | 83 | * @return time_t current timestamp |
maxint | 2:7d153bc7403f | 84 | */ |
maxint | 2:7d153bc7403f | 85 | time_t addSeconds(int nSec); |
maxint | 2:7d153bc7403f | 86 | |
s_inoue_mbed | 0:9ab044e24d20 | 87 | /** Get the year value */ |
s_inoue_mbed | 0:9ab044e24d20 | 88 | int getYear(void); |
s_inoue_mbed | 0:9ab044e24d20 | 89 | |
s_inoue_mbed | 0:9ab044e24d20 | 90 | /** Get the month value */ |
s_inoue_mbed | 0:9ab044e24d20 | 91 | int getMonth(void); |
s_inoue_mbed | 0:9ab044e24d20 | 92 | |
s_inoue_mbed | 0:9ab044e24d20 | 93 | /** Get the day value */ |
s_inoue_mbed | 0:9ab044e24d20 | 94 | int getDay(void); |
s_inoue_mbed | 0:9ab044e24d20 | 95 | |
s_inoue_mbed | 0:9ab044e24d20 | 96 | /** Get the hour value */ |
s_inoue_mbed | 0:9ab044e24d20 | 97 | int getHour(void); |
s_inoue_mbed | 0:9ab044e24d20 | 98 | |
s_inoue_mbed | 0:9ab044e24d20 | 99 | /** Get the minute value */ |
s_inoue_mbed | 0:9ab044e24d20 | 100 | int getMinute(void); |
s_inoue_mbed | 0:9ab044e24d20 | 101 | |
s_inoue_mbed | 0:9ab044e24d20 | 102 | /** Get the second value */ |
s_inoue_mbed | 0:9ab044e24d20 | 103 | int getSecond(void); |
s_inoue_mbed | 0:9ab044e24d20 | 104 | |
s_inoue_mbed | 0:9ab044e24d20 | 105 | private: |
s_inoue_mbed | 0:9ab044e24d20 | 106 | int year; |
s_inoue_mbed | 0:9ab044e24d20 | 107 | int month; |
s_inoue_mbed | 0:9ab044e24d20 | 108 | int day; |
s_inoue_mbed | 0:9ab044e24d20 | 109 | int hour; |
s_inoue_mbed | 0:9ab044e24d20 | 110 | int minute; |
s_inoue_mbed | 0:9ab044e24d20 | 111 | int second; |
maxint | 2:7d153bc7403f | 112 | time_t unixtime; |
s_inoue_mbed | 0:9ab044e24d20 | 113 | Ticker t; |
maxint | 2:7d153bc7403f | 114 | time_t toUnixTime(void); |
s_inoue_mbed | 0:9ab044e24d20 | 115 | void tictoc(void); |
s_inoue_mbed | 0:9ab044e24d20 | 116 | }; |
s_inoue_mbed | 0:9ab044e24d20 | 117 | |
s_inoue_mbed | 0:9ab044e24d20 | 118 | #endif |