Experimental timer-based RTC updates

This commit is contained in:
Lewis Jackson 2023-05-31 04:04:25 +03:00
parent b45b5429f7
commit 6b1c04e1de
5 changed files with 72 additions and 90 deletions

View file

@ -92,7 +92,7 @@ void WatchFace::DrawWatchFace(bool partialRefresh)
DrawBatteryIcon(); DrawBatteryIcon();
tmElements_t currentTime; tmElements_t currentTime;
m_RTC.read(currentTime, m_tzOffset); m_RTC.Get(currentTime, m_tzOffset);
SevenSegment sevenSegment(30, 60, 6, 5, 5); SevenSegment sevenSegment(30, 60, 6, 5, 5);
if (currentTime.Hour < 10) { if (currentTime.Hour < 10) {

View file

@ -17,9 +17,13 @@ void Watchy::Init()
esp_sleep_wakeup_cause_t wakeup_reason; esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause(); wakeup_reason = esp_sleep_get_wakeup_cause();
Wire.begin(SDA, SCL); if (wakeup_reason == ESP_SLEEP_WAKEUP_EXT0) {
m_RTC.init(); if(!m_RTC.CheckWakeup()) {
DeepSleep();
}
}
Wire.begin(SDA, SCL);
m_display.epd2.selectSPI(SPI, SPISettings(20000000, MSBFIRST, SPI_MODE0)); m_display.epd2.selectSPI(SPI, SPISettings(20000000, MSBFIRST, SPI_MODE0));
m_display.init(0, g_displayFullInit, 10, true); m_display.init(0, g_displayFullInit, 10, true);
SetBusyCallback(); SetBusyCallback();
@ -63,6 +67,7 @@ void Watchy::Init()
default: default:
{ {
BmaConfig(); BmaConfig();
m_RTC.Init();
ConnectWiFi(); ConnectWiFi();
SyncNTPTime(); SyncNTPTime();
DisconnectWiFi(); DisconnectWiFi();
@ -83,7 +88,6 @@ void Watchy::DeepSleep()
} }
g_displayFullInit = false; // Notify not to init it again g_displayFullInit = false; // Notify not to init it again
m_RTC.clearAlarm(); // resets the alarm flag in the RTC
// Set GPIOs 0-39 to input to avoid power leaking out // Set GPIOs 0-39 to input to avoid power leaking out
const uint64_t ignore = 0b11110001000000110000100111000010; // Ignore some GPIOs due to resets const uint64_t ignore = 0b11110001000000110000100111000010; // Ignore some GPIOs due to resets
@ -97,6 +101,9 @@ void Watchy::DeepSleep()
esp_sleep_enable_ext1_wakeup( esp_sleep_enable_ext1_wakeup(
BTN_PIN_MASK | ACC_INT_MASK, BTN_PIN_MASK | ACC_INT_MASK,
ESP_EXT1_WAKEUP_ANY_HIGH); ESP_EXT1_WAKEUP_ANY_HIGH);
m_RTC.SetTimer();
esp_deep_sleep_start(); esp_deep_sleep_start();
} }
@ -158,7 +165,7 @@ void Watchy::SyncNTPTime()
if (success) { if (success) {
tmElements_t tm; tmElements_t tm;
breakTime((time_t)timeClient.getEpochTime(), tm); breakTime((time_t)timeClient.getEpochTime(), tm);
m_RTC.set(tm); m_RTC.Set(tm);
} else { } else {
Serial.begin(9600); Serial.begin(9600);
Serial.println("Failed to get NTP time"); Serial.println("Failed to get NTP time");

View file

@ -1,58 +1,53 @@
#include "WatchyRTC.h" #include "WatchyRTC.h"
#if (UPDATE_INTERVAL > 255)
#error "UPDATE_INTERVAL must be either a multiple of 60, or less than 256 seconds"
#endif
RTC_DATA_ATTR bool WatchyRTC::m_timerSet = false;
RTC_DATA_ATTR bool WatchyRTC::m_initialTimer = true;
WatchyRTC::WatchyRTC() WatchyRTC::WatchyRTC()
{ {
} }
void WatchyRTC::init() { 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
int second = rtc_pcf.getSecond();
int minute = rtc_pcf.getMinute();
int hour = rtc_pcf.getHour();
minute += UPDATE_INTERVAL;
if (minute >= 60) {
hour += minute / 60;
minute = minute % 60;
}
rtc_pcf.setAlarm(minute, hour, 99, 99);
}
void WatchyRTC::read(tmElements_t & tm, int offsetInSeconds)
{ {
rtc_pcf.getDate(); }
void WatchyRTC::Get(tmElements_t & tm, int offsetInSeconds)
{
rtc_pcf.getDateTime();
tm.Year = y2kYearToTm(rtc_pcf.getYear()); tm.Year = y2kYearToTm(rtc_pcf.getYear());
tm.Month = rtc_pcf.getMonth(); tm.Month = rtc_pcf.getMonth();
tm.Day = rtc_pcf.getDay(); 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.Wday = rtc_pcf.getWeekday() + 1; // TimeLib has Wday range of 1-7, but PCF8563 stores day of week in 0-6 range
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();
OffsetTime(tm, offsetInSeconds); OffsetTime(tm, offsetInSeconds);
} }
void WatchyRTC::set(tmElements_t tm) { void WatchyRTC::Set(tmElements_t tm)
{
time_t t = makeTime(tm); // make and break to calculate tm.Wday time_t t = makeTime(tm); // make and break to calculate tm.Wday
breakTime(t, tm); breakTime(t, tm);
// 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(tm.Day, tm.Wday - 1, tm.Month, 0, tmYearToY2k(tm.Year)); rtc_pcf.setDateTime(tm.Day, tm.Wday - 1, tm.Month, 0, tmYearToY2k(tm.Year), tm.Hour, tm.Minute, tm.Second);
rtc_pcf.setTime(tm.Hour, tm.Minute, tm.Second); }
clearAlarm();
void WatchyRTC::SetTimer()
{
if (!m_timerSet) {
rtc_pcf.getDateTime();
// Sleep just long enough to get to a multiple of the update interval, makes updates happen on exact turn of the minute
int seconds = UPDATE_INTERVAL - (rtc_pcf.getSecond() % UPDATE_INTERVAL);
// Non repeating
rtc_pcf.setTimer(seconds, TMR_1Hz, false);
m_timerSet = true;
m_initialTimer = true;
}
} }
void WatchyRTC::OffsetTime(tmElements_t & tm, int offsetInSeconds) void WatchyRTC::OffsetTime(tmElements_t & tm, int offsetInSeconds)
@ -119,44 +114,25 @@ void WatchyRTC::OffsetTime(tmElements_t & tm, int offsetInSeconds)
tm.Second = second; tm.Second = second;
} }
void WatchyRTC::_PCFConfig(String datetime) { // String datetime is YYYY:MM:DD:HH:MM:SS // TODO: implement more advanced wakeup logic, i.e. > 255 seconds that are not a multiple of 60
if (datetime != "") { bool WatchyRTC::CheckWakeup()
tmElements_t tm; {
tm.Year = CalendarYrToTm(_getValue(datetime, ':', 0).toInt()); // YYYY - if(m_initialTimer) {
// 1970 m_initialTimer = false;
tm.Month = _getValue(datetime, ':', 1).toInt();
tm.Day = _getValue(datetime, ':', 2).toInt(); byte frequency, interval;
tm.Hour = _getValue(datetime, ':', 3).toInt(); if (UPDATE_INTERVAL % 60 == 0) {
tm.Minute = _getValue(datetime, ':', 4).toInt(); frequency = TMR_1MIN; // 1 minute
tm.Second = _getValue(datetime, ':', 5).toInt(); interval = UPDATE_INTERVAL / 60;
time_t t = makeTime(tm); // make and break to calculate tm.Wday } else {
breakTime(t, tm); frequency = TMR_1Hz; // 1 second
// day, weekday, month, century(1=1900, 0=2000), year(0-99) interval = UPDATE_INTERVAL;
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 rtc_pcf.setTimer(interval, frequency, true);
// months are 1-12
clearAlarm(); return true;
} }
String WatchyRTC::_getValue(String data, char separator, int index) { return true;
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]) : "";
} }

