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

Committer:
hunter64288
Date:
2021-01-17
Revision:
15:9c5fb6600570
Parent:
14:526e0b503646
Child:
16:c90044414a96

File content as of revision 15:9c5fb6600570:

#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;

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 led2(LED2);

LCD_DISCO_F429ZI lcd;        //LCD Library
Thread thread1;              //threads
Thread thread2;
Thread thread3;
Thread thread4;
 
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];

    
while(1) {
    led1 = !led1;
    //ain.set_reference_voltage(3.0);
    printf("%s has received the data, now sending it via Queue...\n\r", (const char *)name);

    message_t *message = mpool.alloc();
    message->voltage = ain.read()*3;  
    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]);
    }*/
    
}
}

void data_receive(void const *name) {
    lcd.DisplayStringAt(0, LINE(2), (uint8_t *)"Data Received !!", CENTER_MODE);
      


    while(true) {
    //float i=0;
    led2 = !led2;
    one_slot.wait();
    printf("%s has acquired the semaphore & received the data\n\r", (const char *)name);
    printf("Results are displayed by %s\n\r", (const char *)name);
    Thread::wait(1000);

    osEvent evt = queue.get();
    if (evt.status == osEventMessage) {
     message_t *message = (message_t*)evt.value.p;
     printf("\nVoltage: %.2f V\n\r"   , message->voltage);
     printf("Current: %.2f A\n\r"     , message->current);
     mpool.free(message);}

    one_slot.release();
    printf("%s releases semaphore\r", (const char *)name);
    printf("\n-----------------------\n");
    Thread::wait(1000);
   // 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.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();
    thread2.join();
    thread3.join();
    thread4.join();
    
    //wait for threads to complete
    printf("The end of main.\n\r");
}