Skip to content

Commit 36fbba5

Browse files
committed
Add support for PCF8563T RTC
1 parent cb6fc99 commit 36fbba5

File tree

7 files changed

+900
-1
lines changed

7 files changed

+900
-1
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#pragma once
2+
3+
#include <Arduino.h>
4+
#include <Arduino_EdgeControl.h>
5+
#include <mbed_mktime.h>
6+
7+
// Convert build time to UNIX time
8+
time_t getBuildDateTime(bool local_time = true, int tz = 0)
9+
{
10+
char s_month[5];
11+
int year;
12+
13+
tm t;
14+
time_t seconds;
15+
16+
static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
17+
18+
sscanf(__DATE__, "%s %d %d", s_month, &t.tm_mday, &year);
19+
sscanf(__TIME__, "%2d %*c %2d %*c %2d", &t.tm_hour, &t.tm_min, &t.tm_sec);
20+
21+
// Find where is s_month in month_names. Deduce month value.
22+
t.tm_mon = (strstr(month_names, s_month) - month_names) / 3 + 1;
23+
t.tm_year = year - 1900;
24+
_rtc_maketime(&t, &seconds, RTC_FULL_LEAP_YEAR_SUPPORT);
25+
26+
if (!local_time) {
27+
if (tz > 200) {
28+
tz = 0x100 - tz; // Handle negative values
29+
seconds += (3600UL) * tz;
30+
} else {
31+
seconds -= (3600UL) * tz;
32+
}
33+
}
34+
35+
return seconds;
36+
}
37+
38+
String getRTCDate()
39+
{
40+
// APIs to get date fields.
41+
auto years = RealTimeClock.getYears();
42+
auto months = RealTimeClock.getMonths();
43+
auto days = RealTimeClock.getDays();
44+
45+
char buf[12] {};
46+
47+
snprintf(buf, 11, "20%02d-%02d-%02d", years, months, days);
48+
49+
return String(buf);
50+
}
51+
52+
String getRTCTime()
53+
{
54+
// APIs to get time fields.
55+
auto hours = RealTimeClock.getHours();
56+
auto minutes = RealTimeClock.getMinutes();
57+
auto seconds = RealTimeClock.getSeconds();
58+
59+
char buf[11] {};
60+
61+
snprintf(buf, 10, "%02d:%02d:%02d", hours, minutes, seconds);
62+
63+
return String(buf);
64+
}
65+
66+
String getRTCDateTime()
67+
{
68+
auto date = getRTCDate();
69+
auto time = getRTCTime();
70+
71+
auto dateTime = date + ' ' + time;
72+
73+
return dateTime;
74+
}
75+
76+
String getLocaltime()
77+
{
78+
char buffer[32];
79+
tm t;
80+
_rtc_localtime(time(NULL), &t, RTC_FULL_LEAP_YEAR_SUPPORT);
81+
strftime(buffer, 32, "%Y-%m-%d %k:%M:%S", &t);
82+
return String(buffer);
83+
}
84+
85+
String getLocaltime(const time_t& build_time)
86+
{
87+
char buffer[32];
88+
tm t;
89+
_rtc_localtime(build_time, &t, RTC_FULL_LEAP_YEAR_SUPPORT);
90+
strftime(buffer, 32, "%Y-%m-%d %k:%M:%S", &t);
91+
return String(buffer);
92+
}
93+
94+
String getLocaltime(const char* fmt, bool local_time = true, int tz = 0)
95+
{
96+
char buffer[64];
97+
time_t tmp_time = time(NULL);
98+
tm t;
99+
100+
if (!local_time) {
101+
if (tz > 200) {
102+
tz = 0x100 - tz; // Handle negative values
103+
tmp_time -= (3600UL) * tz;
104+
} else {
105+
tmp_time += (3600UL) * tz;
106+
}
107+
}
108+
109+
_rtc_localtime(tmp_time, &t, RTC_FULL_LEAP_YEAR_SUPPORT);
110+
strftime(buffer, 64, fmt, &t);
111+
return String(buffer);
112+
}
113+
114+
String getLocaltime(const time_t build_time, const char* fmt, bool local_time = true, int tz = 0)
115+
{
116+
char buffer[64];
117+
time_t tmp_time = build_time;
118+
tm t;
119+
120+
if (!local_time) {
121+
if (tz > 200) {
122+
tz = 0x100 - tz; // Handle negative values
123+
tmp_time -= (3600UL) * tz;
124+
} else {
125+
tmp_time += (3600UL) * tz;
126+
}
127+
}
128+
129+
_rtc_localtime(tmp_time, &t, RTC_FULL_LEAP_YEAR_SUPPORT);
130+
strftime(buffer, 64, fmt, &t);
131+
return String(buffer);
132+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
Arduino Edge Control - RTC Example
3+
4+
This sketch shows how to use the RTC (PCF8563T) on the Arduino
5+
Edge Control and how to configure the RTC's time registers.
6+
7+
Circuit:
8+
- Arduino Edge Control
9+
- CR2032 Battery
10+
- Optional: Arduino Edge Control LCD + Button brekout
11+
12+
*/
13+
14+
#include "Helpers.h"
15+
#include <Arduino_EdgeControl.h>
16+
17+
void setup()
18+
{
19+
pinMode(POWER_ON, INPUT);
20+
21+
Serial.begin(9600);
22+
23+
for (auto timeout = millis() + 2500l; !Serial && millis() < timeout; delay(250))
24+
;
25+
26+
Serial.println("Hello, Arduino Edge Control!");
27+
28+
EdgeControl.begin();
29+
30+
Power.on(PWR_3V3);
31+
Power.on(PWR_VBAT);
32+
33+
Wire.begin();
34+
delay(500);
35+
36+
Serial.print("Initializating the Real Time Clock...");
37+
while (!RealTimeClock.begin()) {
38+
Serial.println(" failed! Retrying...");
39+
delay(250);
40+
}
41+
Serial.println(" done!");
42+
43+
// APIs to set date's fields: years, months, days, hours, minutes and seconds
44+
// The RTC time can be set as epoch, using one of the following two options:
45+
// - Calendar time: RealTimeClock.setEpoch(years, months, days, hours, minutes, seconds);
46+
// - UTC time: RealTimeClock.setEpoch(date_in_seconds);
47+
48+
// Set the RTC only when LCD's PowerOn button is pressed.
49+
// YMMV.
50+
if (digitalRead(POWER_ON) == LOW) {
51+
Serial.println("Resetting the RTC to Sketch Build Datetime!");
52+
auto buildDateTime = getBuildDateTime();
53+
RealTimeClock.setEpoch(buildDateTime);
54+
Serial.print("Build ");
55+
}
56+
}
57+
58+
void loop()
59+
{
60+
Serial.print("Date: ");
61+
Serial.println(getRTCDateTime());
62+
63+
Serial.print("Unix time: ");
64+
Serial.println(time(NULL));
65+
66+
delay(1000);
67+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#pragma once
2+
3+
#include <Arduino.h>
4+
#include <mbed_mktime.h>
5+
6+
// Convert build time to UNIX time
7+
time_t getBuildDateTime(bool local_time = true, int tz = 0)
8+
{
9+
char s_month[5];
10+
int year;
11+
12+
tm t;
13+
time_t seconds;
14+
15+
static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
16+
17+
sscanf(__DATE__, "%s %d %d", s_month, &t.tm_mday, &year);
18+
sscanf(__TIME__, "%2d %*c %2d %*c %2d", &t.tm_hour, &t.tm_min, &t.tm_sec);
19+
20+
// Find where is s_month in month_names. Deduce month value.
21+
t.tm_mon = (strstr(month_names, s_month) - month_names) / 3 + 1;
22+
t.tm_year = year - 1900;
23+
_rtc_maketime(&t, &seconds, RTC_FULL_LEAP_YEAR_SUPPORT);
24+
25+
if (!local_time) {
26+
if (tz > 200) {
27+
tz = 0x100 - tz; // Handle negative values
28+
seconds += (3600UL) * tz;
29+
} else {
30+
seconds -= (3600UL) * tz;
31+
}
32+
}
33+
34+
return seconds;
35+
}
36+
37+
String getLocaltime()
38+
{
39+
char buffer[32];
40+
tm t;
41+
_rtc_localtime(time(NULL), &t, RTC_FULL_LEAP_YEAR_SUPPORT);
42+
strftime(buffer, 32, "%Y-%m-%d %k:%M:%S", &t);
43+
return String(buffer);
44+
}
45+
46+
String getLocaltime(const time_t& build_time)
47+
{
48+
char buffer[32];
49+
tm t;
50+
_rtc_localtime(build_time, &t, RTC_FULL_LEAP_YEAR_SUPPORT);
51+
strftime(buffer, 32, "%Y-%m-%d %k:%M:%S", &t);
52+
return String(buffer);
53+
}
54+
55+
String getLocaltime(const char* fmt, bool local_time = true, int tz = 0)
56+
{
57+
char buffer[64];
58+
time_t tmp_time = time(NULL);
59+
tm t;
60+
61+
if (!local_time) {
62+
if (tz > 200) {
63+
tz = 0x100 - tz; // Handle negative values
64+
tmp_time -= (3600UL) * tz;
65+
} else {
66+
tmp_time += (3600UL) * tz;
67+
}
68+
}
69+
70+
_rtc_localtime(tmp_time, &t, RTC_FULL_LEAP_YEAR_SUPPORT);
71+
strftime(buffer, 64, fmt, &t);
72+
return String(buffer);
73+
}
74+
75+
String getLocaltime(const time_t build_time, const char* fmt, bool local_time = true, int tz = 0)
76+
{
77+
char buffer[64];
78+
time_t tmp_time = build_time;
79+
tm t;
80+
81+
if (!local_time) {
82+
if (tz > 200) {
83+
tz = 0x100 - tz; // Handle negative values
84+
tmp_time -= (3600UL) * tz;
85+
} else {
86+
tmp_time += (3600UL) * tz;
87+
}
88+
}
89+
90+
_rtc_localtime(tmp_time, &t, RTC_FULL_LEAP_YEAR_SUPPORT);
91+
strftime(buffer, 64, fmt, &t);
92+
return String(buffer);
93+
}

0 commit comments

Comments
 (0)