Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Task633Solution-mbedos54 by
main.cpp@6:2e463846b575, 2016-03-14 (annotated)
- Committer:
- noutram
- Date:
- Mon Mar 14 16:30:25 2016 +0000
- Revision:
- 6:2e463846b575
- Parent:
- 5:31707531f715
- Child:
- 7:cd015e83995a
Comparing a yielding wait and a spinning-wait
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| noutram | 0:f916cefba2f4 | 1 | #include "mbed.h" |
| noutram | 0:f916cefba2f4 | 2 | #include "rtos.h" |
| noutram | 2:70084af839d3 | 3 | #include "string.h" |
| noutram | 2:70084af839d3 | 4 | #include <stdio.h> |
| noutram | 2:70084af839d3 | 5 | #include <ctype.h> |
| noutram | 0:f916cefba2f4 | 6 | |
| noutram | 4:dae8898e55fe | 7 | #define DELAY 200 |
| noutram | 0:f916cefba2f4 | 8 | |
| noutram | 0:f916cefba2f4 | 9 | //Digital outputs |
| noutram | 0:f916cefba2f4 | 10 | DigitalOut onBoardLED(LED1); |
| noutram | 0:f916cefba2f4 | 11 | DigitalOut redLED(D7); |
| noutram | 0:f916cefba2f4 | 12 | DigitalOut yellowLED(D6); |
| noutram | 0:f916cefba2f4 | 13 | DigitalOut greenLED(D5); |
| noutram | 0:f916cefba2f4 | 14 | |
| noutram | 2:70084af839d3 | 15 | //Serial Interface |
| noutram | 2:70084af839d3 | 16 | Serial pc(USBTX, USBRX); |
| noutram | 2:70084af839d3 | 17 | |
| noutram | 0:f916cefba2f4 | 18 | //Digital inputs |
| noutram | 0:f916cefba2f4 | 19 | DigitalIn onBoardSwitch(USER_BUTTON); |
| noutram | 0:f916cefba2f4 | 20 | DigitalIn SW1(D4); |
| noutram | 0:f916cefba2f4 | 21 | DigitalIn SW2(D3); |
| noutram | 0:f916cefba2f4 | 22 | |
| noutram | 0:f916cefba2f4 | 23 | //Thread ID for the Main function (CMSIS API) |
| noutram | 0:f916cefba2f4 | 24 | osThreadId tidMain; |
| noutram | 0:f916cefba2f4 | 25 | |
| noutram | 4:dae8898e55fe | 26 | void thread1( const void* arg ) |
| noutram | 4:dae8898e55fe | 27 | { |
| noutram | 4:dae8898e55fe | 28 | pc.printf("Entering thread 1\n"); |
| noutram | 4:dae8898e55fe | 29 | while (true) { |
| noutram | 5:31707531f715 | 30 | yellowLED = 1; |
| noutram | 6:2e463846b575 | 31 | Thread::wait(DELAY); |
| noutram | 5:31707531f715 | 32 | yellowLED = 0; |
| noutram | 6:2e463846b575 | 33 | Thread::wait(DELAY); |
| noutram | 4:dae8898e55fe | 34 | } |
| noutram | 2:70084af839d3 | 35 | } |
| noutram | 2:70084af839d3 | 36 | |
| noutram | 6:2e463846b575 | 37 | //This thread has higher priority |
| noutram | 4:dae8898e55fe | 38 | void thread2( const void* arg ) |
| noutram | 0:f916cefba2f4 | 39 | { |
| noutram | 4:dae8898e55fe | 40 | pc.printf("Entering thread 2\n"); |
| noutram | 2:70084af839d3 | 41 | while (true) { |
| noutram | 5:31707531f715 | 42 | redLED = 1; |
| noutram | 6:2e463846b575 | 43 | if (SW1 == 1) { |
| noutram | 6:2e463846b575 | 44 | //wait_ms(osWaitForever); |
| noutram | 6:2e463846b575 | 45 | Thread::wait(osWaitForever); |
| noutram | 6:2e463846b575 | 46 | } else { |
| noutram | 6:2e463846b575 | 47 | Thread::wait(DELAY); |
| noutram | 6:2e463846b575 | 48 | } |
| noutram | 5:31707531f715 | 49 | redLED = 0; |
| noutram | 6:2e463846b575 | 50 | Thread::wait(DELAY); |
| noutram | 6:2e463846b575 | 51 | } |
| noutram | 0:f916cefba2f4 | 52 | } |
| noutram | 0:f916cefba2f4 | 53 | |
| noutram | 4:dae8898e55fe | 54 | |
| noutram | 0:f916cefba2f4 | 55 | //Main thread |
| noutram | 0:f916cefba2f4 | 56 | int main() { |
| noutram | 0:f916cefba2f4 | 57 | redLED = 0; |
| noutram | 0:f916cefba2f4 | 58 | yellowLED = 0; |
| noutram | 2:70084af839d3 | 59 | greenLED = 0; |
| noutram | 4:dae8898e55fe | 60 | |
| noutram | 0:f916cefba2f4 | 61 | //Main thread ID |
| noutram | 0:f916cefba2f4 | 62 | tidMain = Thread::gettid(); |
| noutram | 0:f916cefba2f4 | 63 | |
| noutram | 4:dae8898e55fe | 64 | //Threads |
| noutram | 6:2e463846b575 | 65 | Thread t1(thread1, NULL, osPriorityNormal); |
| noutram | 6:2e463846b575 | 66 | //Thread t2(thread2, NULL, osPriorityNormal); |
| noutram | 6:2e463846b575 | 67 | Thread t2(thread2, NULL, osPriorityAboveNormal); |
| noutram | 1:4fb27aea76b2 | 68 | |
| noutram | 4:dae8898e55fe | 69 | pc.printf("Main Thread\n"); |
| noutram | 2:70084af839d3 | 70 | while (true) { |
| noutram | 4:dae8898e55fe | 71 | Thread::wait(osWaitForever); |
| noutram | 0:f916cefba2f4 | 72 | } |
| noutram | 0:f916cefba2f4 | 73 | |
| noutram | 0:f916cefba2f4 | 74 | } |
| noutram | 2:70084af839d3 | 75 | |
| noutram | 2:70084af839d3 | 76 |