Nicholas Outram
/
Task612-mbedos54
updated for mbed 5.4
Fork of Task612-mbedos54 by
main.cpp@0:69af4e3595b6, 2016-03-08 (annotated)
- Committer:
- noutram
- Date:
- Tue Mar 08 11:46:22 2016 +0000
- Revision:
- 0:69af4e3595b6
- Child:
- 1:fdf9ceb4a107
Task 6.1.2
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 | #include "rtos.h" |
noutram | 0:69af4e3595b6 | 3 | |
noutram | 0:69af4e3595b6 | 4 | #define RED_TOGGLE 1 |
noutram | 0:69af4e3595b6 | 5 | #define YELLOW_TOGGLE 2 |
noutram | 0:69af4e3595b6 | 6 | #define GREEN_TOGGLE 4 |
noutram | 0:69af4e3595b6 | 7 | |
noutram | 0:69af4e3595b6 | 8 | //Function declarations |
noutram | 0:69af4e3595b6 | 9 | void FunctionRed(void const *args); |
noutram | 0:69af4e3595b6 | 10 | void FunctionYellow(void const *args); |
noutram | 0:69af4e3595b6 | 11 | void FunctionGreen(void const *args); |
noutram | 0:69af4e3595b6 | 12 | |
noutram | 0:69af4e3595b6 | 13 | //I/O |
noutram | 0:69af4e3595b6 | 14 | DigitalOut onBoardLED(LED1); |
noutram | 0:69af4e3595b6 | 15 | DigitalOut redLED(D7); |
noutram | 0:69af4e3595b6 | 16 | DigitalOut yellowLED(D6); |
noutram | 0:69af4e3595b6 | 17 | DigitalOut greenLED(D5); |
noutram | 0:69af4e3595b6 | 18 | |
noutram | 0:69af4e3595b6 | 19 | DigitalIn onBoardSwitch(USER_BUTTON); |
noutram | 0:69af4e3595b6 | 20 | DigitalIn SW1(D4); |
noutram | 0:69af4e3595b6 | 21 | DigitalIn SW2(D3); |
noutram | 0:69af4e3595b6 | 22 | |
noutram | 0:69af4e3595b6 | 23 | |
noutram | 0:69af4e3595b6 | 24 | //Each of the following 3 functions is listening for a signal |
noutram | 0:69af4e3595b6 | 25 | void FunctionRed(void const *args) |
noutram | 0:69af4e3595b6 | 26 | { |
noutram | 0:69af4e3595b6 | 27 | while (true) { |
noutram | 0:69af4e3595b6 | 28 | Thread::signal_wait(RED_TOGGLE); |
noutram | 0:69af4e3595b6 | 29 | redLED = !redLED; |
noutram | 0:69af4e3595b6 | 30 | } |
noutram | 0:69af4e3595b6 | 31 | } |
noutram | 0:69af4e3595b6 | 32 | |
noutram | 0:69af4e3595b6 | 33 | void FunctionYellow(void const *args) |
noutram | 0:69af4e3595b6 | 34 | { |
noutram | 0:69af4e3595b6 | 35 | while (true) { |
noutram | 0:69af4e3595b6 | 36 | Thread::signal_wait(YELLOW_TOGGLE); |
noutram | 0:69af4e3595b6 | 37 | yellowLED = !yellowLED; |
noutram | 0:69af4e3595b6 | 38 | } |
noutram | 0:69af4e3595b6 | 39 | } |
noutram | 0:69af4e3595b6 | 40 | |
noutram | 0:69af4e3595b6 | 41 | void FunctionGreen(void const *args) |
noutram | 0:69af4e3595b6 | 42 | { |
noutram | 0:69af4e3595b6 | 43 | while (true) { |
noutram | 0:69af4e3595b6 | 44 | Thread::signal_wait(GREEN_TOGGLE); |
noutram | 0:69af4e3595b6 | 45 | greenLED = !greenLED; |
noutram | 0:69af4e3595b6 | 46 | } |
noutram | 0:69af4e3595b6 | 47 | } |
noutram | 0:69af4e3595b6 | 48 | |
noutram | 0:69af4e3595b6 | 49 | //Main thread |
noutram | 0:69af4e3595b6 | 50 | int main() { |
noutram | 0:69af4e3595b6 | 51 | redLED = 0; |
noutram | 0:69af4e3595b6 | 52 | yellowLED = 0; |
noutram | 0:69af4e3595b6 | 53 | greenLED = 0; |
noutram | 0:69af4e3595b6 | 54 | |
noutram | 0:69af4e3595b6 | 55 | //Create and run threads |
noutram | 0:69af4e3595b6 | 56 | Thread t1(FunctionRed); |
noutram | 0:69af4e3595b6 | 57 | Thread t2(FunctionYellow); |
noutram | 0:69af4e3595b6 | 58 | Thread t3(FunctionGreen); |
noutram | 0:69af4e3595b6 | 59 | |
noutram | 0:69af4e3595b6 | 60 | //Main loop |
noutram | 0:69af4e3595b6 | 61 | while(1) { |
noutram | 0:69af4e3595b6 | 62 | |
noutram | 0:69af4e3595b6 | 63 | //Read stdin (serial port) |
noutram | 0:69af4e3595b6 | 64 | int selection, hits; |
noutram | 0:69af4e3595b6 | 65 | char strInput[64]; |
noutram | 0:69af4e3595b6 | 66 | do { |
noutram | 0:69af4e3595b6 | 67 | puts("Please choose:"); |
noutram | 0:69af4e3595b6 | 68 | puts("1 - Red"); |
noutram | 0:69af4e3595b6 | 69 | puts("2 - Yellow"); |
noutram | 0:69af4e3595b6 | 70 | puts("3 - Green"); |
noutram | 0:69af4e3595b6 | 71 | scanf("%64s", strInput); //Read a string |
noutram | 0:69af4e3595b6 | 72 | hits = sscanf(strInput, "%d", &selection); //Look for an integer |
noutram | 0:69af4e3595b6 | 73 | } while (hits != 1); //Repeat if not found |
noutram | 0:69af4e3595b6 | 74 | |
noutram | 0:69af4e3595b6 | 75 | //Signal the thread |
noutram | 0:69af4e3595b6 | 76 | switch (selection) { |
noutram | 0:69af4e3595b6 | 77 | case 1: |
noutram | 0:69af4e3595b6 | 78 | //Signal thread 1 |
noutram | 0:69af4e3595b6 | 79 | t1.signal_set(RED_TOGGLE); |
noutram | 0:69af4e3595b6 | 80 | break; |
noutram | 0:69af4e3595b6 | 81 | case 2: |
noutram | 0:69af4e3595b6 | 82 | //Signal thread 2 |
noutram | 0:69af4e3595b6 | 83 | t2.signal_set(YELLOW_TOGGLE); |
noutram | 0:69af4e3595b6 | 84 | break; |
noutram | 0:69af4e3595b6 | 85 | case 3: |
noutram | 0:69af4e3595b6 | 86 | //Signal thread 3 |
noutram | 0:69af4e3595b6 | 87 | t3.signal_set(GREEN_TOGGLE); |
noutram | 0:69af4e3595b6 | 88 | break; |
noutram | 0:69af4e3595b6 | 89 | default: |
noutram | 0:69af4e3595b6 | 90 | puts("Invalid option"); |
noutram | 0:69af4e3595b6 | 91 | break; |
noutram | 0:69af4e3595b6 | 92 | } //end switch |
noutram | 0:69af4e3595b6 | 93 | |
noutram | 0:69af4e3595b6 | 94 | } //end while |
noutram | 0:69af4e3595b6 | 95 | } //end main |