Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
libraries/tests/rtos/cmsis/mail/main.cpp@0:6c56fb4bc5f0, 2016-11-04 (annotated)
- Committer:
- nexpaq
- Date:
- Fri Nov 04 20:27:58 2016 +0000
- Revision:
- 0:6c56fb4bc5f0
Moving to library for sharing updates
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| nexpaq | 0:6c56fb4bc5f0 | 1 | #include "mbed.h" |
| nexpaq | 0:6c56fb4bc5f0 | 2 | #include "cmsis_os.h" |
| nexpaq | 0:6c56fb4bc5f0 | 3 | |
| nexpaq | 0:6c56fb4bc5f0 | 4 | #if defined(MBED_RTOS_SINGLE_THREAD) |
| nexpaq | 0:6c56fb4bc5f0 | 5 | #error [NOT_SUPPORTED] test not supported |
| nexpaq | 0:6c56fb4bc5f0 | 6 | #endif |
| nexpaq | 0:6c56fb4bc5f0 | 7 | |
| nexpaq | 0:6c56fb4bc5f0 | 8 | typedef struct { |
| nexpaq | 0:6c56fb4bc5f0 | 9 | float voltage; /* AD result of measured voltage */ |
| nexpaq | 0:6c56fb4bc5f0 | 10 | float current; /* AD result of measured current */ |
| nexpaq | 0:6c56fb4bc5f0 | 11 | uint32_t counter; /* A counter value */ |
| nexpaq | 0:6c56fb4bc5f0 | 12 | } mail_t; |
| nexpaq | 0:6c56fb4bc5f0 | 13 | |
| nexpaq | 0:6c56fb4bc5f0 | 14 | osMailQDef(mail_box, 16, mail_t); |
| nexpaq | 0:6c56fb4bc5f0 | 15 | osMailQId mail_box; |
| nexpaq | 0:6c56fb4bc5f0 | 16 | |
| nexpaq | 0:6c56fb4bc5f0 | 17 | void send_thread (void const *argument) { |
| nexpaq | 0:6c56fb4bc5f0 | 18 | uint32_t i = 0; |
| nexpaq | 0:6c56fb4bc5f0 | 19 | while (true) { |
| nexpaq | 0:6c56fb4bc5f0 | 20 | i++; // fake data update |
| nexpaq | 0:6c56fb4bc5f0 | 21 | mail_t *mail = (mail_t*)osMailAlloc(mail_box, osWaitForever); |
| nexpaq | 0:6c56fb4bc5f0 | 22 | mail->voltage = (i * 0.1) * 33; |
| nexpaq | 0:6c56fb4bc5f0 | 23 | mail->current = (i * 0.1) * 11; |
| nexpaq | 0:6c56fb4bc5f0 | 24 | mail->counter = i; |
| nexpaq | 0:6c56fb4bc5f0 | 25 | osMailPut(mail_box, mail); |
| nexpaq | 0:6c56fb4bc5f0 | 26 | osDelay(1000); |
| nexpaq | 0:6c56fb4bc5f0 | 27 | } |
| nexpaq | 0:6c56fb4bc5f0 | 28 | } |
| nexpaq | 0:6c56fb4bc5f0 | 29 | |
| nexpaq | 0:6c56fb4bc5f0 | 30 | osThreadDef(send_thread, osPriorityNormal, DEFAULT_STACK_SIZE); |
| nexpaq | 0:6c56fb4bc5f0 | 31 | |
| nexpaq | 0:6c56fb4bc5f0 | 32 | int main (void) { |
| nexpaq | 0:6c56fb4bc5f0 | 33 | mail_box = osMailCreate(osMailQ(mail_box), NULL); |
| nexpaq | 0:6c56fb4bc5f0 | 34 | osThreadCreate(osThread(send_thread), NULL); |
| nexpaq | 0:6c56fb4bc5f0 | 35 | |
| nexpaq | 0:6c56fb4bc5f0 | 36 | while (true) { |
| nexpaq | 0:6c56fb4bc5f0 | 37 | osEvent evt = osMailGet(mail_box, osWaitForever); |
| nexpaq | 0:6c56fb4bc5f0 | 38 | if (evt.status == osEventMail) { |
| nexpaq | 0:6c56fb4bc5f0 | 39 | mail_t *mail = (mail_t*)evt.value.p; |
| nexpaq | 0:6c56fb4bc5f0 | 40 | printf("\nVoltage: %.2f V\n\r" , mail->voltage); |
| nexpaq | 0:6c56fb4bc5f0 | 41 | printf("Current: %.2f A\n\r" , mail->current); |
| nexpaq | 0:6c56fb4bc5f0 | 42 | printf("Number of cycles: %u\n\r", mail->counter); |
| nexpaq | 0:6c56fb4bc5f0 | 43 | |
| nexpaq | 0:6c56fb4bc5f0 | 44 | osMailFree(mail_box, mail); |
| nexpaq | 0:6c56fb4bc5f0 | 45 | } |
| nexpaq | 0:6c56fb4bc5f0 | 46 | } |
| nexpaq | 0:6c56fb4bc5f0 | 47 | } |