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