2018-12-16 03:27:08 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the "bluetoothheater" distribution
|
|
|
|
* (https://gitlab.com/mrjones.id.au/bluetoothheater)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018 Ray Jones <ray@mrjones.id.au>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-12-08 22:55:55 +00:00
|
|
|
#include <Arduino.h>
|
|
|
|
#include "Clock.h"
|
|
|
|
#include "BTCDateTime.h"
|
2019-02-20 10:50:28 +00:00
|
|
|
#include "TimerManager.h"
|
2018-12-16 07:34:39 +00:00
|
|
|
#include <Wire.h>
|
2019-07-18 12:28:40 +00:00
|
|
|
//#include "DS3231Ex.h"
|
2019-06-15 23:09:29 +00:00
|
|
|
#include "../Utility/helpers.h"
|
2018-12-16 07:34:39 +00:00
|
|
|
#include "../Utility/NVStorage.h"
|
|
|
|
#include "../Utility/DebugPort.h"
|
2018-12-09 01:10:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
// create ONE of the RTClib supported real time clock classes
|
|
|
|
#if RTC_USE_DS3231 == 1
|
2019-07-18 12:28:40 +00:00
|
|
|
RTC_DS3231Ex rtc;
|
2018-12-09 01:10:56 +00:00
|
|
|
#elif RTC_USE_DS1307 == 1
|
|
|
|
RTC_DS1307 rtc;
|
|
|
|
#elif RTC_USE_PCF8523 == 1
|
|
|
|
RTC_PCF8523 rtc;
|
|
|
|
#else
|
|
|
|
RTC_Millis rtc;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
CClock Clock(rtc);
|
2018-12-08 22:55:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
CClock::begin()
|
|
|
|
{
|
2018-12-09 01:10:56 +00:00
|
|
|
// 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
|
2018-12-08 22:55:55 +00:00
|
|
|
_rtc.begin();
|
2018-12-09 01:10:56 +00:00
|
|
|
#endif
|
|
|
|
|
2018-12-08 22:55:55 +00:00
|
|
|
_nextRTCfetch = millis();
|
2019-01-27 01:17:49 +00:00
|
|
|
|
|
|
|
update();
|
2019-07-28 07:40:12 +00:00
|
|
|
|
|
|
|
CTimerManager::createMap();
|
2018-12-08 22:55:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const BTCDateTime&
|
|
|
|
CClock::update()
|
|
|
|
{
|
|
|
|
long deltaT = millis() - _nextRTCfetch;
|
|
|
|
if(deltaT >= 0) {
|
|
|
|
uint32_t origClock = Wire.getClock();
|
|
|
|
Wire.setClock(400000);
|
|
|
|
_currentTime = _rtc.now(); // moderate I2C accesses
|
|
|
|
Wire.setClock(origClock);
|
|
|
|
_nextRTCfetch = millis() + 500;
|
2019-02-20 10:50:28 +00:00
|
|
|
// _checkTimers();
|
|
|
|
// check timers upon minute rollovers
|
|
|
|
if(_currentTime.minute() != _prevMinute) {
|
|
|
|
CTimerManager::manageTime(_currentTime.hour(), _currentTime.minute(), _currentTime.dayOfTheWeek());
|
|
|
|
_prevMinute = _currentTime.minute();
|
|
|
|
}
|
2018-12-08 22:55:55 +00:00
|
|
|
}
|
|
|
|
return _currentTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
const BTCDateTime&
|
|
|
|
CClock::get() const
|
|
|
|
{
|
|
|
|
return _currentTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CClock::set(const DateTime& newTimeDate)
|
|
|
|
{
|
|
|
|
_rtc.adjust(newTimeDate);
|
|
|
|
}
|
|
|
|
|
2019-07-18 12:28:40 +00:00
|
|
|
void
|
|
|
|
CClock::saveData(uint8_t* pData, int len, int ofs)
|
|
|
|
{
|
|
|
|
_rtc.writeData(pData, len, ofs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CClock::readData(uint8_t* pData, int len, int ofs)
|
|
|
|
{
|
|
|
|
_rtc.readData(pData, len, ofs);
|
|
|
|
}
|
2018-12-08 22:55:55 +00:00
|
|
|
|
2019-07-19 20:53:12 +00:00
|
|
|
bool
|
|
|
|
CClock::lostPower()
|
|
|
|
{
|
|
|
|
return _rtc.lostPower();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CClock::resetLostPower()
|
|
|
|
{
|
|
|
|
_rtc.resetLostPower();
|
|
|
|
}
|
2018-12-15 09:34:58 +00:00
|
|
|
|
|
|
|
void setDateTime(const char* newTime)
|
|
|
|
{
|
2019-05-12 10:15:18 +00:00
|
|
|
DebugPort.printf("setting time to: %s\r\n", newTime);
|
2018-12-15 09:34:58 +00:00
|
|
|
int month,day,year,hour,minute,second;
|
|
|
|
if(6 == sscanf(newTime, "%d/%d/%d %d:%d:%d", &day, &month, &year, &hour, &minute, &second)) {
|
|
|
|
DateTime newDateTime(year, month, day, hour, minute, second);
|
|
|
|
Clock.set(newDateTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void setDate(const char* newDate)
|
|
|
|
{
|
2019-05-12 10:15:18 +00:00
|
|
|
DebugPort.printf("setting date to: %s\r\n", newDate);
|
2018-12-15 09:34:58 +00:00
|
|
|
int month,day,year;
|
|
|
|
if(3 == sscanf(newDate, "%d/%d/%d", &day, &month, &year)) {
|
|
|
|
DateTime currentDateTime = Clock.get();
|
|
|
|
DateTime newDateTime(year, month, day, currentDateTime.hour(), currentDateTime.minute(), currentDateTime.second());
|
|
|
|
Clock.set(newDateTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void setTime(const char* newTime)
|
|
|
|
{
|
2019-05-12 10:15:18 +00:00
|
|
|
DebugPort.printf("setting time to: %s\r\n", newTime);
|
2018-12-15 09:34:58 +00:00
|
|
|
int hour,minute,second;
|
|
|
|
if(3 == sscanf(newTime, "%d:%d:%d", &hour, &minute, &second)) {
|
|
|
|
DateTime currentDateTime = Clock.get();
|
|
|
|
DateTime newDateTime(currentDateTime.year(), currentDateTime.month(), currentDateTime.day(), hour, minute, second);
|
|
|
|
Clock.set(newDateTime);
|
|
|
|
}
|
2018-12-19 08:39:07 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 12:28:40 +00:00
|
|
|
#define _I2C_WRITE write
|
|
|
|
#define _I2C_READ read
|
|
|
|
|
2019-07-19 20:53:12 +00:00
|
|
|
void
|
|
|
|
RTC_DS3231Ex::writeData(uint8_t* pData, int len, int ofs) {
|
2019-07-18 12:28:40 +00:00
|
|
|
Wire.beginTransmission(DS3231_ADDRESS);
|
|
|
|
Wire._I2C_WRITE((byte)(7+ofs)); // start at alarm bytes
|
|
|
|
for(int i=0; i<len; i++) {
|
|
|
|
Wire._I2C_WRITE(*pData++);
|
|
|
|
}
|
|
|
|
Wire.endTransmission();
|
|
|
|
}
|
|
|
|
|
2019-07-19 20:53:12 +00:00
|
|
|
void
|
|
|
|
RTC_DS3231Ex::readData(uint8_t* pData, int len, int ofs) {
|
2019-07-18 12:28:40 +00:00
|
|
|
Wire.beginTransmission(DS3231_ADDRESS);
|
|
|
|
Wire._I2C_WRITE((byte)(7+ofs)); // start at alarm bytes
|
|
|
|
Wire.endTransmission();
|
|
|
|
|
|
|
|
Wire.requestFrom(DS3231_ADDRESS, len);
|
|
|
|
for(int i=0; i<len; i++) {
|
|
|
|
*pData++ = Wire._I2C_READ();
|
|
|
|
}
|
|
|
|
Wire.endTransmission();
|
|
|
|
}
|
|
|
|
|
2019-07-20 06:08:43 +00:00
|
|
|
void
|
2019-07-19 20:53:12 +00:00
|
|
|
RTC_DS3231Ex::resetLostPower()
|
|
|
|
{
|
|
|
|
Wire.beginTransmission(DS3231_ADDRESS);
|
|
|
|
Wire._I2C_WRITE(DS3231_STATUSREG);
|
|
|
|
Wire.endTransmission();
|
|
|
|
|
|
|
|
Wire.requestFrom(DS3231_ADDRESS, 1);
|
|
|
|
uint8_t sts = Wire._I2C_READ();
|
|
|
|
|
|
|
|
sts &= 0x7f; // clear power loss flag
|
|
|
|
|
|
|
|
Wire.beginTransmission(DS3231_ADDRESS);
|
|
|
|
Wire._I2C_WRITE(DS3231_STATUSREG);
|
|
|
|
Wire._I2C_WRITE(sts);
|
|
|
|
Wire.endTransmission();
|
|
|
|
}
|
|
|
|
|