Luca Mottola
/
AthensFall19-BlinkThreadCallback
Blink using Thread and Callback
main.cpp@0:13988e5bca16, 2018-11-08 (annotated)
- Committer:
- lmottola
- Date:
- Thu Nov 08 17:29:13 2018 +0000
- Revision:
- 0:13988e5bca16
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lmottola | 0:13988e5bca16 | 1 | #include "mbed.h" |
lmottola | 0:13988e5bca16 | 2 | |
lmottola | 0:13988e5bca16 | 3 | Thread thread; |
lmottola | 0:13988e5bca16 | 4 | DigitalOut led1(LED1); |
lmottola | 0:13988e5bca16 | 5 | volatile bool running = true; |
lmottola | 0:13988e5bca16 | 6 | |
lmottola | 0:13988e5bca16 | 7 | // Blink function toggles the led in a long running loop |
lmottola | 0:13988e5bca16 | 8 | void blink(DigitalOut *led) { |
lmottola | 0:13988e5bca16 | 9 | while (running) { |
lmottola | 0:13988e5bca16 | 10 | *led = !*led; |
lmottola | 0:13988e5bca16 | 11 | wait(1); |
lmottola | 0:13988e5bca16 | 12 | } |
lmottola | 0:13988e5bca16 | 13 | } |
lmottola | 0:13988e5bca16 | 14 | |
lmottola | 0:13988e5bca16 | 15 | // Spawns a thread to run blink for 5 seconds |
lmottola | 0:13988e5bca16 | 16 | int main() { |
lmottola | 0:13988e5bca16 | 17 | thread.start(callback(blink, &led1)); |
lmottola | 0:13988e5bca16 | 18 | wait(5); |
lmottola | 0:13988e5bca16 | 19 | running = false; |
lmottola | 0:13988e5bca16 | 20 | thread.join(); |
lmottola | 0:13988e5bca16 | 21 | } |