Skip to content

Commit 24bb4fa

Browse files
committed
Merge pull request #1 from arduino-libraries/master
Pulling in changes from the main fork
2 parents 07ef730 + f75ebf6 commit 24bb4fa

File tree

8 files changed

+118
-83
lines changed

8 files changed

+118
-83
lines changed

README.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
= RTC Library for Arduino =
22

3-
The RTC library enables an Arduino Zero board to take control of the internal RTC.
3+
The RTC library enables an Arduino Zero or MKR1000 board to take control of the internal RTC.
44

55
For more information about this library please visit us at
66
http://arduino.cc/en/Reference/RTC

examples/Epoch/Epoch.ino

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
Epoch time example for Arduino Zero
3+
4+
Demonstrates the use of the RTC library for the Arduino Zero
5+
6+
This example code is in the public domain
7+
8+
created by Sandeep Mistry <[email protected]>
9+
31 Dec 2015
10+
*/
11+
12+
#include <RTCZero.h>
13+
14+
/* Create an rtc object */
15+
RTCZero rtc;
16+
17+
void setup() {
18+
Serial.begin(9600);
19+
20+
rtc.begin(); // initialize RTC
21+
22+
rtc.setEpoch(1451606400); // Jan 1, 2016
23+
}
24+
25+
void loop() {
26+
Serial.print("Unix time = ");
27+
Serial.println(rtc.getEpoch());
28+
29+
Serial.print("Seconds since Jan 1 2000 = ");
30+
Serial.println(rtc.getY2kEpoch());
31+
32+
// Print date...
33+
Serial.print(rtc.getDay());
34+
Serial.print("/");
35+
Serial.print(rtc.getMonth());
36+
Serial.print("/");
37+
Serial.print(rtc.getYear());
38+
Serial.print("\t");
39+
40+
// ...and time
41+
print2digits(rtc.getHours());
42+
Serial.print(":");
43+
print2digits(rtc.getMinutes());
44+
Serial.print(":");
45+
print2digits(rtc.getSeconds());
46+
47+
Serial.println();
48+
49+
delay(1000);
50+
}
51+
52+
void print2digits(int number) {
53+
if (number < 10) {
54+
Serial.print("0");
55+
}
56+
Serial.print(number);
57+
}
58+

examples/SimpleRTC/SimpleRTC.ino

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
RTCZero rtc;
2121

2222
/* Change these values to set the current initial time */
23-
const uint8_t seconds = 0;
24-
const uint8_t minutes = 0;
25-
const uint8_t hours = 16;
23+
const byte seconds = 0;
24+
const byte minutes = 0;
25+
const byte hours = 16;
2626

2727
/* Change these values to set the current initial date */
28-
const uint8_t day = 15;
29-
const uint8_t month = 6;
30-
const uint8_t year = 15;
28+
const byte day = 15;
29+
const byte month = 6;
30+
const byte year = 15;
3131

