Task 6.1.3 Starting point updated for mbed OS 5.4
Fork of Task613-mbedos54 by
main.cpp@1:cf2510fcf09a, 2017-03-30 (annotated)
- Committer:
- noutram
- Date:
- Thu Mar 30 13:52:37 2017 +0000
- Revision:
- 1:cf2510fcf09a
- Parent:
- 0:1f4d20070876
updated for mbed OS 5.4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 0:1f4d20070876 | 1 | #include "mbed.h" |
noutram | 0:1f4d20070876 | 2 | |
noutram | 0:1f4d20070876 | 3 | #define RED_ON 1 |
noutram | 0:1f4d20070876 | 4 | #define YELLOW_ON 2 |
noutram | 0:1f4d20070876 | 5 | #define GREEN_ON 4 |
noutram | 0:1f4d20070876 | 6 | #define ALL_ON 7 |
noutram | 0:1f4d20070876 | 7 | |
noutram | 0:1f4d20070876 | 8 | //Function declarations |
noutram | 1:cf2510fcf09a | 9 | void Function1(); |
noutram | 1:cf2510fcf09a | 10 | void Function2(); |
noutram | 1:cf2510fcf09a | 11 | void Function3(); |
noutram | 1:cf2510fcf09a | 12 | void Function4(); |
noutram | 0:1f4d20070876 | 13 | |
noutram | 0:1f4d20070876 | 14 | //I/O |
noutram | 0:1f4d20070876 | 15 | DigitalOut onBoardLED(LED1); |
noutram | 0:1f4d20070876 | 16 | DigitalOut redLED(D7); |
noutram | 0:1f4d20070876 | 17 | DigitalOut yellowLED(D6); |
noutram | 0:1f4d20070876 | 18 | DigitalOut greenLED(D5); |
noutram | 0:1f4d20070876 | 19 | |
noutram | 0:1f4d20070876 | 20 | DigitalIn onBoardSwitch(USER_BUTTON); |
noutram | 0:1f4d20070876 | 21 | DigitalIn SW1(D4); |
noutram | 0:1f4d20070876 | 22 | DigitalIn SW2(D3); |
noutram | 0:1f4d20070876 | 23 | |
noutram | 1:cf2510fcf09a | 24 | Thread t1; |
noutram | 1:cf2510fcf09a | 25 | Thread t2; |
noutram | 1:cf2510fcf09a | 26 | Thread t3; |
noutram | 1:cf2510fcf09a | 27 | Thread t4; |
noutram | 0:1f4d20070876 | 28 | |
noutram | 0:1f4d20070876 | 29 | //Thread ID |
noutram | 0:1f4d20070876 | 30 | osThreadId idMain; |
noutram | 0:1f4d20070876 | 31 | osThreadId id1; |
noutram | 0:1f4d20070876 | 32 | osThreadId id2; |
noutram | 0:1f4d20070876 | 33 | osThreadId id3; |
noutram | 0:1f4d20070876 | 34 | osThreadId id4; |
noutram | 0:1f4d20070876 | 35 | |
noutram | 1:cf2510fcf09a | 36 | void Function1() |
noutram | 0:1f4d20070876 | 37 | { |
noutram | 0:1f4d20070876 | 38 | while (true) { |
noutram | 0:1f4d20070876 | 39 | redLED = !redLED; |
noutram | 0:1f4d20070876 | 40 | if (redLED == 1) { |
noutram | 1:cf2510fcf09a | 41 | t4.signal_set(RED_ON); |
noutram | 0:1f4d20070876 | 42 | } |
noutram | 0:1f4d20070876 | 43 | |
noutram | 0:1f4d20070876 | 44 | Thread::wait(5100); |
noutram | 0:1f4d20070876 | 45 | } |
noutram | 0:1f4d20070876 | 46 | } |
noutram | 0:1f4d20070876 | 47 | |
noutram | 1:cf2510fcf09a | 48 | void Function2() |
noutram | 0:1f4d20070876 | 49 | { |
noutram | 0:1f4d20070876 | 50 | while (true) { |
noutram | 0:1f4d20070876 | 51 | yellowLED = !yellowLED; |
noutram | 0:1f4d20070876 | 52 | if (yellowLED == 1) { |
noutram | 1:cf2510fcf09a | 53 | t4.signal_set(YELLOW_ON); |
noutram | 0:1f4d20070876 | 54 | } |
noutram | 0:1f4d20070876 | 55 | Thread::wait(1900); |
noutram | 0:1f4d20070876 | 56 | } |
noutram | 0:1f4d20070876 | 57 | } |
noutram | 0:1f4d20070876 | 58 | |
noutram | 0:1f4d20070876 | 59 | //Green Flashing |
noutram | 1:cf2510fcf09a | 60 | void Function3() |
noutram | 0:1f4d20070876 | 61 | { |
noutram | 0:1f4d20070876 | 62 | while (true) { |
noutram | 0:1f4d20070876 | 63 | greenLED = !greenLED; |
noutram | 0:1f4d20070876 | 64 | if (greenLED == 1) { |
noutram | 1:cf2510fcf09a | 65 | t4.signal_set(GREEN_ON); |
noutram | 0:1f4d20070876 | 66 | } |
noutram | 0:1f4d20070876 | 67 | Thread::wait(700); |
noutram | 0:1f4d20070876 | 68 | } |
noutram | 0:1f4d20070876 | 69 | } |
noutram | 0:1f4d20070876 | 70 | |
noutram | 0:1f4d20070876 | 71 | //This function waits for signals from all other threads |
noutram | 1:cf2510fcf09a | 72 | void Function4() |
noutram | 0:1f4d20070876 | 73 | { |
noutram | 0:1f4d20070876 | 74 | while (true) { |
noutram | 0:1f4d20070876 | 75 | |
noutram | 0:1f4d20070876 | 76 | Thread::signal_wait(RED_ON); |
noutram | 0:1f4d20070876 | 77 | printf("Red\n"); |
noutram | 0:1f4d20070876 | 78 | Thread::signal_wait(YELLOW_ON); |
noutram | 0:1f4d20070876 | 79 | printf("Yellow\n"); |
noutram | 0:1f4d20070876 | 80 | Thread::signal_wait(GREEN_ON); |
noutram | 0:1f4d20070876 | 81 | printf("Green\n"); |
noutram | 0:1f4d20070876 | 82 | //Signal main thread |
noutram | 0:1f4d20070876 | 83 | osSignalSet(idMain, ALL_ON); |
noutram | 0:1f4d20070876 | 84 | } |
noutram | 0:1f4d20070876 | 85 | } |
noutram | 0:1f4d20070876 | 86 | |
noutram | 0:1f4d20070876 | 87 | //Main thread |
noutram | 0:1f4d20070876 | 88 | int main() { |
noutram | 0:1f4d20070876 | 89 | redLED = 1; |
noutram | 0:1f4d20070876 | 90 | yellowLED = 0; |
noutram | 0:1f4d20070876 | 91 | greenLED = 0; |
noutram | 0:1f4d20070876 | 92 | |
noutram | 0:1f4d20070876 | 93 | //Main thread ID |
noutram | 0:1f4d20070876 | 94 | idMain = osThreadGetId(); //CMSIS RTOS call |
noutram | 0:1f4d20070876 | 95 | |
noutram | 1:cf2510fcf09a | 96 | //Run threads |
noutram | 1:cf2510fcf09a | 97 | t4.start(Function4); |
noutram | 1:cf2510fcf09a | 98 | t1.start(Function1); |
noutram | 1:cf2510fcf09a | 99 | t2.start(Function2); |
noutram | 1:cf2510fcf09a | 100 | t3.start(Function3); |
noutram | 0:1f4d20070876 | 101 | |
noutram | 0:1f4d20070876 | 102 | |
noutram | 0:1f4d20070876 | 103 | //Thread ID |
noutram | 1:cf2510fcf09a | 104 | id1 = t1.gettid(); |
noutram | 1:cf2510fcf09a | 105 | id2 = t2.gettid(); |
noutram | 1:cf2510fcf09a | 106 | id3 = t3.gettid(); |
noutram | 1:cf2510fcf09a | 107 | id4 = t4.gettid(); |
noutram | 0:1f4d20070876 | 108 | |
noutram | 0:1f4d20070876 | 109 | while(1) { |
noutram | 0:1f4d20070876 | 110 | //Wait for the ALL_ON signal |
noutram | 0:1f4d20070876 | 111 | osSignalWait(ALL_ON,osWaitForever); |
noutram | 0:1f4d20070876 | 112 | printf("ALL\n"); |
noutram | 0:1f4d20070876 | 113 | } |
noutram | 0:1f4d20070876 | 114 | } |