Skip to content
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

Open
wants to merge 47 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
cb0bf8d
Wrote `isLeapYear()` function
Jul 30, 2020
0af4be5
Wrote `fixDateTime()` method to fix invalid DateTime objects
Jul 30, 2020
6f49b2b
Write additional comment specifying that if DateTime is already valid,
Jul 30, 2020
bca61c2
Merge pull request #1 from Harrison-O/DateTime_eqaulity
Harrison-O Jul 30, 2020
ba9c0ed
Merge pull request #2 from Harrison-O/fixDateTime
Harrison-O Jul 30, 2020
ae82978
Merge pull request #3 from adafruit/master
Harrison-O Dec 28, 2020
dded178
Merge pull request #4 from Harrison-O/master
Harrison-O Dec 28, 2020
7654480
Wrote RTC superclass
Dec 28, 2020
e3757d3
Modified RTClib.h to use RTC.h
Dec 28, 2020
9dfa49b
Fixed IDE errors
Dec 28, 2020
00075a4
Fixed compile errors
Dec 28, 2020
eaec406
Moved _I2C_WRITE and _I2C_READ to RTClib.cpp
Dec 29, 2020
68bed44
Wrote new keywords
Dec 29, 2020
54fcd63
Fixed usage of millisPerSecond and RTC_Millis::adjustDrift()
Dec 29, 2020
2b98009
Updated doxygen comments for adjustDrift()
Dec 29, 2020
4a25373
Wrote second `begin()` function for RTC classes
Dec 29, 2020
c1001bc
Wrote adjustDraft() functions for RTC classes
Dec 29, 2020
f00f0af
Removed adjustDrift() for future pull request
Dec 29, 2020
58e5432
Made RTC superclass public for subclasses
Dec 29, 2020
1614120
Fixed compile error with begin(const DateTime&)
Dec 29, 2020
de9a42e
Removed fixDateTime() function
Dec 29, 2020
189ddc9
Fixed file placement in utility folder
Dec 29, 2020
0975cc5
Ensured that TimeSpan.h file includes Arduino.h
Dec 29, 2020
3ab7669
Removed COMPILE_DT constant due to compile error
Dec 29, 2020
3721491
Removed adjustDrift() from RTC for future pull request
Dec 29, 2020
290bea2
Fixed build errors
Dec 29, 2020
41b522d
Fixed adjustDrift() for RTC_Millis
Dec 30, 2020
2f99da8
Fixed micro to milli typo
Dec 31, 2020
edc0af4
Fixed clang formatting issues
Dec 31, 2020
fd67d34
Wrote adjustDrift() back in to enforce consistency
Dec 31, 2020
80b5650
:w
Dec 31, 2020
7213586
Fixed clang formatting issues
Dec 31, 2020
5eba8f6
Removed millisPerSecond from RTC_Millis
Dec 31, 2020
db255bf
Fixed compile error involving millisPerSecond
Jan 1, 2021
e2a981c
Renamed RTC_Super to RTC to find RTC macro
Jan 1, 2021
8f7e656
Changed begin(DateTime&) to non-pure virtual
Jan 2, 2021
c13d1c4
Renamed RTC_Super to RTC_Base
Jan 2, 2021
56f3426
Changed begin(void) method for pseudo RTCs
Jan 2, 2021
678f814
Updated doxygen comment
Jan 2, 2021
2e664b4
Renamed RTC_Base to RealTimeClock
Jan 3, 2021
3151cf7
Wrote note for RealTimeClock class
Jan 3, 2021
b8d8b77
Changed ppm to non-const
Jan 3, 2021
266cc68
Made lostPower() virtual in base class
Jan 4, 2021
84fbf61
Commented out adjustDrift() for future versions
Jan 4, 2021
ad85753
Fixed clang formatting issue
Jan 5, 2021
5cee6ba
Fixed clang formatting issue
Jan 5, 2021
a5a037d
Fixed clang formatting issue
Jan 7, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
863 changes: 158 additions & 705 deletions RTClib.cpp

Large diffs are not rendered by default.

286 changes: 56 additions & 230 deletions RTClib.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
#define _RTCLIB_H_

#include <Arduino.h>
class TimeSpan;
#include "utility/RTC.h"
#include "utility/DateTime.h"
#include "utility/TimeSpan.h"

/** Registers */
#define PCF8523_ADDRESS 0x68 ///< I2C address for PCF8523
Expand Down Expand Up @@ -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 &copy);
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 &copy);

/*!
@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
Expand All @@ -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 {
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);
Expand Down Expand Up @@ -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 {
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);
Expand Down Expand Up @@ -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 {
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);
Expand Down Expand Up @@ -425,15 +233,16 @@ enum Pcf8563SqwPinMode {
*/
/**************************************************************************/

class RTC_PCF8563 {
class RTC_PCF8563 : public RTC {
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);
};
Expand All @@ -444,17 +253,27 @@ class RTC_PCF8563 {
use. NOTE: this is immune to millis() rollover events.
*/
/**************************************************************************/
class RTC_Millis {
class RTC_Millis : public RTC {
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 uint32_t microsPerSecond; ///< Number of milliseconds reported by
///< millis() per "true" (calibrated) second
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
Expand All @@ -470,16 +289,23 @@ class RTC_Millis {
approximately 71.6 minutes.
*/
/**************************************************************************/
class RTC_Micros {
class RTC_Micros : public RTC {
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
Expand Down
2 changes: 2 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

DateTime KEYWORD1
TimeSpan KEYWORD1
RTC KEYWORD1
RTC_DS1307 KEYWORD1
RTC_DS3231 KEYWORD1
RTC_PCF8523 KEYWORD1
Expand Down Expand Up @@ -77,6 +78,7 @@ isEnabled32K KEYWORD2
#######################################
# Constants (LITERAL1)
#######################################
COMPILE_DT LITERAL1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not used anymore.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

TIMESTAMP_FULL LITERAL1
TIMESTAMP_DATE LITERAL1
TIMESTAMP_TIME LITERAL1
Expand Down
Loading