Pull in a couple other methods

pull/120/head
Andre LaFleur 2021-12-18 14:09:10 -07:00
parent a4ab4b0144
commit 619b80232b
2 changed files with 26 additions and 24 deletions

View File

@ -33,11 +33,7 @@ void WatchyRTC::config(String datetime){
}
void WatchyRTC::clearAlarm(){
if (rtcType == DS3232_RTC_TYPE) {
((DS3232*) _rtc)->rtc_ds.alarm(ALARM_2);
} else if (rtcType == PCF8563_RTC_TYPE) {
_rtc->clearAlarm();
}
_rtc->clearAlarm();
}
void WatchyRTC::read(tmElements_t &tm){
@ -61,23 +57,7 @@ void WatchyRTC::read(tmElements_t &tm){
}
void WatchyRTC::set(tmElements_t tm){
if (rtcType == DS3232_RTC_TYPE) {
tm.Year = tm.Year + 2000 - YEAR_OFFSET_DS3232;
time_t t = makeTime(tm);
((DS3232*) _rtc)->rtc_ds.set(t);
} else if (rtcType == PCF8563_RTC_TYPE) {
((PCF8563 *) _rtc)->rtc_pcf.setDate(tm.Day, _getDayOfWeek(tm.Day, tm.Month, tm.Year+YEAR_OFFSET_PCF), tm.Month, 0, tm.Year);
((PCF8563 *) _rtc)->rtc_pcf.setTime(tm.Hour, tm.Minute, tm.Second);
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;
_rtc->set(tm);
}
uint8_t WatchyRTC::temperature(){

View File

@ -56,6 +56,7 @@ class DS3232 : public AbstractRTC {
public:
DS3232RTC rtc_ds; // TODO: We should not have public member variables
~DS3232() {}
void config(String datetime) {
if (datetime != "") {
tmElements_t tm;
@ -75,12 +76,23 @@ public:
rtc_ds.setAlarm(ALM2_EVERY_MINUTE, 0, 0, 0, 0); //alarm wakes up Watchy every minute
rtc_ds.alarmInterrupt(ALARM_2, true); //enable alarm interrupt
}
void clearAlarm() {
rtc_ds.alarm(ALARM_2);
}
void set(tmElements_t tm) {
tm.Year = tm.Year + 2000 - YEAR_OFFSET_DS3232;
time_t t = makeTime(tm);
rtc_ds.set(t);
}
};
class PCF8563 : public AbstractRTC {
public:
Rtc_Pcf8563 rtc_pcf; // TODO: We should not have public member variables
~PCF8563() {}
void config(String datetime) {
if (datetime != "") {
int Year = getValue(datetime, ':', 0).toInt();
@ -90,13 +102,17 @@ public:
int Minute = getValue(datetime, ':', 4).toInt();
int Second = getValue(datetime, ':', 5).toInt();
//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
int dayOfWeek = getDayOfWeek(Day, Month, Year);
// offset from 2000
rtc_pcf.setDate(Day, dayOfWeek, Month, 0, Year - YEAR_OFFSET_PCF);
//hr, min, sec
rtc_pcf.setTime(Hour, Minute, Second);
}
clearAlarm();
}
void clearAlarm() {
int nextAlarmMinute = 0;
rtc_pcf.clearAlarm(); // resets the alarm flag in the RTC
@ -104,6 +120,13 @@ public:
nextAlarmMinute = (nextAlarmMinute == 59) ? 0 : (nextAlarmMinute + 1); //set alarm to trigger 1 minute from now
rtc_pcf.setAlarm(nextAlarmMinute, 99, 99, 99);
}
void set(tmElements_t tm) {
int dayOfWeek = getDayOfWeek(tm.Day, tm.Month, tm.Year + YEAR_OFFSET_PCF);
rtc_pcf.setDate(tm.Day, dayOfWeek, tm.Month, 0, tm.Year);
rtc_pcf.setTime(tm.Hour, tm.Minute, tm.Second);
clearAlarm();
}
private:
int getDayOfWeek(int d, int m, int y) {
static int t[] = { 0, 3, 2, 5, 0, 3,
@ -127,7 +150,6 @@ class WatchyRTC {
uint8_t temperature();
private:
bool _canConnectTo(int addr);
int _getDayOfWeek(int d, int m, int y);
AbstractRTC* _rtc;
};