Basic example showing the Queue and MemoryPool API

Dependencies:   mbed-rtos mbed

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 
00004 typedef struct {
00005     float    voltage;   /* AD result of measured voltage */
00006     float    current;   /* AD result of measured current */
00007     uint32_t counter;   /* A counter value               */
00008 } message_t;
00009 
00010 MemoryPool<message_t, 16> mpool;
00011 Queue<message_t, 16> queue;
00012 
00013 /* Send Thread */
00014 void send_thread (void) {
00015     uint32_t i = 0;
00016     while (true) {
00017         i++; // fake data update
00018         message_t *message = mpool.alloc();
00019         message->voltage = (i * 0.1) * 33; 
00020         message->current = (i * 0.1) * 11;
00021         message->counter = i;
00022         queue.put(message);
00023         Thread::wait(1000);
00024     }
00025 }
00026 
00027 int main (void) {
00028     Thread thread;
00029     thread.start(callback(send_thread));
00030     
00031     while (true) {
00032         osEvent evt = queue.get();
00033         if (evt.status == osEventMessage) {
00034             message_t *message = (message_t*)evt.value.p;
00035             printf("\nVoltage: %.2f V\n\r"   , message->voltage);
00036             printf("Current: %.2f A\n\r"     , message->current);
00037             printf("Number of cycles: %u\n\r", message->counter);
00038             
00039             mpool.free(message);
00040         }
00041     }
00042 }