done pool

Dependencies:   C12832 mbed-rtos mbed

Fork of rtos_queue by mbed official

Committer:
emilmont
Date:
Fri Jul 13 11:22:59 2012 +0000
Revision:
1:d2ba5afbf91f
Parent:
0:e0607f77b5bc
Child:
3:c490e2d69dd8
First implementation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 1:d2ba5afbf91f 1 #include "mbed.h"
emilmont 1:d2ba5afbf91f 2 #include "rtos.h"
emilmont 1:d2ba5afbf91f 3
emilmont 1:d2ba5afbf91f 4 typedef struct {
emilmont 1:d2ba5afbf91f 5 float voltage; /* AD result of measured voltage */
emilmont 1:d2ba5afbf91f 6 float current; /* AD result of measured current */
emilmont 1:d2ba5afbf91f 7 uint32_t counter; /* A counter value */
emilmont 1:d2ba5afbf91f 8 } message_t;
emilmont 1:d2ba5afbf91f 9
emilmont 1:d2ba5afbf91f 10 MemoryPool<message_t, 16> mpool;
emilmont 1:d2ba5afbf91f 11 Queue<message_t, 16> queue;
emilmont 1:d2ba5afbf91f 12
emilmont 1:d2ba5afbf91f 13 /* Send Thread */
emilmont 1:d2ba5afbf91f 14 void send_thread (void const *argument) {
emilmont 1:d2ba5afbf91f 15 uint32_t i = 0;
emilmont 1:d2ba5afbf91f 16 while (true) {
emilmont 1:d2ba5afbf91f 17 i++; // fake data update
emilmont 1:d2ba5afbf91f 18 message_t *message = mpool.alloc();
emilmont 1:d2ba5afbf91f 19 message->voltage = (i * 0.1) * 33;
emilmont 1:d2ba5afbf91f 20 message->current = (i * 0.1) * 11;
emilmont 1:d2ba5afbf91f 21 message->counter = i;
emilmont 1:d2ba5afbf91f 22 queue.put(message);
emilmont 1:d2ba5afbf91f 23 Thread::wait(1000);
emilmont 1:d2ba5afbf91f 24 }
emilmont 1:d2ba5afbf91f 25 }
emilmont 1:d2ba5afbf91f 26
emilmont 1:d2ba5afbf91f 27 int main (void) {
emilmont 1:d2ba5afbf91f 28 Thread thread(send_thread);
emilmont 1:d2ba5afbf91f 29
emilmont 1:d2ba5afbf91f 30 while (true) {
emilmont 1:d2ba5afbf91f 31 osEvent evt = queue.get();
emilmont 1:d2ba5afbf91f 32 if (evt.status == osEventMessage) {
emilmont 1:d2ba5afbf91f 33 message_t *message = (message_t*)evt.value.p;
emilmont 1:d2ba5afbf91f 34 printf("\nVoltage: %.2f V\n\r" , message->voltage);
emilmont 1:d2ba5afbf91f 35 printf("Current: %.2f A\n\r" , message->current);
emilmont 1:d2ba5afbf91f 36 printf("Number of cycles: %u\n\r", message->counter);
emilmont 1:d2ba5afbf91f 37
emilmont 1:d2ba5afbf91f 38 mpool.free(message);
emilmont 1:d2ba5afbf91f 39 }
emilmont 1:d2ba5afbf91f 40 }
emilmont 1:d2ba5afbf91f 41 }