From 85ff28d347601d8c4fee43fa23e8aaa39a24a54e Mon Sep 17 00:00:00 2001 From: rljonesau Date: Sun, 9 Dec 2018 12:10:56 +1100 Subject: [PATCH] Support for DS3231, DS1307, PCF8523 or millis() based RTCs - Use BTCConfig.h to select --- Arduino/BTCDieselHeater/BTCConfig.h | 13 +++++++ Arduino/BTCDieselHeater/BTCDateTime.cpp | 2 +- Arduino/BTCDieselHeater/BTCDateTime.h | 3 +- Arduino/BTCDieselHeater/BTCDieselHeater.ino | 2 - Arduino/BTCDieselHeater/Clock.cpp | 36 ++++++++++++++++-- Arduino/BTCDieselHeater/Clock.h | 28 ++++++++++---- Arduino/BTCDieselHeater/Screen6.cpp | 2 - Arduino/BTCDieselHeater/ScreenHeader.cpp | 5 ++- Arduino/BTCDieselHeater/ScreenHeader.h | 1 + Arduino/BTCDieselHeater/ScreenManager.cpp | 42 ++++++++------------- 10 files changed, 90 insertions(+), 44 deletions(-) diff --git a/Arduino/BTCDieselHeater/BTCConfig.h b/Arduino/BTCDieselHeater/BTCConfig.h index e1a800b..a11d420 100644 --- a/Arduino/BTCDieselHeater/BTCConfig.h +++ b/Arduino/BTCDieselHeater/BTCConfig.h @@ -75,7 +75,20 @@ // DS18B20 temperature sensing // #define TEMPERATURE_INTERVAL 1000 + +/////////////////////////////////////////////////////////////////////////////// +// Real Time Clock support +// +// only select one option to use the indicated hardware +// if none are selected, RTC_Millis will be used, which is volatile (based upon millis()) +// +#define RTC_USE_DS3231 1 +#define RTC_USE_DS1307 0 +#define RTC_USE_PCF8523 0 +/////////////////////////////////////////////////////////////////////////////// +// Blue wire handling +// #define SUPPORT_OEM_CONTROLLER 1 diff --git a/Arduino/BTCDieselHeater/BTCDateTime.cpp b/Arduino/BTCDieselHeater/BTCDateTime.cpp index 049c61a..3819e9e 100644 --- a/Arduino/BTCDieselHeater/BTCDateTime.cpp +++ b/Arduino/BTCDieselHeater/BTCDateTime.cpp @@ -112,7 +112,7 @@ BTCDateTime::adjustSecond(int dir) } int -BTCDateTime::daysInMonth(int month, int year) +BTCDateTime::daysInMonth(int month, int year) const { if(month >= 1 && month <= 12) { int days = monthDays[month-1]; diff --git a/Arduino/BTCDieselHeater/BTCDateTime.h b/Arduino/BTCDieselHeater/BTCDateTime.h index e606bda..dfe767a 100644 --- a/Arduino/BTCDieselHeater/BTCDateTime.h +++ b/Arduino/BTCDieselHeater/BTCDateTime.h @@ -10,8 +10,7 @@ class BTCDateTime : public DateTime { public: const char* monthStr() const; const char* dowStr() const; - const char* briefDowStr() const; - int daysInMonth(int month, int year); + int daysInMonth(int month, int year) const; void adjustDay(int val); void adjustMonth(int val); void adjustYear(int dir); diff --git a/Arduino/BTCDieselHeater/BTCDieselHeater.ino b/Arduino/BTCDieselHeater/BTCDieselHeater.ino index 26f86aa..131db68 100644 --- a/Arduino/BTCDieselHeater/BTCDieselHeater.ino +++ b/Arduino/BTCDieselHeater/BTCDieselHeater.ino @@ -158,8 +158,6 @@ CProtocol DefaultBTCParams(CProtocol::CtrlMode); // defines the default paramet CSmartError SmartError; CKeyPad KeyPad; CScreenManager ScreenManager; -RTC_DS3231 rtc; -CClock Clock(rtc); sRxLine PCline; long lastRxTime; // used to observe inter character delays diff --git a/Arduino/BTCDieselHeater/Clock.cpp b/Arduino/BTCDieselHeater/Clock.cpp index c2667c2..9c8a2c4 100644 --- a/Arduino/BTCDieselHeater/Clock.cpp +++ b/Arduino/BTCDieselHeater/Clock.cpp @@ -5,15 +5,45 @@ #include "RTClib.h" #include "helpers.h" #include "NVStorage.h" +#include "DebugPort.h" + + +// create ONE of the RTClib supported real time clock classes +#if RTC_USE_DS3231 == 1 +RTC_DS3231 rtc; +#elif RTC_USE_DS1307 == 1 +RTC_DS1307 rtc; +#elif RTC_USE_PCF8523 == 1 +RTC_PCF8523 rtc; +#else +RTC_Millis rtc; +#endif + +CClock Clock(rtc); -CClock::CClock(RTC_DS3231& rtc) : _rtc(rtc) -{ -} void CClock::begin() { + // announce which sort of RTC is being used +#if RTC_USE_DS3231 == 1 +DebugPort.println("Using DS3231 Real Time Clock"); +#elif RTC_USE_DS1307 == 1 +DebugPort.println("Using DS1307 Real Time Clock"); +#elif RTC_USE_PCF8523 == 1 +DebugPort.println("Using PCF8523 Real Time Clock"); +#else +#define SW_RTC // enable different begin() call for the millis() based RTC +DebugPort.println("Using millis() based psuedo \"Real Time Clock\""); +#endif + +#ifdef SW_RTC + DateTime zero(2019, 1, 1); // can be pushed along as seen fit! + _rtc.begin(zero); +#else _rtc.begin(); +#endif + _nextRTCfetch = millis(); } diff --git a/Arduino/BTCDieselHeater/Clock.h b/Arduino/BTCDieselHeater/Clock.h index c4c295c..b667e5a 100644 --- a/Arduino/BTCDieselHeater/Clock.h +++ b/Arduino/BTCDieselHeater/Clock.h @@ -2,23 +2,37 @@ #define __BTC_TIMERS_H__ #include "BTCDateTime.h" +#include "BTCConfig.h" -/*const BTCDateTime& getCurrentTime(); -void setCurrentTime(const DateTime& newDateTime); - -void initClock(); -void checkClock();*/ class CClock { - + // allow use of ONE of the RTClib supported RTC chips + // reference to the selected rtc stored here +#if RTC_USE_DS3231 == 1 RTC_DS3231& _rtc; +#elif RTC_USE_DS1307 == 1 + RTC_DS1307& _rtc; +#elif RTC_USE_PCF8523 == 1 + RTC_PCF8523& _rtc; +#else + RTC_Millis& _rtc; +#endif unsigned long _nextRTCfetch; BTCDateTime _currentTime; void _checkTimers(); void _checkTimer(int timer, const DateTime& now); public: - CClock(RTC_DS3231& rtc); + // constructors for ONE of the RTClib supported RTC chips +#if RTC_USE_DS3231 == 1 + CClock(RTC_DS3231& rtc) : _rtc(rtc) {}; +#elif RTC_USE_DS1307 == 1 + CClock(RTC_1307& rtc) : _rtc(rtc) {}; +#elif RTC_USE_PCF8523 == 1 + CClock(RTC_PCF8523& rtc) : _rtc(rtc) {}; +#else + CClock(RTC_Millis& rtc) : _rtc(rtc) {}; +#endif void begin(); const BTCDateTime& update(); const BTCDateTime& get() const; diff --git a/Arduino/BTCDieselHeater/Screen6.cpp b/Arduino/BTCDieselHeater/Screen6.cpp index 10dc789..f7bfca2 100644 --- a/Arduino/BTCDieselHeater/Screen6.cpp +++ b/Arduino/BTCDieselHeater/Screen6.cpp @@ -35,8 +35,6 @@ #include "Arial.h" #include "Clock.h" -extern RTC_DS3231 rtc; - CScreen6::CScreen6(C128x64_OLED& display, CScreenManager& mgr) : CScreenHeader(display, mgr) { diff --git a/Arduino/BTCDieselHeater/ScreenHeader.cpp b/Arduino/BTCDieselHeater/ScreenHeader.cpp index ccc64f6..bf9e35b 100644 --- a/Arduino/BTCDieselHeater/ScreenHeader.cpp +++ b/Arduino/BTCDieselHeater/ScreenHeader.cpp @@ -31,6 +31,7 @@ CScreenHeader::CScreenHeader(C128x64_OLED& disp, CScreenManager& mgr) : CScreen( { _clearUpAnimation = false; _clearDnAnimation = false; + _colon = false; } void @@ -194,10 +195,12 @@ CScreenHeader::showTime(int numTimers) const BTCDateTime& now = Clock.get(); char msg[16]; - if(now.second() & 0x01) + if(_colon) sprintf(msg, "%02d:%02d", now.hour(), now.minute()); else sprintf(msg, "%02d %02d", now.hour(), now.minute()); + _colon = !_colon; + { CTransientFont AF(_display, &arial_8ptFontInfo); // determine centre position of remaining real estate diff --git a/Arduino/BTCDieselHeater/ScreenHeader.h b/Arduino/BTCDieselHeater/ScreenHeader.h index 9eb7b57..7e10bf8 100644 --- a/Arduino/BTCDieselHeater/ScreenHeader.h +++ b/Arduino/BTCDieselHeater/ScreenHeader.h @@ -31,6 +31,7 @@ class CScreenHeader : public CScreen { bool _clearUpAnimation; bool _clearDnAnimation; + bool _colon; protected: void showBTicon(); void showWifiIcon(); diff --git a/Arduino/BTCDieselHeater/ScreenManager.cpp b/Arduino/BTCDieselHeater/ScreenManager.cpp index dcd9522..281e6b8 100644 --- a/Arduino/BTCDieselHeater/ScreenManager.cpp +++ b/Arduino/BTCDieselHeater/ScreenManager.cpp @@ -1,9 +1,7 @@ #include "ScreenManager.h" -#include #include #include "128x64OLED.h" #include "pins.h" -#include "BluetoothAbstract.h" #include "Screen1.h" #include "Screen2.h" #include "Screen3.h" @@ -12,21 +10,8 @@ #include "Screen6.h" #include "Screen7.h" #include "Screen8.h" +#include "BTCConfig.h" -// -// **** NOTE: If trying use hardware SPI on the ESP32 there are two very lame -// **** libaries conspiring to make life difficult -// -// A/ The ESP32 SPI.cpp library instatiates an instance of SPI, using the VSPI port (and pins) -// B/ The Adfruit_SH1106 library hard codes "SPI" as the SPI port instance -// -// As an ESP32 has a pin multiplexer, this is very bad form. -// The actual design here uses the defacto HSPI pins (and port), -// You **MUST comment out the SPIClass SPI(VSPI);** at the end of the ESP32 SPI library -// then we declare "SPI" here, which will use HSPI!!!! - -// 128 x 64 OLED support -//SPIClass SPI; // default constructor opens HSPI on standard pins : MOSI=13,CLK=14,MISO=12(unused) //////////////////////////////////////////////////////////////////////////////////////////////// // splash creen created using image2cpp http://javl.github.io/image2cpp/ @@ -144,16 +129,21 @@ CScreenManager::init() _pDisplay->display(); DebugPort.println("Creating Screens"); - _Screens.push_back(new CScreen1(*_pDisplay, *this)); // detail control - _Screens.push_back(new CScreen2(*_pDisplay, *this)); // basic control - _Screens.push_back(new CScreen8(*_pDisplay, *this)); // clock - _Screens.push_back(new CScreen3(*_pDisplay, *this)); // mode / priming - _Screens.push_back(new CScreen4(*_pDisplay, *this)); // comms info - _Screens.push_back(new CScreen5(*_pDisplay, *this)); // tuning - _Screens.push_back(new CScreen6(*_pDisplay, *this)); // clock set - _Screens.push_back(new CScreen7(*_pDisplay, *this, 0)); // timer 1 - _Screens.push_back(new CScreen7(*_pDisplay, *this, 1)); // timer 2 - _currentScreen = 1; + _Screens.push_back(new CScreen1(*_pDisplay, *this)); // 0: detail control + _Screens.push_back(new CScreen2(*_pDisplay, *this)); // 1: basic control + _Screens.push_back(new CScreen8(*_pDisplay, *this)); // 2: clock + _Screens.push_back(new CScreen3(*_pDisplay, *this)); // 3: mode / priming + _Screens.push_back(new CScreen4(*_pDisplay, *this)); // 4: comms info + _Screens.push_back(new CScreen5(*_pDisplay, *this)); // 5: tuning + _Screens.push_back(new CScreen6(*_pDisplay, *this)); // 6: clock set + _Screens.push_back(new CScreen7(*_pDisplay, *this, 0)); // 7: set timer 1 + _Screens.push_back(new CScreen7(*_pDisplay, *this, 1)); // 8: set timer 2 + +#if RTC_USE_DS3231==0 && RTC_USE_DS1307==0 && RTC_USE_PCF8523==0 + _currentScreen = 6; // bring up clock set screen first if using millis based RTC! +#else + _currentScreen = 1; // basic control screen +#endif _switchScreen(); }