PROJ515 / Mbed OS ELEC-351-GROUP-E-CW

Dependencies:   BMP280

Files at this revision

API Documentation at this revision

Comitter:
Swaggie
Date:
Thu Jan 04 17:34:57 2018 +0000
Parent:
6:8e1795a5886b
Child:
8:dbb57b4d5ba4
Commit message:
Sampling functions and flow now complete, except for time.

Changed in this revision

Sampling.cpp Show annotated file Show diff for this revision Revisions of this file
Sampling.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Sampling.cpp	Wed Jan 03 16:37:33 2018 +0000
+++ b/Sampling.cpp	Thu Jan 04 17:34:57 2018 +0000
@@ -5,38 +5,104 @@
 Mutex tempReadingsLock;
 Mutex presReadingsLock;
 Mutex LDRReadingsLock;
+Mutex timeReadingsLock;
 
 //Buffers
 float tempReadings[BUFFERSIZE];
 float presReadings[BUFFERSIZE];
 float LDRReadings[BUFFERSIZE];
+float timeReadings[BUFFERSIZE];
 
-unsigned short newestTempIndex = BUFFERSIZE-1;
-unsigned short oldestTempIndex = BUFFERSIZE-1;
+//Index
+unsigned short newestIndex = BUFFERSIZE-1;
+unsigned short oldestIndex = BUFFERSIZE-1;
+
+bool firstSample = true;
 
-bool NewEnvSample = false;  //Is there new data from the envirom sensor to output?
-bool NewLDRSample = false;  //Is there new data from the LDR to output?
+//Hardware
+#ifdef BME
+BME280 sensor(D14, D15);
+#else
+BMP280 sensor(D14, D15);
+#endif
+AnalogIn LDRSensor(PA_1); //Check this is the correct pin
 
 void SampleTimerISR(void)
 {
     //Flag Threads
+    t1.signal_set(1);
+    t2.signal_set(1);
 }
-    
+
 void ConfigThreadsAndIR(void)
 {
-   sampleRate.attach(SampleTimerISR, 15); //15 second interval
+    NewEnvSample = false;  //Reset
+    NewLDRSample = false;  //Reset
+
+    t1.start(ThreadSampleEnvSensor);
+    t2.start(ThreadSampleLDR);
+
+    sampleRate.attach(SampleTimerISR, 15); //15 second interval
+}
+
+void AddTempSample(float temp)
+{
+    tempReadingsLock.lock(); //Take the key
+    tempReadings[newestIndex+1] = temp; //Add the sample after the most recent
+    tempReadingsLock.unlock(); // Release the key
+}
+
+void AddPresSample(float pres)
+{
+    presReadingsLock.lock();    //Take the key
+    presReadings[newestIndex+1] = pres; //Add to register
+    presReadingsLock.unclock(); //Release the key
 }
 
-void AddPresSample(float* Pres)
+void ThreadSampleEnvSensor(void)
 {
-   tempReadingsLock.lock(); //Take the key
-   tempReadings[newestTempIndex+1] = Pres; //Add the sample after the most recet
-   tempReadingsLock.unlock(); // Release the key
+    while (true) {
+        Thread::signal_wait(1); //Wait for signal 1
+        //Get readings
+        float temp = sensor.getTemperature();
+        float pres = sensor.getPressure();
+        AddPresSample(pres);    //Add value to register
+        AddTempSample(temp);    //Add value to register
+        NewEnvSample = true;    //Signal to main thread
+    }
+}
+
+void AddLDRSample(float LDRval)
+{
+    LDRReadingsLock.lock(); //Take the key
+    LDRReadings[newestIndex+1] = LDR; //Add the sample after the most recent
+    LDRReadingsLock.unlock(); // Release the key
 }
 
