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