Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Task612-mbedos54 by
main.cpp@2:410eacc3006a, 2017-10-31 (annotated)
- Committer:
- noutram
- Date:
- Tue Oct 31 14:09:33 2017 +0000
- Revision:
- 2:410eacc3006a
- Parent:
- 1:fdf9ceb4a107
- Child:
- 3:de36dab8492f
Updated for ELEC350
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 | 0:69af4e3595b6 | 27 | Thread::signal_wait(RED_TOGGLE); |
| noutram | 0:69af4e3595b6 | 28 | redLED = !redLED; |
| noutram | 0:69af4e3595b6 | 29 | } |
| noutram | 0:69af4e3595b6 | 30 | } |
| noutram | 0:69af4e3595b6 | 31 | |
| noutram | 1:fdf9ceb4a107 | 32 | void FunctionYellow() |
| noutram | 0:69af4e3595b6 | 33 | { |
| noutram | 0:69af4e3595b6 | 34 | while (true) { |
| noutram | 0:69af4e3595b6 | 35 | Thread::signal_wait(YELLOW_TOGGLE); |
| noutram | 0:69af4e3595b6 | 36 | yellowLED = !yellowLED; |
| noutram | 0:69af4e3595b6 | 37 | } |
| noutram | 0:69af4e3595b6 | 38 | } |
| noutram | 0:69af4e3595b6 | 39 | |
| noutram | 1:fdf9ceb4a107 | 40 | void FunctionGreen() |
| noutram | 0:69af4e3595b6 | 41 | { |
| noutram | 0:69af4e3595b6 | 42 | while (true) { |
| noutram | 0:69af4e3595b6 | 43 | Thread::signal_wait(GREEN_TOGGLE); |
| noutram | 0:69af4e3595b6 | 44 | greenLED = !greenLED; |
| noutram | 0:69af4e3595b6 | 45 | } |
| noutram | 0:69af4e3595b6 | 46 | } |
| noutram | 0:69af4e3595b6 | 47 | |
| noutram | 0:69af4e3595b6 | 48 | //Main thread |
| noutram | 0:69af4e3595b6 | 49 | int main() { |
| noutram | 0:69af4e3595b6 | 50 | redLED = 0; |
| noutram | 0:69af4e3595b6 | 51 | yellowLED = 0; |
| noutram | 0:69af4e3595b6 | 52 | greenLED = 0; |
| noutram | 0:69af4e3595b6 | 53 | |
| noutram | 1:fdf9ceb4a107 | 54 | //Create threads |
| noutram | 1:fdf9ceb4a107 | 55 | Thread t1; |
| noutram | 1:fdf9ceb4a107 | 56 | Thread t2; |
| noutram | 1:fdf9ceb4a107 | 57 | Thread t3; |
| noutram | 1:fdf9ceb4a107 | 58 | |
| noutram | 1:fdf9ceb4a107 | 59 | //Start threads |
| noutram | 1:fdf9ceb4a107 | 60 | t1.start(FunctionRed); |
| noutram | 1:fdf9ceb4a107 | 61 | t2.start(FunctionYellow); |
| noutram | 1:fdf9ceb4a107 | 62 | t3.start(FunctionGreen); |
| noutram | 0:69af4e3595b6 | 63 | |
| noutram | 0:69af4e3595b6 | 64 | //Main loop |
| noutram | 0:69af4e3595b6 | 65 | while(1) { |
| noutram | 0:69af4e3595b6 | 66 | |
| noutram | 0:69af4e3595b6 | 67 | //Read stdin (serial port) |
| noutram | 0:69af4e3595b6 | 68 | int selection, hits; |
| noutram | 0:69af4e3595b6 | 69 | char strInput[64]; |
| noutram | 0:69af4e3595b6 | 70 | do { |
| noutram | 0:69af4e3595b6 | 71 | puts("Please choose:"); |
| noutram | 0:69af4e3595b6 | 72 | puts("1 - Red"); |
| noutram | 0:69af4e3595b6 | 73 | puts("2 - Yellow"); |
| noutram | 0:69af4e3595b6 | 74 | puts("3 - Green"); |
| noutram | 0:69af4e3595b6 | 75 | scanf("%64s", strInput); //Read a string |
| noutram | 0:69af4e3595b6 | 76 | hits = sscanf(strInput, "%d", &selection); //Look for an integer |
| noutram | 0:69af4e3595b6 | 77 | } while (hits != 1); //Repeat if not found |
| noutram | 0:69af4e3595b6 | 78 | |
| noutram | 0:69af4e3595b6 | 79 | //Signal the thread |
| noutram | 0:69af4e3595b6 | 80 | switch (selection) { |
| noutram | 0:69af4e3595b6 | 81 | case 1: |
| noutram | 0:69af4e3595b6 | 82 | //Signal thread 1 |
| noutram | 0:69af4e3595b6 | 83 | t1.signal_set(RED_TOGGLE); |
| noutram | 0:69af4e3595b6 | 84 | break; |
| noutram | 0:69af4e3595b6 | 85 | case 2: |
| noutram | 0:69af4e3595b6 | 86 | //Signal thread 2 |
| noutram | 0:69af4e3595b6 | 87 | t2.signal_set(YELLOW_TOGGLE); |
| noutram | 0:69af4e3595b6 | 88 | break; |
| noutram | 0:69af4e3595b6 | 89 | case 3: |
| noutram | 0:69af4e3595b6 | 90 | //Signal thread 3 |
| noutram | 0:69af4e3595b6 | 91 | t3.signal_set(GREEN_TOGGLE); |
| noutram | 0:69af4e3595b6 | 92 | break; |
| noutram | 0:69af4e3595b6 | 93 | default: |
| noutram | 0:69af4e3595b6 | 94 | puts("Invalid option"); |
| noutram | 0:69af4e3595b6 | 95 | break; |
| noutram | 0:69af4e3595b6 | 96 | } //end switch |
| noutram | 0:69af4e3595b6 | 97 | |
| noutram | 0:69af4e3595b6 | 98 | } //end while |
| noutram | 0:69af4e3595b6 | 99 | } //end main |