-void threadSampleEnvSensor(void)
+void ThreadSampleLDR(void)
 {
-    //Get readings
-    //float temp =
-    //float pres = 
-    AddPresSample(pres);
\ No newline at end of file
+    while (true) {
+        Thread::signal_wait(1); //Wait for signal 1
+        //get readings
+        float LDRval = LDRSensor.value(); //Read the analogue pin value
+        //get time function
+        AddLDRSample(LDRval);
+        //add time sample
+        NewLDRSample = true;    //signal to main thread
+    }
+}
+
+void IncrementIndex(void)
+{
+    newestIndex++; //Move the index forward one
+    if (newestIndex == oldestIndex) {
+        //If this is true then the memory is full and has looped back around and overwritten the oldest sample
+        //Therefore, we need to move the oldest index pointer
+        if (firstSample)
+        {
+            //this prevents the initial error
+            oldestIndex++;
+            firstSample = false;
+        }
+    }
+}
--- a/Sampling.h	Wed Jan 03 16:37:33 2018 +0000
+++ b/Sampling.h	Thu Jan 04 17:34:57 2018 +0000
@@ -17,26 +17,34 @@
 extern BMP280 sensor;
 #endif
 
+AnalogIn LDRSensor; //Input pin for LDR
+
 //Thread Sync Tools
 Mutex tempReadingsLock;
 Mutex presReadingsLock;
 Mutex LDRReadingsLock;
+Mutex timeReadingsLock;
 
 //Buffers
 float tempReadings[BUFFERSIZE];
 float presReadings[BUFFERSIZE];
 float LDRReadings[BUFFERSIZE];
+float timeReadings[BUFFERSIZE];
 
-unsigned short newestTempIndex;
+unsigned short newestIndex;
 //Position in the buffer of the newest sample
-unsigned short oldestTempIndex;
+unsigned short oldestIndex;
 //Position in the buffer of the oldest sample
 
+bool firstSample;
+//Used to indicate this is the first sample to be taken. used in IncrementIndex func
+
 Thread t1; //Sample Enviromental Sensor
 Thread t2; //Sample LDR Sensor
 
 Ticker sampleRate;
 AnalogIn LDR(A13);  //LDR Pin
+DigitalOut SamplingLED(LED1); //Onboard LED showing when a sample happens
 
 /*These can be deleted I think
 extern float fLatestTemp;
@@ -44,8 +52,8 @@
 extern float fLatestPres;
 */
 
-extern bool NewEnvSample;  //Is there new data from the envirom sensor to output?
-extern bool NewLDRSample;  //Is there new data from the LDR to output?
+bool NewEnvSample;  //Is there new data from the envirom sensor to output?
+bool NewLDRSample;  //Is there new data from the LDR to output?
 
 void SampleTimerISR(void);
 //Called by ticker. Calls the sample funcs of each device by flagging the threads
@@ -53,19 +61,26 @@
 void ConfigThreadsAndIR(void);
 //Setup Interrupts and Threads
 
-void threadSampleEnvSensor(void);
+void ThreadSampleEnvSensor(void);
 //when flagged by interrupt will capture a sample then calls the addtobufferfuncs
 
-void AddTempSample(float* Temp);
+void AddTempSample(float temp);
 //Producer function
 
-void AddPresSample(float* Pres);
+void AddPresSample(float pres);
 //Producer Function
 
 void ThreadSampleLDR(void);
-//When flagged by interrupt will read LDR value.
+//When flagged by interrupt will read time and LDR value.
 
-void AddLDRSample(float* LDR);
+void AddLDRSample(float LDR);
 //Poducer Function
 
+void AddTimeSample(/*Whatever time*/);
+//Producer Function
+
+void IncrementIndex(void);
+//Called after samples are added. Increments the index and moves the oldest
+//along if necessary
+
 #endif
\ No newline at end of file
--- a/main.cpp	Wed Jan 03 16:37:33 2018 +0000
+++ b/main.cpp	Thu Jan 04 17:34:57 2018 +0000
@@ -21,19 +21,20 @@
     
     //Hardware Self Test
     
-    //Initilaiase interrupts and times
+    //Initialise interrupts and times
     ConfigThreadsAndIR();
+    firstSample = true; //Set only at start of program
     
     //Run
-    while (1)
+    while (true)
     {
         if (NewEnvSample && NewLDRSample)
         {
-            //If new samples have been captured, update history and LCD
+            //New samples have been captured and are in the register
+            IncrementIndex();
             //LCD Update Function
-            //Write values to history
             NewEnvSample = false;
             NewLDRSample = false;
         }
     }   
-}
+}
\ No newline at end of file