ArduinoLibs
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
RTC.h
1 /*
2  * Copyright (C) 2012 Southern Storm Software, Pty Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef RTC_h
24 #define RTC_h
25 
26 #include <inttypes.h>
27 
28 struct RTCTime
29 {
30  uint8_t hour;
31  uint8_t minute;
32  uint8_t second;
33 };
34 
35 struct RTCDate
36 {
37  unsigned int year;
38  uint8_t month;
39  uint8_t day;
40 };
41 
42 struct RTCAlarm
43 {
44  uint8_t day;
45  uint8_t dow;
46  uint8_t hour;
47  uint8_t minute;
48  uint8_t second;
49  uint8_t flags;
50 };
51 
52 class RTC
53 {
54 public:
55  RTC();
56  ~RTC();
57 
58  enum DayOfWeek
59  {
60  Monday = 1,
61  Tuesday,
62  Wednesday,
63  Thursday,
64  Friday,
65  Saturday,
66  Sunday,
67  };
68 
69  virtual bool hasUpdates();
70 
71  virtual void readTime(RTCTime *value);
72  virtual void readDate(RTCDate *value);
73 
74  virtual void writeTime(const RTCTime *value);
75  virtual void writeDate(const RTCDate *value);
76 
77  static const uint8_t ALARM_COUNT = 4;
78 
79  virtual void readAlarm(uint8_t alarmNum, RTCAlarm *value);
80  virtual void writeAlarm(uint8_t alarmNum, const RTCAlarm *value);
81 
82  virtual int byteCount() const;
83  virtual uint8_t readByte(uint8_t offset);
84  virtual void writeByte(uint8_t offset, uint8_t value);
85 
86  static const int NO_TEMPERATURE = 32767;
87 
88  virtual int readTemperature();
89 
90  // Flags for adjustDays(), adjustMonths(), and adjustYears().
91  static const uint8_t INCREMENT = 0x0000;
92  static const uint8_t DECREMENT = 0x0001;
93  static const uint8_t WRAP = 0x0002;
94 
95  static void adjustDays(RTCDate *date, uint8_t flags);
96  static void adjustMonths(RTCDate *date, uint8_t flags);
97  static void adjustYears(RTCDate *date, uint8_t flags);
98 
99  static DayOfWeek dayOfWeek(const RTCDate *date);
100 
101 private:
102  unsigned long midnight;
103  RTCDate date;
104  RTCAlarm alarms[ALARM_COUNT];
105  uint8_t *nvram;
106 };
107 
108 #endif
uint8_t month
Month of the year (1-12)
Definition: RTC.h:38
virtual void writeTime(const RTCTime *value)
Updates the time in the realtime clock to match value.
Definition: RTC.cpp:179
RTC()
Constructs a new realtime clock handler.
Definition: RTC.cpp:105
static DayOfWeek dayOfWeek(const RTCDate *date)
Returns the day of the week corresponding to date.
Definition: RTC.cpp:399
uint8_t minute
Minute within the hour (0-59)
Definition: RTC.h:31
virtual void readAlarm(uint8_t alarmNum, RTCAlarm *value)
Reads the details of the alarm with index alarmNum into value.
Definition: RTC.cpp:209
virtual void readDate(RTCDate *value)
Reads the current date from the realtime clock into value.
Definition: RTC.cpp:169
uint8_t day
Day of the month for the alarm if not zero.
Definition: RTC.h:44
static const uint8_t DECREMENT
Decrement the day, month, or year in a call to adjustDays(), adjustMonths(), or adjustYears().
Definition: RTC.h:92
DayOfWeek
Day of the week corresponding to a date.
Definition: RTC.h:58
virtual void writeAlarm(uint8_t alarmNum, const RTCAlarm *value)
Updates the details of the alarm with index alarmNum from value.
Definition: RTC.cpp:224
static const uint8_t ALARM_COUNT
Number of alarms that are supported by RTC::readAlarm() and RTC::writeAlarm().
Definition: RTC.h:77
virtual void writeDate(const RTCDate *value)
Updates the date in the realtime clock to match value.
Definition: RTC.cpp:194
virtual int byteCount() const
Returns the number of bytes of non-volatile memory that can be used for storage of arbitrary settings...
Definition: RTC.cpp:235
uint8_t hour
Hour of the day for the alarm (0-23).
Definition: RTC.h:46
static const int NO_TEMPERATURE
Value that is returned from readTemperature() if the realtime clock chip cannot determine the tempera...
Definition: RTC.h:86
uint8_t flags
Additional flags for the alarm.
Definition: RTC.h:49
uint8_t dow
Day of the week for the alarm if not zero.
Definition: RTC.h:45
Stores date information from a realtime clock chip.
Definition: RTC.h:35
unsigned int year
Year (4-digit)
Definition: RTC.h:37
virtual int readTemperature()
Reads the value of the temperature sensor and returns the temperature in quarters of a degree celcius...
Definition: RTC.cpp:288
virtual void writeByte(uint8_t offset, uint8_t value)
Writes value to offset within the realtime clock's non-volatile memory.
Definition: RTC.cpp:262
static void adjustYears(RTCDate *date, uint8_t flags)
Adjusts date up or down one year according to flags.
Definition: RTC.cpp:370
uint8_t minute
Minute of the hour for the alarm (0-59).
Definition: RTC.h:47
static const uint8_t INCREMENT
Increment the day, month, or year in a call to adjustDays(), adjustMonths(), or adjustYears().
Definition: RTC.h:91
virtual uint8_t readByte(uint8_t offset)
Reads the byte at offset within the realtime clock's non-volatile memory.
Definition: RTC.cpp:247
static void adjustDays(RTCDate *date, uint8_t flags)
Adjusts date up or down one day according to flags.
Definition: RTC.cpp:313
uint8_t second
Second of the minute for the alarm (0-59).
Definition: RTC.h:48
Stores time information from a realtime clock chip.
Definition: RTC.h:28
Stores alarm information from a realtime clock chip.
Definition: RTC.h:42
static void adjustMonths(RTCDate *date, uint8_t flags)
Adjusts date up or down one month according to flags.
Definition: RTC.cpp:343
uint8_t hour
Hour of the day (0-23)
Definition: RTC.h:30
uint8_t day
Day of the month (1-31)
Definition: RTC.h:39
uint8_t second
Second within the minute (0-59)
Definition: RTC.h:32
virtual bool hasUpdates()
Returns true if the realtime clock has updated since the last call to this function.
Definition: RTC.cpp:134
virtual void readTime(RTCTime *value)
Reads the current time from the realtime clock into value.
Definition: RTC.cpp:144
Base class for realtime clock handlers.
Definition: RTC.h:52
static const uint8_t WRAP
Wrap around to the beginning of the current month/year rather than advance to the next one...
Definition: RTC.h:93