Tutorial 2 - SOS: This is an embedded program which controls LED1 in order to show a blinking SOS pattern. The tutorial starts to organize helper functions like "morse" in a brick library which will grow and grow with each tutorial. Platform specifics are identified in "bricks/platform.h" by setting proper defines (like LED_INVERTED), which are used to achieve platform independent behaviour. The program has been tested on Nordic nRF51-DK and STM NUCLEO-F303K8, NUCLEO-F401RE and NUCLEO-L476RG.
Revision 1:10299215b49e, committed 2016-12-10
- Comitter:
- hux
- Date:
- Sat Dec 10 18:34:23 2016 +0000
- Parent:
- 0:9bbb539e0614
- Commit message:
- Some fine tuning; readme file added
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Readme.md Sat Dec 10 18:34:23 2016 +0000 @@ -0,0 +1,31 @@ +T02_SOS - Blinking LED according to Morse SOS pattern + +This demo program runs a repeated SOS morse pattern on LED1. +Helper function morse() is implemented as part of the bricks library (files +bricks/blink.cpp, bricks/blink.h and bricks/target.h. + +We need a special header target.h with platformtarget specific definitions. +For example on a NUCLEO board LED1 needs to be assigned with a value of 1 for ON +and 0 for OFF. On a nRF51 board it is exactly the oposite. + +In order to achieve opposite behavour the header target.h defines conditionally +the define LED_INVERTED (only if nRF51 platform board is selected), which is +processed in blink.cpp to proper definitions for LED_ON and LED_OFF. + +See blink.cpp: + +#ifndef LED_INVERTED +# define LED_ON 1 +# define LED_OFF 0 +#else +# define LED_ON 0 +# define LED_OFF 1 +#endif + +To make it running the libraries BLE_API and X_NUCLEO_IDB0XA1 have to be +updated (e.g. to be copied from a working NUCLEO BLE program). + +Tested Boards: + NUCLEO-L476RG, NUCLEO-F303K8, NUCLEO-F401RE + nRF51-DK + \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bricks/blink.cpp Sat Dec 10 18:34:23 2016 +0000 @@ -0,0 +1,32 @@ +// 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 +// + +#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 + + void morse(const char *pattern, double interval) + { + for (; *pattern; pattern++) + { + led = (*pattern == ' ') ? LED_OFF : LED_ON; + wait(interval); // busy waiting for interval time + } + } + + \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bricks/blink.h Sat Dec 10 18:34:23 2016 +0000 @@ -0,0 +1,9 @@ +// blink.h - blinking sequences for LED1 +#ifndef _BLINK_H_ +#define _BLINK_H_ + +#include <mbed.h> + + void morse(const char *pattern, double periode = 0.2); + +#endif // _BLINK_H_ \ No newline at end of file
--- a/bricks/morse.cpp Thu Dec 08 08:07:23 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,20 +0,0 @@ -// morse.cpp - send a morse pattern to LED1 - -#include "bricks/platform.h" -#include "bricks/morse.h" - -#ifdef NUCLEO -# define LED_ON 0 -#else -# define LED_ON 1 -#endif - -void morse(double periode, char *pattern) -{ - DigitalOut led(LED1); - - while (*pattern) { - led = (*pattern++ == ' ') ? LED_ON : !LED_ON; - wait(periode); - } -} \ No newline at end of file
--- a/bricks/morse.h Thu Dec 08 08:07:23 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,9 +0,0 @@ -// morse.h - send a morse pattern to LED1 -#ifndef _MORSE_H_ -#define _MORSE_H_ - -#include <mbed.h> - -void morse(double periode, char *pattern); - -#endif // _MORSE_H_ \ No newline at end of file
--- a/bricks/platform.h Thu Dec 08 08:07:23 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,11 +0,0 @@ -// platform.h - define platform specifics - -#ifndef _PLATFORM_H_ -#define _PLATFORM_H_ - -// platform specific defines - comment out platforms which are not used - -//#define NORDIC -#define NUCLEO - -#endif // _PLATFORM_H_
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bricks/target.h Sat Dec 10 18:34:23 2016 +0000 @@ -0,0 +1,19 @@ +// bricks/target.h - define target specifics + +#ifndef _BRICKS_TARGET_H_ +#define _BRICKS_TARGET_H_ + +// platform specific defines - comment out platforms which are not used +// We work only with two kinds of platform: +// +// a) a NUCLEO platform from STMicroelectronics +// b) a nRF51 platform from Nordic Semiconductor +// +// If we have not a nRF51 target then we coclude it is a NUCLEO target. This is +// not really clean, but under above assumptions it works so far! + +#ifdef TARGET_NRF51822 +# define LED_INVERTED // inverted LED for nRF51 targets +#endif + +#endif // _BRICKS_TARGET_H_
--- a/main.cpp Thu Dec 08 08:07:23 2016 +0000 +++ b/main.cpp Sat Dec 10 18:34:23 2016 +0000 @@ -1,10 +1,12 @@ // SOS - let LED1 blink according to a SOS pattern // Tested with nRF51-DK, NUCLEO-F401RE, NUCLEO-L476RG, NUCLEO-F303K8 -#include "bricks/morse.h" +#include "bricks/blink.h" -main() -{ - for(;;) - morse(0.2,"x x x xxx xxx xxx x x x "); -} + main() + { + for(;;) + { + morse("x x x xxx xxx xxx x x x "); // morse 'SOS' + } + }