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.cpp
00001 /* Copyright (c) 2014 Shigenori Inoue, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00005 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00006 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00007 * furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 */ 00018 00019 #include "PseudoRTC.h" 00020 00021 PseudoRTC::PseudoRTC(void) 00022 { 00023 year = 0; 00024 month = 1; 00025 day = 1; 00026 hour = 1; 00027 minute = 0; 00028 second = 0; 00029 unixtime = 0; 00030 t.attach(this, &PseudoRTC::tictoc, 1); 00031 } 00032 00033 PseudoRTC::~PseudoRTC(void) {} 00034 00035 00036 void PseudoRTC::setTime(int y, int mo, int d, int h, int mi, int s) 00037 { 00038 /* Check the validity */ 00039 if (month < 13 && day < 32 && hour < 24 && minute < 60 && second < 60) 00040 { 00041 year = y; 00042 month = mo; 00043 day = d; 00044 hour = h; 00045 minute = mi; 00046 second = s; 00047 unixtime=toUnixTime(); 00048 } 00049 } 00050 00051 void PseudoRTC::set_time(time_t thetime) 00052 { 00053 unixtime=thetime; 00054 00055 struct tm *ptm; 00056 ptm = localtime (&unixtime); 00057 00058 second=ptm->tm_sec; 00059 minute=ptm->tm_min; 00060 hour=ptm->tm_hour; 00061 day=ptm->tm_mday; 00062 month=ptm->tm_mon+1; 00063 year=ptm->tm_year+1900; 00064 } 00065 00066 time_t PseudoRTC::time(time_t* timer=NULL) 00067 { 00068 if(timer!=NULL) 00069 *timer=unixtime; 00070 return(unixtime); 00071 } 00072 00073 time_t PseudoRTC::addSeconds(int nSec) 00074 { // add (or substract) some seconds to the rtc to adjust time as needed 00075 this->set_time(unixtime+nSec); 00076 return(unixtime); 00077 } 00078 00079 int PseudoRTC::getYear(void) 00080 { 00081 return year; 00082 } 00083 00084 int PseudoRTC::getMonth(void) 00085 { 00086 return month; 00087 } 00088 00089 int PseudoRTC::getDay(void) 00090 { 00091 return day; 00092 } 00093 00094 int PseudoRTC::getHour(void) 00095 { 00096 return hour; 00097 } 00098 00099 int PseudoRTC::getMinute(void) 00100 { 00101 return minute; 00102 } 00103 00104 int PseudoRTC::getSecond(void) 00105 { 00106 return second; 00107 } 00108 00109 /****** private methods *******/ 00110 time_t PseudoRTC::toUnixTime(void) 00111 { 00112 struct tm tms; 00113 /* 00114 tm_sec int seconds after the minute 0-61* 00115 tm_min int minutes after the hour 0-59 00116 tm_hour int hours since midnight 0-23 00117 tm_mday int day of the month 1-31 00118 tm_mon int months since January 0-11 00119 tm_year int years since 1900 00120 tm_wday int days since Sunday 0-6 00121 tm_yday int days since January 1 0-365 00122 tm_isdst int Daylight Saving Time flag 00123 */ 00124 tms.tm_sec=second; 00125 tms.tm_min=minute; 00126 tms.tm_hour=hour; 00127 tms.tm_mday=day; 00128 tms.tm_mon=month-1; 00129 tms.tm_year=year-1900; 00130 return(mktime(&tms)); 00131 } 00132 00133 void PseudoRTC::tictoc(void) 00134 { 00135 unixtime++; 00136 this->set_time(unixtime); // update second, minute, hour, etc. member variables 00137 00138 /* old code 00139 00140 if(second < 59) { 00141 second++; 00142 } else { 00143 second = 0; 00144 minute++; 00145 } 00146 if(minute > 59) { 00147 minute = 0; 00148 hour++; 00149 } 00150 if(hour > 23) { 00151 hour = 0; 00152 day++; 00153 } 00154 if(day > 30 && (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)) { 00155 day = 1; 00156 month++; 00157 } 00158 if(day > 29 && (month == 4 || month == 6 || month == 9 || month == 11)) { 00159 day = 1; 00160 month++; 00161 } 00162 if(day > 27 && month == 2) { 00163 day = 1; 00164 month++; 00165 } 00166 if(month > 12) { 00167 year++; 00168 } 00169 */ 00170 }
Generated on Tue Jul 12 2022 20:45:19 by 1.7.2