![](/media/cache/group/mbed-logo-square-512_3.png.50x50_q85.png)
queue example
main.cpp@5:2c73ccb2a032, 2017-01-11 (annotated)
- Committer:
- Bartek Szatkowski
- Date:
- Wed Jan 11 15:26:27 2017 +0000
- Revision:
- 5:2c73ccb2a032
- Parent:
- 3:c490e2d69dd8
- Child:
- 6:0cb43a362538
Update to current RTOS API
Who changed what in which revision?
User | Revision | Line number | New 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 */ |
Bartek Szatkowski |
5:2c73ccb2a032 | 14 | void send_thread (void) { |
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) { |
Bartek Szatkowski |
5:2c73ccb2a032 | 28 | Thread thread; |
Bartek Szatkowski |
5:2c73ccb2a032 | 29 | thread.start(callback(send_thread)); |
emilmont | 1:d2ba5afbf91f | 30 | |
emilmont | 1:d2ba5afbf91f | 31 | while (true) { |
emilmont | 1:d2ba5afbf91f | 32 | osEvent evt = queue.get(); |
emilmont | 1:d2ba5afbf91f | 33 | if (evt.status == osEventMessage) { |
emilmont | 1:d2ba5afbf91f | 34 | message_t *message = (message_t*)evt.value.p; |
emilmont | 1:d2ba5afbf91f | 35 | printf("\nVoltage: %.2f V\n\r" , message->voltage); |
emilmont | 1:d2ba5afbf91f | 36 | printf("Current: %.2f A\n\r" , message->current); |
emilmont | 1:d2ba5afbf91f | 37 | printf("Number of cycles: %u\n\r", message->counter); |
emilmont | 1:d2ba5afbf91f | 38 | |
emilmont | 1:d2ba5afbf91f | 39 | mpool.free(message); |
emilmont | 1:d2ba5afbf91f | 40 | } |
emilmont | 1:d2ba5afbf91f | 41 | } |
emilmont | 1:d2ba5afbf91f | 42 | } |