Sampling ldr temp and pressure data into 120 sample FIFO buffer.

Dependencies:   BME280 BMP280

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

Revision:
5:cc1b9f5c27a0
Parent:
4:04cd5171c7ff
Child:
6:c2299e3de428
--- a/main.cpp	Thu Nov 23 14:03:12 2017 +0000
+++ b/main.cpp	Sun Dec 10 20:17:31 2017 +0000
@@ -1,79 +1,131 @@
-/* Access an SD Card using SPI */
- 
- #include "mbed.h"
- #include "SDBlockDevice.h"
- #include "FATFileSystem.h"
- #include "sample_hardware.hpp"
+#include "mbed.h"
+//#define BME
+#ifdef BME
+#include "BME280.h"
+#else
+#include "BMP280.h"
+#endif
+
+
+// Buffer
+#include "buffer.hpp"
+
+
  
- //SD Card Object
- SDBlockDevice sd(D11, D12, D13, D10); // mosi, miso, sclk, cs
+#define Signal 1
 
- uint8_t block[512] = "Hello World!\n";
- int main()
-{
-    //POWER ON SELF TEST
-    post();
-    
-    printf("Initialise\n");
-    //FileSystemLike(*sd);
+//Global objects
+Serial pc(USBTX, USBRX);
+AnalogIn LDR_In(A1);
+DigitalOut led(D7);
+
+DigitalOut led1(LED1);
+
+//Threads
+Thread t1(osPriorityRealtime);
+Thread t2;
 
-    // call the SDBlockDevice instance initialisation method.
-    if ( sd.init() != 0) {
-        printf("Init failed \n");
-        errorCode(FATAL);
-    }    
-    
-    //Create a filing system for SD Card
-    FATFileSystem fs("sd", &sd);
-    
-    // *************
-    // Open to WRITE
-    // *************
-    printf("Write to a file\n");
-    FILE* fp = fopen("/sd/test.txt","a");
-    //Check file handle (stream)
-    if (fp == NULL) {
-        error("Could not open file for write\n");
-        errorCode(FATAL);
+//The ticker, used to sample data at a fixed rate
+Ticker t;
+
+
+//Global Variables
+float fLDR = 0.0; //probably don't want this global?
+Mutex sensorLock;
+
+
+//Environmental Sensor driver
+#ifdef BME
+BME280 sensor(D14, D15);
+#else
+BMP280 sensor(D14, D15);
+#endif
+
+
+// Function declarations
+void FunctionSample();
+
+
+void doCaptureSamples() {
+    t1.signal_set(Signal);
+}
+
+
+void decrementBuffer(){
+    while(true){
+    float oldData = takeFromBuffer();  
     }
     
-    //Put some text in the file...
-    fprintf(fp, "Welcome to ELEC350\n");
-    
-    //Close the file
-    fclose(fp);
+}
+
+
+void FunctionSample()
+{
+    pc.printf("Testing");
+    pc.printf("%d\n", BUFFERSIZE);
+    while (true) {
+        Thread::signal_wait(Signal);
+        led1 = !led1;
+   
+        sensorLock.lock();   
+        
+        // Read LDR
+        fLDR = LDR_In;
+        pc.printf("LDRinThread = %6.4f\n", fLDR);
+        
+        addToBuffer(fLDR);
+        
+        
+        //Read BMP280 Sensors (I2C)
+        float temp = sensor.getTemperature();
+        float pressure = sensor.getPressure();
+        //Display in PuTTY
+        pc.printf("Temperature: %5.1f\n", temp);
+        pc.printf("Pressure: %5.1f\n", pressure);
+        
+        
+        //decrement old data when buffer is full
+        //int32_t Nsamples = samplesInBuffer.wait();
+       // pc.printf("Nsamples = %i\n", Nsamples);
+        //if (Nspaces == BUFFERSIZE)
+        //{
+            
+        
+        //nextChar = tolower(nextChar);
+        //}        
+        
+        sensorLock.unlock();
+        
+
+    }
+}
+
+
+//Main function
+int main()
+{
     
-    // ************
-    // Open to READ
-    // ************
-    printf("Read a file\n");
-    fp = fopen("/sd/test.txt","r");
-    if (fp == NULL) {
-        error("Could not open file for read\n");
-        errorCode(FATAL);
-    }   
+    t1.start(FunctionSample);
+    t2.start(decrementBuffer);
     
-    //Read back all strings
-    char s1[64];
-    while (fscanf(fp, "%s", s1) == 1) {
-        printf("READ BACK: %s\n", s1);
-    }
-    //To read a whole line, use: fgets(s1, sizeof(s1), fp);
+    //Ticker in seconds
+    t.attach(&doCaptureSamples, 1);
     
     
-    //Close File
-    fclose(fp);
-    
-    //Close down
-    sd.deinit();
-    printf("All done...\n");
-    errorCode(OK);
-    
-    //Flash to indicate goodness
-    while(true) {
-        greenLED = 1;
-        wait(0.5);
-        greenLED = 0;
-        wait(0.1);    
-    }
-}
+    //Set PuTTY baud rate to 9600
+    pc.baud(9600);
+ 
+    while(1) {
+ 
+        //Displauy the LDR
+        //ldrLock.lock();
+        //float _ldr = fLDR;
+        //ldrLock.unlock();
+        //pc.printf("LDR = %6.4f\n", _ldr);
+        Thread::wait(1000);
+        
+        // make scheduler put the board to sleep until a signal is set?
+        //Thread::wait(osWaitForever);
+ 
+    } //end while(1)
+} //end main
\ No newline at end of file