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 Shigenori Inoue

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?

UserRevisionLine numberNew 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 #include "PseudoRTC.h"
s_inoue_mbed 0:9ab044e24d20 20
s_inoue_mbed 0:9ab044e24d20 21 PseudoRTC::PseudoRTC(void)
s_inoue_mbed 0:9ab044e24d20 22 {
s_inoue_mbed 0:9ab044e24d20 23 year = 0;
s_inoue_mbed 0:9ab044e24d20 24 month = 1;
s_inoue_mbed 0:9ab044e24d20 25 day = 1;
s_inoue_mbed 0:9ab044e24d20 26 hour = 1;
s_inoue_mbed 0:9ab044e24d20 27 minute = 0;
s_inoue_mbed 0:9ab044e24d20 28 second = 0;
maxint 2:7d153bc7403f 29 unixtime = 0;
s_inoue_mbed 0:9ab044e24d20 30 t.attach(this, &PseudoRTC::tictoc, 1);
s_inoue_mbed 0:9ab044e24d20 31 }
s_inoue_mbed 0:9ab044e24d20 32
s_inoue_mbed 0:9ab044e24d20 33 PseudoRTC::~PseudoRTC(void) {}
s_inoue_mbed 0:9ab044e24d20 34
s_inoue_mbed 0:9ab044e24d20 35
s_inoue_mbed 0:9ab044e24d20 36 void PseudoRTC::setTime(int y, int mo, int d, int h, int mi, int s)
s_inoue_mbed 0:9ab044e24d20 37 {
s_inoue_mbed 0:9ab044e24d20 38 /* Check the validity */
maxint 2:7d153bc7403f 39 if (month < 13 && day < 32 && hour < 24 && minute < 60 && second < 60)
maxint 2:7d153bc7403f 40 {
s_inoue_mbed 0:9ab044e24d20 41 year = y;
s_inoue_mbed 0:9ab044e24d20 42 month = mo;
s_inoue_mbed 0:9ab044e24d20 43 day = d;
s_inoue_mbed 0:9ab044e24d20 44 hour = h;
s_inoue_mbed 0:9ab044e24d20 45 minute = mi;
s_inoue_mbed 0:9ab044e24d20 46 second = s;
maxint 2:7d153bc7403f 47 unixtime=toUnixTime();
s_inoue_mbed 0:9ab044e24d20 48 }
s_inoue_mbed 0:9ab044e24d20 49 }
s_inoue_mbed 0:9ab044e24d20 50
maxint 2:7d153bc7403f 51 void PseudoRTC::set_time(time_t thetime)
maxint 2:7d153bc7403f 52 {
maxint 2:7d153bc7403f 53 unixtime=thetime;
maxint 2:7d153bc7403f 54
maxint 2:7d153bc7403f 55 struct tm *ptm;
maxint 2:7d153bc7403f 56 ptm = localtime (&unixtime);
maxint 2:7d153bc7403f 57
maxint 2:7d153bc7403f 58 second=ptm->tm_sec;
maxint 2:7d153bc7403f 59 minute=ptm->tm_min;
maxint 2:7d153bc7403f 60 hour=ptm->tm_hour;
maxint 2:7d153bc7403f 61 day=ptm->tm_mday;
maxint 3:c8bfeb8a2989 62 month=ptm->tm_mon+1;
maxint 2:7d153bc7403f 63 year=ptm->tm_year+1900;
maxint 2:7d153bc7403f 64 }
maxint 2:7d153bc7403f 65
maxint 2:7d153bc7403f 66 time_t PseudoRTC::time(time_t* timer=NULL)
maxint 2:7d153bc7403f 67 {
maxint 2:7d153bc7403f 68 if(timer!=NULL)
maxint 2:7d153bc7403f 69 *timer=unixtime;
maxint 2:7d153bc7403f 70 return(unixtime);
maxint 2:7d153bc7403f 71 }
maxint 2:7d153bc7403f 72
maxint 2:7d153bc7403f 73 time_t PseudoRTC::addSeconds(int nSec)
maxint 2:7d153bc7403f 74 { // add (or substract) some seconds to the rtc to adjust time as needed
maxint 2:7d153bc7403f 75 this->set_time(unixtime+nSec);
maxint 2:7d153bc7403f 76 return(unixtime);
maxint 2:7d153bc7403f 77 }
maxint 2:7d153bc7403f 78
s_inoue_mbed 0:9ab044e24d20 79 int PseudoRTC::getYear(void)
s_inoue_mbed 0:9ab044e24d20 80 {
s_inoue_mbed 0:9ab044e24d20 81 return year;
s_inoue_mbed 0:9ab044e24d20 82 }
s_inoue_mbed 0:9ab044e24d20 83
s_inoue_mbed 0:9ab044e24d20 84 int PseudoRTC::getMonth(void)
s_inoue_mbed 0:9ab044e24d20 85 {
s_inoue_mbed 0:9ab044e24d20 86 return month;
s_inoue_mbed 0:9ab044e24d20 87 }
s_inoue_mbed 0:9ab044e24d20 88
s_inoue_mbed 0:9ab044e24d20 89 int PseudoRTC::getDay(void)
s_inoue_mbed 0:9ab044e24d20 90 {
s_inoue_mbed 0:9ab044e24d20 91 return day;
s_inoue_mbed 0:9ab044e24d20 92 }
s_inoue_mbed 0:9ab044e24d20 93
s_inoue_mbed 0:9ab044e24d20 94 int PseudoRTC::getHour(void)
s_inoue_mbed 0:9ab044e24d20 95 {
s_inoue_mbed 0:9ab044e24d20 96 return hour;
s_inoue_mbed 0:9ab044e24d20 97 }
s_inoue_mbed 0:9ab044e24d20 98
s_inoue_mbed 0:9ab044e24d20 99 int PseudoRTC::getMinute(void)
s_inoue_mbed 0:9ab044e24d20 100 {
s_inoue_mbed 0:9ab044e24d20 101 return minute;
s_inoue_mbed 0:9ab044e24d20 102 }
s_inoue_mbed 0:9ab044e24d20 103
s_inoue_mbed 0:9ab044e24d20 104 int PseudoRTC::getSecond(void)
s_inoue_mbed 0:9ab044e24d20 105 {
s_inoue_mbed 0:9ab044e24d20 106 return second;
s_inoue_mbed 0:9ab044e24d20 107 }
s_inoue_mbed 0:9ab044e24d20 108
maxint 2:7d153bc7403f 109 /****** private methods *******/
maxint 2:7d153bc7403f 110 time_t PseudoRTC::toUnixTime(void)
maxint 2:7d153bc7403f 111 {
maxint 2:7d153bc7403f 112 struct tm tms;
maxint 2:7d153bc7403f 113 /*
maxint 2:7d153bc7403f 114 tm_sec int seconds after the minute 0-61*
maxint 2:7d153bc7403f 115 tm_min int minutes after the hour 0-59
maxint 2:7d153bc7403f 116 tm_hour int hours since midnight 0-23
maxint 2:7d153bc7403f 117 tm_mday int day of the month 1-31
maxint 2:7d153bc7403f 118 tm_mon int months since January 0-11
maxint 2:7d153bc7403f 119 tm_year int years since 1900
maxint 2:7d153bc7403f 120 tm_wday int days since Sunday 0-6
maxint 2:7d153bc7403f 121 tm_yday int days since January 1 0-365
maxint 2:7d153bc7403f 122 tm_isdst int Daylight Saving Time flag
maxint 2:7d153bc7403f 123 */
maxint 2:7d153bc7403f 124 tms.tm_sec=second;
maxint 2:7d153bc7403f 125 tms.tm_min=minute;
maxint 2:7d153bc7403f 126 tms.tm_hour=hour;
maxint 2:7d153bc7403f 127 tms.tm_mday=day;
maxint 3:c8bfeb8a2989 128 tms.tm_mon=month-1;
maxint 2:7d153bc7403f 129 tms.tm_year=year-1900;
maxint 2:7d153bc7403f 130 return(mktime(&tms));
maxint 2:7d153bc7403f 131 }
maxint 2:7d153bc7403f 132
s_inoue_mbed 0:9ab044e24d20 133 void PseudoRTC::tictoc(void)
s_inoue_mbed 0:9ab044e24d20 134 {
maxint 2:7d153bc7403f 135 unixtime++;
maxint 2:7d153bc7403f 136 this->set_time(unixtime); // update second, minute, hour, etc. member variables
maxint 2:7d153bc7403f 137
maxint 2:7d153bc7403f 138 /* old code
maxint 2:7d153bc7403f 139
s_inoue_mbed 0:9ab044e24d20 140 if(second < 59) {
s_inoue_mbed 0:9ab044e24d20 141 second++;
s_inoue_mbed 0:9ab044e24d20 142 } else {
s_inoue_mbed 0:9ab044e24d20 143 second = 0;
s_inoue_mbed 0:9ab044e24d20 144 minute++;
s_inoue_mbed 0:9ab044e24d20 145 }
s_inoue_mbed 0:9ab044e24d20 146 if(minute > 59) {
s_inoue_mbed 0:9ab044e24d20 147 minute = 0;
s_inoue_mbed 0:9ab044e24d20 148 hour++;
s_inoue_mbed 0:9ab044e24d20 149 }
s_inoue_mbed 0:9ab044e24d20 150 if(hour > 23) {
s_inoue_mbed 0:9ab044e24d20 151 hour = 0;
s_inoue_mbed 0:9ab044e24d20 152 day++;
s_inoue_mbed 0:9ab044e24d20 153 }
s_inoue_mbed 0:9ab044e24d20 154 if(day > 30 && (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)) {
s_inoue_mbed 0:9ab044e24d20 155 day = 1;
s_inoue_mbed 0:9ab044e24d20 156 month++;
s_inoue_mbed 0:9ab044e24d20 157 }
s_inoue_mbed 0:9ab044e24d20 158 if(day > 29 && (month == 4 || month == 6 || month == 9 || month == 11)) {
s_inoue_mbed 0:9ab044e24d20 159 day = 1;
s_inoue_mbed 0:9ab044e24d20 160 month++;
s_inoue_mbed 0:9ab044e24d20 161 }
s_inoue_mbed 0:9ab044e24d20 162 if(day > 27 && month == 2) {
s_inoue_mbed 0:9ab044e24d20 163 day = 1;
s_inoue_mbed 0:9ab044e24d20 164 month++;
s_inoue_mbed 0:9ab044e24d20 165 }
s_inoue_mbed 0:9ab044e24d20 166 if(month > 12) {
s_inoue_mbed 0:9ab044e24d20 167 year++;
s_inoue_mbed 0:9ab044e24d20 168 }
maxint 2:7d153bc7403f 169 */
s_inoue_mbed 0:9ab044e24d20 170 }