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.
main.cpp@5:b19a92f1f5ec, 2020-10-13 (annotated)
- Committer:
- jasonberry
- Date:
- Tue Oct 13 14:06:59 2020 +0000
- Revision:
- 5:b19a92f1f5ec
- Parent:
- 3:c490e2d69dd8
serial plotter example
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" |
jasonberry | 5:b19a92f1f5ec | 3 | #include "LM75B.h" |
jasonberry | 5:b19a92f1f5ec | 4 | #include "MMA7660.h" |
emilmont | 1:d2ba5afbf91f | 5 | |
jasonberry | 5:b19a92f1f5ec | 6 | MMA7660 MMA(p28, p27); //I2C Accelerometer |
jasonberry | 5:b19a92f1f5ec | 7 | LM75B sensor(p28,p27); |
jasonberry | 5:b19a92f1f5ec | 8 | |
jasonberry | 5:b19a92f1f5ec | 9 | //serial data |
jasonberry | 5:b19a92f1f5ec | 10 | //////////////////////////////////////////////////////////// |
jasonberry | 5:b19a92f1f5ec | 11 | //typedef struct { |
jasonberry | 5:b19a92f1f5ec | 12 | // float voltage; /* AD result of measured voltage */ |
jasonberry | 5:b19a92f1f5ec | 13 | // float current; /* AD result of measured current */ |
jasonberry | 5:b19a92f1f5ec | 14 | // uint32_t counter; /* A counter value */ |
jasonberry | 5:b19a92f1f5ec | 15 | //} message_t; |
jasonberry | 5:b19a92f1f5ec | 16 | |
jasonberry | 5:b19a92f1f5ec | 17 | //plotter data temperature |
jasonberry | 5:b19a92f1f5ec | 18 | ////////////////////////////////////////////////////////////// |
jasonberry | 5:b19a92f1f5ec | 19 | //typedef struct { |
jasonberry | 5:b19a92f1f5ec | 20 | // uint32_t temperature; /* AD result of measured voltage */ |
jasonberry | 5:b19a92f1f5ec | 21 | //} message_t; |
jasonberry | 5:b19a92f1f5ec | 22 | |
jasonberry | 5:b19a92f1f5ec | 23 | //plotter data acceleromter |
jasonberry | 5:b19a92f1f5ec | 24 | ////////////////////////////////////////////////////////////// |
emilmont | 1:d2ba5afbf91f | 25 | typedef struct { |
jasonberry | 5:b19a92f1f5ec | 26 | uint32_t x_axis; /* AD result of measured voltage */ |
emilmont | 1:d2ba5afbf91f | 27 | } message_t; |
emilmont | 1:d2ba5afbf91f | 28 | |
emilmont | 1:d2ba5afbf91f | 29 | MemoryPool<message_t, 16> mpool; |
emilmont | 1:d2ba5afbf91f | 30 | Queue<message_t, 16> queue; |
emilmont | 1:d2ba5afbf91f | 31 | |
emilmont | 1:d2ba5afbf91f | 32 | /* Send Thread */ |
emilmont | 3:c490e2d69dd8 | 33 | void send_thread (void const *args) { |
emilmont | 1:d2ba5afbf91f | 34 | uint32_t i = 0; |
emilmont | 1:d2ba5afbf91f | 35 | while (true) { |
emilmont | 1:d2ba5afbf91f | 36 | i++; // fake data update |
emilmont | 1:d2ba5afbf91f | 37 | message_t *message = mpool.alloc(); |
jasonberry | 5:b19a92f1f5ec | 38 | |
jasonberry | 5:b19a92f1f5ec | 39 | //fake message |
jasonberry | 5:b19a92f1f5ec | 40 | //message->voltage = (i * 0.1) * 33; |
jasonberry | 5:b19a92f1f5ec | 41 | //message->current = (i * 0.1) * 11; |
jasonberry | 5:b19a92f1f5ec | 42 | //message->counter = i; |
jasonberry | 5:b19a92f1f5ec | 43 | |
jasonberry | 5:b19a92f1f5ec | 44 | //temperature message |
jasonberry | 5:b19a92f1f5ec | 45 | //message->temperature = (uint32_t)sensor; |
jasonberry | 5:b19a92f1f5ec | 46 | |
jasonberry | 5:b19a92f1f5ec | 47 | //acceleration message |
jasonberry | 5:b19a92f1f5ec | 48 | message->x_axis = (uint32_t)(MMA.x()*100); |
jasonberry | 5:b19a92f1f5ec | 49 | |
emilmont | 1:d2ba5afbf91f | 50 | queue.put(message); |
jasonberry | 5:b19a92f1f5ec | 51 | Thread::wait(10); |
jasonberry | 5:b19a92f1f5ec | 52 | |
jasonberry | 5:b19a92f1f5ec | 53 | |
emilmont | 1:d2ba5afbf91f | 54 | } |
emilmont | 1:d2ba5afbf91f | 55 | } |
emilmont | 1:d2ba5afbf91f | 56 | |
emilmont | 1:d2ba5afbf91f | 57 | int main (void) { |
emilmont | 1:d2ba5afbf91f | 58 | Thread thread(send_thread); |
emilmont | 1:d2ba5afbf91f | 59 | |
jasonberry | 5:b19a92f1f5ec | 60 | sensor.open(); |
jasonberry | 5:b19a92f1f5ec | 61 | MMA.testConnection(); |
jasonberry | 5:b19a92f1f5ec | 62 | |
emilmont | 1:d2ba5afbf91f | 63 | while (true) { |
emilmont | 1:d2ba5afbf91f | 64 | osEvent evt = queue.get(); |
emilmont | 1:d2ba5afbf91f | 65 | if (evt.status == osEventMessage) { |
emilmont | 1:d2ba5afbf91f | 66 | message_t *message = (message_t*)evt.value.p; |
jasonberry | 5:b19a92f1f5ec | 67 | //serial print out |
jasonberry | 5:b19a92f1f5ec | 68 | //printf("\nVoltage: %.2f V\n\r" , message->voltage); |
jasonberry | 5:b19a92f1f5ec | 69 | //printf("Current: %.2f A\n\r" , message->current); |
jasonberry | 5:b19a92f1f5ec | 70 | //printf("Number of cycles: %u\n\r", message->counter); |
jasonberry | 5:b19a92f1f5ec | 71 | |
emilmont | 1:d2ba5afbf91f | 72 | |
jasonberry | 5:b19a92f1f5ec | 73 | //serial plotter start with $ end with ; |
jasonberry | 5:b19a92f1f5ec | 74 | //printf("$%d;\n\r", message->temperature); |
jasonberry | 5:b19a92f1f5ec | 75 | |
jasonberry | 5:b19a92f1f5ec | 76 | //serial plotter start with $ end with ; |
jasonberry | 5:b19a92f1f5ec | 77 | printf("$%d;\n\r", message->x_axis); |
jasonberry | 5:b19a92f1f5ec | 78 | |
emilmont | 1:d2ba5afbf91f | 79 | mpool.free(message); |
emilmont | 1:d2ba5afbf91f | 80 | } |
emilmont | 1:d2ba5afbf91f | 81 | } |
emilmont | 1:d2ba5afbf91f | 82 | } |