Updated for ELEC350

Fork of Task612-mbedos54 by Nicholas Outram

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 #define RED_TOGGLE    1
00004 #define YELLOW_TOGGLE 2
00005 #define GREEN_TOGGLE  4
00006 
00007 //Function declarations
00008 void FunctionRed();
00009 void FunctionYellow();
00010 void FunctionGreen();
00011 
00012 //I/O
00013 DigitalOut onBoardLED(LED1);
00014 DigitalOut redLED(PE_15);
00015 DigitalOut yellowLED(PB_10);
00016 DigitalOut greenLED(PB_11);
00017 
00018 DigitalIn  onBoardSwitch(USER_BUTTON);
00019 DigitalIn  SW1(PE_12);
00020 DigitalIn  SW2(PE_14);
00021 
00022 
00023 //Each of the following 3 functions is listening for a signal
00024 void FunctionRed()
00025 {
00026     while (true) {
00027 //      Thread::signal_wait(RED_TOGGLE);  //Now using new API
00028         ThisThread::flags_wait_any(RED_TOGGLE);
00029         redLED = !redLED;                   
00030     }
00031 }
00032 
00033 void FunctionYellow()
00034 {
00035     while (true) {
00036 //      Thread::signal_wait(YELLOW_TOGGLE);
00037         ThisThread::flags_wait_any(YELLOW_TOGGLE);
00038         yellowLED = !yellowLED;
00039     }
00040 }
00041 
00042 void FunctionGreen()
00043 {
00044     while (true) {
00045         ThisThread::flags_wait_any(GREEN_TOGGLE);
00046         greenLED = !greenLED;
00047     }
00048 }
00049 
00050 //Main thread
00051 int main() {
00052     redLED    = 0;
00053     yellowLED = 0;
00054     greenLED  = 0;
00055         
00056     //Create threads
00057     Thread t1;           
00058     Thread t2;    
00059     Thread t3;   
00060     
00061     //Start threads
00062     t1.start(FunctionRed);           
00063     t2.start(FunctionYellow);    
00064     t3.start(FunctionGreen);      
00065     
00066     //Main loop
00067     while(1) {
00068         
00069         //Read stdin (serial port)
00070         int selection, hits;
00071         char strInput[64];
00072         do {
00073             puts("Please choose:");
00074             puts("1 - Red");
00075             puts("2 - Yellow");
00076             puts("3 - Green");            
00077             scanf("%64s", strInput);                    //Read a string
00078             hits = sscanf(strInput, "%d", &selection);  //Look for an integer
00079         } while (hits != 1);                            //Repeat if not found
00080         
00081         //Signal the thread
00082         switch (selection) {
00083         case 1:
00084             //Signal thread 1
00085             t1.flags_set(RED_TOGGLE);
00086             break;
00087         case 2:
00088             //Signal thread 2
00089             t2.flags_set(YELLOW_TOGGLE);
00090             break;
00091         case 3:
00092             //Signal thread 3
00093             t3.flags_set(GREEN_TOGGLE);
00094             break;
00095         default:
00096             puts("Invalid option");
00097             break;
00098         } //end switch
00099         
00100     } //end while
00101 } //end main