M5Unit-RTC 0.1.0 git rev:26c1349
Loading...
Searching...
No Matches
unit_PCF8563_types.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
10#ifndef M5_UNIT_RTC_UNIT_PCF8563_TYPES_HPP
11#define M5_UNIT_RTC_UNIT_PCF8563_TYPES_HPP
12
13#include <cstdint>
14#include <tuple>
15#include <time.h>
16
17namespace m5 {
18namespace unit {
19namespace pcf8563 {
20
25struct __attribute__((packed)) rtc_time_t {
26 int8_t hours{};
27 int8_t minutes{};
28 int8_t seconds{};
29
30 rtc_time_t(int8_t hours_ = -1, int8_t minutes_ = -1, int8_t seconds_ = -1)
31 : hours{hours_}, minutes{minutes_}, seconds{seconds_}
32 {
33 }
34 rtc_time_t(const tm& t) : hours{(int8_t)t.tm_hour}, minutes{(int8_t)t.tm_min}, seconds{(int8_t)t.tm_sec}
35 {
36 }
38 static inline rtc_time_t from_tm(const tm& t)
39 {
40 return rtc_time_t(t);
41 }
43 inline tm to_tm() const
44 {
45 tm t{};
46 t.tm_hour = hours;
47 t.tm_min = minutes;
48 t.tm_sec = seconds;
49 return t;
50 }
51};
52
55inline bool operator==(const rtc_time_t& lhs, const rtc_time_t& rhs)
56{
57 return lhs.hours == rhs.hours && lhs.minutes == rhs.minutes && lhs.seconds == rhs.seconds;
58}
59inline bool operator!=(const rtc_time_t& lhs, const rtc_time_t& rhs)
60{
61 return !(lhs == rhs);
62}
63inline bool operator<(const rtc_time_t& lhs, const rtc_time_t& rhs)
64{
65 return std::tie(lhs.hours, lhs.minutes, lhs.seconds) < std::tie(rhs.hours, rhs.minutes, rhs.seconds);
66}
67inline bool operator<=(const rtc_time_t& lhs, const rtc_time_t& rhs)
68{
69 return !(rhs < lhs);
70}
71inline bool operator>(const rtc_time_t& lhs, const rtc_time_t& rhs)
72{
73 return rhs < lhs;
74}
75inline bool operator>=(const rtc_time_t& lhs, const rtc_time_t& rhs)
76{
77 return !(lhs < rhs);
78}
80
85struct __attribute__((packed)) rtc_date_t {
87 int16_t year{};
89 int8_t month{};
91 int8_t date{};
93 int8_t weekDay{};
94
95 rtc_date_t(int16_t year_ = 2000, int8_t month_ = 1, int8_t date_ = -1, int8_t weekDay_ = -1)
96 : year{year_}, month{month_}, date{date_}, weekDay{weekDay_}
97 {
98 }
99 rtc_date_t(const tm& t)
100 : year{(int16_t)(t.tm_year + 1900)},
101 month{(int8_t)(t.tm_mon + 1)},
102 date{(int8_t)t.tm_mday},
103 weekDay{(int8_t)t.tm_wday}
104 {
105 }
107 static inline rtc_date_t from_tm(const tm& t)
108 {
109 return rtc_date_t(t);
110 }
112 inline tm to_tm() const
113 {
114 tm t{};
115 t.tm_year = year - 1900;
116 t.tm_mon = month - 1;
117 t.tm_mday = date;
118 t.tm_wday = weekDay;
119 return t;
120 }
121};
122
125inline bool operator==(const rtc_date_t& lhs, const rtc_date_t& rhs)
126{
127 return lhs.year == rhs.year && lhs.month == rhs.month && lhs.date == rhs.date;
128}
129inline bool operator!=(const rtc_date_t& lhs, const rtc_date_t& rhs)
130{
131 return !(lhs == rhs);
132}
133inline bool operator<(const rtc_date_t& lhs, const rtc_date_t& rhs)
134{
135 return std::tie(lhs.year, lhs.month, lhs.date) < std::tie(rhs.year, rhs.month, rhs.date);
136}
137inline bool operator<=(const rtc_date_t& lhs, const rtc_date_t& rhs)
138{
139 return !(rhs < lhs);
140}
141inline bool operator>(const rtc_date_t& lhs, const rtc_date_t& rhs)
142{
143 return rhs < lhs;
144}
145inline bool operator>=(const rtc_date_t& lhs, const rtc_date_t& rhs)
146{
147 return !(lhs < rhs);
148}
150
155struct __attribute__((packed)) rtc_datetime_t {
156 rtc_date_t date{};
157 rtc_time_t time{};
158
159 rtc_datetime_t() = default;
160 rtc_datetime_t(const rtc_date_t& d, const rtc_time_t& t) : date{d}, time{t}
161 {
162 }
163 rtc_datetime_t(const tm& t) : date{t}, time{t}
164 {
165 }
167 inline rtc_datetime_t& operator=(const tm& t)
168 {
169 date = rtc_date_t(t);
170 time = rtc_time_t(t);
171 return *this;
172 }
174 explicit inline operator tm() const
175 {
176 return to_tm();
177 }
179 static inline rtc_datetime_t from_tm(const tm& t)
180 {
181 return rtc_datetime_t(t);
182 }
184 inline tm to_tm() const
185 {
186 tm t{};
187 t.tm_year = date.year - 1900;
188 t.tm_mon = date.month - 1;
189 t.tm_mday = date.date;
190 t.tm_wday = date.weekDay;
191 t.tm_hour = time.hours;
192 t.tm_min = time.minutes;
193 t.tm_sec = time.seconds;
194 t.tm_isdst = -1;
195 return t;
196 }
197};
198
201inline bool operator==(const rtc_datetime_t& lhs, const rtc_datetime_t& rhs)
202{
203 return lhs.date == rhs.date && lhs.time == rhs.time;
204}
205inline bool operator!=(const rtc_datetime_t& lhs, const rtc_datetime_t& rhs)
206{
207 return !(lhs == rhs);
208}
209inline bool operator<(const rtc_datetime_t& lhs, const rtc_datetime_t& rhs)
210{
211 if (lhs.date != rhs.date) {
212 return lhs.date < rhs.date;
213 }
214 return lhs.time < rhs.time;
215}
216inline bool operator<=(const rtc_datetime_t& lhs, const rtc_datetime_t& rhs)
217{
218 return !(rhs < lhs);
219}
220inline bool operator>(const rtc_datetime_t& lhs, const rtc_datetime_t& rhs)
221{
222 return rhs < lhs;
223}
224inline bool operator>=(const rtc_datetime_t& lhs, const rtc_datetime_t& rhs)
225{
226 return !(lhs < rhs);
227}
229
234enum class TimerClock : uint8_t {
235 Hz4096 = 0,
236 Hz64 = 1,
237 Hz1 = 2,
238 HzPM = 3,
239};
240
245enum class ClockOutput : uint8_t {
246 None = 0x00,
247 Hz32768 = 0x80,
248 Hz1024 = 0x81,
249 Hz32 = 0x82,
250 Hz1 = 0x83,
251};
252
253} // namespace pcf8563
254} // namespace unit
255} // namespace m5
256#endif
Calendar date (year, month, day, weekday)
Definition unit_PCF8563_types.hpp:85
tm to_tm() const
Convert to struct tm (only date fields are set)
Definition unit_PCF8563_types.hpp:112
static rtc_date_t from_tm(const tm &t)
Construct from struct tm (date fields only)
Definition unit_PCF8563_types.hpp:107
Combined date and time.
Definition unit_PCF8563_types.hpp:155
tm to_tm() const
Convert to struct tm.
Definition unit_PCF8563_types.hpp:184
static rtc_datetime_t from_tm(const tm &t)
Construct from struct tm.
Definition unit_PCF8563_types.hpp:179
rtc_datetime_t & operator=(const tm &t)
Assign from struct tm.
Definition unit_PCF8563_types.hpp:167
Time of day (hours, minutes, seconds)
Definition unit_PCF8563_types.hpp:25
tm to_tm() const
Convert to struct tm (only time fields are set)
Definition unit_PCF8563_types.hpp:43
static rtc_time_t from_tm(const tm &t)
Construct from struct tm (time fields only)
Definition unit_PCF8563_types.hpp:38
ClockOutput
CLKOUT pin output frequency (register 0x0D)
Definition unit_PCF8563_types.hpp:245
@ Hz32768
32.768 kHz (FE=1, FD=00)
@ None
CLKOUT disabled (high-impedance)
@ Hz32
32 Hz (FE=1, FD=10)
@ Hz1024
1.024 kHz (FE=1, FD=01)
TimerClock
Timer clock source for PCF8563 countdown timer.
Definition unit_PCF8563_types.hpp:234
@ Hz64
64 Hz (~15.6ms resolution)
@ HzPM
1/60 Hz (1 minute resolution)
@ Hz1
1 Hz (1 second resolution)
@ Hz4096
4.096 kHz (~244us resolution)