Skip to content

Commit 475be89

Browse files
committed
fix(example): enable alarm(s) if neeed
Formatted with Arduino astyle. Signed-off-by: Frederic Pillon <[email protected]>
1 parent 3a29b0e commit 475be89

File tree

1 file changed

+33
-18
lines changed

1 file changed

+33
-18
lines changed

examples/RTCReset/RTCReset.ino

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ typedef struct {
3636
bool alarm_a;
3737
} cb_data_t;
3838

39-
static cb_data_t atime = { 2222, true};
39+
static cb_data_t atime = { 2222, true };
4040
#ifdef RTC_ALARM_B
41-
static cb_data_t btime = { 3333, false};
41+
static cb_data_t btime = { 3333, false };
4242
#endif
4343
static byte seconds = 0;
4444
static byte minutes = 0;
@@ -64,7 +64,7 @@ static uint8_t conv2d(const char* p) {
6464
}
6565

6666
// sample input: date = "Dec 26 2009", time = "12:34:56"
67-
void initDateTime (void) {
67+
void initDateTime(void) {
6868
Serial.printf("Build date & time %s, %s\n", mydate, mytime);
6969

7070
year = conv2d(mydate + 9);
@@ -89,15 +89,15 @@ void initDateTime (void) {
8989
seconds = conv2d(mytime + 6);
9090
}
9191

92-
void setup()
93-
{
92+
void setup() {
9493
pinMode(USER_BTN, INPUT_PULLUP);
9594
int32_t default_state = digitalRead(USER_BTN);
96-
97-
Serial.begin(9600);
98-
while (!Serial);
95+
Serial.begin(115200);
96+
while (!Serial)
97+
;
9998
// Wait user input to start
100-
while (digitalRead(USER_BTN) == default_state);
99+
while (digitalRead(USER_BTN) == default_state)
100+
;
101101
// Convenient function to init date and time variables
102102
initDateTime();
103103

@@ -110,7 +110,7 @@ void setup()
110110
#ifdef RTC_ALARM_B
111111
rtc.attachInterrupt(alarmMatch, &btime, STM32RTC::ALARM_B);
112112
#endif
113-
rtc.begin(); // Initialize RTC 24H format
113+
rtc.begin(); // Initialize RTC 24H format
114114
if (!rtc.isTimeSet()) {
115115
Serial.printf("RTC time not set\n Set it.\n");
116116
// Set the time
@@ -129,6 +129,10 @@ void setup()
129129
} else {
130130
// RTC already initialized
131131
time_t epoc, alarm_epoc;
132+
rtc.getTime(&hours, &minutes, &seconds, &subSeconds, &period);
133+
year = rtc.getYear();
134+
month = rtc.getMonth();
135+
day = rtc.getDay();
132136
if (rtc.isAlarmEnabled()) {
133137
rtc.enableAlarm(rtc.MATCH_DHHMMSS);
134138
alarm_epoc = rtc.getAlarmEpoch();
@@ -156,16 +160,28 @@ void setup()
156160
#endif
157161
Serial.printf("RTC time already set\n");
158162
}
159-
Serial.printf("Alarm A enable status: %s\n", (rtc.isAlarmEnabled(STM32RTC::ALARM_A)) ? "True" : "False");
163+
// For STM32F1xx series, alarm is always disabled after a reset.
164+
bool alarmA = rtc.isAlarmEnabled(STM32RTC::ALARM_A);
165+
Serial.printf("Alarm A enable status: %s\n", (alarmA) ? "True" : "False");
166+
if (!alarmA) {
167+
rtc.setAlarmDay(day);
168+
rtc.setAlarmTime(hours, minutes, seconds + 5, 567);
169+
rtc.enableAlarm(rtc.MATCH_DHHMMSS);
170+
}
160171
#ifdef RTC_ALARM_B
161-
Serial.printf("Alarm B enable status: %s\n", (rtc.isAlarmEnabled(STM32RTC::ALARM_B)) ? "True" : "False");
172+
bool alarmB = rtc.isAlarmEnabled(STM32RTC::ALARM_B);
173+
Serial.printf("Alarm B enable status: %s\n", (alarmB) ? "True" : "False");
174+
if (!alarmB) {
175+
rtc.setAlarmDay(day, STM32RTC::ALARM_B);
176+
rtc.setAlarmTime(hours, minutes, seconds + 5, 567, STM32RTC::ALARM_B);
177+
rtc.enableAlarm(rtc.MATCH_DHHMMSS, STM32RTC::ALARM_B);
178+
}
162179
#else
163180
Serial.println("Alarm B not available.");
164181
#endif
165182
}
166183

167-
void loop()
168-
{
184+
void loop() {
169185
rtc.getTime(&hours, &minutes, &seconds, &subSeconds, &period);
170186
// Print current date & time
171187
Serial.printf("\n%02d/%02d/%02d %02d:%02d:%02d.%03d\n", rtc.getDay(), rtc.getMonth(), rtc.getYear(), hours, minutes, seconds, subSeconds);
@@ -177,13 +193,12 @@ void loop()
177193
delay(1000);
178194
}
179195

180-
void alarmMatch(void *data)
181-
{
196+
void alarmMatch(void* data) {
182197
time_t epoc;
183198
uint32_t epoc_ms;
184199
uint32_t sec = 0;
185200
uint32_t _millis = 1000;
186-
cb_data_t cbdata = {.next = 1000, .alarm_a = true};
201+
cb_data_t cbdata = { .next = 1000, .alarm_a = true };
187202
if (data != NULL) {
188203
cbdata.next = ((cb_data_t*)data)->next;
189204
cbdata.alarm_a = ((cb_data_t*)data)->alarm_a;
@@ -204,7 +219,7 @@ void alarmMatch(void *data)
204219
// Update epoch_ms - might need to add a second to epoch
205220
epoc_ms += _millis;
206221
if (epoc_ms >= 1000) {
207-
sec ++;
222+
sec++;
208223
epoc_ms -= 1000;
209224
}
210225
#endif

0 commit comments

Comments
 (0)