M5Unified
RTC8563_Class.hpp
Go to the documentation of this file.
1 // Copyright (c) M5Stack. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 
4 #ifndef __M5_RTC8563_CLASS_H__
5 #define __M5_RTC8563_CLASS_H__
6 
7 #include "m5unified_common.h"
8 
9 #include "I2C_Class.hpp"
10 
11 #if __has_include(<sys/time.h>)
12 #include <sys/time.h>
13 #else
14 typedef void timezone;
15 #endif
16 #include <time.h>
17 
18 namespace m5
19 {
20  struct __attribute__((packed)) rtc_time_t
21  {
22  std::int8_t hours;
23  std::int8_t minutes;
24  std::int8_t seconds;
25 
26  rtc_time_t(std::int8_t hours_ = -1, std::int8_t minutes_ = -1, std::int8_t seconds_ = -1)
27  : hours { hours_ }
28  , minutes { minutes_ }
29  , seconds { seconds_ }
30  {}
31 
32  rtc_time_t(const tm& t)
33  : hours { (int8_t)t.tm_hour }
34  , minutes { (int8_t)t.tm_min }
35  , seconds { (int8_t)t.tm_sec }
36  {}
37  };
38 
39  struct __attribute__((packed)) rtc_date_t
40  {
42  std::int16_t year;
43 
45  std::int8_t month;
46 
48  std::int8_t date;
49 
51  std::int8_t weekDay;
52 
53  rtc_date_t(std::int16_t year_ = 2000, std::int8_t month_ = 1, std::int8_t date_ = -1, std::int8_t weekDay_ = -1)
54  : year { year_ }
55  , month { month_ }
56  , date { date_ }
57  , weekDay { weekDay_ }
58  {}
59 
60  rtc_date_t(const tm& t)
61  : year { (int16_t)(t.tm_year + 1900) }
62  , month { (int8_t )(t.tm_mon + 1 ) }
63  , date { (int8_t ) t.tm_mday }
64  , weekDay { (int8_t ) t.tm_wday }
65  {}
66  };
67 
68  struct __attribute__((packed)) rtc_datetime_t
69  {
70  rtc_date_t date;
71  rtc_time_t time;
72  rtc_datetime_t() = default;
73  rtc_datetime_t(const rtc_date_t& d, const rtc_time_t& t) : date { d }, time { t } {};
74  rtc_datetime_t(const tm& t) : date { t }, time { t } {}
75  tm get_tm(void) const;
76  void set_tm(tm& time);
77  void set_tm(tm* t) { if (t) set_tm(*t); }
78  };
79 
80  class RTC8563_Class : public I2C_Device
81  {
82  public:
83  static constexpr std::uint8_t DEFAULT_ADDRESS = 0x51;
84 
85  RTC8563_Class(std::uint8_t i2c_addr = DEFAULT_ADDRESS, std::uint32_t freq = 400000, I2C_Class* i2c = &In_I2C)
86  : I2C_Device ( i2c_addr, freq, i2c )
87  {}
88 
89  bool begin(I2C_Class* i2c = nullptr);
90 
91  bool getVoltLow(void);
92 
93  bool getTime(rtc_time_t* time) const;
94  bool getDate(rtc_date_t* date) const;
95  bool getDateTime(rtc_datetime_t* datetime) const;
96 
97  void setTime(const rtc_time_t &time);
98  void setTime(const rtc_time_t* const time) { if (time) { setTime(*time); } }
99 
100  void setDate(const rtc_date_t &date);
101  void setDate(const rtc_date_t* const date) { if (date) { setDate(*date); } }
102 
103  void setDateTime(const rtc_datetime_t &datetime) { setDate(datetime.date); setTime(datetime.time); }
104  void setDateTime(const rtc_datetime_t* const datetime) { if (datetime) { setDateTime(*datetime); } }
105  void setDateTime(const tm* const datetime)
106  {
107  if (datetime)
108  {
109  rtc_datetime_t dt { *datetime };
110  setDateTime(dt);
111  }
112  }
113 
117  int setAlarmIRQ(int afterSeconds);
118 
120  int setAlarmIRQ(const rtc_time_t &time);
121  int setAlarmIRQ(const rtc_date_t &date, const rtc_time_t &time);
122 
123  void setSystemTimeFromRtc(struct timezone* tz = nullptr);
124 
125  bool getIRQstatus(void);
126  void clearIRQ(void);
127  void disableIRQ(void);
128 
129  rtc_time_t getTime(void) const
130  {
131  rtc_time_t time;
132  getTime(&time);
133  return time;
134  }
135 
136  rtc_date_t getDate(void) const
137  {
138  rtc_date_t date;
139  getDate(&date);
140  return date;
141  }
142 
143  rtc_datetime_t getDateTime(void) const
144  {
145  rtc_datetime_t res;
146  getDateTime(&res);
147  return res;
148  }
149 
150  };
151 }
152 
153 #endif
void timezone
bool getIRQstatus(void)
bool begin(I2C_Class *i2c=nullptr)
void setDateTime(const tm *const datetime)
void setDateTime(const rtc_datetime_t &datetime)
RTC8563_Class(std::uint8_t i2c_addr=DEFAULT_ADDRESS, std::uint32_t freq=400000, I2C_Class *i2c=&In_I2C)
void setDate(const rtc_date_t *const date)
void setSystemTimeFromRtc(struct timezone *tz=nullptr)
static constexpr std::uint8_t DEFAULT_ADDRESS
rtc_date_t getDate(void) const
void setDateTime(const rtc_datetime_t *const datetime)
rtc_time_t getTime(void) const
bool getVoltLow(void)
rtc_datetime_t getDateTime(void) const
void setTime(const rtc_time_t *const time)
void setDate(const rtc_date_t &date)
void setTime(const rtc_time_t &time)
int setAlarmIRQ(int afterSeconds)
Definition: M5Unified.cpp:48
struct __attribute__((packed)) rtc_time_t
I2C_Class In_I2C
for internal I2C device
Definition: I2C_Class.cpp:10