Skip to content

Commit 863adf2

Browse files
committed
Added ability to set multiple alarms
Like requested in issue #6
1 parent e42d0cd commit 863adf2

File tree

5 files changed

+191
-2
lines changed

5 files changed

+191
-2
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
Multiple RTC Alarm for Arduino Zero
3+
4+
Demonstrates multiple use of alarms using the RTC library for the Arduino Zero
5+
6+
This example code is in the public domain
7+
8+
http://arduino.cc/en/Tutorial/MultipleRTCAlarm
9+
10+
created by Arturo Guadalupi <[email protected]>
11+
15 Jan 2016
12+
*/
13+
14+
#include <RTCZero.h>
15+
16+
/* Create an rtc object */
17+
RTCZero rtc;
18+
19+
/* Change these values to set the current initial time */
20+
const byte seconds = 0;
21+
const byte minutes = 0;
22+
const byte hours = 16;
23+
24+
/* Change these values to set the current initial date */
25+
const byte day = 25;
26+
const byte month = 9;
27+
const byte year = 15;
28+
29+
void setup()
30+
{
31+
Serial.begin(9600);
32+
33+
rtc.begin(); // initialize RTC 24H format
34+
35+
rtc.setTime(hours, minutes, seconds);
36+
rtc.setDate(day, month, year);
37+
38+
rtc.addAlarm(16, 0, 10, 0, 0, 0, rtc.MATCH_HHMMSS, alarmMatch1);
39+
rtc.addAlarm(16, 0, 20, 0, 0, 0, rtc.MATCH_HHMMSS, alarmMatch2);
40+
rtc.addAlarm(16, 0, 30, 0, 0, 0, rtc.MATCH_HHMMSS, alarmMatch3);
41+
42+
rtc.updateAlarms();
43+
44+
delay(3000);
45+
Serial.println("First alarm is:");
46+
Serial.print(RTC->MODE2.Mode2Alarm[0].ALARM.bit.HOUR);
47+
Serial.print(RTC->MODE2.Mode2Alarm[0].ALARM.bit.MINUTE);
48+
Serial.println(RTC->MODE2.Mode2Alarm[0].ALARM.bit.SECOND);
49+
}
50+
51+
void loop()
52+
{
53+
54+
}
55+
56+
void alarmMatch1()
57+
{
58+
Serial.println("Alarm Match1!");
59+
rtc.updateAlarms();
60+
Serial.println("Next alarm is");
61+
Serial.print(rtc.getAlarmHours());
62+
Serial.print(":");
63+
Serial.print(rtc.getAlarmMinutes());
64+
Serial.print(":");
65+
Serial.println(rtc.getAlarmSeconds());
66+
}
67+
68+
void alarmMatch2()
69+
{
70+
Serial.println("Alarm Match2!");
71+
rtc.updateAlarms();
72+
Serial.println("Next alarm is");
73+
Serial.print(rtc.getAlarmHours());
74+
Serial.print(":");
75+
Serial.print(rtc.getAlarmMinutes());
76+
Serial.print(":");
77+
Serial.println(rtc.getAlarmSeconds());
78+
}
79+
80+
void alarmMatch3()
81+
{
82+
Serial.println("Alarm Match3!");
83+
rtc.updateAlarms();
84+
Serial.println("Next alarm is");
85+
Serial.println("Next alarm is");
86+
Serial.print(rtc.getAlarmHours());
87+
Serial.print(":");
88+
Serial.print(rtc.getAlarmMinutes());
89+
Serial.print(":");
90+
Serial.println(rtc.getAlarmSeconds());
91+
}

keywords.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ disableAlarm KEYWORD2
5454

5555
standbyMode KEYWORD2
5656

57+
addAlarm KEYWORD2
58+
updateAlarms KEYWORD2
59+
5760
#######################################
5861
# Constants (LITERAL1)
5962
#######################################

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=RTCZero
2-
version=1.4.1
2+
version=1.5.0
33
author=Arduino
44
maintainer=Arduino <[email protected]>
55
sentence=Allows to use the RTC functionalities. For Arduino Zero only.

src/RTCZero.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ void RTCZero::begin()
7272

7373
RTCenable();
7474
RTCresetRemove();
75+
76+
_alarmsStartPtr = NULL;
77+
isFirstAlarm = true;
7578
}
7679

7780
void RTC_Handler(void)
@@ -342,11 +345,33 @@ void RTCZero::setY2kEpoch(uint32_t ts)
342345
setEpoch(ts + EPOCH_TIME_OFF);
343346
}
344347

348+
349+
void RTCZero::addAlarm(uint8_t hours, uint8_t minutes, uint8_t seconds, uint8_t day, uint8_t month, uint8_t year, uint8_t typeOfMatch, voidFuncPtr callback){
350+
_alarmsEndPtr = insertAlarm ( hours, minutes, seconds, day, month, year, typeOfMatch, callback);
351+
}
352+
353+
void RTCZero::updateAlarms(void){
354+
if(_alarmsStartPtr != NULL){
355+
setAlarmTime(_alarmsStartPtr->hours, _alarmsStartPtr->minutes, _alarmsStartPtr->seconds);
356+
setAlarmDate(_alarmsStartPtr->day, _alarmsStartPtr->month, _alarmsStartPtr->year);
357+
enableAlarm((Alarm_Match) _alarmsStartPtr->typeOfMatch);
358+
attachInterrupt(_alarmsStartPtr->thisCallback);
359+
}
360+
else{
361+
setAlarmTime(0, 0, 0);
362+
setAlarmDate(0, 0, 0);
363+
enableAlarm(MATCH_OFF);
364+
}
365+
366+
destroyAlarm();
367+
}
368+
345369
/*
346370
* Private Utility Functions
347371
*/
348372

