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