main.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include "mbed.h"
  2. #include "cmsis_os.h"
  3. typedef struct {
  4. float voltage; /* AD result of measured voltage */
  5. float current; /* AD result of measured current */
  6. uint32_t counter; /* A counter value */
  7. } mail_t;
  8. osMailQDef(mail_box, 16, mail_t);
  9. osMailQId mail_box;
  10. void send_thread (void const *argument) {
  11. uint32_t i = 0;
  12. while (true) {
  13. i++; // fake data update
  14. mail_t *mail = (mail_t*)osMailAlloc(mail_box, osWaitForever);
  15. mail->voltage = (i * 0.1) * 33;
  16. mail->current = (i * 0.1) * 11;
  17. mail->counter = i;
  18. osMailPut(mail_box, mail);
  19. osDelay(1000);
  20. }
  21. }
  22. osThreadDef(send_thread, osPriorityNormal, DEFAULT_STACK_SIZE);
  23. int main (void) {
  24. mail_box = osMailCreate(osMailQ(mail_box), NULL);
  25. osThreadCreate(osThread(send_thread), NULL);
  26. while (true) {
  27. osEvent evt = osMailGet(mail_box, osWaitForever);
  28. if (evt.status == osEventMail) {
  29. mail_t *mail = (mail_t*)evt.value.p;
  30. printf("\nVoltage: %.2f V\n\r" , mail->voltage);
  31. printf("Current: %.2f A\n\r" , mail->current);
  32. printf("Number of cycles: %u\n\r", mail->counter);
  33. osMailFree(mail_box, mail);
  34. }
  35. }
  36. }