Mail 类层次结构
Mail 的工作方式类似于队列,还有一个额外的好处,即提供用于分配消息的内存池(不仅仅是指针)。
Mail 类参考
rtos::Mail< T, queue_sz > 类模板参考
公共成员函数 | |
Mail () | |
bool | empty () const |
bool | full () const |
T * | alloc (uint32_t millisec=0) |
T * | calloc (uint32_t millisec=0) |
osStatus | put (T *mptr) |
osEvent | get (uint32_t millisec=osWaitForever) |
osStatus | free (T *mptr) |
Mail 示例
此代码使用邮件来管理度量。
#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);
}
}
}