Mail class hierarchy
Mail works like a queue, with the added benefit of providing a memory pool for allocating messages (not only pointers).

Mail class reference
| Public Member Functions | |
| Mail () | |
| Create and initialize Mail queue. More... | |
| bool | empty () const |
| Check if the mail queue is empty. More... | |
| bool | full () const |
| Check if the mail queue is full. More... | |
| T * | alloc (MBED_UNUSED uint32_t millisec=0) |
| Allocate a memory block of type T, without blocking. More... | |
| T * | alloc_for (uint32_t millisec) |
| Allocate a memory block of type T, optionally blocking. More... | |
| T * | alloc_until (uint64_t millisec) |
| Allocate a memory block of type T, blocking. More... | |
| T * | calloc (MBED_UNUSED uint32_t millisec=0) |
| Allocate a memory block of type T, and set memory block to zero. More... | |
| T * | calloc_for (uint32_t millisec) |
| Allocate a memory block of type T, optionally blocking, and set memory block to zero. More... | |
| T * | calloc_until (uint64_t millisec) |
| Allocate a memory block of type T, blocking, and set memory block to zero. More... | |
| osStatus | put (T *mptr) |
| Put a mail in the queue. More... | |
| osEvent | get (uint32_t millisec=osWaitForever) |
| Get a mail from the queue. More... | |
| osStatus | free (T *mptr) |
| Free a memory block from a mail. More... | |
Mail example
This code uses mail to manage measurement.
#include "mbed.h"
/* Mail */
typedef struct {
float voltage; /* AD result of measured voltage */
float current; /* AD result of measured current */
uint32_t counter; /* A counter value */
} mail_t;
Mail<mail_t, 16> mail_box;
Thread thread;
void send_thread (void) {
uint32_t i = 0;
while (true) {
i++; // fake data update
mail_t *mail = mail_box.alloc();
mail->voltage = (i * 0.1) * 33;
mail->current = (i * 0.1) * 11;
mail->counter = i;
mail_box.put(mail);
wait(1);
}
}
int main (void) {
thread.start(callback(send_thread));
while (true) {
osEvent evt = mail_box.get();
if (evt.status == osEventMail) {
mail_t *mail = (mail_t*)evt.value.p;
printf("\nVoltage: %.2f V\n\r" , mail->voltage);
printf("Current: %.2f A\n\r" , mail->current);
printf("Number of cycles: %u\n\r", mail->counter);
mail_box.free(mail);
}
}
}