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