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/blink.cpp@29:8eb46b976f0f, 2018-05-19 (annotated)
- Committer:
- hux
- Date:
- Sat May 19 15:53:19 2018 +0000
- Revision:
- 29:8eb46b976f0f
Published
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| hux | 29:8eb46b976f0f | 1 | // blink.cpp - send a morse pattern to LED1 |
| hux | 29:8eb46b976f0f | 2 | // |
| hux | 29:8eb46b976f0f | 3 | // Function morse() is one way for running LED1 with a blinking sequence using |
| hux | 29:8eb46b976f0f | 4 | // a busy wait, until the sequence is completed. |
| hux | 29:8eb46b976f0f | 5 | // |
| hux | 29:8eb46b976f0f | 6 | // morse(" x xxx x "); send one time morse sequence, interval = 0.2 |
| hux | 29:8eb46b976f0f | 7 | // morse(" x xxx x ",0.5); send one time morse sequence, interval = 0.5 |
| hux | 29:8eb46b976f0f | 8 | // |
| hux | 29:8eb46b976f0f | 9 | // Function morse() got the additional feature to stop an ongoing timer based |
| hux | 29:8eb46b976f0f | 10 | // blinking sequence. |
| hux | 29:8eb46b976f0f | 11 | // |
| hux | 29:8eb46b976f0f | 12 | // The alternative is to setup an ever repeating blink sequence via LED1 using |
| hux | 29:8eb46b976f0f | 13 | // function blink(), which is non waiting. |
| hux | 29:8eb46b976f0f | 14 | // |
| hux | 29:8eb46b976f0f | 15 | // blink(" x xxx x "); repeating blink sequence, interval = 0.2 |
| hux | 29:8eb46b976f0f | 16 | // blink(" x xxx x ",0.5); repeating blink sequence, interval = 0.5 |
| hux | 29:8eb46b976f0f | 17 | // blink(0); stops blinking sequence |
| hux | 29:8eb46b976f0f | 18 | // |
| hux | 29:8eb46b976f0f | 19 | |
| hux | 29:8eb46b976f0f | 20 | #include "bricks/target.h" |
| hux | 29:8eb46b976f0f | 21 | #include "bricks/blink.h" |
| hux | 29:8eb46b976f0f | 22 | |
| hux | 29:8eb46b976f0f | 23 | #ifndef LED_INVERTED |
| hux | 29:8eb46b976f0f | 24 | # define LED_ON 1 |
| hux | 29:8eb46b976f0f | 25 | # define LED_OFF 0 |
| hux | 29:8eb46b976f0f | 26 | #else |
| hux | 29:8eb46b976f0f | 27 | # define LED_ON 0 |
| hux | 29:8eb46b976f0f | 28 | # define LED_OFF 1 |
| hux | 29:8eb46b976f0f | 29 | #endif |
| hux | 29:8eb46b976f0f | 30 | |
| hux | 29:8eb46b976f0f | 31 | static DigitalOut led(LED1); // LED1, being used for morse sequence |
| hux | 29:8eb46b976f0f | 32 | static Ticker ticker; // triggers periodic callbacks |
| hux | 29:8eb46b976f0f | 33 | static const char *pointer = 0; // 0 means morse activity disabled |
| hux | 29:8eb46b976f0f | 34 | static const char *sequence = 0; // next morse sequence for repeats |
| hux | 29:8eb46b976f0f | 35 | |
| hux | 29:8eb46b976f0f | 36 | |
| hux | 29:8eb46b976f0f | 37 | void morse(const char *pattern, double interval) |
| hux | 29:8eb46b976f0f | 38 | { |
| hux | 29:8eb46b976f0f | 39 | pointer = 0; // disable ticker based blinking |
| hux | 29:8eb46b976f0f | 40 | sequence = 0; // set also empty sequence |
| hux | 29:8eb46b976f0f | 41 | |
| hux | 29:8eb46b976f0f | 42 | for (; *pattern; pattern++) |
| hux | 29:8eb46b976f0f | 43 | { |
| hux | 29:8eb46b976f0f | 44 | led = (*pattern == ' ') ? LED_OFF : LED_ON; |
| hux | 29:8eb46b976f0f | 45 | wait(interval); // busy waiting for interval time |
| hux | 29:8eb46b976f0f | 46 | } |
| hux | 29:8eb46b976f0f | 47 | } |
| hux | 29:8eb46b976f0f | 48 | |
| hux | 29:8eb46b976f0f | 49 | // callback for LED1 ticker controlled blinking |
| hux | 29:8eb46b976f0f | 50 | |
| hux | 29:8eb46b976f0f | 51 | static void callback(void) |
| hux | 29:8eb46b976f0f | 52 | { |
| hux | 29:8eb46b976f0f | 53 | if (pointer != 0) |
| hux | 29:8eb46b976f0f | 54 | { |
| hux | 29:8eb46b976f0f | 55 | if (*pointer == 0) |
| hux | 29:8eb46b976f0f | 56 | { |
| hux | 29:8eb46b976f0f | 57 | pointer = sequence; // reset pointer to followup sequence |
| hux | 29:8eb46b976f0f | 58 | } |
| hux | 29:8eb46b976f0f | 59 | |
| hux | 29:8eb46b976f0f | 60 | if (*pointer) |
| hux | 29:8eb46b976f0f | 61 | { |
| hux | 29:8eb46b976f0f | 62 | led = (*pointer++ == ' ') ? LED_OFF : LED_ON; |
| hux | 29:8eb46b976f0f | 63 | } |
| hux | 29:8eb46b976f0f | 64 | } |
| hux | 29:8eb46b976f0f | 65 | } |
| hux | 29:8eb46b976f0f | 66 | |
| hux | 29:8eb46b976f0f | 67 | void blink(const char *pattern, const char* next, double interval) |
| hux | 29:8eb46b976f0f | 68 | { |
| hux | 29:8eb46b976f0f | 69 | pointer = 0; // stop current activities |
| hux | 29:8eb46b976f0f | 70 | led = LED_OFF; // reset led with LED_OFF |
| hux | 29:8eb46b976f0f | 71 | |
| hux | 29:8eb46b976f0f | 72 | sequence = next; // init morse sequence |
| hux | 29:8eb46b976f0f | 73 | |
| hux | 29:8eb46b976f0f | 74 | ticker.attach(callback,interval); // next LED state after every interval |
| hux | 29:8eb46b976f0f | 75 | pointer = pattern; // enable callback activty |
| hux | 29:8eb46b976f0f | 76 | } |
| hux | 29:8eb46b976f0f | 77 | |
| hux | 29:8eb46b976f0f | 78 | void blink(const char *pattern, double interval) |
| hux | 29:8eb46b976f0f | 79 | { |
| hux | 29:8eb46b976f0f | 80 | blink(pattern,pattern,interval); |
| hux | 29:8eb46b976f0f | 81 | } |
| hux | 29:8eb46b976f0f | 82 |
