Skip to content

Commit 6592765

Browse files
author
Cruz Monrreal
authored
Merge pull request #14 from kegilbert/add-alarm-example
Add Alarm example to docs
2 parents 12a1c56 + 58e7bb9 commit 6592765

File tree

3 files changed

+169
-0
lines changed

3 files changed

+169
-0
lines changed

Alarm/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
## Alarm example using Mbed OS
2+
3+
This guide reviews the steps required to build and run a basic alarm application on an Mbed OS platform.
4+
5+
Please install [Mbed CLI](https://github.com/ARMmbed/mbed-cli#installing-mbed-cli).
6+
7+
### Import the example application
8+
9+
From the command-line, import the example:
10+
11+
```
12+
mbed import mbed-os-example-alarm
13+
cd mbed-os-example-alarm
14+
```
15+
16+
#### Now compile
17+
18+
Invoke `mbed compile`, and specify the name of your platform and your favorite toolchain (`GCC_ARM`, `ARM`, `IAR`). For example, for the ARM Compiler 5:
19+
20+
```
21+
mbed compile -m K64F -t ARM
22+
```
23+
24+
Your PC may take a few minutes to compile your code. At the end, you see the following result:
25+
26+
```
27+
[snip]
28+
| Module | .text | .data | .bss |
29+
|----------------------|---------------|-----------|-------------|
30+
| [lib]/dl7M_tlf.a | 10780(+10780) | 364(+364) | 716(+716) |
31+
| [lib]/dlpp7M_tl_fc.a | 84(+84) | 0(+0) | 0(+0) |
32+
| [lib]/m7M_tls.a | 2358(+2358) | 0(+0) | 0(+0) |
33+
| [lib]/rt7M_tl.a | 1194(+1194) | 0(+0) | 0(+0) |
34+
| [misc] | 215(+215) | 0(+0) | 0(+0) |
35+
| main.o | 820(+820) | 0(+0) | 200(+200) |
36+
| mbed-os/drivers | 490(+490) | 0(+0) | 0(+0) |
37+
| mbed-os/features | 114(+114) | 0(+0) | 184(+184) |
38+
| mbed-os/hal | 2070(+2070) | 8(+8) | 132(+132) |
39+
| mbed-os/platform | 2938(+2938) | 112(+112) | 176(+176) |
40+
| mbed-os/rtos | 8928(+8928) | 168(+168) | 6437(+6437) |
41+
| mbed-os/targets | 10174(+10174) | 20(+20) | 1018(+1018) |
42+
| Subtotals | 40165(+40165) | 672(+672) | 8863(+8863) |
43+
Total Static RAM memory (data + bss): 9535(+9535) bytes
44+
Total Flash memory (text + data): 40837(+40837) bytes
45+
46+
Image: ./BUILD/K64F/IAR/mbed-os-example-alarm.bin
47+
```
48+
49+
#### Program your board
50+
51+
1. Connect your Mbed device to the computer over USB.
52+
1. Copy the binary file to the Mbed device.
53+
1. Press the reset button to start the program.
54+
1. Press **Button1** for the number of desired hours to delay. Press **Button2** to cycle to minutes, and repeat the previous step for the number of desired minutes.
55+
1. Press **Button2** again to start the alarm.
56+
1. Press **Button2** again once the alarm triggers to silence it. Both an LED and a digital out pin will go high on the alarm trigger and go back low on an alarm reset.
57+
58+
### Troubleshooting
59+
60+
If you have problems, you can review the [documentation](https://os.mbed.com/docs/latest/tutorials/debugging.html) for suggestions on what could be wrong and how to fix it.

Alarm/main.cpp

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

Alarm/mbed-os.lib

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://github.com/ARMmbed/mbed-os/#e2ae84ec5922cb5c0a0f204ce84049e84113862e

0 commit comments

Comments
 (0)