mail example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 /* Mail */
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 } mail_t;
00009 
00010 Mail<mail_t, 16> mail_box;
00011 Thread thread;
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         wait(1);
00023     }
00024 }
00025 
00026 int main (void) {
00027     thread.start(callback(send_thread));
00028     
00029     while (true) {
00030         osEvent evt = mail_box.get();
00031         if (evt.status == osEventMail) {
00032             mail_t *mail = (mail_t*)evt.value.p;
00033             printf("\nVoltage: %.2f V\n\r"   , mail->voltage);
00034             printf("Current: %.2f A\n\r"     , mail->current);
00035             printf("Number of cycles: %u\n\r", mail->counter);
00036             
00037             mail_box.free(mail);
00038         }
00039     }
00040 }