![](/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@17:09bea038e7c0, 2021-01-18 (annotated)
- Committer:
- hunter64288
- Date:
- Mon Jan 18 23:28:52 2021 +0000
- Revision:
- 17:09bea038e7c0
- Parent:
- 16:c90044414a96
- Child:
- 18:545a94c4a5b2
Final Commit with comments;
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 | 17:09bea038e7c0 | 4 | |
hunter64288 | 12:441ee5b1cf71 | 5 | |
hunter64288 | 17:09bea038e7c0 | 6 | Serial bluetooth(PA_9, PA_10); //Bluetooth Transmitter , Receiver Pins |
hunter64288 | 17:09bea038e7c0 | 7 | AnalogIn ain(PA_0); //Potentiometer Analog Input |
hunter64288 | 17:09bea038e7c0 | 8 | LCD_DISCO_F429ZI lcd; //LCD Library |
hunter64288 | 15:9c5fb6600570 | 9 | |
hunter64288 | 17:09bea038e7c0 | 10 | DigitalOut led1(LED1); //LED Outputs |
emilmont | 1:491820ee784d | 11 | DigitalOut led2(LED2); |
hunter64288 | 12:441ee5b1cf71 | 12 | |
hunter64288 | 17:09bea038e7c0 | 13 | |
hunter64288 | 17:09bea038e7c0 | 14 | Thread thread1; //Instantiating threads |
hunter64288 | 12:441ee5b1cf71 | 15 | Thread thread2; |
hunter64288 | 13:d21f6477ba19 | 16 | Thread thread3; |
hunter64288 | 13:d21f6477ba19 | 17 | Thread thread4; |
hunter64288 | 17:09bea038e7c0 | 18 | |
hunter64288 | 17:09bea038e7c0 | 19 | typedef struct { |
hunter64288 | 17:09bea038e7c0 | 20 | float voltage; |
hunter64288 | 17:09bea038e7c0 | 21 | float current; |
hunter64288 | 17:09bea038e7c0 | 22 | } results; //struct type for the results |
hunter64288 | 17:09bea038e7c0 | 23 | |
hunter64288 | 17:09bea038e7c0 | 24 | MemoryPool<results, 1> mpool; //Creating a memory pool for queue |
hunter64288 | 17:09bea038e7c0 | 25 | Queue<results, 1> queue; //Queue for data transfer |
hunter64288 | 17:09bea038e7c0 | 26 | |
hunter64288 | 17:09bea038e7c0 | 27 | Semaphore one_slot(1); //Instantiating a single semaphore resource slot |
hunter64288 | 17:09bea038e7c0 | 28 | |
hunter64288 | 17:09bea038e7c0 | 29 | |
emilmont | 1:491820ee784d | 30 | |
hunter64288 | 17:09bea038e7c0 | 31 | |
hunter64288 | 17:09bea038e7c0 | 32 | |
hunter64288 | 17:09bea038e7c0 | 33 | /*The following functions gets the ADC data & sends the results to the receiver end via memory queue |
hunter64288 | 17:09bea038e7c0 | 34 | Input Parameter : Thread Name */ |
hunter64288 | 17:09bea038e7c0 | 35 | |
hunter64288 | 15:9c5fb6600570 | 36 | void data_send(void const *name) { |
hunter64288 | 17:09bea038e7c0 | 37 | lcd.Clear(LCD_COLOR_WHITE); //LCD Background Clear |
hunter64288 | 17:09bea038e7c0 | 38 | lcd.DisplayStringAt(0, LINE(1), (uint8_t *)"Sending Data ... ", CENTER_MODE); //Display info on LCD |
hunter64288 | 14:526e0b503646 | 39 | |
hunter64288 | 12:441ee5b1cf71 | 40 | while(1) { |
hunter64288 | 17:09bea038e7c0 | 41 | led1 = !led1; //LED1 toggle |
hunter64288 | 17:09bea038e7c0 | 42 | bluetooth.printf("\n(%s has received the data from ADC, now sending it via Queue...)\n\r", (const char *)name); |
hunter64288 | 17:09bea038e7c0 | 43 | |
hunter64288 | 17:09bea038e7c0 | 44 | //Thread-1 reads the ADC Data |
hunter64288 | 17:09bea038e7c0 | 45 | results *message = mpool.alloc(); //allocating fixed size memory |
hunter64288 | 17:09bea038e7c0 | 46 | message->voltage = ain.read()*3; //reference voltage = 3v |
hunter64288 | 15:9c5fb6600570 | 47 | message->current = (ain.read()*3)/100; |
hunter64288 | 17:09bea038e7c0 | 48 | queue.put(message); //Adding data to the queue |
hunter64288 | 17:09bea038e7c0 | 49 | Thread::wait(2000); |
hunter64288 | 12:441ee5b1cf71 | 50 | } |
hunter64288 | 12:441ee5b1cf71 | 51 | } |
hunter64288 | 13:d21f6477ba19 | 52 | |
hunter64288 | 15:9c5fb6600570 | 53 | |
hunter64288 | 17:09bea038e7c0 | 54 | /*The following function receives the data from the memory queue & |
hunter64288 | 17:09bea038e7c0 | 55 | transmits the data to be displayed over the phone using the Bluetooth module |
hunter64288 | 17:09bea038e7c0 | 56 | Input Parameter : Thread Name */ |
hunter64288 | 15:9c5fb6600570 | 57 | |
hunter64288 | 17:09bea038e7c0 | 58 | void data_receive(void const *name) { |
hunter64288 | 17:09bea038e7c0 | 59 | lcd.DisplayStringAt(0, LINE(2), (uint8_t *)"Data Received !!", CENTER_MODE); //Display info on LCD |
hunter64288 | 17:09bea038e7c0 | 60 | |
hunter64288 | 17:09bea038e7c0 | 61 | while(1) { |
hunter64288 | 17:09bea038e7c0 | 62 | led2 = !led2; //LED2 toggle |
hunter64288 | 17:09bea038e7c0 | 63 | |
hunter64288 | 17:09bea038e7c0 | 64 | one_slot.wait(); //wait until a semaphore resource becomes available |
hunter64288 | 17:09bea038e7c0 | 65 | |
hunter64288 | 16:c90044414a96 | 66 | bluetooth.printf("\n---------------------------------------------------------------------\n"); |
hunter64288 | 16:c90044414a96 | 67 | bluetooth.printf("%s has acquired the semaphore & received the data.\n\r", (const char *)name); |
hunter64288 | 16:c90044414a96 | 68 | bluetooth.printf("\nResults are now displayed by %s\n\r", (const char *)name); |
hunter64288 | 12:441ee5b1cf71 | 69 | Thread::wait(1000); |
hunter64288 | 15:9c5fb6600570 | 70 | |
hunter64288 | 17:09bea038e7c0 | 71 | /*Receive queue data and display via Bluetooth*/ |
hunter64288 | 17:09bea038e7c0 | 72 | |
hunter64288 | 17:09bea038e7c0 | 73 | osEvent evt = queue.get(); // get a message from the queue |
hunter64288 | 17:09bea038e7c0 | 74 | |
hunter64288 | 17:09bea038e7c0 | 75 | //check for the message |
hunter64288 | 15:9c5fb6600570 | 76 | if (evt.status == osEventMessage) { |
hunter64288 | 17:09bea038e7c0 | 77 | results *message = (results*)evt.value.p; |
hunter64288 | 16:c90044414a96 | 78 | bluetooth.printf("\nVoltage: %.2f V\n\r" , message->voltage); |
hunter64288 | 16:c90044414a96 | 79 | bluetooth.printf("Current: %.2f A\n\r" , message->current); |
hunter64288 | 15:9c5fb6600570 | 80 | mpool.free(message);} |
hunter64288 | 15:9c5fb6600570 | 81 | |
hunter64288 | 17:09bea038e7c0 | 82 | one_slot.release(); //release the semaphore resource |
hunter64288 | 16:c90044414a96 | 83 | bluetooth.printf("\n%s releases semaphore\r", (const char *)name); |
hunter64288 | 15:9c5fb6600570 | 84 | Thread::wait(1000); |
hunter64288 | 15:9c5fb6600570 | 85 | |
hunter64288 | 13:d21f6477ba19 | 86 | } |
hunter64288 | 15:9c5fb6600570 | 87 | } |
hunter64288 | 12:441ee5b1cf71 | 88 | |
hunter64288 | 14:526e0b503646 | 89 | |
emilmont | 1:491820ee784d | 90 | int main() { |
hunter64288 | 12:441ee5b1cf71 | 91 | printf("We are in main.\n\r"); |
hunter64288 | 12:441ee5b1cf71 | 92 | |
hunter64288 | 17:09bea038e7c0 | 93 | thread1.set_priority(osPriorityHigh); //setting Task Priorities |
hunter64288 | 17:09bea038e7c0 | 94 | thread2.set_priority(osPriorityNormal); |
hunter64288 | 17:09bea038e7c0 | 95 | thread3.set_priority(osPriorityNormal); |
hunter64288 | 12:441ee5b1cf71 | 96 | |
hunter64288 | 15:9c5fb6600570 | 97 | thread1.start(data_send, (void *)"Thread 1"); // Starting the processes |
hunter64288 | 15:9c5fb6600570 | 98 | thread2.start(data_receive, (void *)"Thread 2"); |
hunter64288 | 15:9c5fb6600570 | 99 | thread3.start(data_receive, (void *)"Thread 3"); |
hunter64288 | 17:09bea038e7c0 | 100 | |
hunter64288 | 17:09bea038e7c0 | 101 | thread1.join(); //synchronization of threads |
hunter64288 | 12:441ee5b1cf71 | 102 | thread2.join(); |
hunter64288 | 13:d21f6477ba19 | 103 | thread3.join(); |
hunter64288 | 12:441ee5b1cf71 | 104 | |
hunter64288 | 12:441ee5b1cf71 | 105 | //wait for threads to complete |
hunter64288 | 12:441ee5b1cf71 | 106 | printf("The end of main.\n\r"); |
emilmont | 1:491820ee784d | 107 | } |
hunter64288 | 12:441ee5b1cf71 | 108 | |
hunter64288 | 12:441ee5b1cf71 | 109 |