-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
117 lines (102 loc) · 2.96 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* mbed Microcontroller Library
* Copyright (c) 2018 ARM Limited
* SPDX-License-Identifier: Apache-2.0
*/
#include "mbed.h"
// Time constants in seconds
#define HOUR 60 * 60
#define MINUTE 60
// Globals
DigitalOut alarm_out(D2, 0);
DigitalOut alarm_led(LED_RED, 1);
DigitalOut hour_led(LED_GREEN, 1);
DigitalOut min_led(LED_BLUE, 1);
InterruptIn inc_time(BUTTON1);
InterruptIn sel(BUTTON2);
LowPowerTicker alarm_event;
volatile uint64_t delay = 0;
volatile uint8_t hour_count = 0;
volatile uint8_t min_count = 0;
volatile uint8_t select_state = 0;
// Timer Callbacks
void inc_select(void)
{
if (select_state < 2) {
select_state++;
} else {
// Use select button to disable alarm
alarm_out = 0;
alarm_led = 1;
}
}
void set_time_leds(void)
{
if (select_state == 0) {
hour_led = !hour_led;
} else {
min_led = !min_led;
}
}
void inc_delay(void)
{
if (select_state == 0) {
delay += HOUR;
hour_count++;
hour_led = !hour_led;
} else {
delay += MINUTE;
min_count++;
min_led = !min_led;
}
}
void trigger_alarm_out(void)
{
alarm_out = 1;
alarm_led = 0;
}
/* Use buttons to select the alarm time. Cycle through hours in an incrementing
* fashion using Button1. Press select, and increment through minutes. Press
* select one more time to begin the alarm timer.
*
* The Time LEDs blink in time with the button inputs to show the
* currently selected alarm time. Once you press select a second time to begin
* the timer, the LEDs will blink out the configured delay in hours and
* minutes before going into a low power sleep mode.
*
* Once the alarm fires, pressing the select button will turn the alarm off
* until the next time it fires.
*__________________________________________________________________________
* You may also use the RTC (hardware or software through the Time API) to
* set a real world time and set an alarm for a specific timestamp rather
* than on a delay. However, this requires manually setting the time on each
* reset, or an internet connection to automatically collect the
* time.
*/
// Main thread
int main()
{
// Configure interrupt in pins (button controls)
sel.rise(inc_select);
inc_time.fall(set_time_leds);
inc_time.rise(inc_delay);
// Sleep while waiting for user input to set the desired delay
while (select_state < 2) {
ThisThread::sleep_for(10);
}
// Once the delay has been input, blink back the configured hours and
// minutes selected
for (uint8_t i = 0; i < hour_count * 2; i++) {
hour_led = !hour_led;
ThisThread::sleep_for(250);
}
for (uint8_t i = 0; i < min_count * 2; i++) {
min_led = !min_led;
ThisThread::sleep_for(250);
}
// Attach the low power ticker with the configured alarm delay
alarm_event.attach(&trigger_alarm_out, delay);
// Sleep in the main thread
while (1) {
sleep();
}
}