Skip to content

add rtos Mail example #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions APIs_RTOS/Mail/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Mail example

This example shows how to use Mail for threads communication.

44 changes: 44 additions & 0 deletions APIs_RTOS/Mail/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
#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);
ThisThread::sleep_for(1000);
}
}

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: %lu\n\r", mail->counter);

mail_box.free(mail);
}
}
}