Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
7148_Labs_2223/Session5/Mail/src/main.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
44 lines (38 sloc)
1.13 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) { | |
// Non Blocking we coculd use try_get_for() to block | |
// Will also become blocking in a future release | |
osEvent evt = mail_box.get(); | |
if (evt.status == osEventMail) { | |
mail_t *mail = (mail_t *)evt.value.p; | |
printf("\nVoltage: %0.2f V\n\r", mail->voltage); | |
printf("Current: %0.2f A\n\r", mail->current); | |
printf("Number of cycles: %lu\n\r", mail->counter); | |
mail_box.free(mail); | |
} | |
} | |
} |