|
| 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 | +} |
0 commit comments