3232
void setup()
3333
{

examples/SimpleRTCAlarm/SimpleRTCAlarm.ino

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
RTCZero rtc;
2121

2222
/* Change these values to set the current initial time */
23-
const uint8_t seconds = 0;
24-
const uint8_t minutes = 0;
25-
const uint8_t hours = 16;
23+
const byte seconds = 0;
24+
const byte minutes = 0;
25+
const byte hours = 16;
2626

2727
/* Change these values to set the current initial date */
28-
const uint8_t day = 25;
29-
const uint8_t month = 9;
30-
const uint8_t year = 15;
28+
const byte day = 25;
29+
const byte month = 9;
30+
const byte year = 15;
3131

3232
void setup()
3333
{

examples/SleepRTCAlarm/SleepRTCAlarm.ino

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
RTCZero rtc;
1818

1919
/* Change these values to set the current initial time */
20-
const uint8_t seconds = 0;
21-
const uint8_t minutes = 00;
22-
const uint8_t hours = 17;
20+
const byte seconds = 0;
21+
const byte minutes = 00;
22+
const byte hours = 17;
2323

2424
/* Change these values to set the current initial date */
25-
const uint8_t day = 17;
26-
const uint8_t month = 11;
27-
const uint8_t year = 15;
25+
const byte day = 17;
26+
const byte month = 11;
27+
const byte year = 15;
2828

2929
void setup()
3030
{

keywords.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ setSeconds KEYWORD2
2828
setDate KEYWORD2
2929
setTime KEYWORD2
3030

31+
getEpoch KEYWORD2
32+
getY2kEpoch KEYWORD2
33+
setEpoch KEYWORD2
34+
setY2kEpoch KEYWORD2
35+
3136
getAlarmDay KEYWORD2
3237
getAlarmMonth KEYWORD2
3338
getAlarmYear KEYWORD2

library.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name=RTCZero
2-
version=1.3.0
2+
version=1.4.2
33
author=Arduino
44
maintainer=Arduino <[email protected]>
5-
sentence=Allows to use the RTC functionalities. For Arduino Zero only.
6-
paragraph=With this library you can use the RTC peripheral of an Arduino Zero in order to program actions related to date and time.
5+
sentence=Allows to use the RTC functionalities. For Arduino Zero and MKR1000 only.
6+
paragraph=With this library you can use the RTC peripheral of an Arduino Zero or MKR1000 in order to program actions related to date and time.
77
category=Timing
88
url=http://www.arduino.cc/en/Reference/RTCZero
99
architectures=samd

src/RTCZero.cpp

Lines changed: 33 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
License along with this library; if not, write to the Free Software
1717
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1818
*/
19-
20-
#include "RTCZero.h"
2119

22-
#define EPOCH_TIME_OFF 946684800 // This is 1st January 2000, 00:00:00 in epoch time
20+
#include <time.h>
21+
22+
#include "RTCZero.h"
2323

24-
static const uint8_t daysInMonth[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
24+
#define EPOCH_TIME_OFF 946684800 // This is 1st January 2000, 00:00:00 in epoch time
25+
#define EPOCH_TIME_YEAR_OFF 100 // years since 1900
2526

2627
voidFuncPtr RTC_callBack = NULL;
2728

@@ -296,80 +297,51 @@ void RTCZero::setAlarmDate(uint8_t day, uint8_t month, uint8_t year)
296297

297298
uint32_t RTCZero::getEpoch()
298299
{
299-
return getY2kEpoch() + EPOCH_TIME_OFF;
300+
struct tm tm;
301+
302+
tm.tm_isdst = -1;
303+
tm.tm_yday = 0;
304+
tm.tm_wday = 0;
305+
tm.tm_year = getYear() + EPOCH_TIME_YEAR_OFF;
306+
tm.tm_mon = getMonth() - 1;
307+
tm.tm_mday = getDay();
308+
tm.tm_hour = getHours();
309+
tm.tm_min = getMinutes();
310+
tm.tm_sec = getSeconds();
311+
312+
return mktime(&tm);
300313
}
301314

302315
uint32_t RTCZero::getY2kEpoch()
303316
{
304-
uint16_t days = RTC->MODE2.CLOCK.bit.DAY;
305-
days = days > 0 ? days : 1;
306-
uint8_t months = RTC->MODE2.CLOCK.bit.MONTH;
307-
uint16_t years = RTC->MODE2.CLOCK.bit.YEAR;
308-
309-
for (uint8_t i = 1; i < months; ++i) {
310-
days += daysInMonth[i - 1];
311-
}
312-
313-
if ((months > 2) && (years % 4 == 0)) {
314-
++days;
315-
}
316-
days += 365 * years + (years + 3) / 4 - 1;
317-
318-
uint8_t hours = RTC->MODE2.CLOCK.bit.HOUR;
319-
320-
return ((days * 24 + hours) * 60 +
321-
RTC->MODE2.CLOCK.bit.MINUTE) * 60 + RTC->MODE2.CLOCK.bit.SECOND;
317+
return (getEpoch() - EPOCH_TIME_OFF);
322318
}
323319

324320
void RTCZero::setEpoch(uint32_t ts)
325321
{
326322
if (ts < EPOCH_TIME_OFF) {
327-
setY2kEpoch(0);
328-
}
329-
else {
330-
setY2kEpoch(ts - EPOCH_TIME_OFF);
323+
ts = EPOCH_TIME_OFF;
331324
}
332-
}
333-
334-
void RTCZero::setY2kEpoch(uint32_t ts)
335-
{
336-
RTC->MODE2.CLOCK.bit.SECOND = ts % 60;
337-
ts /= 60;
338-
RTC->MODE2.CLOCK.bit.MINUTE = ts % 60;
339-
ts /= 60;
340-
RTC->MODE2.CLOCK.bit.HOUR = ts % 24;
341-
342-
uint16_t days = ts / 24;
343-
uint8_t months;
344-
uint8_t years;
345-
346-
uint8_t leap;
347325

348-
// Calculate years
349-
for (years = 0; ; ++years) {
350-
leap = years % 4 == 0;
351-
if (days < 365 + leap)
352-
break;
353-
days -= 365 + leap;
354-
}
326+
time_t t = ts;
327+
struct tm* tmp = gmtime(&t);
355328

356-
// Calculate months
357-
for (months = 1; ; ++months) {
358-
uint8_t daysPerMonth = daysInMonth[months - 1];
359-
if (leap && months == 2)
360-
++daysPerMonth;
361-
if (days < daysPerMonth)
362-
break;
363-
days -= daysPerMonth;
364-
}
329+
RTC->MODE2.CLOCK.bit.YEAR = tmp->tm_year - EPOCH_TIME_YEAR_OFF;
330+
RTC->MODE2.CLOCK.bit.MONTH = tmp->tm_mon + 1;
331+
RTC->MODE2.CLOCK.bit.DAY = tmp->tm_mday;
332+
RTC->MODE2.CLOCK.bit.HOUR = tmp->tm_hour;
333+
RTC->MODE2.CLOCK.bit.MINUTE = tmp->tm_min;
334+
RTC->MODE2.CLOCK.bit.SECOND = tmp->tm_sec;
365335

366-
RTC->MODE2.CLOCK.bit.YEAR = years;
367-
RTC->MODE2.CLOCK.bit.MONTH = months;
368-
RTC->MODE2.CLOCK.bit.DAY = days + 1;
369336
while (RTCisSyncing())
370337
;
371338
}
372339

340+
void RTCZero::setY2kEpoch(uint32_t ts)
341+
{
342+
setEpoch(ts + EPOCH_TIME_OFF);
343+
}
344+
373345
/*
374346
* Private Utility Functions
375347
*/

0 commit comments

Comments
 (0)