Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API X_NUCLEO_IDB0XA1 mbed
Fork of BLE_HeartRate_IDB0XA1 by
Diff: bricks/blink.cpp
- Revision:
- 22:e82c7b8a6072
- Child:
- 27:09ec26511db8
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/bricks/blink.cpp Sat Dec 10 20:32:35 2016 +0000
@@ -0,0 +1,82 @@
+// blink.cpp - send a morse pattern to LED1
+//
+// Function morse() is one way for running LED1 with a blinking sequence using
+// a busy wait, until the sequence is completed.
+//
+// morse(" x xxx x "); send one time morse sequence, interval = 0.2
+// morse(" x xxx x ",0.5); send one time morse sequence, interval = 0.5
+//
+// Function morse() got the additional feature to stop an ongoing timer based
+// blinking sequence.
+//
+// The alternative is to setup an ever repeating blink sequence via LED1 using
+// function blinking(), which is non waiting.
+//
+// blinking(" x xxx x "); repeating blink sequence, interval = 0.2
+// blinking(" x xxx x ",0.5); repeating blink sequence, interval = 0.5
+// blinking(0); stops blinking sequence
+//
+
+#include "bricks/target.h"
+#include "bricks/blink.h"
+
+#ifndef LED_INVERTED
+# define LED_ON 1
+# define LED_OFF 0
+#else
+# define LED_ON 0
+# define LED_OFF 1
+#endif
+
+ static DigitalOut led(LED1); // LED1, being used for morse sequence
+ static Ticker ticker; // triggers periodic callbacks
+ static const char *pointer = 0; // 0 means morse activity disabled
+ static const char *sequence = 0; // next morse sequence for repeats
+
+
+ void morse(const char *pattern, double interval)
+ {
+ pointer = 0; // disable ticker based blinking
+ sequence = 0; // set also empty sequence
+
+ for (; *pattern; pattern++)
+ {
+ led = (*pattern == ' ') ? LED_OFF : LED_ON;
+ wait(interval); // busy waiting for interval time
+ }
+ }
+
+// callback for LED1 ticker controlled blinking
+
+ static void callback(void)
+ {
+ if (pointer != 0)
+ {
+ if (*pointer == 0)
+ {
+ pointer = sequence; // reset pointer to followup sequence
+ }
+
+ if (*pointer)
+ {
+ led = (*pointer++ == ' ') ? LED_OFF : LED_ON;
+ }
+ }
+ }
+
+ void blinking(const char *pattern, const char* next, double interval)
+ {
+ pointer = 0; // stop current activities
+ led = LED_OFF; // reset led with LED_OFF
+
+ sequence = next; // init morse sequence
+
+ ticker.attach(callback,interval); // next LED state after every interval
+ pointer = pattern; // enable callback activty
+ }
+
+ void blinking(const char *pattern, double interval)
+ {
+ blinking(pattern,pattern,interval);
+ }
+
\ No newline at end of file
