deni setiawan
/
thread-example2-5
Lab 1 Number 5
Fork of thread-example2 by
main.cpp@0:83abbbeb9a3d, 2018-01-13 (annotated)
- Committer:
- WilliamMarshQMUL
- Date:
- Sat Jan 13 18:26:10 2018 +0000
- Revision:
- 0:83abbbeb9a3d
- Child:
- 1:a542052a259f
Thread example using mbed 5.7
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
WilliamMarshQMUL | 0:83abbbeb9a3d | 1 | // Simple Example fo use of Threads on mbed OS 5 |
WilliamMarshQMUL | 0:83abbbeb9a3d | 2 | // Only need to include mbed.h |
WilliamMarshQMUL | 0:83abbbeb9a3d | 3 | |
WilliamMarshQMUL | 0:83abbbeb9a3d | 4 | #include "mbed.h" |
WilliamMarshQMUL | 0:83abbbeb9a3d | 5 | |
WilliamMarshQMUL | 0:83abbbeb9a3d | 6 | // Variable for a second thread |
WilliamMarshQMUL | 0:83abbbeb9a3d | 7 | Thread thread; |
WilliamMarshQMUL | 0:83abbbeb9a3d | 8 | |
WilliamMarshQMUL | 0:83abbbeb9a3d | 9 | DigitalOut ledA(LED1); // Red LED |
WilliamMarshQMUL | 0:83abbbeb9a3d | 10 | DigitalOut ledB(LED2); // Green LED |
WilliamMarshQMUL | 0:83abbbeb9a3d | 11 | |
WilliamMarshQMUL | 0:83abbbeb9a3d | 12 | // This method is run in the second thread |
WilliamMarshQMUL | 0:83abbbeb9a3d | 13 | void ledB_thread() { |
WilliamMarshQMUL | 0:83abbbeb9a3d | 14 | while (true) { |
WilliamMarshQMUL | 0:83abbbeb9a3d | 15 | Thread::wait(250); |
WilliamMarshQMUL | 0:83abbbeb9a3d | 16 | ledB = !ledB; |
WilliamMarshQMUL | 0:83abbbeb9a3d | 17 | } |
WilliamMarshQMUL | 0:83abbbeb9a3d | 18 | } |
WilliamMarshQMUL | 0:83abbbeb9a3d | 19 | |
WilliamMarshQMUL | 0:83abbbeb9a3d | 20 | // This is the main thread |
WilliamMarshQMUL | 0:83abbbeb9a3d | 21 | int main (void) { |
WilliamMarshQMUL | 0:83abbbeb9a3d | 22 | ledA = 1 ; // off |
WilliamMarshQMUL | 0:83abbbeb9a3d | 23 | ledB = 1 ; // off |
WilliamMarshQMUL | 0:83abbbeb9a3d | 24 | |
WilliamMarshQMUL | 0:83abbbeb9a3d | 25 | // start the second thread |
WilliamMarshQMUL | 0:83abbbeb9a3d | 26 | thread.start(callback(ledB_thread)); |
WilliamMarshQMUL | 0:83abbbeb9a3d | 27 | |
WilliamMarshQMUL | 0:83abbbeb9a3d | 28 | while (true) { |
WilliamMarshQMUL | 0:83abbbeb9a3d | 29 | Thread::wait(400); |
WilliamMarshQMUL | 0:83abbbeb9a3d | 30 | ledA = !ledA; |
WilliamMarshQMUL | 0:83abbbeb9a3d | 31 | } |
WilliamMarshQMUL | 0:83abbbeb9a3d | 32 | } |