349373
/* Configure the 32768Hz Oscillator */
374+
350375
void RTCZero::config32kOSC()
351376
{
352377
SYSCTRL->XOSC32K.reg = SYSCTRL_XOSC32K_ONDEMAND |
@@ -390,3 +415,52 @@ void RTCZero::RTCresetRemove()
390415
while (RTCisSyncing())
391416
;
392417
}
418+
419+
alarms* RTCZero::insertAlarm (uint8_t hours, uint8_t minutes, uint8_t seconds, uint8_t day, uint8_t month, uint8_t year, uint8_t typeOfMatch, voidFuncPtr callback){
420+
421+
alarms *currentPtr, *newPtr;
422+
423+
currentPtr = _alarmsStartPtr;
424+
425+
if(isFirstAlarm){
426+
_alarmsStartPtr = (alarms*) malloc(sizeof (alarms));
427+
_alarmsStartPtr->hours = hours;
428+
_alarmsStartPtr->minutes = minutes;
429+
_alarmsStartPtr->seconds = seconds;
430+
_alarmsStartPtr->day = day;
431+
_alarmsStartPtr->month = month;
432+
_alarmsStartPtr->year = year;
433+
_alarmsStartPtr->typeOfMatch = (Alarm_Match) typeOfMatch;
434+
_alarmsStartPtr->thisCallback = callback;
435+
_alarmsStartPtr->nextAlarmPtr = NULL;
436+
isFirstAlarm = false;
437+
}
438+
else{
439+
while (currentPtr->nextAlarmPtr != NULL){
440+
currentPtr = currentPtr->nextAlarmPtr;
441+
}
442+
443+
newPtr = (alarms*) malloc(sizeof (alarms));
444+
newPtr->hours = hours;
445+
newPtr->minutes = minutes;
446+
newPtr->seconds = seconds;
447+
newPtr->day = day;
448+
newPtr->month = month;
449+
newPtr->year = year;
450+
newPtr->typeOfMatch = typeOfMatch;
451+
newPtr->thisCallback = callback;
452+
newPtr->nextAlarmPtr = NULL;
453+
}
454+
currentPtr->nextAlarmPtr = newPtr;
455+
456+
return newPtr;
457+
}
458+
459+
void RTCZero::destroyAlarm(void){
460+
alarms *currentPtr;
461+
462+
currentPtr = _alarmsStartPtr;
463+
_alarmsStartPtr = currentPtr->nextAlarmPtr;
464+
free(currentPtr);
465+
466+
}

src/RTCZero.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@
2424

2525
typedef void(*voidFuncPtr)(void);
2626

27+
typedef struct ALARMS{
28+
uint8_t seconds;
29+
uint8_t minutes;
30+
uint8_t hours;
31+
uint8_t day;
32+
uint8_t month;
33+
uint8_t year;
34+
uint8_t typeOfMatch;
35+
voidFuncPtr thisCallback;
36+
struct ALARMS *nextAlarmPtr;
37+
} alarms;
38+
2739
class RTCZero {
2840
public:
2941

@@ -38,6 +50,10 @@ class RTCZero {
3850
MATCH_YYMMDDHHMMSS = RTC_MODE2_MASK_SEL_YYMMDDHHMMSS_Val // Once, on a specific date and a specific time
3951
};
4052

53+
alarms *_alarmsStartPtr, *_alarmsEndPtr;
54+
55+
bool isFirstAlarm;
56+
4157
RTCZero() {};
4258
void begin();
4359

@@ -90,7 +106,9 @@ class RTCZero {
90106
void setAlarmMonth(uint8_t month);
91107
void setAlarmYear(uint8_t year);
92108
void setAlarmDate(uint8_t day, uint8_t month, uint8_t year);
93-
109+
110+
void addAlarm(uint8_t hours, uint8_t minutes, uint8_t seconds, uint8_t day, uint8_t month, uint8_t year, uint8_t typeOfMatch, voidFuncPtr callback);
111+
void updateAlarms(void);
94112
/* Epoch Functions */
95113

96114
uint32_t getEpoch();
@@ -105,6 +123,9 @@ class RTCZero {
105123
void RTCenable();
106124
void RTCreset();
107125
void RTCresetRemove();
126+
127+
alarms* insertAlarm (uint8_t hours, uint8_t minutes, uint8_t seconds, uint8_t day, uint8_t month, uint8_t year, uint8_t typeOfMatch, voidFuncPtr callback);
128+
void destroyAlarm(void);
108129
};
109130

110131
#endif // RTC_ZERO_H

0 commit comments

Comments
 (0)