Hugo Pristauz / Mbed 2 deprecated S05_Advertising

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 mbed

Fork of BLE_HeartRate_IDB0XA1 by ST

Committer:
hux
Date:
Sun Dec 11 19:18:02 2016 +0000
Revision:
27:09ec26511db8
Parent:
22:e82c7b8a6072
Fine tuning of Blob class; looking very smart now!

Who changed what in which revision?

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