Initial commit
This commit is contained in:
commit
62bcff8485
17 changed files with 1035 additions and 0 deletions
112
src/WatchyRTC.cpp
Normal file
112
src/WatchyRTC.cpp
Normal file
|
@ -0,0 +1,112 @@
|
|||
#include "WatchyRTC.h"
|
||||
|
||||
WatchyRTC::WatchyRTC()
|
||||
{
|
||||
}
|
||||
|
||||
void WatchyRTC::init() {
|
||||
byte error;
|
||||
Wire.beginTransmission(RTC_PCF_ADDR);
|
||||
error = Wire.endTransmission();
|
||||
}
|
||||
|
||||
void WatchyRTC::config(String datetime) { // String datetime format is YYYY:MM:DD:HH:MM:SS
|
||||
_PCFConfig(datetime);
|
||||
}
|
||||
|
||||
void WatchyRTC::clearAlarm() {
|
||||
int nextAlarmMinute = 0;
|
||||
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);
|
||||
}
|
||||
|
||||
void WatchyRTC::read(tmElements_t & tm, int offsetInSeconds) {
|
||||
rtc_pcf.getDate();
|
||||
tm.Year = y2kYearToTm(rtc_pcf.getYear());
|
||||
tm.Month = rtc_pcf.getMonth();
|
||||
tm.Day = rtc_pcf.getDay();
|
||||
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();
|
||||
|
||||
int day = tm.Day;
|
||||
int hour = tm.Hour;
|
||||
int minute = tm.Minute;
|
||||
int second = tm.Second;
|
||||
|
||||
// adjust for offset - making some assumptions here: month and year will never change.
|
||||
second += offsetInSeconds;
|
||||
if (second >= 60 || second < 0) {
|
||||
minute += second / 60;
|
||||
second = second % 60;
|
||||
}
|
||||
|
||||
if (minute >= 60 || minute < 0) {
|
||||
hour += minute / 60;
|
||||
minute = minute % 60;
|
||||
}
|
||||
|
||||
if (hour >= 24 || hour < 0) {
|
||||
day += hour / 24;
|
||||
hour = hour % 24;
|
||||
}
|
||||
|
||||
tm.Day = day;
|
||||
tm.Hour = hour;
|
||||
tm.Minute = minute;
|
||||
tm.Second = second;
|
||||
}
|
||||
|
||||
void WatchyRTC::set(tmElements_t tm) {
|
||||
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));
|
||||
rtc_pcf.setTime(tm.Hour, tm.Minute, tm.Second);
|
||||
clearAlarm();
|
||||
}
|
||||
|
||||
void WatchyRTC::_PCFConfig(String datetime) { // String datetime is YYYY:MM:DD:HH:MM:SS
|
||||
if (datetime != "") {
|
||||
tmElements_t tm;
|
||||
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(
|
||||
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);
|
||||
}
|
||||
|
||||
// on POR event, PCF8563 sets month to 0, which will give an error since
|
||||
// months are 1-12
|
||||
clearAlarm();
|
||||
}
|
||||
|
||||
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]) : "";
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue