Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API X_NUCLEO_IDB0XA1 mbed
Fork of BLE_HeartRate_IDB0XA1 by
bricks/blinker.cpp@28:307f58df778a, 2017-10-01 (annotated)
- Committer:
- hux
- Date:
- Sun Oct 01 12:48:58 2017 +0000
- Revision:
- 28:307f58df778a
A blue button is always a nice toy ...
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| hux | 28:307f58df778a | 1 | // blinker.cpp - send a morse pattern to LED1 |
| hux | 28:307f58df778a | 2 | // |
| hux | 28:307f58df778a | 3 | // Function morse() is one way for running LED1 with a blinking sequence using |
| hux | 28:307f58df778a | 4 | // a busy wait, until the sequence is completed. |
| hux | 28:307f58df778a | 5 | // |
| hux | 28:307f58df778a | 6 | // Blinker b; |
| hux | 28:307f58df778a | 7 | // b.morse(" x xxx x "); send one time morse sequence, interval = 0.2 |
| hux | 28:307f58df778a | 8 | // b.morse(" x xxx x ",0.5); send one time morse sequence, interval = 0.5 |
| hux | 28:307f58df778a | 9 | // |
| hux | 28:307f58df778a | 10 | // Function morse(o) got the additional feature to stop an ongoing timer based |
| hux | 28:307f58df778a | 11 | // blinking sequence. |
| hux | 28:307f58df778a | 12 | // |
| hux | 28:307f58df778a | 13 | // The alternative is to setup an ever repeating blink sequence via LED1 using |
| hux | 28:307f58df778a | 14 | // function blink(), which is non waiting. |
| hux | 28:307f58df778a | 15 | // |
| hux | 28:307f58df778a | 16 | // Blinker b; |
| hux | 28:307f58df778a | 17 | // b.blink(" x xxx x "); repeating blink sequence, interval = 0.2 |
| hux | 28:307f58df778a | 18 | // b.blink(" x xxx x ",0.5); repeating blink sequence, interval = 0.5 |
| hux | 28:307f58df778a | 19 | // b.blink(); stops blinking sequence |
| hux | 28:307f58df778a | 20 | // |
| hux | 28:307f58df778a | 21 | |
| hux | 28:307f58df778a | 22 | #include "bricks/target.h" |
| hux | 28:307f58df778a | 23 | #include "bricks/blinker.h" |
| hux | 28:307f58df778a | 24 | |
| hux | 28:307f58df778a | 25 | #ifndef LED_INVERTED |
| hux | 28:307f58df778a | 26 | # define LED_ON 1 |
| hux | 28:307f58df778a | 27 | # define LED_OFF 0 |
| hux | 28:307f58df778a | 28 | #else |
| hux | 28:307f58df778a | 29 | # define LED_ON 0 |
| hux | 28:307f58df778a | 30 | # define LED_OFF 1 |
| hux | 28:307f58df778a | 31 | #endif |
| hux | 28:307f58df778a | 32 | |
| hux | 28:307f58df778a | 33 | #ifndef BLINK_LED |
| hux | 28:307f58df778a | 34 | # define BLINK_LED LED1 |
| hux | 28:307f58df778a | 35 | #endif |
| hux | 28:307f58df778a | 36 | |
| hux | 28:307f58df778a | 37 | static DigitalOut led(BLINK_LED); // being used for morse sequence |
| hux | 28:307f58df778a | 38 | static Ticker ticker; // triggers periodic callbacks |
| hux | 28:307f58df778a | 39 | static const char *pointer = 0; // 0 means morse activity disabled |
| hux | 28:307f58df778a | 40 | static const char *sequence = 0; // next morse sequence for repeats |
| hux | 28:307f58df778a | 41 | |
| hux | 28:307f58df778a | 42 | void Blinker::morse(const char *pattern, double interval) |
| hux | 28:307f58df778a | 43 | { |
| hux | 28:307f58df778a | 44 | pointer = 0; // disable ticker based blinking |
| hux | 28:307f58df778a | 45 | sequence = 0; // set also empty sequence |
| hux | 28:307f58df778a | 46 | |
| hux | 28:307f58df778a | 47 | for (; *pattern; pattern++) |
| hux | 28:307f58df778a | 48 | { |
| hux | 28:307f58df778a | 49 | led = (*pattern == ' ') ? LED_OFF : LED_ON; |
| hux | 28:307f58df778a | 50 | wait(interval); // busy waiting for interval time |
| hux | 28:307f58df778a | 51 | } |
| hux | 28:307f58df778a | 52 | } |
| hux | 28:307f58df778a | 53 | |
| hux | 28:307f58df778a | 54 | // callback for LED1 ticker controlled blinking |
| hux | 28:307f58df778a | 55 | |
| hux | 28:307f58df778a | 56 | static void cbBlinker(void) // blinker callback |
| hux | 28:307f58df778a | 57 | { |
| hux | 28:307f58df778a | 58 | if (pointer != 0) |
| hux | 28:307f58df778a | 59 | { |
| hux | 28:307f58df778a | 60 | if (*pointer == 0) |
| hux | 28:307f58df778a | 61 | { |
| hux | 28:307f58df778a | 62 | pointer = sequence; // reset pointer to followup sequence |
| hux | 28:307f58df778a | 63 | } |
| hux | 28:307f58df778a | 64 | |
| hux | 28:307f58df778a | 65 | if (*pointer) |
| hux | 28:307f58df778a | 66 | { |
| hux | 28:307f58df778a | 67 | led = (*pointer++ == ' ') ? LED_OFF : LED_ON; |
| hux | 28:307f58df778a | 68 | } |
| hux | 28:307f58df778a | 69 | } |
| hux | 28:307f58df778a | 70 | } |
| hux | 28:307f58df778a | 71 | |
| hux | 28:307f58df778a | 72 | void Blinker::blink(const char *pattern, const char* next, double interval) |
| hux | 28:307f58df778a | 73 | { |
| hux | 28:307f58df778a | 74 | pointer = 0; // stop current activities |
| hux | 28:307f58df778a | 75 | led = LED_OFF; // reset led with LED_OFF |
| hux | 28:307f58df778a | 76 | |
| hux | 28:307f58df778a | 77 | sequence = next; // init morse sequence |
| hux | 28:307f58df778a | 78 | |
| hux | 28:307f58df778a | 79 | ticker.attach(cbBlinker,interval);// next LED state after every interval |
| hux | 28:307f58df778a | 80 | pointer = pattern; // enable callback activty |
| hux | 28:307f58df778a | 81 | } |
| hux | 28:307f58df778a | 82 | |
| hux | 28:307f58df778a | 83 | void Blinker::blink(const char *pattern, double interval) |
| hux | 28:307f58df778a | 84 | { |
| hux | 28:307f58df778a | 85 | blink(pattern,pattern,interval); |
| hux | 28:307f58df778a | 86 | } |
| hux | 28:307f58df778a | 87 | |
| hux | 28:307f58df778a | 88 |
