Buffer working with LCD, Serial and SD Card
Dependencies: BMP280
Fork of Thread_Communication by
Diff: main.cpp
- Revision:
- 7:f017a37bcf1b
- Parent:
- 6:64d346936f0e
- Child:
- 8:ab6322afa341
--- a/main.cpp Tue Dec 26 21:54:41 2017 +0000 +++ b/main.cpp Thu Dec 28 19:32:22 2017 +0000 @@ -1,8 +1,6 @@ #include "mbed.h" #include "main.h" - -#define ENTER_KEY 1 -#define MAX_SAMPLES 4 +#include "stdio.h" LCD lcd(PD_15, PF_12, PF_13, PE_9, PF_14, PF_15); BMP280 Sensor(D14, D15); @@ -10,92 +8,124 @@ //Define Functions void PrintLCD (); void Rx_interrupt(); -void Serial_CMD(); +void serialCMD(); +void sensorRead(); +void readISR(); +void circBuff(); -//data FIFO buffer -char data_buffer[MAX_SAMPLES][64]; -int sample_h = 0; -int sample_t = 0; -int data_h = 0; -int data_t = 0; -struct tm * sample_epoch; +/* LOCKS */ Mutex DataBuffer; +Mutex sensorData; -//Serial_CMD -volatile int rx_in=0; -char rx_buffer[32]; -time_t raw_time = time(NULL); -char serial_buffer[80]; +/* THREADS */ +Thread _PrintLCD, _serialCMD, _circBuff; +Thread _sensorRead (osPriorityRealtime); //sensorData Thread + -/* Mail */ -typedef struct { - float LDR_Value; - float temp_Value; - float press_Value; -} mail_t; +/* GLOBAL DATA */ +volatile float LDR = 0; +volatile double PRES = 0; +volatile double TEMP = 0; -Mail<mail_t, 16> mail_box; +Ticker read; -//Threads -Thread t1; -Thread t2; -Thread S_CMD; - +/* INTERRUPTS */ + /*--------------------------------MAIN--------------------------------*/ int main() { - - t1.start(PrintLCD); pc.baud(9600); pc.attach(&Rx_interrupt, Serial::RxIrq); - S_CMD.start(Serial_CMD); + POST(); + + _serialCMD.start(serialCMD); + _PrintLCD.start(PrintLCD); + _sensorRead.start(sensorRead); + _circBuff.start(circBuff); + + read.attach(readISR, SAMPLING_PERIOD); - while(1) { - Green_int = !Green_int; + + while (1) {} + } +/*--------------------------------------------------------------------*/ +void circBuff () { + + while(1) { + Thread::signal_wait(DATA_READY); // wait for signal from sensorRead - //Read sensors, send to mail-queue - mail_t *mail = mail_box.alloc(); - mail->LDR_Value = LDR_In.read(); - mail->temp_Value = Sensor.getTemperature(); - mail->press_Value = Sensor.getPressure(); - mail_box.put(mail); - - //Lock data buffer + //Lock data buffer DataBuffer.lock(); - //Format samples, send to FIFO buffer head + //Format samples, send to FIFO buffer head memset(data_buffer[sample_h],NULL,64); time( &raw_time ); sample_epoch = localtime( &raw_time ); char sample_time[20]; strftime(sample_time,20,"%d/%m/%Y %X",sample_epoch); - sprintf(data_buffer[sample_h],"%s, %2.2f, %4.2f, %.4f\n\r", sample_time, mail->temp_Value, mail->press_Value, mail->LDR_Value); + + sensorData.lock(); //lock critical section + sprintf(data_buffer[sample_h],"%s, %2.2f, %4.2f, %.4f\n\r", sample_time, TEMP, PRES, LDR); + sensorData.unlock(); //unlock critical section + memset(sample_time,NULL,20); - //Set seperate FIFO head and tail for printing data + //Set seperate FIFO head and tail for printing data data_h = sample_h; data_t = sample_t; - //Move sample FIFO buffer head to next row in buffer + //Move sample FIFO buffer head to next row in buffer sample_h++; - //Check sample FIFO buffer head + //Check sample FIFO buffer head if(sample_h >= MAX_SAMPLES){ sample_h = 0; } - //Check sample FIFO buffer tail + //Check sample FIFO buffer tail if(sample_t == sample_h){ sample_t++; if(sample_t >= (MAX_SAMPLES)){ sample_t = 0; } } - //Unlock data buffer + //Unlock data buffer DataBuffer.unlock(); + } +} + +/*---------------------Read Sensors ---------------------------*/ + +void readISR () { // Ticker interrupt defined in main + + _sensorRead.signal_set(SENSOR_UPDATE); +} + +void sensorRead () { + + while (1) { + + sensorData.lock(); // Entering Critial Section + + // Store Data in global Variables + LDR = LDR_In.read(); + TEMP = Sensor.getTemperature(); + PRES = Sensor.getPressure(); + + sensorData.unlock(); // Exiting Critical Section + + Green_int = !Green_int; // debugging - Thread::wait (15000); - } + //Read sensors, send to mail-queue + mail_t *mail = mail_box.alloc(); + mail->LDR_Value = LDR; + mail->temp_Value = TEMP; + mail->press_Value = PRES; + mail_box.put(mail); + + _circBuff.signal_set(DATA_READY); // Set signal to buffer to store updated values + Thread::signal_wait(SENSOR_UPDATE); // Wait for the Timer interrupt + } } -/*--------------------------------------------------------------------*/ + /*--------------------------------LCD---------------------------------*/ void PrintLCD () { @@ -159,19 +189,19 @@ //Interrupt when recieving from serial port void Rx_interrupt() { -//Wait for serial input + //Wait for serial input while (pc.readable()) { - //Return input to serial + //Return input to serial rx_buffer[rx_in] = pc.getc(); pc.putc(rx_buffer[rx_in]); - //If enter key is pressed, set serial thread signal + //If enter key is pressed, set serial thread signal if(rx_buffer[rx_in] == 0xD){ - S_CMD.signal_set(ENTER_KEY); + _serialCMD.signal_set(ENTER_KEY); } - //Increment buffer head + //Increment buffer head else{ rx_in = (rx_in + 1); } @@ -179,13 +209,13 @@ } //Check what command what recieved and execute -void Serial_CMD(){ +void serialCMD(){ while(1){ - //Wait for thread signal + //Wait for thread signal Thread::signal_wait(ENTER_KEY); - //Detach serial interrupt + //Detach serial interrupt pc.attach(NULL, Serial::RxIrq); struct tm * s_time; @@ -199,10 +229,10 @@ else if(strstr(rx_buffer, "READ ALL")){ pc.puts(" READ ALL\n\r"); - //Lock data buffer + //Lock data buffer DataBuffer.lock(); - //Print all samples to serial + //Print all samples to serial for(int n=data_t; n<=MAX_SAMPLES; n++){ pc.puts(data_buffer[n]); } @@ -212,7 +242,7 @@ } } - //Lock data buffer + //Lock data buffer DataBuffer.unlock(); } /*----DELETE ALL----------------------------------*/ @@ -352,4 +382,50 @@ pc.attach(&Rx_interrupt, Serial::RxIrq); } } -/*--------------------------------------------------------------------*/ \ No newline at end of file +/*------------------------------------------------*/ + +void POST () { + + pc.printf(" ALL Leds should be flashing\n\r"); + + for(unsigned int n = 0; n<10; n++) { + Green_int = ON; + Blue_int = ON; + Red_int = ON; + Green_ext = ON; + Yellow_ext = ON; + Red_ext = ON; + + wait (0.2); + Green_int = OFF; + Blue_int = OFF; + Red_int = OFF; + Green_ext = OFF; + Yellow_ext = OFF; + Red_ext = OFF; + wait (0.2); + } + + pc.printf("Switch states:\n\r"); + pc.printf("\tSW_L: %d\n\r\tSW_R %d\n\r", SW_L.read(), SW_R.read()); + pc.printf("\tSW_B: %d\n\r", SW_B.read()); + + float Temp = Sensor.getTemperature(); + float Pres = Sensor.getPressure(); + float ldrs = LDR_In.read(); + + pc.printf("Sensor test:\n\r"); + pc.printf("T: %f\tP: %f\tL: %f\n\r",Temp,Pres,ldrs); + + pc.printf("LCD Test\n\r"); + + lcd.Clear(); + lcd.RowSelect(1); + lcd.Write("*******LCD******"); + lcd.RowSelect(2); + lcd.Write("******TEST******"); + wait(1); + lcd.Clear(); +} + + \ No newline at end of file