diff --git a/Arduino/BTCDieselHeater/BTCDieselHeater.ino b/Arduino/BTCDieselHeater/BTCDieselHeater.ino index 2b9c861..661f5a6 100644 --- a/Arduino/BTCDieselHeater/BTCDieselHeater.ino +++ b/Arduino/BTCDieselHeater/BTCDieselHeater.ino @@ -139,7 +139,7 @@ void initBlueWireSerial(); bool validateFrame(const CProtocol& frame, const char* name); void checkDisplayUpdate(); void checkTimer(); -void checkTimer(const DateTime& now, sTimer timerInfo); +void checkTimer(int timer, const DateTime& now); void checkDebugCommands(); // DS18B20 temperature sensor support @@ -950,32 +950,44 @@ void checkTimer() { const DateTime& now = getCurrentTime(); - sTimer timerInfo; - // test timer 1 - NVstore.getTimerInfo(0, timerInfo); - checkTimer(now, timerInfo); - // test timer 2 - NVstore.getTimerInfo(1, timerInfo); - checkTimer(now, timerInfo); + checkTimer(0, now); // test timer 1 + checkTimer(1, now); // test timer 2 } -void checkTimer(const DateTime& now, sTimer timerInfo) +void checkTimer(int timer, const DateTime& now) { - int maskDOW = 0x01 << now.dayOfTheWeek(); - if(timerInfo.enabled & (maskDOW | 0x80) ) { // specific day, or next day - // check start - if(now.hour() == timerInfo.start.hour && now.minute() == timerInfo.start.min) { + sTimer Info; + NVstore.getTimerInfo(timer, Info); + int DOW = now.dayOfTheWeek(); + int timeNow = now.hour() * 60 + now.minute(); + int timeStart = Info.start.hour * 60 + Info.start.min; + int timeStop = Info.stop.hour * 60 + Info.stop.min; + + // ensure DOW tracks expected start day should timer straddle midnight + if(timeStop < timeStart) { // true if stop is next morning + if(timeNow <= timeStop) { // current time has passed midnight - enable flag is based upon prior day + DOW--; + ROLLLOWERLIMIT(DOW, 0, 6); // fixup for saturday night! + } + } + // DOW of week is now correct for the day this timer started + int maskDOW = 0x01 << DOW; + + if(Info.enabled & (maskDOW | 0x80) ) { // specific day of week, or next day + + if(timeNow == timeStart && now.second() < 3) { // check start, within 2 seconds of the minute rollover requestOn(); } - // check stop - if(now.hour() == timerInfo.stop.hour && now.minute() == timerInfo.stop.min) { + + if(timeNow == timeStop) { // check stop requestOff(); - if(!timerInfo.repeat) { // cancel timer if non repeating - if(timerInfo.enabled & 0x80) // next day start flag? - timerInfo.enabled = 0; // outright cancel - else - timerInfo.enabled &= ~maskDOW; // otherwise clear particular day - NVstore.setTimerInfo(0, timerInfo); + if(!Info.repeat) { // cancel timer if non repeating + if(Info.enabled & 0x80) // next day start flag set? + Info.enabled = 0; // outright cancel + else { + Info.enabled &= ~maskDOW; // otherwise clear specific day + } + NVstore.setTimerInfo(timer, Info); NVstore.save(); } } diff --git a/Arduino/BTCDieselHeater/Screen.cpp b/Arduino/BTCDieselHeater/Screen.cpp index 3ae17da..572b4fc 100644 --- a/Arduino/BTCDieselHeater/Screen.cpp +++ b/Arduino/BTCDieselHeater/Screen.cpp @@ -29,6 +29,11 @@ CScreen::show() { } +void +CScreen::onSelect() +{ +} + void CScreen::_printMenuText(int x, int y, const char* str, bool selected, eJUSTIFY justify, int border, int radius) diff --git a/Arduino/BTCDieselHeater/Screen.h b/Arduino/BTCDieselHeater/Screen.h index 053a280..acf8e50 100644 --- a/Arduino/BTCDieselHeater/Screen.h +++ b/Arduino/BTCDieselHeater/Screen.h @@ -48,6 +48,7 @@ protected: public: CScreen(C128x64_OLED& disp, CScreenManager& mgr); virtual ~CScreen(); + virtual void onSelect(); virtual bool animate(); virtual void show(); virtual void keyHandler(uint8_t event) {}; diff --git a/Arduino/BTCDieselHeater/Screen7.cpp b/Arduino/BTCDieselHeater/Screen7.cpp index 76cf3bc..e26c17b 100644 --- a/Arduino/BTCDieselHeater/Screen7.cpp +++ b/Arduino/BTCDieselHeater/Screen7.cpp @@ -42,9 +42,13 @@ CScreen7::CScreen7(C128x64_OLED& display, CScreenManager& mgr, int instance) : C _rowSel = 0; _colSel = 0; _instance = instance; - NVstore.getTimerInfo(_instance, _timer); } +void +CScreen7::onSelect() +{ + NVstore.getTimerInfo(_instance, _timer); +} void CScreen7::show() diff --git a/Arduino/BTCDieselHeater/Screen7.h b/Arduino/BTCDieselHeater/Screen7.h index fa392c3..d07f2bb 100644 --- a/Arduino/BTCDieselHeater/Screen7.h +++ b/Arduino/BTCDieselHeater/Screen7.h @@ -37,6 +37,7 @@ class CScreen7 : public CScreenHeader { public: CScreen7(C128x64_OLED& display, CScreenManager& mgr, int instance); + void onSelect(); void show(); void keyHandler(uint8_t event); }; diff --git a/Arduino/BTCDieselHeater/ScreenManager.cpp b/Arduino/BTCDieselHeater/ScreenManager.cpp index 14c15aa..1739bc1 100644 --- a/Arduino/BTCDieselHeater/ScreenManager.cpp +++ b/Arduino/BTCDieselHeater/ScreenManager.cpp @@ -193,6 +193,9 @@ CScreenManager::refresh() void CScreenManager::_switchScreen() { + if(_currentScreen >= 0) + _Screens[_currentScreen]->onSelect(); + reqUpdate(); }