TOF based Presence Detector

Dependencies:   BLE_API X_NUCLEO_6180XA1 X_NUCLEO_IDB0XA1 mbed

Fork of BLE_HeartRate_IDB0XA1 by ST

Committer:
hux
Date:
Sun Dec 18 05:33:58 2016 +0000
Revision:
22:d467526abc4a
Child:
27:32267cee7cb8
some new stuff added - works now

Who changed what in which revision?

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