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
Revision 2:7d153bc7403f, committed 2015-08-03
- Comitter:
- maxint
- Date:
- Mon Aug 03 09:04:48 2015 +0000
- Parent:
- 1:fb8fd750e935
- Child:
- 3:c8bfeb8a2989
- Commit message:
- added unix timestamp functions for boards that don't implement them (suc as mBuino)
Changed in this revision
PseudoRTC.cpp | Show annotated file Show diff for this revision Revisions of this file |
PseudoRTC.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/PseudoRTC.cpp Sat Sep 20 14:25:38 2014 +0000 +++ b/PseudoRTC.cpp Mon Aug 03 09:04:48 2015 +0000 @@ -26,6 +26,7 @@ hour = 1; minute = 0; second = 0; + unixtime = 0; t.attach(this, &PseudoRTC::tictoc, 1); } @@ -35,16 +36,46 @@ void PseudoRTC::setTime(int y, int mo, int d, int h, int mi, int s) { /* Check the validity */ - if (month < 13 && day < 32 && hour < 24 && minute < 60 && second < 60) { + if (month < 13 && day < 32 && hour < 24 && minute < 60 && second < 60) + { year = y; month = mo; day = d; hour = h; minute = mi; second = s; + unixtime=toUnixTime(); } } +void PseudoRTC::set_time(time_t thetime) +{ + unixtime=thetime; + + struct tm *ptm; + ptm = localtime (&unixtime); + + second=ptm->tm_sec; + minute=ptm->tm_min; + hour=ptm->tm_hour; + day=ptm->tm_mday; + month=ptm->tm_mon; + year=ptm->tm_year+1900; +} + +time_t PseudoRTC::time(time_t* timer=NULL) +{ + if(timer!=NULL) + *timer=unixtime; + return(unixtime); +} + +time_t PseudoRTC::addSeconds(int nSec) +{ // add (or substract) some seconds to the rtc to adjust time as needed + this->set_time(unixtime+nSec); + return(unixtime); +} + int PseudoRTC::getYear(void) { return year; @@ -75,8 +106,37 @@ return second; } +/****** private methods *******/ +time_t PseudoRTC::toUnixTime(void) +{ + struct tm tms; + /* + tm_sec int seconds after the minute 0-61* + tm_min int minutes after the hour 0-59 + tm_hour int hours since midnight 0-23 + tm_mday int day of the month 1-31 + tm_mon int months since January 0-11 + tm_year int years since 1900 + tm_wday int days since Sunday 0-6 + tm_yday int days since January 1 0-365 + tm_isdst int Daylight Saving Time flag + */ + tms.tm_sec=second; + tms.tm_min=minute; + tms.tm_hour=hour; + tms.tm_mday=day; + tms.tm_mon=month; + tms.tm_year=year-1900; + return(mktime(&tms)); +} + void PseudoRTC::tictoc(void) { + unixtime++; + this->set_time(unixtime); // update second, minute, hour, etc. member variables + +/* old code + if(second < 59) { second++; } else { @@ -106,4 +166,5 @@ if(month > 12) { year++; } +*/ } \ No newline at end of file
--- a/PseudoRTC.h Sat Sep 20 14:25:38 2014 +0000 +++ b/PseudoRTC.h Mon Aug 03 09:04:48 2015 +0000 @@ -30,7 +30,7 @@ * * main() * { - * /* Example: September 20, 2014, 21:05:30 + * // Example: September 20, 2014, 21:05:30 * c.setTime(2014, 09, 20, 21, 05, 30); * * while(true) { @@ -59,6 +59,23 @@ */ void setTime(int y, int mo, int d, int h, int mi, int s); + /** Set the time using a unix timestamp value (the number of seconds since January 1, 1970) + * @param thetime unix time as time_t + */ + void set_time(time_t thetime); + + /** Get the unix timestamp value (the number of seconds since January 1, 1970) + * @param timer Pointer to the time_t variable + * @return time_t current timestamp + */ + time_t time(time_t *timer); + + /** add (or subtract) some seconds to the rtc to adjust time as needed + * @param nSec number of seconds to add or subtract + * @return time_t current timestamp + */ + time_t addSeconds(int nSec); + /** Get the year value */ int getYear(void); @@ -84,7 +101,9 @@ int hour; int minute; int second; + time_t unixtime; Ticker t; + time_t toUnixTime(void); void tictoc(void); };