@ -20,7 +20,7 @@ void WatchyRTC::init(){
}
}
void WatchyRTC : : config ( String datetime ) {
void WatchyRTC : : config ( String datetime ) { //String datetime format is YYYY:MM:DD:HH:MM:SS
if ( rtcType = = DS3231 ) {
_DSConfig ( datetime ) ;
} else {
@ -43,17 +43,11 @@ void WatchyRTC::clearAlarm(){
void WatchyRTC : : read ( tmElements_t & tm ) {
if ( rtcType = = DS3231 ) {
rtc_ds . read ( tm ) ;
tm . Year = tm . Year - 30 ; //reset to offset from 2000
} else {
tm . Year = y2kYearToTm ( rtc_pcf . getYear ( ) ) ;
tm . Month = rtc_pcf . getMonth ( ) ;
if ( tm . Month = = 0 ) { //PCF8563 POR sets month = 0 for some reason
tm . Month = 1 ;
tm . Year = 21 ;
} else {
tm . Year = rtc_pcf . getYear ( ) ;
}
tm . Day = rtc_pcf . getDay ( ) ;
tm . Wday = rtc_pcf . getWeekday ( ) + 1 ;
tm . Wday = rtc_pcf . getWeekday ( ) + 1 ; //TimeLib & DS3231 has Wday range of 1-7, but PCF8563 stores day of week in 0-6 range
tm . Hour = rtc_pcf . getHour ( ) ;
tm . Minute = rtc_pcf . getMinute ( ) ;
tm . Second = rtc_pcf . getSecond ( ) ;
@ -62,11 +56,14 @@ void WatchyRTC::read(tmElements_t &tm){
void WatchyRTC : : set ( tmElements_t tm ) {
if ( rtcType = = DS3231 ) {
tm . Year = tm . Year + 2000 - YEAR_OFFSET_DS ;
time_t t = makeTime ( tm ) ;
rtc_ds . set ( t ) ;
} else {
rtc_pcf . setDate ( tm . Day , _getDayOfWeek ( tm . Day , tm . Month , tm . Year + YEAR_OFFSET_PCF ) , tm . Month , 0 , tm . Year ) ;
time_t t = makeTime ( tm ) ; //make and break to calculate tm.Wday
breakTime ( t , tm ) ;
//day, weekday, month, century(1=1900, 0=2000), year(0-99)
rtc_pcf . setDate ( tm . Day , tm . Wday - 1 , tm . Month , 0 , tmYearToY2k ( tm . Year ) ) ; //TimeLib & DS3231 has Wday range of 1-7, but PCF8563 stores day of week in 0-6 range
//hr, min, sec
rtc_pcf . setTime ( tm . Hour , tm . Minute , tm . Second ) ;
clearAlarm ( ) ;
}
@ -80,10 +77,10 @@ uint8_t WatchyRTC::temperature(){
}
}
void WatchyRTC : : _DSConfig ( String datetime ) {
void WatchyRTC : : _DSConfig ( String datetime ) { //String datetime is YYYY:MM:DD:HH:MM:SS
if ( datetime ! = " " ) {
tmElements_t tm ;
tm . Year = _getValue( datetime , ' : ' , 0 ) . toInt ( ) - YEAR_OFFSET_DS ; //offset from 1970, since year is stored in uint8_t
tm . Year = CalendarYrToTm( _getValue( datetime , ' : ' , 0 ) . toInt ( ) ) ; //YYYY - 1970
tm . Month = _getValue ( datetime , ' : ' , 1 ) . toInt ( ) ;
tm . Day = _getValue ( datetime , ' : ' , 2 ) . toInt ( ) ;
tm . Hour = _getValue ( datetime , ' : ' , 3 ) . toInt ( ) ;
@ -98,30 +95,54 @@ void WatchyRTC::_DSConfig(String datetime){
rtc_ds . alarmInterrupt ( ALARM_2 , true ) ; //enable alarm interrupt
}
void WatchyRTC : : _PCFConfig ( String datetime ) {
void WatchyRTC : : _PCFConfig ( String datetime ) { //String datetime is YYYY:MM:DD:HH:MM:SS
if ( datetime ! = " " ) {
tmElements_t tm ;
int Year = _getValue ( datetime , ' : ' , 0 ) . toInt ( ) ;
int Month = _getValue ( datetime , ' : ' , 1 ) . toInt ( ) ;
int Day = _getValue ( datetime , ' : ' , 2 ) . toInt ( ) ;
int Hour = _getValue ( datetime , ' : ' , 3 ) . toInt ( ) ;
int Minute = _getValue ( datetime , ' : ' , 4 ) . toInt ( ) ;
int Second = _getValue ( datetime , ' : ' , 5 ) . toInt ( ) ;
tm . Year = CalendarYrToTm ( _getValue ( datetime , ' : ' , 0 ) . toInt ( ) ) ; //YYYY - 1970
tm . Month = _getValue ( datetime , ' : ' , 1 ) . toInt ( ) ;
tm . Day = _getValue ( datetime , ' : ' , 2 ) . toInt ( ) ;
tm . Hour = _getValue ( datetime , ' : ' , 3 ) . toInt ( ) ;
tm . Minute = _getValue ( datetime , ' : ' , 4 ) . toInt ( ) ;
tm . Second = _getValue ( datetime , ' : ' , 5 ) . toInt ( ) ;
time_t t = makeTime ( tm ) ; //make and break to calculate tm.Wday
breakTime ( t , tm ) ;
//day, weekday, month, century(1=1900, 0=2000), year(0-99)
rtc_pcf . setDate ( Day , _getDayOfWeek ( Day , Month , Year ) , Month , 0 , Year - YEAR_OFFSET_PCF ) ; //offset from 2000
rtc_pcf . setDate ( tm. Day , tm . Wday - 1 , tm . Month , 0 , tmYearToY2k ( tm . Year ) ) ; //TimeLib & DS3231 has Wday range of 1-7, but PCF8563 stores day of week in 0-6 range
//hr, min, sec
rtc_pcf . setTime ( Hour, Minute, Second ) ;
rtc_pcf . setTime ( tm. Hour, tm. Minute, tm . Second ) ;
}
//on POR event, PCF8563 sets month to 0, which will give an error since months are 1-12
clearAlarm ( ) ;
}
int WatchyRTC : : _getDayOfWeek ( int d , int m , int y )
{
static int t [ ] = { 0 , 3 , 2 , 5 , 0 , 3 ,
5 , 1 , 4 , 6 , 2 , 4 } ;
y - = m < 3 ;
return ( y + y / 4 - y / 100 +
y / 400 + t [ m - 1 ] + d ) % 7 ;
void WatchyRTC : : syncNtpTime ( ) { //NTP sync - call after connecting to WiFi and remember to turn it back off
configTime ( GMT_OFFSET_SEC , DST_OFFSET_SEC , NTP_SERVER ) ;
struct tm timeinfo ;
if ( ! getLocalTime ( & timeinfo ) ) {
return ; //NTP sync failed
}
/****************************************************
struct tm
{
int tm_sec ; // Seconds [0,60].
int tm_min ; // Minutes [0,59].
int tm_hour ; // Hour [0,23].
int tm_mday ; // Day of month [1,31].
int tm_mon ; // Month of year [0,11].
int tm_year ; // Years since 1900.
int tm_wday ; // Day of week [0,6] (Sunday =0).
int tm_yday ; // Day of year [0,365].
int tm_isdst ; // Daylight Savings flag.
}
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
tmElements_t tm ;
tm . Year = CalendarYrToTm ( timeinfo . tm_year + 1900 ) ;
tm . Month = timeinfo . tm_mon + 1 ; //tm.Month 1 - 12
tm . Day = timeinfo . tm_mday ;
tm . Hour = timeinfo . tm_hour ;
tm . Minute = timeinfo . tm_min ;
tm . Second = timeinfo . tm_sec ;
set ( tm ) ;
}
String WatchyRTC : : _getValue ( String data , char separator , int index )