done pool
Dependencies: C12832 mbed-rtos mbed
Fork of Case_study_rtos_queue by
main.cpp@6:6cdc2d2e804b, 2015-02-13 (annotated)
- Committer:
- cathal66
- Date:
- Fri Feb 13 13:34:51 2015 +0000
- Revision:
- 6:6cdc2d2e804b
- Parent:
- 3:c490e2d69dd8
done
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 1:d2ba5afbf91f | 1 | #include "mbed.h" |
emilmont | 1:d2ba5afbf91f | 2 | #include "rtos.h" |
cathal66 | 6:6cdc2d2e804b | 3 | #include "C12832.h" // Include the LCD header files |
emilmont | 1:d2ba5afbf91f | 4 | |
cathal66 | 6:6cdc2d2e804b | 5 | C12832 lcd(p5, p7, p6, p8, p11); //Initialise LCD |
cathal66 | 6:6cdc2d2e804b | 6 | |
cathal66 | 6:6cdc2d2e804b | 7 | DigitalIn Up(p15); //Initialise Joystick up |
cathal66 | 6:6cdc2d2e804b | 8 | DigitalIn Down(p12); //Initialise Joystick down |
cathal66 | 6:6cdc2d2e804b | 9 | DigitalIn Left(p13); //Initialise Joystick left |
cathal66 | 6:6cdc2d2e804b | 10 | DigitalIn Right(p16); //Initialise Joystick right |
cathal66 | 6:6cdc2d2e804b | 11 | |
cathal66 | 6:6cdc2d2e804b | 12 | |
cathal66 | 6:6cdc2d2e804b | 13 | AnalogIn Pot1(p19); //Initialise Pot1 to be an analog input |
cathal66 | 6:6cdc2d2e804b | 14 | AnalogIn Pot2(p20); //Initialise Pot2 to be an analog input |
cathal66 | 6:6cdc2d2e804b | 15 | |
cathal66 | 6:6cdc2d2e804b | 16 | PwmOut LED_1(p25); //Initialise Blue |
cathal66 | 6:6cdc2d2e804b | 17 | PwmOut LED_2(p24); //Initialise Green |
cathal66 | 6:6cdc2d2e804b | 18 | PwmOut LED_3(p23); //Initialise Red |
cathal66 | 6:6cdc2d2e804b | 19 | |
cathal66 | 6:6cdc2d2e804b | 20 | PwmOut spkr(p26); //Initialise PWM to speaker |
emilmont | 1:d2ba5afbf91f | 21 | |
cathal66 | 6:6cdc2d2e804b | 22 | typedef struct //Initialise Sturture |
cathal66 | 6:6cdc2d2e804b | 23 | { |
cathal66 | 6:6cdc2d2e804b | 24 | float Joystick; //Set Joystick as a float |
cathal66 | 6:6cdc2d2e804b | 25 | float Light; //Set Light as a float |
cathal66 | 6:6cdc2d2e804b | 26 | float Pot1_Value; //Initialise Red |
cathal66 | 6:6cdc2d2e804b | 27 | float Pot2_Value; //Initialise Red |
cathal66 | 6:6cdc2d2e804b | 28 | |
cathal66 | 6:6cdc2d2e804b | 29 | } message_t; //Sturecture name "message_t" |
cathal66 | 6:6cdc2d2e804b | 30 | |
cathal66 | 6:6cdc2d2e804b | 31 | |
cathal66 | 6:6cdc2d2e804b | 32 | |
cathal66 | 6:6cdc2d2e804b | 33 | MemoryPool<message_t, 16> mpool; //Allocate a Mpool of value 16 |
cathal66 | 6:6cdc2d2e804b | 34 | Queue<message_t, 16> queue; //Allocate a Queue of value 16 |
cathal66 | 6:6cdc2d2e804b | 35 | |
cathal66 | 6:6cdc2d2e804b | 36 | |
cathal66 | 6:6cdc2d2e804b | 37 | void Send (void const *args) //Setup the thread function |
cathal66 | 6:6cdc2d2e804b | 38 | { |
emilmont | 1:d2ba5afbf91f | 39 | |
cathal66 | 6:6cdc2d2e804b | 40 | while (true) //Super loop |
cathal66 | 6:6cdc2d2e804b | 41 | { |
cathal66 | 6:6cdc2d2e804b | 42 | if (Up == 1) //When Joystick is a value of 1 (Pushed up) |
cathal66 | 6:6cdc2d2e804b | 43 | { |
cathal66 | 6:6cdc2d2e804b | 44 | message_t *message = mpool.alloc(); //allocate memory |
cathal66 | 6:6cdc2d2e804b | 45 | message->Joystick = 1; //Set message will be the Joystick Value 1 |
cathal66 | 6:6cdc2d2e804b | 46 | message->Light = 1; //The message will be the Light Value 1 |
cathal66 | 6:6cdc2d2e804b | 47 | message->Pot1_Value = Pot1; //Read Pot1 and store in message->Pot1_Value |
cathal66 | 6:6cdc2d2e804b | 48 | message->Pot2_Value = Pot2; //Read Pot2 and store in message->Pot2_Value |
cathal66 | 6:6cdc2d2e804b | 49 | queue.put(message); //Place it in the Queue |
cathal66 | 6:6cdc2d2e804b | 50 | Thread::wait(500); //Thread wait for 500 msec |
cathal66 | 6:6cdc2d2e804b | 51 | } |
cathal66 | 6:6cdc2d2e804b | 52 | |
cathal66 | 6:6cdc2d2e804b | 53 | else if (Down == 1) //When Joystick is a value of 1 (Pushed up) |
cathal66 | 6:6cdc2d2e804b | 54 | { |
emilmont | 1:d2ba5afbf91f | 55 | message_t *message = mpool.alloc(); |
cathal66 | 6:6cdc2d2e804b | 56 | message->Joystick = 2; //Set message will be the Joystick Value 2 |
cathal66 | 6:6cdc2d2e804b | 57 | message->Light = 2; //The message will be the Light Value 2 |
cathal66 | 6:6cdc2d2e804b | 58 | message->Pot1_Value = Pot1; //Read Pot1 and store in message->Pot1_Value |
cathal66 | 6:6cdc2d2e804b | 59 | message->Pot2_Value = Pot2; //Read Pot2 and store in message->Pot2_Value |
cathal66 | 6:6cdc2d2e804b | 60 | queue.put(message); //Place it in the Queue |
cathal66 | 6:6cdc2d2e804b | 61 | Thread::wait(500); //Thread wait for 500 msec |
cathal66 | 6:6cdc2d2e804b | 62 | } |
cathal66 | 6:6cdc2d2e804b | 63 | |
cathal66 | 6:6cdc2d2e804b | 64 | else if (Left == 1) //When Joystick is a value of 1 (Pushed up) |
cathal66 | 6:6cdc2d2e804b | 65 | { |
cathal66 | 6:6cdc2d2e804b | 66 | message_t *message = mpool.alloc(); |
cathal66 | 6:6cdc2d2e804b | 67 | message->Joystick = 4; //Set message will be the Joystick Value 4 |
cathal66 | 6:6cdc2d2e804b | 68 | message->Light = 3; //The message will be the Light Value 3 |
cathal66 | 6:6cdc2d2e804b | 69 | message->Pot1_Value = Pot1; //Read Pot1 and store in message->Pot1_Value |
cathal66 | 6:6cdc2d2e804b | 70 | message->Pot2_Value = Pot2; //Read Pot2 and store in message->Pot2_Value |
cathal66 | 6:6cdc2d2e804b | 71 | queue.put(message); //Place it in the Queue |
cathal66 | 6:6cdc2d2e804b | 72 | Thread::wait(500); //Thread wait for 500 msec |
cathal66 | 6:6cdc2d2e804b | 73 | } |
cathal66 | 6:6cdc2d2e804b | 74 | |
cathal66 | 6:6cdc2d2e804b | 75 | else if (Right == 1) //When Joystick is a value of 1 (Pushed up) |
cathal66 | 6:6cdc2d2e804b | 76 | { |
cathal66 | 6:6cdc2d2e804b | 77 | message_t *message = mpool.alloc(); |
cathal66 | 6:6cdc2d2e804b | 78 | message->Joystick = 3; //Set message will be the Joystick Value 3 |
cathal66 | 6:6cdc2d2e804b | 79 | message->Light = 4; //The message will be the Light Value 4 |
cathal66 | 6:6cdc2d2e804b | 80 | message->Pot1_Value = Pot1; //Read Pot1 and store in message->Pot1_Value |
cathal66 | 6:6cdc2d2e804b | 81 | message->Pot2_Value = Pot2; //Read Pot2 and store in message->Pot2_Value |
cathal66 | 6:6cdc2d2e804b | 82 | queue.put(message); //Place it in the Queue |
cathal66 | 6:6cdc2d2e804b | 83 | Thread::wait(500); //Thread wait for 500 msec |
cathal66 | 6:6cdc2d2e804b | 84 | } |
cathal66 | 6:6cdc2d2e804b | 85 | |
cathal66 | 6:6cdc2d2e804b | 86 | |
emilmont | 1:d2ba5afbf91f | 87 | } |
emilmont | 1:d2ba5afbf91f | 88 | } |
emilmont | 1:d2ba5afbf91f | 89 | |
cathal66 | 6:6cdc2d2e804b | 90 | int main (void) |
cathal66 | 6:6cdc2d2e804b | 91 | { |
cathal66 | 6:6cdc2d2e804b | 92 | LED_1 = 1; //Turn off R |
cathal66 | 6:6cdc2d2e804b | 93 | LED_2 = 1; //Turn off B |
cathal66 | 6:6cdc2d2e804b | 94 | LED_3 = 1; //Turn off G |
cathal66 | 6:6cdc2d2e804b | 95 | |
cathal66 | 6:6cdc2d2e804b | 96 | Thread thread(Send); //Start up tread for send |
cathal66 | 6:6cdc2d2e804b | 97 | |
emilmont | 1:d2ba5afbf91f | 98 | |
cathal66 | 6:6cdc2d2e804b | 99 | while (true) |
cathal66 | 6:6cdc2d2e804b | 100 | { |
cathal66 | 6:6cdc2d2e804b | 101 | osEvent evt = queue.get(); //Get next queue |
cathal66 | 6:6cdc2d2e804b | 102 | if (evt.status == osEventMessage) |
cathal66 | 6:6cdc2d2e804b | 103 | { |
cathal66 | 6:6cdc2d2e804b | 104 | message_t *message = (message_t*)evt.value.p; //ppinter in the queue |
cathal66 | 6:6cdc2d2e804b | 105 | lcd.cls(); // clear screen of the lcd |
cathal66 | 6:6cdc2d2e804b | 106 | lcd.locate(0,3); //set location on the LCD |
cathal66 | 6:6cdc2d2e804b | 107 | lcd.printf("Joystick Value: %.2f \n\r" , message->Joystick); //display joystick value on LCD |
cathal66 | 6:6cdc2d2e804b | 108 | lcd.printf("Light Value: %.2f \n\r" , message->Light); //display Light value on LCD |
cathal66 | 6:6cdc2d2e804b | 109 | lcd.printf("Pot1:%.2f Pot2:%.4f\n\r" , message->Pot1_Value, message->Pot2_Value); //display Pot1 & Pot2 value on LCD |
cathal66 | 6:6cdc2d2e804b | 110 | |
emilmont | 1:d2ba5afbf91f | 111 | |
cathal66 | 6:6cdc2d2e804b | 112 | if (message->Light == 1) // When message is value 1 display Light sequence |
cathal66 | 6:6cdc2d2e804b | 113 | { |
cathal66 | 6:6cdc2d2e804b | 114 | LED_1 = message->Pot1_Value; // dim the Blue LED with the value in pot1 |
cathal66 | 6:6cdc2d2e804b | 115 | LED_2 = 1; //Turn off LED |
cathal66 | 6:6cdc2d2e804b | 116 | LED_3 = 1; //Turn off LED |
cathal66 | 6:6cdc2d2e804b | 117 | spkr = message->Pot2_Value; // dim the Blue LED with the value in pot1 |
cathal66 | 6:6cdc2d2e804b | 118 | } |
cathal66 | 6:6cdc2d2e804b | 119 | else if (message->Light == 2) |
cathal66 | 6:6cdc2d2e804b | 120 | { |
cathal66 | 6:6cdc2d2e804b | 121 | LED_1 = 1; //Turn off LED |
cathal66 | 6:6cdc2d2e804b | 122 | LED_2 = message->Pot1_Value; // dim the Green LED with the value in pot1 |
cathal66 | 6:6cdc2d2e804b | 123 | LED_3 = 1; //Turn off LED |
cathal66 | 6:6cdc2d2e804b | 124 | spkr = message->Pot2_Value; // dim the Blue LED with the value in pot1 |
cathal66 | 6:6cdc2d2e804b | 125 | } |
cathal66 | 6:6cdc2d2e804b | 126 | else if (message->Light == 3) |
cathal66 | 6:6cdc2d2e804b | 127 | { |
cathal66 | 6:6cdc2d2e804b | 128 | LED_1 = 1; //Turn off LED |
cathal66 | 6:6cdc2d2e804b | 129 | LED_2 = 1; //Turn off LED |
cathal66 | 6:6cdc2d2e804b | 130 | LED_3 = message->Pot1_Value; // dim the Red LED with the value in pot1 |
cathal66 | 6:6cdc2d2e804b | 131 | spkr = message->Pot2_Value; |
cathal66 | 6:6cdc2d2e804b | 132 | } |
cathal66 | 6:6cdc2d2e804b | 133 | else if (message->Light == 4) |
cathal66 | 6:6cdc2d2e804b | 134 | { |
cathal66 | 6:6cdc2d2e804b | 135 | LED_1 = message->Pot1_Value; // dim the Blue LED with the value in pot1 |
cathal66 | 6:6cdc2d2e804b | 136 | LED_2 = message->Pot1_Value; // dim the Green LED with the value in pot1 |
cathal66 | 6:6cdc2d2e804b | 137 | LED_3 = 1; //Turn off LED |
cathal66 | 6:6cdc2d2e804b | 138 | spkr = message->Pot2_Value; // dim the Blue LED with the value in pot1 |
cathal66 | 6:6cdc2d2e804b | 139 | } |
cathal66 | 6:6cdc2d2e804b | 140 | |
cathal66 | 6:6cdc2d2e804b | 141 | mpool.free(message); // Frees up memory |
emilmont | 1:d2ba5afbf91f | 142 | } |
emilmont | 1:d2ba5afbf91f | 143 | } |
cathal66 | 6:6cdc2d2e804b | 144 | } |