![](/media/cache/profiles/8631e5b91354a846f00dbbf16ad94259.jpg.50x50_q85.jpg)
Thread example using mbed 5.7
main.cpp@1:bbbe0fae16f3, 2019-01-09 (annotated)
- Committer:
- WilliamMarshQMUL
- Date:
- Wed Jan 09 22:58:08 2019 +0000
- Revision:
- 1:bbbe0fae16f3
- Parent:
- 0:83abbbeb9a3d
- Child:
- 2:b13b343de0f5
Wait API change
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 | 1:bbbe0fae16f3 | 15 | wait(0.25); |
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 | 1:bbbe0fae16f3 | 29 | wait(0.4); |
WilliamMarshQMUL | 0:83abbbeb9a3d | 30 | ledA = !ledA; |
WilliamMarshQMUL | 0:83abbbeb9a3d | 31 | } |
WilliamMarshQMUL | 0:83abbbeb9a3d | 32 | } |