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