View file

@ -15,17 +15,16 @@ public:
public: public:
WatchyRTC(); WatchyRTC();
void init(); void Init();
void config(String datetime); // String datetime format is YYYY:MM:DD:HH:MM:SS void Get(tmElements_t & tm, int offsetInSeconds = 0);
void clearAlarm(); void Set(tmElements_t tm);
void read(tmElements_t & tm, int offsetInSeconds = 0); void SetTimer();
void set(tmElements_t tm); bool CheckWakeup(); // Checks to really wake up or not, also resets the timer after the initial sleep
static void OffsetTime(tmElements_t & tm, int offsetInSeconds); static void OffsetTime(tmElements_t & tm, int offsetInSeconds);
private: private:
void _PCFConfig(String datetime); static RTC_DATA_ATTR bool m_timerSet, m_initialTimer;
String _getValue(String data, char separator, int index);
}; };
#endif #endif

View file

@ -30,5 +30,5 @@
#define NTP_SERVER "pool.ntp.org" #define NTP_SERVER "pool.ntp.org"
#define UPDATE_INTERVAL 1 #define UPDATE_INTERVAL 60 // seconds
#define WAKE_ON_ACCEL_EVENTS false #define WAKE_ON_ACCEL_EVENTS false // useful if saving battery by not updating every minute