Simple example to use a Mail object in order to send complex messages from thread1 to thread2. Thread1 generates transition between random RGB colors and sends messages to Thread2. Thread2 displays the requested color on the built in RGB LED. Data items for messages are stored in a Mail object. Thread1 allocates mailbox slots, Thread2 should free up these slots after using up data stored in the slot.

Dependencies:   mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /** 10_rtos_mailbox
00002  *
00003  * Simple example to use a Mail object in order to send 
00004  * complex messages from thread1 to thread2.
00005  * Thread1 generates transition between random RGB colors and sends
00006  * messages to Thread2. Thread2 displays the requested color on the
00007  * built in RGB LED.
00008  *
00009  * Data items for messages are stored in a Mail object.
00010  * Thread1 allocates mailbox slots, Thread2 should free up
00011  * these slots after using up data stored in the slot.
00012  */
00013 
00014 #include "mbed.h"
00015 #include "rtos.h"
00016 PwmOut rled(LED_RED);
00017 PwmOut gled(LED_GREEN);
00018 PwmOut bled(LED_BLUE);
00019 
00020 typedef struct {
00021     float red;
00022     float green;
00023     float blue;
00024 } message_t;
00025 
00026 Mail<message_t, 4> mbox;                            //Mailboxes for 4 messages
00027 
00028 
00029 void led_thread(void const *argument)
00030 {
00031     rled.period_ms(20);                             //Set period to 20 ms
00032     rled.write(1.0f);                               //Initialize to 0% duty cycle
00033     gled.period_ms(20);                             //Set period to 20 ms
00034     gled.write(1.0f);                               //Initialize to 0% duty cycle
00035     bled.period_ms(20);                             //Set period to 20 ms
00036     bled.write(1.0f);                               //Initialize to 0% duty cycle
00037     while (true) {
00038         osEvent evt = mbox.get();                   //Wait for a message
00039         if(evt.status == osEventMail) {
00040             message_t *mymessage = (message_t*)evt.value.p;
00041             rled = 1.0f - mymessage->red;
00042             gled = 1.0f - mymessage->green;
00043             bled = 1.0f - mymessage->blue;
00044             mbox.free(mymessage);                  //Free up memory
00045         }
00046     }
00047 }
00048 
00049 float frand(void)
00050 {
00051     int32_t rv = 0x8000 -(rand()&0xFFFF);
00052     return (rv*rv/1073741824.0f);
00053 }
00054 
00055 int main (void)
00056 {
00057     float RGB1[3];
00058     float RGB2[3];
00059     float INC[3];
00060     Thread thread2(led_thread);
00061 //--- Create a random color ---------------------
00062     for (int x=0; x<3; x++) {
00063         RGB1[x] = frand();
00064     }
00065 
00066     while (true) {
00067 //--- Create a new random color -----------------
00068         for (int x=0; x<3; x++) {
00069             RGB2[x] = frand();
00070         }
00071 //--- Determine increments to go from color 1 to color 2 in 25 steps
00072         for (int x=0; x<3; x++) {
00073             INC[x] = (RGB1[x] - RGB2[x]) / 25;
00074         }       
00075 //--- Send color codes to thread2 ---------------
00076         for (int s=0; s<25; s++) {
00077             message_t *message = mbox.alloc();      //Allocate memory
00078             message->red = RGB1[0];
00079             message->green = RGB1[1];
00080             message->blue = RGB1[2];
00081             mbox.put(message);                      //Send data as message
00082             Thread::wait(100);
00083             for (int x=0; x<3; x++) {
00084                 RGB1[x] -= INC[x];                  //Approach to second colour
00085             }
00086         }
00087     }
00088 }