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@11:80f2c19ecbce, 2017-01-07 (annotated)
- Committer:
- hux
- Date:
- Sat Jan 07 23:48:53 2017 +0000
- Revision:
- 11:80f2c19ecbce
- Child:
- 12:0d0ca44397dd
New Blob class derived from BLE class
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 | 11:80f2c19ecbce | 6 | // morse(" x xxx x "); send one time morse sequence, interval = 0.2 |
hux | 11:80f2c19ecbce | 7 | // morse(" x xxx x ",0.5); send one time morse sequence, interval = 0.5 |
hux | 11:80f2c19ecbce | 8 | // |
hux | 11:80f2c19ecbce | 9 | // Function morse() 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 | 11:80f2c19ecbce | 15 | // blink(" x xxx x "); repeating blink sequence, interval = 0.2 |
hux | 11:80f2c19ecbce | 16 | // blink(" x xxx x ",0.5); repeating blink sequence, interval = 0.5 |
hux | 11:80f2c19ecbce | 17 | // blink(0); 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 | 11:80f2c19ecbce | 31 | static DigitalOut led(LED1); // LED1, being used for morse sequence |
hux | 11:80f2c19ecbce | 32 | static Ticker ticker; // triggers periodic callbacks |
hux | 11:80f2c19ecbce | 33 | static const char *pointer = 0; // 0 means morse activity disabled |
hux | 11:80f2c19ecbce | 34 | static const char *sequence = 0; // next morse sequence for repeats |
hux | 11:80f2c19ecbce | 35 | |
hux | 11:80f2c19ecbce | 36 | |
hux | 11:80f2c19ecbce | 37 | void morse(const char *pattern, double interval) |
hux | 11:80f2c19ecbce | 38 | { |
hux | 11:80f2c19ecbce | 39 | pointer = 0; // disable ticker based blinking |
hux | 11:80f2c19ecbce | 40 | sequence = 0; // set also empty sequence |
hux | 11:80f2c19ecbce | 41 | |
hux | 11:80f2c19ecbce | 42 | for (; *pattern; pattern++) |
hux | 11:80f2c19ecbce | 43 | { |
hux | 11:80f2c19ecbce | 44 | led = (*pattern == ' ') ? LED_OFF : LED_ON; |
hux | 11:80f2c19ecbce | 45 | wait(interval); // busy waiting for interval time |
hux | 11:80f2c19ecbce | 46 | } |
hux | 11:80f2c19ecbce | 47 | } |
hux | 11:80f2c19ecbce | 48 | |
hux | 11:80f2c19ecbce | 49 | // callback for LED1 ticker controlled blinking |
hux | 11:80f2c19ecbce | 50 | |
hux | 11:80f2c19ecbce | 51 | static void callback(void) |
hux | 11:80f2c19ecbce | 52 | { |
hux | 11:80f2c19ecbce | 53 | if (pointer != 0) |
hux | 11:80f2c19ecbce | 54 | { |
hux | 11:80f2c19ecbce | 55 | if (*pointer == 0) |
hux | 11:80f2c19ecbce | 56 | { |
hux | 11:80f2c19ecbce | 57 | pointer = sequence; // reset pointer to followup sequence |
hux | 11:80f2c19ecbce | 58 | } |
hux | 11:80f2c19ecbce | 59 | |
hux | 11:80f2c19ecbce | 60 | if (*pointer) |
hux | 11:80f2c19ecbce | 61 | { |
hux | 11:80f2c19ecbce | 62 | led = (*pointer++ == ' ') ? LED_OFF : LED_ON; |
hux | 11:80f2c19ecbce | 63 | } |
hux | 11:80f2c19ecbce | 64 | } |
hux | 11:80f2c19ecbce | 65 | } |
hux | 11:80f2c19ecbce | 66 | |
hux | 11:80f2c19ecbce | 67 | void blink(const char *pattern, const char* next, double interval) |
hux | 11:80f2c19ecbce | 68 | { |
hux | 11:80f2c19ecbce | 69 | pointer = 0; // stop current activities |
hux | 11:80f2c19ecbce | 70 | led = LED_OFF; // reset led with LED_OFF |
hux | 11:80f2c19ecbce | 71 | |
hux | 11:80f2c19ecbce | 72 | sequence = next; // init morse sequence |
hux | 11:80f2c19ecbce | 73 | |
hux | 11:80f2c19ecbce | 74 | ticker.attach(callback,interval); // next LED state after every interval |
hux | 11:80f2c19ecbce | 75 | pointer = pattern; // enable callback activty |
hux | 11:80f2c19ecbce | 76 | } |
hux | 11:80f2c19ecbce | 77 | |
hux | 11:80f2c19ecbce | 78 | void blink(const char *pattern, double interval) |
hux | 11:80f2c19ecbce | 79 | { |
hux | 11:80f2c19ecbce | 80 | blink(pattern,pattern,interval); |
hux | 11:80f2c19ecbce | 81 | } |
hux | 11:80f2c19ecbce | 82 |