cathal deehy-power / Mbed 2 deprecated Case_study_rtos_queue

Dependencies:   C12832 mbed-rtos mbed

Fork of rtos_queue by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "C12832.h"                                     // Include the LCD header files 
00004 
00005 C12832 lcd(p5, p7, p6, p8, p11);                        //Initialise LCD
00006 
00007 DigitalIn Up(p15);                                      //Initialise Joystick up
00008 DigitalIn Down(p12);                                    //Initialise Joystick down
00009 DigitalIn Left(p13);                                    //Initialise Joystick left
00010 DigitalIn Right(p16);                                   //Initialise Joystick right
00011 
00012 
00013 AnalogIn Pot1(p19);                                     //Initialise Pot1 to be an analog input
00014 AnalogIn Pot2(p20);                                     //Initialise Pot2 to be an analog input
00015 
00016 PwmOut LED_1(p25);                                      //Initialise Blue
00017 PwmOut LED_2(p24);                                      //Initialise Green
00018 PwmOut LED_3(p23);                                      //Initialise Red
00019 
00020 PwmOut spkr(p26);                                       //Initialise PWM to speaker
00021 
00022 typedef struct                                          //Initialise Sturture
00023 {
00024     float   Joystick;                                   //Set Joystick as a float
00025     float   Light;                                      //Set Light as a float
00026     float   Pot1_Value;                                 //Initialise Red
00027     float   Pot2_Value;                                 //Initialise Red
00028 
00029 } message_t;                                            //Sturecture name "message_t"
00030 
00031 
00032 
00033 MemoryPool<message_t, 16> mpool;                        //Allocate a Mpool of value 16
00034 Queue<message_t, 16> queue;                             //Allocate a Queue of value 16
00035 
00036 
00037 void Send (void const *args)                            //Setup the thread function
00038 {
00039 
00040     while (true)                                        //Super loop
00041     {
00042         if (Up == 1)                                    //When Joystick is a value of 1 (Pushed up)
00043         {
00044         message_t *message = mpool.alloc();             //allocate memory 
00045         message->Joystick = 1;                          //Set message will be the Joystick Value 1
00046         message->Light = 1;                             //The message will be the Light Value 1
00047         message->Pot1_Value = Pot1;                     //Read Pot1 and store in message->Pot1_Value
00048         message->Pot2_Value = Pot2;                     //Read Pot2 and store in message->Pot2_Value
00049         queue.put(message);                             //Place it in the Queue
00050         Thread::wait(500);                              //Thread wait for 500 msec
00051          }   
00052          
00053         else if (Down == 1)                                     //When Joystick is a value of 1 (Pushed up)
00054         {
00055         message_t *message = mpool.alloc();
00056         message->Joystick = 2;                          //Set message will be the Joystick Value 2
00057         message->Light = 2;                             //The message will be the Light Value 2
00058         message->Pot1_Value = Pot1;                     //Read Pot1 and store in message->Pot1_Value
00059         message->Pot2_Value = Pot2;                     //Read Pot2 and store in message->Pot2_Value
00060         queue.put(message);                             //Place it in the Queue
00061         Thread::wait(500);                              //Thread wait for 500 msec
00062         }  
00063           
00064         else if (Left == 1)                             //When Joystick is a value of 1 (Pushed up)
00065         {
00066         message_t *message = mpool.alloc();
00067         message->Joystick = 4;                          //Set message will be the Joystick Value 4
00068         message->Light = 3;                             //The message will be the Light Value 3
00069         message->Pot1_Value = Pot1;                     //Read Pot1 and store in message->Pot1_Value
00070         message->Pot2_Value = Pot2;                     //Read Pot2 and store in message->Pot2_Value
00071         queue.put(message);                             //Place it in the Queue
00072         Thread::wait(500);                              //Thread wait for 500 msec 
00073         }
00074             
00075         else if (Right == 1)                            //When Joystick is a value of 1 (Pushed up)
00076         {
00077         message_t *message = mpool.alloc();
00078         message->Joystick = 3;                          //Set message will be the Joystick Value 3
00079         message->Light = 4;                             //The message will be the Light Value 4
00080         message->Pot1_Value = Pot1;                     //Read Pot1 and store in message->Pot1_Value
00081         message->Pot2_Value = Pot2;                     //Read Pot2 and store in message->Pot2_Value      
00082         queue.put(message);                             //Place it in the Queue
00083         Thread::wait(500);                              //Thread wait for 500 msec  
00084         }
00085 
00086 
00087     }
00088 }
00089 
00090 int main (void) 
00091 {
00092     LED_1 = 1;                                          //Turn off R
00093     LED_2 = 1;                                          //Turn off B
00094     LED_3 = 1;                                          //Turn off G
00095     
00096     Thread thread(Send);                                //Start up tread for send
00097 
00098     
00099     while (true) 
00100     {
00101         osEvent evt = queue.get();                      //Get next queue
00102         if (evt.status == osEventMessage)               
00103         {
00104             message_t *message = (message_t*)evt.value.p;                           //ppinter in the queue
00105             lcd.cls();                                                              // clear screen of the lcd
00106             lcd.locate(0,3);                                                        //set location on the LCD
00107             lcd.printf("Joystick Value: %.2f \n\r"   , message->Joystick);          //display joystick value on LCD
00108             lcd.printf("Light Value: %.2f \n\r"     , message->Light);              //display Light value on LCD
00109             lcd.printf("Pot1:%.2f Pot2:%.4f\n\r"     , message->Pot1_Value, message->Pot2_Value);   //display Pot1 & Pot2 value on LCD
00110             
00111             
00112             if (message->Light == 1)                    // When message is value 1 display Light sequence
00113             {
00114              LED_1 = message->Pot1_Value;               // dim the Blue LED with the value in pot1
00115              LED_2 = 1;                                 //Turn off LED
00116              LED_3 = 1;                                 //Turn off LED
00117              spkr = message->Pot2_Value;                // dim the Blue LED with the value in pot1
00118             }
00119             else if (message->Light == 2)
00120             {
00121              LED_1 = 1;                                 //Turn off LED
00122              LED_2 = message->Pot1_Value;               // dim the Green LED with the value in pot1
00123              LED_3 = 1;                                 //Turn off LED
00124              spkr = message->Pot2_Value;                // dim the Blue LED with the value in pot1
00125             }
00126             else if (message->Light == 3)
00127             {
00128              LED_1 = 1;                                 //Turn off LED
00129              LED_2 = 1;                                 //Turn off LED
00130              LED_3 = message->Pot1_Value;               // dim the Red LED with the value in pot1
00131              spkr = message->Pot2_Value;
00132             }            
00133             else if (message->Light == 4)
00134             {
00135              LED_1 = message->Pot1_Value;               // dim the Blue LED with the value in pot1
00136              LED_2 = message->Pot1_Value;               // dim the Green LED with the value in pot1
00137              LED_3 = 1;                                 //Turn off LED
00138              spkr = message->Pot2_Value;                // dim the Blue LED with the value in pot1
00139             }
00140             
00141             mpool.free(message);                        // Frees up memory
00142         }
00143     }
00144 }