ELEC350 - Team Q / Mbed OS Sampling_Data_into_buffer

Dependencies:   BME280 BMP280

Fork of Task690-mbed-os-FZ429ZI by University of Plymouth - Stages 1, 2 and 3

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 #include <string.h>
00004 //#define BME
00005 #ifdef BME
00006 #include "BME280.h"
00007 #else
00008 #include "BMP280.h"
00009 #endif
00010 
00011 
00012 // Buffer
00013 #include "buffer.hpp"
00014 // LCD
00015 #include "hardware_setup.hpp"
00016 
00017  
00018 #define Signal 1
00019 
00020 //Global objects
00021 Serial pc(USBTX, USBRX);
00022 AnalogIn LDR_In(A1);
00023 DigitalOut led(D7);
00024 
00025 DigitalOut led1(LED1);
00026 
00027 //Threads
00028 Thread t1(osPriorityRealtime);
00029 Thread t2;
00030 
00031 //The ticker, used to sample data at a fixed rate
00032 Ticker t;
00033 
00034 
00035 //Global Variables
00036 float fLDR = 0.0; //probably don't want this global?
00037 Mutex sensorLock;
00038 
00039 
00040 //Environmental Sensor driver
00041 #ifdef BME
00042 BME280 sensor(D14, D15);
00043 #else
00044 BMP280 sensor(D14, D15);
00045 #endif
00046 
00047 
00048 // Function declarations
00049 void FunctionSample();
00050 
00051 
00052 void doCaptureSamples() {
00053     t1.signal_set(Signal);
00054 }
00055 
00056 
00057 void decrementBuffer(){
00058     while(true){
00059     float oldData = takeFromBuffer();  
00060     }
00061     
00062 }
00063 
00064 
00065 void FunctionSample()
00066 {
00067     pc.printf("Testing with %d sample buffer", BUFFERSIZE);
00068     //pc.printf("%d\n", BUFFERSIZE);
00069     while (true) {
00070         Thread::signal_wait(Signal);
00071         led1 = !led1;
00072    
00073         sensorLock.lock();   
00074         
00075         // Read LDR
00076         fLDR = LDR_In;
00077         pc.printf("LDRinThread = %6.4f\n", fLDR);
00078         
00079         //Read BMP280 Sensors (I2C)
00080         float temp = sensor.getTemperature();
00081         float pressure = sensor.getPressure();
00082         //Display in PuTTY
00083         pc.printf("Temperature: %5.1f\n", temp);
00084         pc.printf("Pressure: %5.1f\n", pressure);
00085         
00086         addToBuffer(fLDR);
00087         addToBuffer(temp);
00088         addToBuffer(pressure);
00089 
00090         // date and time
00091         int year = LCD.year;
00092         char month = LCD.month;
00093         char day = LCD.day;
00094         char hour = LCD.hours;
00095         char minute = LCD.minutes;
00096         
00097         char DaT[100];
00098         int n; 
00099         n=sprintf (DaT, "%d.%d.%d %d:%d Data Temp:%5.1f Pressure:%5.1f LDR:%6.4f", year, month, day, hour, minute, temp, pressure, fLDR);
00100         pc.printf ("[%s] is a string %d chars long\n",DaT,n);
00101         //addToBuffer(DaT); CHANGE addToBuffer to char, as now we only need to put in DaT, which is a char
00102         
00103         
00104         
00105         sensorLock.unlock();
00106         
00107 
00108     }
00109 }
00110 
00111 
00112 //Main function
00113 int main()
00114 {
00115     
00116     t1.start(FunctionSample);
00117     t2.start(decrementBuffer);
00118     
00119     //Ticker in seconds
00120     t.attach(&doCaptureSamples, 1);
00121     
00122     
00123     //Set PuTTY baud rate to 9600
00124     pc.baud(9600);
00125  
00126     while(1) {
00127  
00128         //Displauy the LDR
00129         //ldrLock.lock();
00130         //float _ldr = fLDR;
00131         //ldrLock.unlock();
00132         //pc.printf("LDR = %6.4f\n", _ldr);
00133         Thread::wait(3000);
00134         
00135         // make scheduler put the board to sleep until a signal is set?
00136         //Thread::wait(osWaitForever);
00137  
00138     } //end while(1)
00139 } //end main