24 #if defined(ARDUINO) && ARDUINO >= 100
58 #define DEFAULT_BYTE_COUNT 43 // Default simulates DS1307 NVRAM size.
60 #define MILLIS_PER_DAY 86400000UL
61 #define MILLIS_PER_SECOND 1000UL
62 #define MILLIS_PER_MINUTE 60000UL
63 #define MILLIS_PER_HOUR 3600000UL
65 static uint8_t monthLengths[] = {
66 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
69 static unsigned int monthOffsets[] = {
75 31 + 28 + 31 + 30 + 31,
76 31 + 28 + 31 + 30 + 31 + 30,
77 31 + 28 + 31 + 30 + 31 + 30 + 31,
78 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
79 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
80 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
81 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
84 inline bool isLeapYear(
unsigned int year)
86 if ((year % 100) == 0)
87 return (year % 400) == 0;
89 return (year % 4) == 0;
92 inline uint8_t monthLength(
const RTCDate *date)
94 if (date->
month != 2 || !isLeapYear(date->
year))
95 return monthLengths[date->
month - 1];
106 : midnight(millis() - 9 * MILLIS_PER_HOUR)
115 for (uint8_t index = 0; index <
ALARM_COUNT; ++index) {
116 alarms[index].
hour = 6;
118 alarms[index].
flags = 0;
147 unsigned long sinceMidnight = millis() - midnight;
148 if (sinceMidnight >= MILLIS_PER_DAY) {
150 midnight += MILLIS_PER_DAY;
151 sinceMidnight -= MILLIS_PER_DAY;
156 value->
second = (uint8_t)(((sinceMidnight / MILLIS_PER_SECOND) % 60));
157 value->
minute = (uint8_t)(((sinceMidnight / MILLIS_PER_MINUTE) % 60));
158 value->
hour = (uint8_t)(sinceMidnight / MILLIS_PER_HOUR);
182 unsigned long sinceMidnight =
183 value->
second * MILLIS_PER_SECOND +
184 value->
minute * MILLIS_PER_MINUTE +
185 value->
hour * MILLIS_PER_HOUR;
186 midnight = millis() - sinceMidnight;
211 *value = alarms[alarmNum];
226 alarms[alarmNum] = *value;
237 return DEFAULT_BYTE_COUNT;
250 return nvram[offset];
265 nvram[offset] = value;
267 nvram = (uint8_t *)malloc(DEFAULT_BYTE_COUNT);
269 memset(nvram, 0, DEFAULT_BYTE_COUNT);
270 nvram[offset] = value;
317 if (date->
day == 0) {
318 if (!(flags &
WRAP)) {
320 if (date->
month == 0)
323 date->
day = monthLength(date);
327 if (date->
day > monthLength(date)) {
328 if (!(flags &
WRAP)) {
330 if (date->
month == 13)
347 if (date->
month == 0) {
349 if (!(flags &
WRAP) && date->
year > 2000)
354 if (date->
month == 13) {
356 if (!(flags &
WRAP) && date->
year < 2099)
360 uint8_t len = monthLength(date);
374 if (date->
year < 2000)
378 if (date->
year > 2099)
381 uint8_t len = monthLength(date);
402 unsigned long daynum = date->
day + 4;
403 daynum += monthOffsets[date->
month - 1];
404 if (date->
month > 2 && isLeapYear(date->
year))
406 daynum += 365UL * (date->
year - 2000);
407 if (date->
year > 2000)
408 daynum += ((date->
year - 2001) / 4) + 1;
uint8_t month
Month of the year (1-12)
virtual void writeTime(const RTCTime *value)
Updates the time in the realtime clock to match value.
RTC()
Constructs a new realtime clock handler.
static DayOfWeek dayOfWeek(const RTCDate *date)
Returns the day of the week corresponding to date.
uint8_t minute
Minute within the hour (0-59)
virtual void readAlarm(uint8_t alarmNum, RTCAlarm *value)
Reads the details of the alarm with index alarmNum into value.
virtual void readDate(RTCDate *value)
Reads the current date from the realtime clock into value.
static const uint8_t DECREMENT
Decrement the day, month, or year in a call to adjustDays(), adjustMonths(), or adjustYears().
DayOfWeek
Day of the week corresponding to a date.
virtual void writeAlarm(uint8_t alarmNum, const RTCAlarm *value)
Updates the details of the alarm with index alarmNum from value.
static const uint8_t ALARM_COUNT
Number of alarms that are supported by RTC::readAlarm() and RTC::writeAlarm().
virtual void writeDate(const RTCDate *value)
Updates the date in the realtime clock to match value.
virtual int byteCount() const
Returns the number of bytes of non-volatile memory that can be used for storage of arbitrary settings...
uint8_t hour
Hour of the day for the alarm (0-23).
static const int NO_TEMPERATURE
Value that is returned from readTemperature() if the realtime clock chip cannot determine the tempera...
uint8_t flags
Additional flags for the alarm.
Stores date information from a realtime clock chip.
unsigned int year
Year (4-digit)
virtual int readTemperature()
Reads the value of the temperature sensor and returns the temperature in quarters of a degree celcius...
virtual void writeByte(uint8_t offset, uint8_t value)
Writes value to offset within the realtime clock's non-volatile memory.
static void adjustYears(RTCDate *date, uint8_t flags)
Adjusts date up or down one year according to flags.
uint8_t minute
Minute of the hour for the alarm (0-59).
static const uint8_t INCREMENT
Increment the day, month, or year in a call to adjustDays(), adjustMonths(), or adjustYears().
virtual uint8_t readByte(uint8_t offset)
Reads the byte at offset within the realtime clock's non-volatile memory.
static void adjustDays(RTCDate *date, uint8_t flags)
Adjusts date up or down one day according to flags.
Stores time information from a realtime clock chip.
Stores alarm information from a realtime clock chip.
static void adjustMonths(RTCDate *date, uint8_t flags)
Adjusts date up or down one month according to flags.
uint8_t hour
Hour of the day (0-23)
uint8_t day
Day of the month (1-31)
uint8_t second
Second within the minute (0-59)
virtual bool hasUpdates()
Returns true if the realtime clock has updated since the last call to this function.
virtual void readTime(RTCTime *value)
Reads the current time from the realtime clock into value.
static const uint8_t WRAP
Wrap around to the beginning of the current month/year rather than advance to the next one...