Simple example to use a Mempool object together with a Queue object in order to send more complex messages from thread1 to thread2.

Dependencies:   mbed-rtos mbed

Committer:
icserny
Date:
Tue Feb 23 13:32:14 2016 +0000
Revision:
0:2a0e4f8a7b76
First version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
icserny 0:2a0e4f8a7b76 1 /** 10_rtos_mempool
icserny 0:2a0e4f8a7b76 2 *
icserny 0:2a0e4f8a7b76 3 * Simple example to use a Mempool object together with a Queue
icserny 0:2a0e4f8a7b76 4 * object in order to send more complex messages from thread1 to thread2.
icserny 0:2a0e4f8a7b76 5 * Thread1 generates transition between random RGB colors and sends
icserny 0:2a0e4f8a7b76 6 * messages to Thread2. Thread2 displays the requested color on the
icserny 0:2a0e4f8a7b76 7 * built in RGB LED.
icserny 0:2a0e4f8a7b76 8 *
icserny 0:2a0e4f8a7b76 9 * Data items for messages are stored in a MemoryPool object.
icserny 0:2a0e4f8a7b76 10 * Thread1 allocates MemoryPool slots, Thread2 should free up
icserny 0:2a0e4f8a7b76 11 * these slots after using up data stored in the slot.
icserny 0:2a0e4f8a7b76 12 */
icserny 0:2a0e4f8a7b76 13
icserny 0:2a0e4f8a7b76 14 #include "mbed.h"
icserny 0:2a0e4f8a7b76 15 #include "rtos.h"
icserny 0:2a0e4f8a7b76 16 PwmOut rled(LED_RED);
icserny 0:2a0e4f8a7b76 17 PwmOut gled(LED_GREEN);
icserny 0:2a0e4f8a7b76 18 PwmOut bled(LED_BLUE);
icserny 0:2a0e4f8a7b76 19
icserny 0:2a0e4f8a7b76 20 typedef struct {
icserny 0:2a0e4f8a7b76 21 float red;
icserny 0:2a0e4f8a7b76 22 float green;
icserny 0:2a0e4f8a7b76 23 float blue;
icserny 0:2a0e4f8a7b76 24 } message_t;
icserny 0:2a0e4f8a7b76 25
icserny 0:2a0e4f8a7b76 26 MemoryPool<message_t, 4> mpool; //Memory pool for data storage
icserny 0:2a0e4f8a7b76 27 Queue<message_t, 4> queue; //Message queue for 4 items
icserny 0:2a0e4f8a7b76 28
icserny 0:2a0e4f8a7b76 29 void led_thread(void const *argument)
icserny 0:2a0e4f8a7b76 30 {
icserny 0:2a0e4f8a7b76 31 rled.period_ms(20); //Set period to 20 ms
icserny 0:2a0e4f8a7b76 32 rled.write(1.0f); //Initialize to 0% duty cycle
icserny 0:2a0e4f8a7b76 33 gled.period_ms(20); //Set period to 20 ms
icserny 0:2a0e4f8a7b76 34 gled.write(1.0f); //Initialize to 0% duty cycle
icserny 0:2a0e4f8a7b76 35 bled.period_ms(20); //Set period to 20 ms
icserny 0:2a0e4f8a7b76 36 bled.write(1.0f); //Initialize to 0% duty cycle
icserny 0:2a0e4f8a7b76 37 while (true) {
icserny 0:2a0e4f8a7b76 38 osEvent evt = queue.get(); //Wait for a message
icserny 0:2a0e4f8a7b76 39 if(evt.status == osEventMessage) {
icserny 0:2a0e4f8a7b76 40 message_t *mymessage = (message_t*)evt.value.p;
icserny 0:2a0e4f8a7b76 41 rled = 1.0f - mymessage->red;
icserny 0:2a0e4f8a7b76 42 gled = 1.0f - mymessage->green;
icserny 0:2a0e4f8a7b76 43 bled = 1.0f - mymessage->blue;
icserny 0:2a0e4f8a7b76 44 mpool.free(mymessage); //Free up memory
icserny 0:2a0e4f8a7b76 45 }
icserny 0:2a0e4f8a7b76 46 }
icserny 0:2a0e4f8a7b76 47 }
icserny 0:2a0e4f8a7b76 48
icserny 0:2a0e4f8a7b76 49 float frand(void)
icserny 0:2a0e4f8a7b76 50 {
icserny 0:2a0e4f8a7b76 51 float rv = (float)(rand()&0xFFFF);
icserny 0:2a0e4f8a7b76 52 return (rv/65536.0f);
icserny 0:2a0e4f8a7b76 53 }
icserny 0:2a0e4f8a7b76 54
icserny 0:2a0e4f8a7b76 55 int main (void)
icserny 0:2a0e4f8a7b76 56 {
icserny 0:2a0e4f8a7b76 57 float RGB1[3];
icserny 0:2a0e4f8a7b76 58 float RGB2[3];
icserny 0:2a0e4f8a7b76 59 float INC[3];
icserny 0:2a0e4f8a7b76 60 Thread thread2(led_thread);
icserny 0:2a0e4f8a7b76 61 //--- Create a random color ---------------------
icserny 0:2a0e4f8a7b76 62 for (int x=0; x<3; x++) {
icserny 0:2a0e4f8a7b76 63 RGB1[x] = frand();
icserny 0:2a0e4f8a7b76 64 }
icserny 0:2a0e4f8a7b76 65
icserny 0:2a0e4f8a7b76 66 while (true) {
icserny 0:2a0e4f8a7b76 67 //--- Create a new random color -----------------
icserny 0:2a0e4f8a7b76 68 for (int x=0; x<3; x++) {
icserny 0:2a0e4f8a7b76 69 RGB2[x] = frand();
icserny 0:2a0e4f8a7b76 70 }
icserny 0:2a0e4f8a7b76 71 //--- Determine increments to go from color 1 to color 2 in 25 steps
icserny 0:2a0e4f8a7b76 72 for (int x=0; x<3; x++) {
icserny 0:2a0e4f8a7b76 73 INC[x] = (RGB1[x] - RGB2[x]) / 25;
icserny 0:2a0e4f8a7b76 74 }
icserny 0:2a0e4f8a7b76 75 //--- Send color codes to thread2 ---------------
icserny 0:2a0e4f8a7b76 76 for (int s=0; s<25; s++) {
icserny 0:2a0e4f8a7b76 77 message_t *message = mpool.alloc(); //Allocate memory
icserny 0:2a0e4f8a7b76 78 message->red = RGB1[0];
icserny 0:2a0e4f8a7b76 79 message->green = RGB1[1];
icserny 0:2a0e4f8a7b76 80 message->blue = RGB1[2];
icserny 0:2a0e4f8a7b76 81 queue.put(message); //Send data as message
icserny 0:2a0e4f8a7b76 82 Thread::wait(100);
icserny 0:2a0e4f8a7b76 83 for (int x=0; x<3; x++) {
icserny 0:2a0e4f8a7b76 84 RGB1[x] -= INC[x]; //Approach to second colour
icserny 0:2a0e4f8a7b76 85 }
icserny 0:2a0e4f8a7b76 86 }
icserny 0:2a0e4f8a7b76 87 }
icserny 0:2a0e4f8a7b76 88 }