Updated for ELEC350
Fork of Task612-mbedos54 by
main.cpp@3:de36dab8492f, 2019-11-08 (annotated)
- Committer:
- noutram
- Date:
- Fri Nov 08 10:10:14 2019 +0000
- Revision:
- 3:de36dab8492f
- Parent:
- 2:410eacc3006a
2019 - new API
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| noutram | 0:69af4e3595b6 | 1 | #include "mbed.h" |
| noutram | 0:69af4e3595b6 | 2 | |
| noutram | 0:69af4e3595b6 | 3 | #define RED_TOGGLE 1 |
| noutram | 0:69af4e3595b6 | 4 | #define YELLOW_TOGGLE 2 |
| noutram | 0:69af4e3595b6 | 5 | #define GREEN_TOGGLE 4 |
| noutram | 0:69af4e3595b6 | 6 | |
| noutram | 0:69af4e3595b6 | 7 | //Function declarations |
| noutram | 1:fdf9ceb4a107 | 8 | void FunctionRed(); |
| noutram | 1:fdf9ceb4a107 | 9 | void FunctionYellow(); |
| noutram | 1:fdf9ceb4a107 | 10 | void FunctionGreen(); |
| noutram | 0:69af4e3595b6 | 11 | |
| noutram | 0:69af4e3595b6 | 12 | //I/O |
| noutram | 0:69af4e3595b6 | 13 | DigitalOut onBoardLED(LED1); |
| noutram | 2:410eacc3006a | 14 | DigitalOut redLED(PE_15); |
| noutram | 2:410eacc3006a | 15 | DigitalOut yellowLED(PB_10); |
| noutram | 2:410eacc3006a | 16 | DigitalOut greenLED(PB_11); |
| noutram | 0:69af4e3595b6 | 17 | |
| noutram | 0:69af4e3595b6 | 18 | DigitalIn onBoardSwitch(USER_BUTTON); |
| noutram | 2:410eacc3006a | 19 | DigitalIn SW1(PE_12); |
| noutram | 2:410eacc3006a | 20 | DigitalIn SW2(PE_14); |
| noutram | 0:69af4e3595b6 | 21 | |
| noutram | 0:69af4e3595b6 | 22 | |
| noutram | 0:69af4e3595b6 | 23 | //Each of the following 3 functions is listening for a signal |
| noutram | 1:fdf9ceb4a107 | 24 | void FunctionRed() |
| noutram | 0:69af4e3595b6 | 25 | { |
| noutram | 0:69af4e3595b6 | 26 | while (true) { |
| noutram | 3:de36dab8492f | 27 | // Thread::signal_wait(RED_TOGGLE); //Now using new API |
| noutram | 3:de36dab8492f | 28 | ThisThread::flags_wait_any(RED_TOGGLE); |
| noutram | 0:69af4e3595b6 | 29 | redLED = !redLED; |
| noutram | 0:69af4e3595b6 | 30 | } |
| noutram | 0:69af4e3595b6 | 31 | } |
| noutram | 0:69af4e3595b6 | 32 | |
| noutram | 1:fdf9ceb4a107 | 33 | void FunctionYellow() |
| noutram | 0:69af4e3595b6 | 34 | { |
| noutram | 0:69af4e3595b6 | 35 | while (true) { |
| noutram | 3:de36dab8492f | 36 | // Thread::signal_wait(YELLOW_TOGGLE); |
| noutram | 3:de36dab8492f | 37 | ThisThread::flags_wait_any(YELLOW_TOGGLE); |
| noutram | 0:69af4e3595b6 | 38 | yellowLED = !yellowLED; |
| noutram | 0:69af4e3595b6 | 39 | } |
| noutram | 0:69af4e3595b6 | 40 | } |
| noutram | 0:69af4e3595b6 | 41 | |
| noutram | 1:fdf9ceb4a107 | 42 | void FunctionGreen() |
| noutram | 0:69af4e3595b6 | 43 | { |
| noutram | 0:69af4e3595b6 | 44 | while (true) { |
| noutram | 3:de36dab8492f | 45 | ThisThread::flags_wait_any(GREEN_TOGGLE); |
| noutram | 0:69af4e3595b6 | 46 | greenLED = !greenLED; |
| noutram | 0:69af4e3595b6 | 47 | } |
| noutram | 0:69af4e3595b6 | 48 | } |
| noutram | 0:69af4e3595b6 | 49 | |
| noutram | 0:69af4e3595b6 | 50 | //Main thread |
| noutram | 0:69af4e3595b6 | 51 | int main() { |
| noutram | 0:69af4e3595b6 | 52 | redLED = 0; |
| noutram | 0:69af4e3595b6 | 53 | yellowLED = 0; |
| noutram | 0:69af4e3595b6 | 54 | greenLED = 0; |
| noutram | 0:69af4e3595b6 | 55 | |
| noutram | 1:fdf9ceb4a107 | 56 | //Create threads |
| noutram | 1:fdf9ceb4a107 | 57 | Thread t1; |
| noutram | 1:fdf9ceb4a107 | 58 | Thread t2; |
| noutram | 1:fdf9ceb4a107 | 59 | Thread t3; |
| noutram | 1:fdf9ceb4a107 | 60 | |
| noutram | 1:fdf9ceb4a107 | 61 | //Start threads |
| noutram | 1:fdf9ceb4a107 | 62 | t1.start(FunctionRed); |
| noutram | 1:fdf9ceb4a107 | 63 | t2.start(FunctionYellow); |
| noutram | 1:fdf9ceb4a107 | 64 | t3.start(FunctionGreen); |
| noutram | 0:69af4e3595b6 | 65 | |
| noutram | 0:69af4e3595b6 | 66 | //Main loop |
| noutram | 0:69af4e3595b6 | 67 | while(1) { |
| noutram | 0:69af4e3595b6 | 68 | |
| noutram | 0:69af4e3595b6 | 69 | //Read stdin (serial port) |
| noutram | 0:69af4e3595b6 | 70 | int selection, hits; |
| noutram | 0:69af4e3595b6 | 71 | char strInput[64]; |
| noutram | 0:69af4e3595b6 | 72 | do { |
| noutram | 0:69af4e3595b6 | 73 | puts("Please choose:"); |
| noutram | 0:69af4e3595b6 | 74 | puts("1 - Red"); |
| noutram | 0:69af4e3595b6 | 75 | puts("2 - Yellow"); |
| noutram | 0:69af4e3595b6 | 76 | puts("3 - Green"); |
| noutram | 0:69af4e3595b6 | 77 | scanf("%64s", strInput); //Read a string |
| noutram | 0:69af4e3595b6 | 78 | hits = sscanf(strInput, "%d", &selection); //Look for an integer |
| noutram | 0:69af4e3595b6 | 79 | } while (hits != 1); //Repeat if not found |
| noutram | 0:69af4e3595b6 | 80 | |
| noutram | 0:69af4e3595b6 | 81 | //Signal the thread |
| noutram | 0:69af4e3595b6 | 82 | switch (selection) { |
| noutram | 0:69af4e3595b6 | 83 | case 1: |
| noutram | 0:69af4e3595b6 | 84 | //Signal thread 1 |
| noutram | 3:de36dab8492f | 85 | t1.flags_set(RED_TOGGLE); |
| noutram | 0:69af4e3595b6 | 86 | break; |
| noutram | 0:69af4e3595b6 | 87 | case 2: |
| noutram | 0:69af4e3595b6 | 88 | //Signal thread 2 |
| noutram | 3:de36dab8492f | 89 | t2.flags_set(YELLOW_TOGGLE); |
| noutram | 0:69af4e3595b6 | 90 | break; |
| noutram | 0:69af4e3595b6 | 91 | case 3: |
| noutram | 0:69af4e3595b6 | 92 | //Signal thread 3 |
| noutram | 3:de36dab8492f | 93 | t3.flags_set(GREEN_TOGGLE); |
| noutram | 0:69af4e3595b6 | 94 | break; |
| noutram | 0:69af4e3595b6 | 95 | default: |
| noutram | 0:69af4e3595b6 | 96 | puts("Invalid option"); |
| noutram | 0:69af4e3595b6 | 97 | break; |
| noutram | 0:69af4e3595b6 | 98 | } //end switch |
| noutram | 0:69af4e3595b6 | 99 | |
| noutram | 0:69af4e3595b6 | 100 | } //end while |
| noutram | 0:69af4e3595b6 | 101 | } //end main |
