fuck this

Dependencies:   BMP280

Revision:
10:261f2b69c4c7
Parent:
9:ac5673cca703
Child:
12:03589f1d5c30
--- a/Sampling.cpp	Sat Jan 06 16:13:37 2018 +0000
+++ b/Sampling.cpp	Sun Jan 07 23:40:10 2018 +0000
@@ -1,4 +1,5 @@
 #include "Sampling.h"
+#include "TimeInterface.h"
 #include "mbed.h"
 #include "rtos.h"
 
@@ -12,7 +13,7 @@
 float tempReadings[BUFFERSIZE] = {};
 float presReadings[BUFFERSIZE] = {};
 float LDRReadings[BUFFERSIZE] = {};
-float timeReadings[BUFFERSIZE] = {};
+time_t timeReadings[BUFFERSIZE] = {};
 
 Thread t1; //Sample Enviromental Sensor
 Thread t2; //Sample LDR Sensor
@@ -24,9 +25,9 @@
 bool NewLDRSample;  //Is there new data from the LDR to output?
 
 //Index
-unsigned short nextIndex = 0;
-unsigned short currentIndex = 0;
-unsigned short oldestIndex = 0;
+volatile unsigned short nextIndex = 0;
+volatile unsigned short currentIndex = 0;
+volatile unsigned short oldestIndex = 0;
 
 bool firstSample = true;
 
@@ -52,7 +53,7 @@
     t1.start(&ThreadSampleEnvSensor);
     t2.start(&ThreadSampleLDR);
 
-    sampleRate.attach(&SampleTimerISR, 1); //15 second interval
+    sampleRate.attach(&SampleTimerISR, SAMPLERATE); //15 second interval
 }
 
 void AddTempSample(float temp)
@@ -89,15 +90,23 @@
     LDRReadingsLock.unlock(); // Release the key
 }
 
+void AddTimeSample(time_t sampledTime)
+{
+    timeReadingsLock.lock();    //Take the key
+    timeReadings[nextIndex] = sampledTime;  //Add the sample after the most recent
+    timeReadingsLock.unlock();  // Release the key
+}
+
 void ThreadSampleLDR(void)
 {
     while (true) {
         Thread::signal_wait(1); //Wait for signal 1
         //get readings
         float LDRval = LDRSensor; //Read the analogue pin value
-        //get time function
+        time_t currentTime = time(0);   //Get the system time
         AddLDRSample(LDRval);
-        //add time sample
+
+        AddTimeSample(currentTime);
         NewLDRSample = true;    //signal to main thread
     }
 }
@@ -112,14 +121,14 @@
     } else {
         currentIndex = IndexIncrement(currentIndex);
         if (currentIndex == oldestIndex) { //When current index overflows, start infrementing oldest
-            oldestIndex = IndexIncrement(oldestIndex); 
+            oldestIndex = IndexIncrement(oldestIndex);
         }
     }
 }
 
 unsigned short IndexIncrement(unsigned short thisIndex)
 {
-    if (thisIndex+1 == BUFFERSIZE) { 
+    if (thisIndex+1 == BUFFERSIZE) {
         thisIndex = 0; //When index reached buffersize, reset to 0
     } else {
         thisIndex++;   //Else increment
@@ -127,6 +136,40 @@
     return thisIndex;
 }
 
+unsigned short IndexDecrement(unsigned short thisIndex)
+{
+    if (thisIndex-1 == -1) { // Wait for underflow
+        thisIndex = BUFFERSIZE-1; //When index reaches 0 reset to Buffersize-1
+    } else {
+        thisIndex--;   //Else decrement
+    }
+    return thisIndex;
+}
+
+void Sampling(bool inputState)
+{
+    if (inputState) {
+        sampleRate.attach(&SampleTimerISR, SAMPLERATE);
+    } else {
+        sampleRate.detach();
+    }
+}
+
+void TakeKeys(bool inputState)
+{
+    if (inputState) {
+        tempReadingsLock.lock();             //Take the key
+        presReadingsLock.lock();
+        LDRReadingsLock.lock();
+        timeReadingsLock.lock();
+    } else {
+        tempReadingsLock.unlock();           // Release the key
+        presReadingsLock.unlock();
+        LDRReadingsLock.unlock();
+        timeReadingsLock.unlock();
+    }
+}
+
 
 void FlipSamplingLED(void)
 {