DS3231 & PCF8563 working WatchyRTC.

pull/131/head
GuruSR 2021-12-27 00:28:08 -05:00 committed by GitHub
parent 0c275c5e72
commit 5e35527b99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 25 additions and 20 deletions

View File

@ -1,5 +1,7 @@
#include "WatchyRTC.h" #include "WatchyRTC.h"
RTC_DATA_ATTR bool UsedCentury = false;
WatchyRTC::WatchyRTC() WatchyRTC::WatchyRTC()
: rtc_ds(false) {} : rtc_ds(false) {}
@ -32,43 +34,40 @@ void WatchyRTC::clearAlarm(){
if(rtcType == DS3231){ if(rtcType == DS3231){
rtc_ds.alarm(ALARM_2); rtc_ds.alarm(ALARM_2);
}else{ }else{
int nextAlarmMinute = 0; int nextAlarmMinute = constrain(rtc_pcf.getMinute() + 1, 0, 59); //set alarm to trigger 1 minute from now
rtc_pcf.clearAlarm(); //resets the alarm flag in the RTC rtc_pcf.clearAlarm(); //resets the alarm flag in the RTC
nextAlarmMinute = rtc_pcf.getMinute();
nextAlarmMinute = (nextAlarmMinute == 59) ? 0 : (nextAlarmMinute + 1); //set alarm to trigger 1 minute from now
rtc_pcf.setAlarm(nextAlarmMinute, 99, 99, 99); rtc_pcf.setAlarm(nextAlarmMinute, 99, 99, 99);
} }
} }
void WatchyRTC::read(tmElements_t &tm){ void WatchyRTC::read(tmElements_t &tm){ // Returns year from 1900 and beyond.
if(rtcType == DS3231){ if(rtcType == DS3231){
rtc_ds.read(tm); rtc_ds.read(tm);
tm.Year = tm.Year - 30; //reset to offset from 2000
}else{ }else{
tm.Month = rtc_pcf.getMonth(); tm.Month = rtc_pcf.getMonth();
if(tm.Month == 0){ //PCF8563 POR sets month = 0 for some reason if(tm.Month == 0){ //PCF8563 POR sets month = 0 for some reason
tm.Month = 1; tm.Month = 1;
tm.Year = 21; tm.Year = 121; // From 1900.
}else{ }else{
tm.Year = rtc_pcf.getYear(); tm.Year = rtc_pcf.getYear() + (UsedCentury ? RTC_CENTURY_OFFSET : 0); // Put 1900 year back on if used prior.
} }
tm.Day = rtc_pcf.getDay(); tm.Day = rtc_pcf.getDay();
tm.Wday = rtc_pcf.getWeekday() + 1; tm.Wday = rtc_pcf.getWeekday();
tm.Hour = rtc_pcf.getHour(); tm.Hour = rtc_pcf.getHour();
tm.Minute = rtc_pcf.getMinute(); tm.Minute = rtc_pcf.getMinute();
tm.Second = rtc_pcf.getSecond(); tm.Second = rtc_pcf.getSecond();
} }
} }
void WatchyRTC::set(tmElements_t tm){ void WatchyRTC::set(tmElements_t tm){ // Requires dates from year 1900 and beyond.
if(rtcType == DS3231){ if(rtcType == DS3231){
tm.Year = tm.Year + 2000 - YEAR_OFFSET_DS; rtc_ds.write(tm);
time_t t = makeTime(tm);
rtc_ds.set(t);
}else{ }else{
rtc_pcf.setDate(tm.Day, _getDayOfWeek(tm.Day, tm.Month, tm.Year+YEAR_OFFSET_PCF), tm.Month, 0, tm.Year); UsedCentury = false;
if (tm.Year > 99) {UsedCentury = true; tm.Year -= RTC_CENTURY_OFFSET; }
rtc_pcf.setDate(tm.Day, _getDayOfWeek(tm.Day, tm.Month, tm.Year), tm.Month, (UsedCentury ? 0 : 1), tm.Year);
rtc_pcf.setTime(tm.Hour, tm.Minute, tm.Second); rtc_pcf.setTime(tm.Hour, tm.Minute, tm.Second);
clearAlarm(); clearAlarm();
} }
} }
@ -80,15 +79,17 @@ uint8_t WatchyRTC::temperature(){
} }
} }
void WatchyRTC::_DSConfig(String datetime){ void WatchyRTC::_DSConfig(String datetime){ // Requires dates from year 1900 and beyond.
if(datetime != ""){ if(datetime != ""){
tmElements_t tm; tmElements_t tm;
tm.Year = _getValue(datetime, ':', 0).toInt() - YEAR_OFFSET_DS;//offset from 1970, since year is stored in uint8_t int Year = _getValue(datetime, ':', 0).toInt();
if (Year >= RTC_YEAR_OFFSET) Year -= RTC_YEAR_OFFSET;
tm.Month = _getValue(datetime, ':', 1).toInt(); tm.Month = _getValue(datetime, ':', 1).toInt();
tm.Day = _getValue(datetime, ':', 2).toInt(); tm.Day = _getValue(datetime, ':', 2).toInt();
tm.Hour = _getValue(datetime, ':', 3).toInt(); tm.Hour = _getValue(datetime, ':', 3).toInt();
tm.Minute = _getValue(datetime, ':', 4).toInt(); tm.Minute = _getValue(datetime, ':', 4).toInt();
tm.Second = _getValue(datetime, ':', 5).toInt(); tm.Second = _getValue(datetime, ':', 5).toInt();
tm.Year = Year;
time_t t = makeTime(tm); time_t t = makeTime(tm);
rtc_ds.set(t); rtc_ds.set(t);
} }
@ -98,19 +99,23 @@ void WatchyRTC::_DSConfig(String datetime){
rtc_ds.alarmInterrupt(ALARM_2, true); //enable alarm interrupt rtc_ds.alarmInterrupt(ALARM_2, true); //enable alarm interrupt
} }
void WatchyRTC::_PCFConfig(String datetime){ void WatchyRTC::_PCFConfig(String datetime){ // Requires dates from year 1900 and beyond.
if(datetime != ""){ if(datetime != ""){
tmElements_t tm; tmElements_t tm;
UsedCentury = false;
int Year = _getValue(datetime, ':', 0).toInt(); int Year = _getValue(datetime, ':', 0).toInt();
int Y = Year;
if (Year >= RTC_YEAR_OFFSET) Year -= RTC_YEAR_OFFSET;
if (Year > 99) {UsedCentury = true; Year -= RTC_CENTURY_OFFSET; }
int Month = _getValue(datetime, ':', 1).toInt(); int Month = _getValue(datetime, ':', 1).toInt();
int Day = _getValue(datetime, ':', 2).toInt(); int Day = _getValue(datetime, ':', 2).toInt();
int Hour = _getValue(datetime, ':', 3).toInt(); int Hour = _getValue(datetime, ':', 3).toInt();
int Minute = _getValue(datetime, ':', 4).toInt(); int Minute = _getValue(datetime, ':', 4).toInt();
int Second = _getValue(datetime, ':', 5).toInt(); int Second = _getValue(datetime, ':', 5).toInt();
//day, weekday, month, century(1=1900, 0=2000), year(0-99) //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(Day, _getDayOfWeek(Day, Month, Y), Month, (UsedCentury ? 0 : 1), Year);
//hr, min, sec //hr, min, sec
rtc_pcf.setTime(Hour, Minute, Second); rtc_pcf.setTime(Hour, Minute, Second);
} }
clearAlarm(); clearAlarm();
} }
@ -139,4 +144,4 @@ String WatchyRTC::_getValue(String data, char separator, int index)
} }
return found>index ? data.substring(strIndex[0], strIndex[1]) : ""; return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
} }