-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Firmware: refactored clock handling into a polymorphic hyerarchy fo…
…r hardware RTC and software timekeeping - Firmware: this fixes the RTC adjust time bug
- Loading branch information
1 parent
cbeda00
commit 048a205
Showing
7 changed files
with
269 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* MeteoClock.h | ||
* | ||
* Abstract class to represent clock devices. | ||
* It defines the basic operations of clocks. There will be | ||
* implementations based on RTC (DS3231) and software timing. | ||
* | ||
* Created on: Sep 25, 2018 | ||
* Author: jguillaumes | ||
* | ||
*/ | ||
|
||
#ifndef METEOCLOCK_H_ | ||
#define METEOCLOCK_H_ | ||
|
||
#include <Arduino.h> | ||
|
||
|
||
class MeteoClock { | ||
public: | ||
virtual ~MeteoClock() {} | ||
virtual void setClock(String ×tamp) = 0; | ||
virtual void setClock(int year, int month, int day, | ||
int hour, int minute, int second) = 0; | ||
|
||
virtual String getClock() = 0; | ||
}; | ||
|
||
#endif /* METEOCLOCK_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* MeteoClockRTC.cpp | ||
* | ||
* Created on: Sep 25, 2018 | ||
* Author: jguillaumes | ||
*/ | ||
|
||
#include <Arduino.h> | ||
#include <RTCLib.h> | ||
|
||
#include "MeteoClockRTC.h" | ||
|
||
|
||
MeteoClockRTC::~MeteoClockRTC() { | ||
RTC_DS3231 *rtc = (RTC_DS3231 *) _theRTC; | ||
delete rtc; | ||
_theRTC = NULL; | ||
} | ||
|
||
//+ | ||
// Constructor: creates a new RTC_DS3231 instance and stores a pointer | ||
// to it as a private variable. | ||
// The variable is stored as a void pointer so the header file does not | ||
// have to #include the RTClib.h. If it did there would be name conflicts | ||
// with the software clock | ||
//- | ||
MeteoClockRTC::MeteoClockRTC() { | ||
RTC_DS3231 *rtc = new RTC_DS3231; | ||
this->_theRTC = (void *) rtc; | ||
} | ||
|
||
//+ | ||
// Set (adjust) the clock from a YYYMMDDhhmmss string | ||
// There is no formal checking done. If the string is not correct the | ||
// results are undefined. | ||
//- | ||
void MeteoClockRTC::setClock(String ×tamp) { | ||
uint16_t year = atoi(timestamp.substring(0,4).c_str()); | ||
uint8_t month = atoi(timestamp.substring(4,6).c_str()); | ||
uint8_t day = atoi(timestamp.substring(6,8).c_str()); | ||
uint8_t hour = atoi(timestamp.substring(8,10).c_str()); | ||
uint8_t minute = atoi(timestamp.substring(10,12).c_str()); | ||
uint8_t second = atoi(timestamp.substring(12,14).c_str()); | ||
|
||
this->setClock(year, month, day, hour, minute, second); | ||
} | ||
|
||
//+ | ||
// Set (adjust) the clock using separate parameters for each | ||
// date and time component. | ||
// There is no formal checking done. If the combo is not correct the | ||
// results are undefined. | ||
//- | ||
void MeteoClockRTC::setClock(int year, int month, int day, | ||
int hour, int minute, int second){ | ||
DateTime dt(year, month, day, hour, minute, second); | ||
((RTC_DS3231 *) this->_theRTC)->adjust(dt); | ||
} | ||
|
||
//+ | ||
// Get the current date and time as a YYYYMMDDhhmmss string | ||
//- | ||
String MeteoClockRTC::getClock() { | ||
DateTime now; | ||
char timbuf[17]; | ||
|
||
now = ((RTC_DS3231 *) this->_theRTC)->now(); | ||
sprintf(timbuf, "%04d%02d%02d%02d%02d%02d", now.year(), now.month(), now.day(), | ||
now.hour(), now.minute(), now.second()); | ||
return String(timbuf); | ||
} | ||
|
||
//+ | ||
// Verify if the clock is physically present on the 2WI bus | ||
// responds with consistent results. | ||
//- | ||
bool MeteoClockRTC::checkClock() { | ||
bool rtcPresent; | ||
RTC_DS3231 *rtc = (RTC_DS3231 *) _theRTC; | ||
|
||
rtc->begin(); | ||
if (rtc->now().year() > 2100) { | ||
rtcPresent = false; | ||
} else { | ||
rtcPresent = true; | ||
} | ||
return rtcPresent; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* MeteoClockRTC.h | ||
* | ||
* Clock handling based on an DS3231 RTC chip. | ||
* | ||
* Created on: Sep 25, 2018 | ||
* Author: jguillaumes | ||
*/ | ||
|
||
#ifndef METEOCLOCKRTC_H_ | ||
#define METEOCLOCKRTC_H_ | ||
|
||
#include "Arduino.h" | ||
#include "MeteoClock.h" | ||
|
||
class MeteoClockRTC: public MeteoClock { | ||
public: | ||
virtual ~MeteoClockRTC(); | ||
MeteoClockRTC(); | ||
|
||
virtual void setClock(String ×tamp) ; | ||
virtual void setClock(int year, int month, int day, | ||
int hour, int minute, int second); | ||
virtual String getClock(); | ||
|
||
bool checkClock(); | ||
|
||
private: | ||
void *_theRTC; // Pointer to a RTC_DS3231 object, stored as void* | ||
// to avoid having to include the RTClib header | ||
|
||
}; | ||
|
||
#endif /* METEOCLOCKRTC_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* MeteoClockSoft.cpp | ||
* | ||
* Created on: Sep 25, 2018 | ||
* Author: jguillaumes | ||
*/ | ||
|
||
#include <Arduino.h> | ||
#include <Time.h> | ||
|
||
#include "MeteoClockSoft.h" | ||
|
||
//+ | ||
// Empty constructor | ||
//- | ||
MeteoClockSoft::MeteoClockSoft() { | ||
} | ||
|
||
//+ | ||
// Empty destructor | ||
//- | ||
MeteoClockSoft::~MeteoClockSoft() { | ||
} | ||
|
||
//+ | ||
// Set (adjust) the time from a YYYMMDDhhmmss string | ||
// There is no formal checking done. If the string is not correct the | ||
// results are undefined. | ||
//- | ||
void MeteoClockSoft::setClock(String ×tamp) { | ||
uint16_t year = atoi(timestamp.substring(0,4).c_str()); | ||
uint8_t month = atoi(timestamp.substring(4,6).c_str()); | ||
uint8_t day = atoi(timestamp.substring(6,8).c_str()); | ||
uint8_t hour = atoi(timestamp.substring(8,10).c_str()); | ||
uint8_t minute = atoi(timestamp.substring(10,12).c_str()); | ||
uint8_t second = atoi(timestamp.substring(12,14).c_str()); | ||
|
||
this->setClock(year, month, day, hour, minute, second); | ||
} | ||
|
||
//+ | ||
// Set (adjust) the time using separate parameters for each | ||
// date and time component. | ||
// There is no formal checking done. If the combo is not correct the | ||
// results are undefined. | ||
//- | ||
|
||
void MeteoClockSoft::setClock(int year, int month, int day, | ||
int hour, int minute, int second) { | ||
setTime(hour, minute, second, day, month, year); | ||
} | ||
|
||
//+ | ||
// Get the current date and time as a YYYYMMDDhhmmss string | ||
//- | ||
String MeteoClockSoft::getClock() { | ||
char timbuf[17]; | ||
|
||
sprintf(timbuf, "%04d%02d%02d%02d%02d%02d", year(), month(), day(), | ||
hour(), minute(), second()); | ||
|
||
|
||
return String(timbuf); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* MeteoClockSoft.h | ||
* | ||
* Clock handling based on software timekeeping | ||
* | ||
* Created on: Sep 25, 2018 | ||
* Author: jguillaumes | ||
*/ | ||
|
||
#ifndef METEOCLOCKSOFT_H_ | ||
#define METEOCLOCKSOFT_H_ | ||
|
||
#include "MeteoClock.h" | ||
|
||
class MeteoClockSoft: public MeteoClock { | ||
public: | ||
MeteoClockSoft(); | ||
virtual ~MeteoClockSoft(); | ||
|
||
virtual void setClock(String ×tamp) ; | ||
virtual void setClock(int year, int month, int day, | ||
int hour, int minute, int second) ; | ||
virtual String getClock(); | ||
|
||
}; | ||
|
||
#endif /* METEOCLOCKSOFT_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
#ifndef _READ_TEMPERATURE_H | ||
#define _READ_TEMPERATURE_H | ||
|
||
#define DEBUG 1 | ||
// #define DEBUG 1 | ||
|
||
#include <OneWire.h> | ||
|
||
|