#include "WatchyRTC.h" WatchyRTC::WatchyRTC() {} // TODO: We can probably put all of this logic into AbstractRTC as a class // function. It would simplify this class even more, which would be nice bool WatchyRTC::_canConnectTo(int addr) { byte error; Wire.beginTransmission(addr); error = Wire.endTransmission(addr); return error == 0; } void WatchyRTC::init(){ if (_canConnectTo(RTC_DS_ADDR)) { rtcType = DS3232_RTC_TYPE; _rtc = new DS3232(); return; } if (_canConnectTo(RTC_PCF_ADDR)) { rtcType = PCF8563_RTC_TYPE; _rtc = new PCF8563(); return; } rtcType = NO_RTC_TYPE; _rtc = new AbstractRTC(); } void WatchyRTC::config(String datetime){ if (rtcType == DS3232_RTC_TYPE) { _DSConfig(datetime); } else if (rtcType == PCF8563_RTC_TYPE) { _PCFConfig(datetime); } } void WatchyRTC::clearAlarm(){ if (rtcType == DS3232_RTC_TYPE) { ((DS3232*) _rtc)->rtc_ds.alarm(ALARM_2); } else if (rtcType == PCF8563_RTC_TYPE) { int nextAlarmMinute = 0; ((PCF8563 *) _rtc)->rtc_pcf.clearAlarm(); //resets the alarm flag in the RTC nextAlarmMinute = ((PCF8563 *) _rtc)->rtc_pcf.getMinute(); nextAlarmMinute = (nextAlarmMinute == 59) ? 0 : (nextAlarmMinute + 1); //set alarm to trigger 1 minute from now ((PCF8563 *) _rtc)->rtc_pcf.setAlarm(nextAlarmMinute, 99, 99, 99); } } void WatchyRTC::read(tmElements_t &tm){ if (rtcType == DS3232_RTC_TYPE) { ((DS3232*) _rtc)->rtc_ds.read(tm); tm.Year = tm.Year - 30; //reset to offset from 2000 } else if (rtcType == PCF8563_RTC_TYPE) { tm.Month = ((PCF8563 *) _rtc)->rtc_pcf.getMonth(); if(tm.Month == 0){ //PCF8563 POR sets month = 0 for some reason tm.Month = 1; tm.Year = 21; }else{ tm.Year = ((PCF8563 *) _rtc)->rtc_pcf.getYear(); } tm.Day = ((PCF8563 *) _rtc)->rtc_pcf.getDay(); tm.Wday = ((PCF8563 *) _rtc)->rtc_pcf.getWeekday() + 1; tm.Hour = ((PCF8563 *) _rtc)->rtc_pcf.getHour(); tm.Minute = ((PCF8563 *) _rtc)->rtc_pcf.getMinute(); tm.Second = ((PCF8563 *) _rtc)->rtc_pcf.getSecond(); } } void WatchyRTC::set(tmElements_t tm){ if (rtcType == DS3232_RTC_TYPE) { tm.Year = tm.Year + 2000 - YEAR_OFFSET_DS; 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(); } } uint8_t WatchyRTC::temperature(){ if (rtcType == DS3232_RTC_TYPE) { return ((DS3232*) _rtc)->rtc_ds.temperature(); } return NO_TEMPERATURE_ERR; } void WatchyRTC::_DSConfig(String datetime){ 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.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); ((DS3232*) _rtc)->rtc_ds.set(t); } //https://github.com/JChristensen/DS3232RTC ((DS3232*) _rtc)->rtc_ds.squareWave(SQWAVE_NONE); //disable square wave output ((DS3232*) _rtc)->rtc_ds.setAlarm(ALM2_EVERY_MINUTE, 0, 0, 0, 0); //alarm wakes up Watchy every minute ((DS3232*) _rtc)->rtc_ds.alarmInterrupt(ALARM_2, true); //enable alarm interrupt } void WatchyRTC::_PCFConfig(String datetime){ if (datetime != "") { 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(); //day, weekday, month, century(1=1900, 0=2000), year(0-99) ((PCF8563 *) _rtc)->rtc_pcf.setDate(Day, _getDayOfWeek(Day, Month, Year), Month, 0, Year - YEAR_OFFSET_PCF);//offset from 2000 //hr, min, sec ((PCF8563 *) _rtc)->rtc_pcf.setTime(Hour, Minute, 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; } String WatchyRTC::_getValue(String data, char separator, int index) { int found = 0; int strIndex[] = {0, -1}; int maxIndex = data.length()-1; for(int i=0; i<=maxIndex && found<=index; i++){ if(data.charAt(i)==separator || i==maxIndex){ found++; strIndex[0] = strIndex[1]+1; strIndex[1] = (i == maxIndex) ? i+1 : i; } } return found>index ? data.substring(strIndex[0], strIndex[1]) : ""; }