Skip to content

Add Alarm example to docs #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 29, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions Alarm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Alarm example using Mbed OS

This guide reviews the steps required to build and run a basic alarm application on an Mbed OS platform.

Please install [mbed CLI](https://github.com/ARMmbed/mbed-cli#installing-mbed-cli).

## Import the example application

From the command-line, import the example:

```
mbed import mbed-os-example-alarm
cd mbed-os-example-alarm
```

### Now compile

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:

```
mbed compile -m K64F -t ARM
```

Your PC may take a few minutes to compile your code. At the end, you see the following result:

```
[snip]
| Module | .text | .data | .bss |
|----------------------|---------------|-----------|-------------|
| [lib]/dl7M_tlf.a | 10780(+10780) | 364(+364) | 716(+716) |
| [lib]/dlpp7M_tl_fc.a | 84(+84) | 0(+0) | 0(+0) |
| [lib]/m7M_tls.a | 2358(+2358) | 0(+0) | 0(+0) |
| [lib]/rt7M_tl.a | 1194(+1194) | 0(+0) | 0(+0) |
| [misc] | 215(+215) | 0(+0) | 0(+0) |
| main.o | 820(+820) | 0(+0) | 200(+200) |
| mbed-os/drivers | 490(+490) | 0(+0) | 0(+0) |
| mbed-os/features | 114(+114) | 0(+0) | 184(+184) |
| mbed-os/hal | 2070(+2070) | 8(+8) | 132(+132) |
| mbed-os/platform | 2938(+2938) | 112(+112) | 176(+176) |
| mbed-os/rtos | 8928(+8928) | 168(+168) | 6437(+6437) |
| mbed-os/targets | 10174(+10174) | 20(+20) | 1018(+1018) |
| Subtotals | 40165(+40165) | 672(+672) | 8863(+8863) |
Total Static RAM memory (data + bss): 9535(+9535) bytes
Total Flash memory (text + data): 40837(+40837) bytes

Image: ./BUILD/K64F/IAR/mbed-os-example-alarm.bin
```

### Program your board

1. Connect your mbed device to the computer over USB.
1. Copy the binary file to the mbed device.
1. Press the reset button to start the program.

Press Button1 for the number of desired hours to delay. Press Button2 to cycle to minutes and repeat the previous step for number of desired minutes. Press Button2 again to start the alarm. 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.

## Troubleshooting

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.
108 changes: 108 additions & 0 deletions Alarm/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/* 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 alarm time. Cycle through hours in an incrementing
* fashion using button1, hit select and increment through minutes. Hit
* select one more time to begin the alarm timer.
*
* The Time LEDs will blink in time with the button inputs to show the
* currently selected alarm time. Once select is hit 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, hitting 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. This would require manually setting the time on each
* reset however, 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) { wait_ms(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;
wait(0.25f);
}

for (uint8_t i = 0; i < min_count * 2; i++) {
min_led = !min_led;
wait(0.25f);
}

// 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(); }
}
1 change: 1 addition & 0 deletions Alarm/mbed-os.lib
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/ARMmbed/mbed-os/#e2ae84ec5922cb5c0a0f204ce84049e84113862e