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.
Revision 1:5ea1c9f5f9f1, committed 2016-02-24
- Comitter:
- icserny
- Date:
- Wed Feb 24 10:33:52 2016 +0000
- Parent:
- 0:2a0e4f8a7b76
- Commit message:
- First version
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Tue Feb 23 13:32:14 2016 +0000 +++ b/main.cpp Wed Feb 24 10:33:52 2016 +0000 @@ -1,13 +1,13 @@ -/** 10_rtos_mempool +/** 10_rtos_mailbox * - * Simple example to use a Mempool object together with a Queue - * object in order to send more complex messages from thread1 to thread2. + * 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 MemoryPool object. - * Thread1 allocates MemoryPool slots, Thread2 should free up + * 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. */ @@ -23,8 +23,8 @@ float blue; } message_t; -MemoryPool<message_t, 4> mpool; //Memory pool for data storage -Queue<message_t, 4> queue; //Message queue for 4 items +Mail<message_t, 4> mbox; //Mailboxes for 4 messages + void led_thread(void const *argument) { @@ -35,21 +35,21 @@ bled.period_ms(20); //Set period to 20 ms bled.write(1.0f); //Initialize to 0% duty cycle while (true) { - osEvent evt = queue.get(); //Wait for a message - if(evt.status == osEventMessage) { + osEvent evt = mbox.get(); //Wait for a message + if(evt.status == osEventMail) { message_t *mymessage = (message_t*)evt.value.p; rled = 1.0f - mymessage->red; gled = 1.0f - mymessage->green; bled = 1.0f - mymessage->blue; - mpool.free(mymessage); //Free up memory + mbox.free(mymessage); //Free up memory } } } float frand(void) { - float rv = (float)(rand()&0xFFFF); - return (rv/65536.0f); + int32_t rv = 0x8000 -(rand()&0xFFFF); + return (rv*rv/1073741824.0f); } int main (void) @@ -74,11 +74,11 @@ } //--- Send color codes to thread2 --------------- for (int s=0; s<25; s++) { - message_t *message = mpool.alloc(); //Allocate memory + message_t *message = mbox.alloc(); //Allocate memory message->red = RGB1[0]; message->green = RGB1[1]; message->blue = RGB1[2]; - queue.put(message); //Send data as message + mbox.put(message); //Send data as message Thread::wait(100); for (int x=0; x<3; x++) { RGB1[x] -= INC[x]; //Approach to second colour