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
Diff: main.cpp
- Revision:
- 17:09bea038e7c0
- Parent:
- 16:c90044414a96
- Child:
- 18:545a94c4a5b2
diff -r c90044414a96 -r 09bea038e7c0 main.cpp --- a/main.cpp Sun Jan 17 23:10:24 2021 +0000 +++ b/main.cpp Mon Jan 18 23:28:52 2021 +0000 @@ -1,138 +1,106 @@ #include "mbed.h" #include "rtos.h" #include "LCD_DISCO_F429ZI.h" -//#include <AnalogIn.h> + -typedef struct { - float voltage; /* AD result of measured voltage */ - float current; /* AD result of measured current */ -} message_t; - -MemoryPool<message_t, 16> mpool; -Queue<message_t, 16> queue; +Serial bluetooth(PA_9, PA_10); //Bluetooth Transmitter , Receiver Pins +AnalogIn ain(PA_0); //Potentiometer Analog Input +LCD_DISCO_F429ZI lcd; //LCD Library -Semaphore one_slot(1); -//InterruptIn button(PA_0); - -Serial bluetooth(PA_9, PA_10); //Blueutooth Tx, Rx -AnalogIn ain(PA_0); //Potentiometer Input - -DigitalOut led1(LED1); +DigitalOut led1(LED1); //LED Outputs DigitalOut led2(LED2); -LCD_DISCO_F429ZI lcd; //LCD Library -Thread thread1; //threads + +Thread thread1; //Instantiating threads Thread thread2; Thread thread3; Thread thread4; + +typedef struct { + float voltage; + float current; +} results; //struct type for the results + +MemoryPool<results, 1> mpool; //Creating a memory pool for queue +Queue<results, 1> queue; //Queue for data transfer + +Semaphore one_slot(1); //Instantiating a single semaphore resource slot + + + + +/*The following functions gets the ADC data & sends the results to the receiver end via memory queue +Input Parameter : Thread Name */ + void data_send(void const *name) { - lcd.Clear(LCD_COLOR_WHITE); - lcd.DisplayStringAt(0, LINE(1), (uint8_t *)"Sending Data ... ", CENTER_MODE); - - //float samples; //float samples[10]; - + lcd.Clear(LCD_COLOR_WHITE); //LCD Background Clear + lcd.DisplayStringAt(0, LINE(1), (uint8_t *)"Sending Data ... ", CENTER_MODE); //Display info on LCD while(1) { - led1 = !led1; - //ain.set_reference_voltage(3.0); - bluetooth.printf("\n%s has received the data from ADC, now sending it via Queue...\n\r", (const char *)name); - - message_t *message = mpool.alloc(); - message->voltage = ain.read()*3; + led1 = !led1; //LED1 toggle + bluetooth.printf("\n(%s has received the data from ADC, now sending it via Queue...)\n\r", (const char *)name); + + //Thread-1 reads the ADC Data + results *message = mpool.alloc(); //allocating fixed size memory + message->voltage = ain.read()*3; //reference voltage = 3v message->current = (ain.read()*3)/100; - queue.put(message); - wait(0.001f); - Thread::wait(2000); - - //samples = ain.read()*3; - //wait(0.001f); - - /*for(int i=0; i<10; i++) { - //ain.set_reference_voltage(3.0); - samples[i] = ain.read()*3; - wait(0.001f); - }*/ - - //bluetooth.printf("Voltage: %f\n", samples); - - /*bluetooth.printf("Results:\n"); - for(int i=0; i<10; i++) { - bluetooth.printf("%d = , %f\n", i, samples[i]); - }*/ - + queue.put(message); //Adding data to the queue + Thread::wait(2000); } } -void data_receive(void const *name) { - lcd.DisplayStringAt(0, LINE(2), (uint8_t *)"Data Received !!", CENTER_MODE); - +/*The following function receives the data from the memory queue & +transmits the data to be displayed over the phone using the Bluetooth module +Input Parameter : Thread Name */ - while(true) { - //float i=0; - led2 = !led2; - one_slot.wait(); +void data_receive(void const *name) { + lcd.DisplayStringAt(0, LINE(2), (uint8_t *)"Data Received !!", CENTER_MODE); //Display info on LCD + +while(1) { + led2 = !led2; //LED2 toggle + + one_slot.wait(); //wait until a semaphore resource becomes available + bluetooth.printf("\n---------------------------------------------------------------------\n"); bluetooth.printf("%s has acquired the semaphore & received the data.\n\r", (const char *)name); bluetooth.printf("\nResults are now displayed by %s\n\r", (const char *)name); Thread::wait(1000); - osEvent evt = queue.get(); + /*Receive queue data and display via Bluetooth*/ + + osEvent evt = queue.get(); // get a message from the queue + + //check for the message if (evt.status == osEventMessage) { - message_t *message = (message_t*)evt.value.p; + results *message = (results*)evt.value.p; bluetooth.printf("\nVoltage: %.2f V\n\r" , message->voltage); bluetooth.printf("Current: %.2f A\n\r" , message->current); mpool.free(message);} - one_slot.release(); + one_slot.release(); //release the semaphore resource bluetooth.printf("\n%s releases semaphore\r", (const char *)name); Thread::wait(1000); - //bluetooth.printf("\n-----------------------\n"); - - // bluetooth.printf("Hello World !\r\n"); -//bluetooth.printf("This program runs since %f seconds.\r\n", i++); } - } -/*void btn_int(){ - - one_slot.release(); - }*/ -/*void semaphore_test(void const *name) -{ - while (true) { - one_slot.wait(); - - printf("%s acquires semaphore\n\r", (const char *)name); - Thread::wait(1000); - one_slot.release(); - printf("%s releases semaphore\n\r", (const char *)name); - } -}*/ - int main() { printf("We are in main.\n\r"); - thread1.set_priority(osPriorityHigh); - thread2.set_priority(osPriorityLow); - thread3.set_priority(osPriorityLow); - //thread4.set_priority(osPriorityLow); + thread1.set_priority(osPriorityHigh); //setting Task Priorities + thread2.set_priority(osPriorityNormal); + thread3.set_priority(osPriorityNormal); thread1.start(data_send, (void *)"Thread 1"); // Starting the processes thread2.start(data_receive, (void *)"Thread 2"); - - //button.rise(&btn_int); thread3.start(data_receive, (void *)"Thread 3"); - //thread4.start(semaphore_test, (void *)"Thread 4"); - - thread1.join(); + + thread1.join(); //synchronization of threads thread2.join(); thread3.join(); - thread4.join(); //wait for threads to complete printf("The end of main.\n\r");