-
Notifications
You must be signed in to change notification settings - Fork 712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RTC Superclass #215
base: master
Are you sure you want to change the base?
RTC Superclass #215
Changes from 29 commits
cb0bf8d
0af4be5
6f49b2b
bca61c2
ba9c0ed
ae82978
dded178
7654480
e3757d3
9dfa49b
00075a4
eaec406
68bed44
54fcd63
2b98009
4a25373
c1001bc
f00f0af
58e5432
1614120
de9a42e
189ddc9
0975cc5
3ab7669
3721491
290bea2
41b522d
2f99da8
edc0af4
fd67d34
80b5650
7213586
5eba8f6
db255bf
e2a981c
8f7e656
c13d1c4
56f3426
678f814
2e664b4
3151cf7
b8d8b77
266cc68
84fbf61
ad85753
5cee6ba
a5a037d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,8 +22,10 @@ | |
#ifndef _RTCLIB_H_ | ||
#define _RTCLIB_H_ | ||
|
||
#include "utility/RTC_Super.h" | ||
#include "utility/DateTime.h" | ||
#include "utility/TimeSpan.h" | ||
#include <Arduino.h> | ||
class TimeSpan; | ||
|
||
/** Registers */ | ||
#define PCF8523_ADDRESS 0x68 ///< I2C address for PCF8523 | ||
|
@@ -57,205 +59,6 @@ class TimeSpan; | |
0x11 ///< Temperature register (high byte - low byte is at 0x12), 10-bit | ||
///< temperature value | ||
|
||
/** Constants */ | ||
#define SECONDS_PER_DAY 86400L ///< 60 * 60 * 24 | ||
#define SECONDS_FROM_1970_TO_2000 \ | ||
946684800 ///< Unixtime for 2000-01-01 00:00:00, useful for initialization | ||
|
||
/**************************************************************************/ | ||
/*! | ||
@brief Simple general-purpose date/time class (no TZ / DST / leap | ||
seconds). | ||
|
||
This class stores date and time information in a broken-down form, as a | ||
tuple (year, month, day, hour, minute, second). The day of the week is | ||
not stored, but computed on request. The class has no notion of time | ||
zones, daylight saving time, or | ||
[leap seconds](http://en.wikipedia.org/wiki/Leap_second): time is stored | ||
in whatever time zone the user chooses to use. | ||
|
||
The class supports dates in the range from 1 Jan 2000 to 31 Dec 2099 | ||
inclusive. | ||
*/ | ||
/**************************************************************************/ | ||
class DateTime { | ||
public: | ||
DateTime(uint32_t t = SECONDS_FROM_1970_TO_2000); | ||
DateTime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour = 0, | ||
uint8_t min = 0, uint8_t sec = 0); | ||
DateTime(const DateTime ©); | ||
DateTime(const char *date, const char *time); | ||
DateTime(const __FlashStringHelper *date, const __FlashStringHelper *time); | ||
DateTime(const char *iso8601date); | ||
bool isValid() const; | ||
char *toString(char *buffer); | ||
|
||
/*! | ||
@brief Return the year. | ||
@return Year (range: 2000--2099). | ||
*/ | ||
uint16_t year() const { return 2000U + yOff; } | ||
/*! | ||
@brief Return the month. | ||
@return Month number (1--12). | ||
*/ | ||
uint8_t month() const { return m; } | ||
/*! | ||
@brief Return the day of the month. | ||
@return Day of the month (1--31). | ||
*/ | ||
uint8_t day() const { return d; } | ||
/*! | ||
@brief Return the hour | ||
@return Hour (0--23). | ||
*/ | ||
uint8_t hour() const { return hh; } | ||
|
||
uint8_t twelveHour() const; | ||
/*! | ||
@brief Return whether the time is PM. | ||
@return 0 if the time is AM, 1 if it's PM. | ||
*/ | ||
uint8_t isPM() const { return hh >= 12; } | ||
/*! | ||
@brief Return the minute. | ||
@return Minute (0--59). | ||
*/ | ||
uint8_t minute() const { return mm; } | ||
/*! | ||
@brief Return the second. | ||
@return Second (0--59). | ||
*/ | ||
uint8_t second() const { return ss; } | ||
|
||
uint8_t dayOfTheWeek() const; | ||
|
||
/* 32-bit times as seconds since 2000-01-01. */ | ||
uint32_t secondstime() const; | ||
|
||
/* 32-bit times as seconds since 1970-01-01. */ | ||
uint32_t unixtime(void) const; | ||
|
||
/*! | ||
Format of the ISO 8601 timestamp generated by `timestamp()`. Each | ||
option corresponds to a `toString()` format as follows: | ||
*/ | ||
enum timestampOpt { | ||
TIMESTAMP_FULL, //!< `YYYY-MM-DDThh:mm:ss` | ||
TIMESTAMP_TIME, //!< `hh:mm:ss` | ||
TIMESTAMP_DATE //!< `YYYY-MM-DD` | ||
}; | ||
String timestamp(timestampOpt opt = TIMESTAMP_FULL); | ||
|
||
DateTime operator+(const TimeSpan &span); | ||
DateTime operator-(const TimeSpan &span); | ||
TimeSpan operator-(const DateTime &right); | ||
bool operator<(const DateTime &right) const; | ||
|
||
/*! | ||
@brief Test if one DateTime is greater (later) than another. | ||
@warning if one or both DateTime objects are invalid, returned value is | ||
meaningless | ||
@see use `isValid()` method to check if DateTime object is valid | ||
@param right DateTime object to compare | ||
@return True if the left DateTime is later than the right one, | ||
false otherwise | ||
*/ | ||
bool operator>(const DateTime &right) const { return right < *this; } | ||
|
||
/*! | ||
@brief Test if one DateTime is less (earlier) than or equal to another | ||
@warning if one or both DateTime objects are invalid, returned value is | ||
meaningless | ||
@see use `isValid()` method to check if DateTime object is valid | ||
@param right DateTime object to compare | ||
@return True if the left DateTime is earlier than or equal to the | ||
right one, false otherwise | ||
*/ | ||
bool operator<=(const DateTime &right) const { return !(*this > right); } | ||
|
||
/*! | ||
@brief Test if one DateTime is greater (later) than or equal to another | ||
@warning if one or both DateTime objects are invalid, returned value is | ||
meaningless | ||
@see use `isValid()` method to check if DateTime object is valid | ||
@param right DateTime object to compare | ||
@return True if the left DateTime is later than or equal to the right | ||
one, false otherwise | ||
*/ | ||
bool operator>=(const DateTime &right) const { return !(*this < right); } | ||
bool operator==(const DateTime &right) const; | ||
|
||
/*! | ||
@brief Test if two DateTime objects are not equal. | ||
@warning if one or both DateTime objects are invalid, returned value is | ||
meaningless | ||
@see use `isValid()` method to check if DateTime object is valid | ||
@param right DateTime object to compare | ||
@return True if the two objects are not equal, false if they are | ||
*/ | ||
bool operator!=(const DateTime &right) const { return !(*this == right); } | ||
|
||
protected: | ||
uint8_t yOff; ///< Year offset from 2000 | ||
uint8_t m; ///< Month 1-12 | ||
uint8_t d; ///< Day 1-31 | ||
uint8_t hh; ///< Hours 0-23 | ||
uint8_t mm; ///< Minutes 0-59 | ||
uint8_t ss; ///< Seconds 0-59 | ||
}; | ||
|
||
/**************************************************************************/ | ||
/*! | ||
@brief Timespan which can represent changes in time with seconds accuracy. | ||
*/ | ||
/**************************************************************************/ | ||
class TimeSpan { | ||
public: | ||
TimeSpan(int32_t seconds = 0); | ||
TimeSpan(int16_t days, int8_t hours, int8_t minutes, int8_t seconds); | ||
TimeSpan(const TimeSpan ©); | ||
|
||
/*! | ||
@brief Number of days in the TimeSpan | ||
e.g. 4 | ||
@return int16_t days | ||
*/ | ||
int16_t days() const { return _seconds / 86400L; } | ||
/*! | ||
@brief Number of hours in the TimeSpan | ||
This is not the total hours, it includes the days | ||
e.g. 4 days, 3 hours - NOT 99 hours | ||
@return int8_t hours | ||
*/ | ||
int8_t hours() const { return _seconds / 3600 % 24; } | ||
/*! | ||
@brief Number of minutes in the TimeSpan | ||
This is not the total minutes, it includes days/hours | ||
e.g. 4 days, 3 hours, 27 minutes | ||
@return int8_t minutes | ||
*/ | ||
int8_t minutes() const { return _seconds / 60 % 60; } | ||
/*! | ||
@brief Number of seconds in the TimeSpan | ||
This is not the total seconds, it includes the days/hours/minutes | ||
e.g. 4 days, 3 hours, 27 minutes, 7 seconds | ||
@return int8_t seconds | ||
*/ | ||
int8_t seconds() const { return _seconds % 60; } | ||
/*! | ||
@brief Total number of seconds in the TimeSpan, e.g. 358027 | ||
@return int32_t seconds | ||
*/ | ||
int32_t totalseconds() const { return _seconds; } | ||
|
||
TimeSpan operator+(const TimeSpan &right); | ||
TimeSpan operator-(const TimeSpan &right); | ||
|
||
protected: | ||
int32_t _seconds; ///< Actual TimeSpan value is stored as seconds | ||
}; | ||
|
||
/** DS1307 SQW pin mode settings */ | ||
enum Ds1307SqwPinMode { | ||
DS1307_OFF = 0x00, // Low | ||
|
@@ -271,14 +74,16 @@ enum Ds1307SqwPinMode { | |
@brief RTC based on the DS1307 chip connected via I2C and the Wire library | ||
*/ | ||
/**************************************************************************/ | ||
class RTC_DS1307 { | ||
class RTC_DS1307 : public RTC_Super { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move to individual file? |
||
public: | ||
boolean begin(void); | ||
static void adjust(const DateTime &dt); | ||
uint8_t isrunning(void); | ||
static DateTime now(); | ||
static Ds1307SqwPinMode readSqwPinMode(); | ||
static void writeSqwPinMode(Ds1307SqwPinMode mode); | ||
boolean begin(const DateTime &dt); | ||
void adjust(const DateTime &dt); | ||
bool isrunning(void); | ||
bool lostPower(void); | ||
DateTime now(); | ||
Ds1307SqwPinMode readSqwPinMode(); | ||
void writeSqwPinMode(Ds1307SqwPinMode mode); | ||
uint8_t readnvram(uint8_t address); | ||
void readnvram(uint8_t *buf, uint8_t size, uint8_t address); | ||
void writenvram(uint8_t address, uint8_t data); | ||
|
@@ -323,14 +128,16 @@ enum Ds3231Alarm2Mode { | |
@brief RTC based on the DS3231 chip connected via I2C and the Wire library | ||
*/ | ||
/**************************************************************************/ | ||
class RTC_DS3231 { | ||
class RTC_DS3231 : public RTC_Super { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move to individual file? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps better left to another pull request? I only split the file into multiple parts because having both the parent and child classes in the same file is a bit silly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It pains me to advocate for multiple files. I dislike long files and multiple files. However I am of the belief a class generally belongs in its own file, assuming the class is independent. I think these driver classes will be independent, however unfortunately probably short. So this could complicate code support. However it may clean things up. I would also be interested in trying to clean up the shared logic. However if that complicated the interface or performance I guess it should stay the way it is. |
||
public: | ||
boolean begin(void); | ||
static void adjust(const DateTime &dt); | ||
boolean begin(const DateTime &dt); | ||
void adjust(const DateTime &dt); | ||
bool isrunning(void); | ||
bool lostPower(void); | ||
static DateTime now(); | ||
static Ds3231SqwPinMode readSqwPinMode(); | ||
static void writeSqwPinMode(Ds3231SqwPinMode mode); | ||
DateTime now(); | ||
Ds3231SqwPinMode readSqwPinMode(); | ||
void writeSqwPinMode(Ds3231SqwPinMode mode); | ||
bool setAlarm1(const DateTime &dt, Ds3231Alarm1Mode alarm_mode); | ||
bool setAlarm2(const DateTime &dt, Ds3231Alarm2Mode alarm_mode); | ||
void disableAlarm(uint8_t alarm_num); | ||
|
@@ -388,16 +195,17 @@ enum Pcf8523OffsetMode { | |
@brief RTC based on the PCF8523 chip connected via I2C and the Wire library | ||
*/ | ||
/**************************************************************************/ | ||
class RTC_PCF8523 { | ||
class RTC_PCF8523 : public RTC_Super { | ||
public: | ||
boolean begin(void); | ||
boolean begin(const DateTime &dt); | ||
void adjust(const DateTime &dt); | ||
boolean isrunning(void); | ||
boolean lostPower(void); | ||
boolean initialized(void); | ||
static DateTime now(); | ||
DateTime now(); | ||
void start(void); | ||
void stop(void); | ||
uint8_t isrunning(); | ||
Pcf8523SqwPinMode readSqwPinMode(); | ||
void writeSqwPinMode(Pcf8523SqwPinMode mode); | ||
void enableSecondTimer(void); | ||
|
@@ -425,15 +233,16 @@ enum Pcf8563SqwPinMode { | |
*/ | ||
/**************************************************************************/ | ||
|
||
class RTC_PCF8563 { | ||
class RTC_PCF8563 : public RTC_Super { | ||
public: | ||
boolean begin(void); | ||
boolean begin(const DateTime &dt); | ||
boolean isrunning(void); | ||
boolean lostPower(void); | ||
void adjust(const DateTime &dt); | ||
static DateTime now(); | ||
DateTime now(); | ||
void start(void); | ||
void stop(void); | ||
uint8_t isrunning(); | ||
Pcf8563SqwPinMode readSqwPinMode(); | ||
void writeSqwPinMode(Pcf8563SqwPinMode mode); | ||
}; | ||
|
@@ -444,17 +253,28 @@ class RTC_PCF8563 { | |
use. NOTE: this is immune to millis() rollover events. | ||
*/ | ||
/**************************************************************************/ | ||
class RTC_Millis { | ||
class RTC_Millis : public RTC_Super { | ||
public: | ||
boolean begin(void); | ||
boolean begin(const DateTime &dt); | ||
/*! | ||
@brief Start the RTC | ||
@param dt DateTime object with the date/time to set | ||
@brief Simulate if the RTC is running | ||
@return true | ||
*/ | ||
static void begin(const DateTime &dt) { adjust(dt); } | ||
static void adjust(const DateTime &dt); | ||
static DateTime now(); | ||
boolean isrunning(void) { return true; } | ||
/*! | ||
@brief Simulate if the RTC has lost power | ||
@return false | ||
*/ | ||
boolean lostPower(void) { return false; } | ||
void adjust(const DateTime &dt); | ||
void adjustDrift(const int ppm); | ||
DateTime now(); | ||
|
||
protected: | ||
static uint16_t | ||
millisPerSecond; ///< Number of milliseconds reported by | ||
///< millis() per "true" (calibrated) kilosecond | ||
static uint32_t lastUnix; ///< Unix time from the previous call to now() - | ||
///< prevents rollover issues | ||
static uint32_t lastMillis; ///< the millis() value corresponding to the last | ||
|
@@ -470,16 +290,23 @@ class RTC_Millis { | |
approximately 71.6 minutes. | ||
*/ | ||
/**************************************************************************/ | ||
class RTC_Micros { | ||
class RTC_Micros : public RTC_Super { | ||
public: | ||
boolean begin(void); | ||
boolean begin(const DateTime &dt); | ||
/*! | ||
@brief Simulate if the RTC is running | ||
@return true | ||
*/ | ||
boolean isrunning(void) { return true; } | ||
/*! | ||
@brief Start the RTC | ||
@param dt DateTime object with the date/time to set | ||
@brief Simulate if the RTC has lost power | ||
@return false | ||
*/ | ||
static void begin(const DateTime &dt) { adjust(dt); } | ||
static void adjust(const DateTime &dt); | ||
static void adjustDrift(int ppm); | ||
static DateTime now(); | ||
boolean lostPower(void) { return false; } | ||
void adjust(const DateTime &dt); | ||
void adjustDrift(const int ppm); | ||
DateTime now(); | ||
|
||
protected: | ||
static uint32_t microsPerSecond; ///< Number of microseconds reported by | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is base class under subfolder but not derived classes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For compatibility with versions of the Arduino IDE before 1.5.x, only the source code files in the root and utility folders (not including source code within a folder in the utility folder) are complied. Please see the Arduino library specifications.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough, I did not know that. However I think this file should be RTC.h in root and the other in utilities.