![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
A mbed RTOS based multimeter application that sends results to a phone over the Bluetooth module.
Dependencies: LCD_DISCO_F429ZI mbed mbed-rtos BSP_DISCO_F429ZI
main.cpp@15:9c5fb6600570, 2021-01-17 (annotated)
- Committer:
- hunter64288
- Date:
- Sun Jan 17 22:37:48 2021 +0000
- Revision:
- 15:9c5fb6600570
- Parent:
- 14:526e0b503646
- Child:
- 16:c90044414a96
create a 2nd function & 3rd thread; ; 2nd func would receive the voltage & current details from the 1st func via memory ; queue , 2nd & 3rd thread would print those details using semaphore.; ; 1st func would only receive the values using thread1.;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 1:491820ee784d | 1 | #include "mbed.h" |
mbed_official | 11:0309bef74ba8 | 2 | #include "rtos.h" |
hunter64288 | 12:441ee5b1cf71 | 3 | #include "LCD_DISCO_F429ZI.h" |
hunter64288 | 12:441ee5b1cf71 | 4 | //#include <AnalogIn.h> |
hunter64288 | 12:441ee5b1cf71 | 5 | |
hunter64288 | 15:9c5fb6600570 | 6 | typedef struct { |
hunter64288 | 15:9c5fb6600570 | 7 | float voltage; /* AD result of measured voltage */ |
hunter64288 | 15:9c5fb6600570 | 8 | float current; /* AD result of measured current */ |
hunter64288 | 15:9c5fb6600570 | 9 | } message_t; |
hunter64288 | 15:9c5fb6600570 | 10 | |
hunter64288 | 15:9c5fb6600570 | 11 | MemoryPool<message_t, 16> mpool; |
hunter64288 | 15:9c5fb6600570 | 12 | Queue<message_t, 16> queue; |
hunter64288 | 15:9c5fb6600570 | 13 | |
hunter64288 | 14:526e0b503646 | 14 | Semaphore one_slot(1); |
hunter64288 | 14:526e0b503646 | 15 | //InterruptIn button(PA_0); |
hunter64288 | 12:441ee5b1cf71 | 16 | |
hunter64288 | 13:d21f6477ba19 | 17 | Serial bluetooth(PA_9, PA_10); //Blueutooth Tx, Rx |
hunter64288 | 14:526e0b503646 | 18 | AnalogIn ain(PA_0); //Potentiometer Input |
hunter64288 | 12:441ee5b1cf71 | 19 | |
emilmont | 1:491820ee784d | 20 | DigitalOut led1(LED1); |
emilmont | 1:491820ee784d | 21 | DigitalOut led2(LED2); |
hunter64288 | 12:441ee5b1cf71 | 22 | |
hunter64288 | 13:d21f6477ba19 | 23 | LCD_DISCO_F429ZI lcd; //LCD Library |
hunter64288 | 13:d21f6477ba19 | 24 | Thread thread1; //threads |
hunter64288 | 12:441ee5b1cf71 | 25 | Thread thread2; |
hunter64288 | 13:d21f6477ba19 | 26 | Thread thread3; |
hunter64288 | 13:d21f6477ba19 | 27 | Thread thread4; |
emilmont | 1:491820ee784d | 28 | |
hunter64288 | 15:9c5fb6600570 | 29 | void data_send(void const *name) { |
hunter64288 | 12:441ee5b1cf71 | 30 | lcd.Clear(LCD_COLOR_WHITE); |
hunter64288 | 15:9c5fb6600570 | 31 | lcd.DisplayStringAt(0, LINE(1), (uint8_t *)"Sending Data ... ", CENTER_MODE); |
hunter64288 | 12:441ee5b1cf71 | 32 | |
hunter64288 | 15:9c5fb6600570 | 33 | //float samples; //float samples[10]; |
hunter64288 | 15:9c5fb6600570 | 34 | |
hunter64288 | 14:526e0b503646 | 35 | |
hunter64288 | 12:441ee5b1cf71 | 36 | while(1) { |
hunter64288 | 15:9c5fb6600570 | 37 | led1 = !led1; |
hunter64288 | 15:9c5fb6600570 | 38 | //ain.set_reference_voltage(3.0); |
hunter64288 | 15:9c5fb6600570 | 39 | printf("%s has received the data, now sending it via Queue...\n\r", (const char *)name); |
hunter64288 | 14:526e0b503646 | 40 | |
hunter64288 | 15:9c5fb6600570 | 41 | message_t *message = mpool.alloc(); |
hunter64288 | 15:9c5fb6600570 | 42 | message->voltage = ain.read()*3; |
hunter64288 | 15:9c5fb6600570 | 43 | message->current = (ain.read()*3)/100; |
hunter64288 | 15:9c5fb6600570 | 44 | queue.put(message); |
hunter64288 | 15:9c5fb6600570 | 45 | wait(0.001f); |
hunter64288 | 15:9c5fb6600570 | 46 | Thread::wait(2000); |
hunter64288 | 15:9c5fb6600570 | 47 | |
hunter64288 | 15:9c5fb6600570 | 48 | //samples = ain.read()*3; |
hunter64288 | 15:9c5fb6600570 | 49 | //wait(0.001f); |
hunter64288 | 15:9c5fb6600570 | 50 | |
hunter64288 | 15:9c5fb6600570 | 51 | /*for(int i=0; i<10; i++) { |
hunter64288 | 12:441ee5b1cf71 | 52 | //ain.set_reference_voltage(3.0); |
hunter64288 | 12:441ee5b1cf71 | 53 | samples[i] = ain.read()*3; |
hunter64288 | 12:441ee5b1cf71 | 54 | wait(0.001f); |
hunter64288 | 15:9c5fb6600570 | 55 | }*/ |
hunter64288 | 12:441ee5b1cf71 | 56 | |
hunter64288 | 15:9c5fb6600570 | 57 | //bluetooth.printf("Voltage: %f\n", samples); |
hunter64288 | 15:9c5fb6600570 | 58 | |
hunter64288 | 15:9c5fb6600570 | 59 | /*bluetooth.printf("Results:\n"); |
hunter64288 | 12:441ee5b1cf71 | 60 | for(int i=0; i<10; i++) { |
hunter64288 | 12:441ee5b1cf71 | 61 | bluetooth.printf("%d = , %f\n", i, samples[i]); |
hunter64288 | 15:9c5fb6600570 | 62 | }*/ |
hunter64288 | 15:9c5fb6600570 | 63 | |
hunter64288 | 12:441ee5b1cf71 | 64 | } |
hunter64288 | 12:441ee5b1cf71 | 65 | } |
hunter64288 | 13:d21f6477ba19 | 66 | |
hunter64288 | 15:9c5fb6600570 | 67 | void data_receive(void const *name) { |
hunter64288 | 15:9c5fb6600570 | 68 | lcd.DisplayStringAt(0, LINE(2), (uint8_t *)"Data Received !!", CENTER_MODE); |
hunter64288 | 15:9c5fb6600570 | 69 | |
hunter64288 | 15:9c5fb6600570 | 70 | |
hunter64288 | 15:9c5fb6600570 | 71 | |
hunter64288 | 12:441ee5b1cf71 | 72 | while(true) { |
hunter64288 | 12:441ee5b1cf71 | 73 | //float i=0; |
hunter64288 | 14:526e0b503646 | 74 | led2 = !led2; |
hunter64288 | 15:9c5fb6600570 | 75 | one_slot.wait(); |
hunter64288 | 15:9c5fb6600570 | 76 | printf("%s has acquired the semaphore & received the data\n\r", (const char *)name); |
hunter64288 | 15:9c5fb6600570 | 77 | printf("Results are displayed by %s\n\r", (const char *)name); |
hunter64288 | 12:441ee5b1cf71 | 78 | Thread::wait(1000); |
hunter64288 | 15:9c5fb6600570 | 79 | |
hunter64288 | 15:9c5fb6600570 | 80 | osEvent evt = queue.get(); |
hunter64288 | 15:9c5fb6600570 | 81 | if (evt.status == osEventMessage) { |
hunter64288 | 15:9c5fb6600570 | 82 | message_t *message = (message_t*)evt.value.p; |
hunter64288 | 15:9c5fb6600570 | 83 | printf("\nVoltage: %.2f V\n\r" , message->voltage); |
hunter64288 | 15:9c5fb6600570 | 84 | printf("Current: %.2f A\n\r" , message->current); |
hunter64288 | 15:9c5fb6600570 | 85 | mpool.free(message);} |
hunter64288 | 15:9c5fb6600570 | 86 | |
hunter64288 | 15:9c5fb6600570 | 87 | one_slot.release(); |
hunter64288 | 15:9c5fb6600570 | 88 | printf("%s releases semaphore\r", (const char *)name); |
hunter64288 | 15:9c5fb6600570 | 89 | printf("\n-----------------------\n"); |
hunter64288 | 15:9c5fb6600570 | 90 | Thread::wait(1000); |
hunter64288 | 15:9c5fb6600570 | 91 | // bluetooth.printf("Hello World !\r\n"); |
hunter64288 | 15:9c5fb6600570 | 92 | //bluetooth.printf("This program runs since %f seconds.\r\n", i++); |
hunter64288 | 15:9c5fb6600570 | 93 | |
hunter64288 | 13:d21f6477ba19 | 94 | } |
hunter64288 | 15:9c5fb6600570 | 95 | |
hunter64288 | 15:9c5fb6600570 | 96 | } |
hunter64288 | 12:441ee5b1cf71 | 97 | |
hunter64288 | 14:526e0b503646 | 98 | /*void btn_int(){ |
hunter64288 | 14:526e0b503646 | 99 | |
hunter64288 | 14:526e0b503646 | 100 | one_slot.release(); |
hunter64288 | 14:526e0b503646 | 101 | }*/ |
hunter64288 | 14:526e0b503646 | 102 | |
hunter64288 | 14:526e0b503646 | 103 | /*void semaphore_test(void const *name) |
hunter64288 | 13:d21f6477ba19 | 104 | { |
hunter64288 | 13:d21f6477ba19 | 105 | while (true) { |
hunter64288 | 14:526e0b503646 | 106 | one_slot.wait(); |
hunter64288 | 14:526e0b503646 | 107 | |
hunter64288 | 14:526e0b503646 | 108 | printf("%s acquires semaphore\n\r", (const char *)name); |
hunter64288 | 14:526e0b503646 | 109 | Thread::wait(1000); |
hunter64288 | 13:d21f6477ba19 | 110 | one_slot.release(); |
hunter64288 | 14:526e0b503646 | 111 | printf("%s releases semaphore\n\r", (const char *)name); |
hunter64288 | 13:d21f6477ba19 | 112 | } |
hunter64288 | 14:526e0b503646 | 113 | }*/ |
emilmont | 1:491820ee784d | 114 | |
emilmont | 1:491820ee784d | 115 | int main() { |
hunter64288 | 12:441ee5b1cf71 | 116 | printf("We are in main.\n\r"); |
hunter64288 | 12:441ee5b1cf71 | 117 | |
hunter64288 | 12:441ee5b1cf71 | 118 | thread1.set_priority(osPriorityHigh); |
hunter64288 | 15:9c5fb6600570 | 119 | thread2.set_priority(osPriorityLow); |
hunter64288 | 15:9c5fb6600570 | 120 | thread3.set_priority(osPriorityLow); |
hunter64288 | 14:526e0b503646 | 121 | //thread4.set_priority(osPriorityLow); |
hunter64288 | 12:441ee5b1cf71 | 122 | |
hunter64288 | 15:9c5fb6600570 | 123 | thread1.start(data_send, (void *)"Thread 1"); // Starting the processes |
hunter64288 | 15:9c5fb6600570 | 124 | thread2.start(data_receive, (void *)"Thread 2"); |
emilmont | 1:491820ee784d | 125 | |
hunter64288 | 14:526e0b503646 | 126 | //button.rise(&btn_int); |
hunter64288 | 15:9c5fb6600570 | 127 | thread3.start(data_receive, (void *)"Thread 3"); |
hunter64288 | 14:526e0b503646 | 128 | //thread4.start(semaphore_test, (void *)"Thread 4"); |
hunter64288 | 13:d21f6477ba19 | 129 | |
hunter64288 | 12:441ee5b1cf71 | 130 | thread1.join(); |
hunter64288 | 12:441ee5b1cf71 | 131 | thread2.join(); |
hunter64288 | 13:d21f6477ba19 | 132 | thread3.join(); |
hunter64288 | 13:d21f6477ba19 | 133 | thread4.join(); |
hunter64288 | 12:441ee5b1cf71 | 134 | |
hunter64288 | 12:441ee5b1cf71 | 135 | //wait for threads to complete |
hunter64288 | 12:441ee5b1cf71 | 136 | printf("The end of main.\n\r"); |
emilmont | 1:491820ee784d | 137 | } |
hunter64288 | 12:441ee5b1cf71 | 138 | |
hunter64288 | 12:441ee5b1cf71 | 139 |