![](/media/cache/profiles/8631e5b91354a846f00dbbf16ad94259.jpg.50x50_q85.jpg)
Thread example using mbed 5.7
main.cpp@2:b13b343de0f5, 2020-01-23 (annotated)
- Committer:
- WilliamMarshQMUL
- Date:
- Thu Jan 23 13:05:26 2020 +0000
- Revision:
- 2:b13b343de0f5
- Parent:
- 1:bbbe0fae16f3
Change wait (depreciated) to sleep_for
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
WilliamMarshQMUL | 2:b13b343de0f5 | 1 | // Simple Example of use of Threads on mbed OS 5 |
WilliamMarshQMUL | 2:b13b343de0f5 | 2 | // Depreciated use of wait replaced by 'sleep_for' |
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 | 2:b13b343de0f5 | 15 | //wait(0.25); -- depreciated call |
WilliamMarshQMUL | 2:b13b343de0f5 | 16 | ThisThread::sleep_for(250) ; |
WilliamMarshQMUL | 0:83abbbeb9a3d | 17 | ledB = !ledB; |
WilliamMarshQMUL | 0:83abbbeb9a3d | 18 | } |
WilliamMarshQMUL | 0:83abbbeb9a3d | 19 | } |
WilliamMarshQMUL | 0:83abbbeb9a3d | 20 | |
WilliamMarshQMUL | 0:83abbbeb9a3d | 21 | // This is the main thread |
WilliamMarshQMUL | 0:83abbbeb9a3d | 22 | int main (void) { |
WilliamMarshQMUL | 0:83abbbeb9a3d | 23 | ledA = 1 ; // off |
WilliamMarshQMUL | 0:83abbbeb9a3d | 24 | ledB = 1 ; // off |
WilliamMarshQMUL | 0:83abbbeb9a3d | 25 | |
WilliamMarshQMUL | 0:83abbbeb9a3d | 26 | // start the second thread |
WilliamMarshQMUL | 0:83abbbeb9a3d | 27 | thread.start(callback(ledB_thread)); |
WilliamMarshQMUL | 0:83abbbeb9a3d | 28 | |
WilliamMarshQMUL | 0:83abbbeb9a3d | 29 | while (true) { |
WilliamMarshQMUL | 2:b13b343de0f5 | 30 | // wait(0.4); - depreciated call |
WilliamMarshQMUL | 2:b13b343de0f5 | 31 | ThisThread::sleep_for(400) ; |
WilliamMarshQMUL | 0:83abbbeb9a3d | 32 | ledA = !ledA; |
WilliamMarshQMUL | 0:83abbbeb9a3d | 33 | } |
WilliamMarshQMUL | 0:83abbbeb9a3d | 34 | } |