University of Plymouth - Stages 1, 2 and 3
/
Task613Solution-mbeds-FZ429
Updated for stage-3
Fork of Task613Solution-mbeds54 by
main.cpp@4:4d74762bf38b, 2019-11-08 (annotated)
- Committer:
- noutram
- Date:
- Fri Nov 08 10:45:29 2019 +0000
- Revision:
- 4:4d74762bf38b
- Parent:
- 3:ef46c1ddefed
2019
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 0:90e393878517 | 1 | #include "mbed.h" |
noutram | 0:90e393878517 | 2 | |
noutram | 0:90e393878517 | 3 | #define RED_OFF 1 |
noutram | 0:90e393878517 | 4 | #define YELLOW_OFF 2 |
noutram | 0:90e393878517 | 5 | #define GREEN_OFF 4 |
noutram | 0:90e393878517 | 6 | #define ALL_OFF 7 |
noutram | 0:90e393878517 | 7 | |
noutram | 0:90e393878517 | 8 | //Function declarations |
noutram | 1:948bd552a2a2 | 9 | void Function1(); |
noutram | 1:948bd552a2a2 | 10 | void Function2(); |
noutram | 1:948bd552a2a2 | 11 | void Function3(); |
noutram | 1:948bd552a2a2 | 12 | void Function4(); |
noutram | 0:90e393878517 | 13 | |
noutram | 4:4d74762bf38b | 14 | typedef struct { |
noutram | 4:4d74762bf38b | 15 | float temp; |
noutram | 4:4d74762bf38b | 16 | float press; |
noutram | 4:4d74762bf38b | 17 | float humid; |
noutram | 4:4d74762bf38b | 18 | } Measurement; |
noutram | 4:4d74762bf38b | 19 | |
noutram | 4:4d74762bf38b | 20 | Measurement buffer[120]; |
noutram | 4:4d74762bf38b | 21 | |
noutram | 4:4d74762bf38b | 22 | |
noutram | 0:90e393878517 | 23 | //I/O |
noutram | 0:90e393878517 | 24 | DigitalOut onBoardLED(LED1); |
noutram | 3:ef46c1ddefed | 25 | DigitalOut redLED(PE_15); |
noutram | 3:ef46c1ddefed | 26 | DigitalOut yellowLED(PB_10); |
noutram | 3:ef46c1ddefed | 27 | DigitalOut greenLED(PB_11); |
noutram | 0:90e393878517 | 28 | |
noutram | 0:90e393878517 | 29 | DigitalIn onBoardSwitch(USER_BUTTON); |
noutram | 3:ef46c1ddefed | 30 | DigitalIn SW1(PE_12); |
noutram | 3:ef46c1ddefed | 31 | DigitalIn SW2(PE_14); |
noutram | 0:90e393878517 | 32 | |
noutram | 1:948bd552a2a2 | 33 | Thread t1; |
noutram | 1:948bd552a2a2 | 34 | Thread t2; |
noutram | 1:948bd552a2a2 | 35 | Thread t3; |
noutram | 1:948bd552a2a2 | 36 | Thread t4; |
noutram | 0:90e393878517 | 37 | |
noutram | 0:90e393878517 | 38 | //Thread ID |
noutram | 0:90e393878517 | 39 | osThreadId idMain; |
noutram | 0:90e393878517 | 40 | osThreadId id1; |
noutram | 0:90e393878517 | 41 | osThreadId id2; |
noutram | 0:90e393878517 | 42 | osThreadId id3; |
noutram | 0:90e393878517 | 43 | osThreadId id4; |
noutram | 0:90e393878517 | 44 | |
noutram | 1:948bd552a2a2 | 45 | void Function1() |
noutram | 0:90e393878517 | 46 | { |
noutram | 0:90e393878517 | 47 | while (true) { |
noutram | 0:90e393878517 | 48 | redLED = !redLED; |
noutram | 0:90e393878517 | 49 | if (redLED == 0) { |
noutram | 4:4d74762bf38b | 50 | t2.flags_set(RED_OFF); |
noutram | 0:90e393878517 | 51 | } |
noutram | 0:90e393878517 | 52 | Thread::wait(1000); |
noutram | 0:90e393878517 | 53 | } |
noutram | 0:90e393878517 | 54 | } |
noutram | 0:90e393878517 | 55 | |
noutram | 1:948bd552a2a2 | 56 | void Function2() |
noutram | 0:90e393878517 | 57 | { |
noutram | 0:90e393878517 | 58 | while (true) { |
noutram | 4:4d74762bf38b | 59 | ThisThread::flags_wait_all(RED_OFF); |
noutram | 0:90e393878517 | 60 | yellowLED = !yellowLED; |
noutram | 0:90e393878517 | 61 | if (yellowLED == 0) { |
noutram | 4:4d74762bf38b | 62 | t3.flags_set(YELLOW_OFF); |
noutram | 0:90e393878517 | 63 | } |
noutram | 0:90e393878517 | 64 | } |
noutram | 0:90e393878517 | 65 | } |
noutram | 0:90e393878517 | 66 | |
noutram | 0:90e393878517 | 67 | //Green Flashing |
noutram | 1:948bd552a2a2 | 68 | void Function3() |
noutram | 0:90e393878517 | 69 | { |
noutram | 0:90e393878517 | 70 | while (true) { |
noutram | 4:4d74762bf38b | 71 | ThisThread::flags_wait_all(YELLOW_OFF); |
noutram | 0:90e393878517 | 72 | greenLED = !greenLED; |
noutram | 0:90e393878517 | 73 | if (greenLED == 0) { |
noutram | 4:4d74762bf38b | 74 | t4.flags_set(GREEN_OFF); |
noutram | 0:90e393878517 | 75 | } |
noutram | 0:90e393878517 | 76 | } |
noutram | 0:90e393878517 | 77 | } |
noutram | 0:90e393878517 | 78 | |
noutram | 0:90e393878517 | 79 | //This function waits for signals from all other threads |
noutram | 1:948bd552a2a2 | 80 | void Function4() |
noutram | 0:90e393878517 | 81 | { |
noutram | 0:90e393878517 | 82 | while (true) { |
noutram | 4:4d74762bf38b | 83 | ThisThread::flags_wait_all(GREEN_OFF); |
noutram | 0:90e393878517 | 84 | //Signal main thread |
noutram | 0:90e393878517 | 85 | osSignalSet(idMain, ALL_OFF); |
noutram | 0:90e393878517 | 86 | } |
noutram | 0:90e393878517 | 87 | } |
noutram | 0:90e393878517 | 88 | |
noutram | 0:90e393878517 | 89 | //Main thread |
noutram | 0:90e393878517 | 90 | int main() { |
noutram | 0:90e393878517 | 91 | redLED = 0; |
noutram | 0:90e393878517 | 92 | yellowLED = 0; |
noutram | 0:90e393878517 | 93 | greenLED = 0; |
noutram | 0:90e393878517 | 94 | |
noutram | 0:90e393878517 | 95 | //Main thread ID |
noutram | 0:90e393878517 | 96 | idMain = osThreadGetId(); //CMSIS RTOS call |
noutram | 0:90e393878517 | 97 | |
noutram | 1:948bd552a2a2 | 98 | // run threads |
noutram | 1:948bd552a2a2 | 99 | t4.start(Function4); |
noutram | 1:948bd552a2a2 | 100 | t1.start(Function1); |
noutram | 1:948bd552a2a2 | 101 | t2.start(Function2); |
noutram | 1:948bd552a2a2 | 102 | t3.start(Function3); |
noutram | 0:90e393878517 | 103 | |
noutram | 0:90e393878517 | 104 | |
noutram | 0:90e393878517 | 105 | //Thread ID |
noutram | 4:4d74762bf38b | 106 | // id1 = t1.gettid(); |
noutram | 4:4d74762bf38b | 107 | // id2 = t2.gettid(); |
noutram | 4:4d74762bf38b | 108 | // id3 = t3.gettid(); |
noutram | 4:4d74762bf38b | 109 | // id4 = t4.gettid(); |
noutram | 0:90e393878517 | 110 | |
noutram | 0:90e393878517 | 111 | while(1) { |
noutram | 0:90e393878517 | 112 | //Wait for the ALL_ON signal |
noutram | 0:90e393878517 | 113 | osSignalWait(ALL_OFF,osWaitForever); |
noutram | 0:90e393878517 | 114 | printf("ALL OFF\n"); |
noutram | 0:90e393878517 | 115 | } |
noutram | 0:90e393878517 | 116 | } |