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

Files at this revision

API Documentation at this revision

Comitter:
osmith2
Date:
Sun Dec 10 20:17:31 2017 +0000
Parent:
4:04cd5171c7ff
Child:
6:c2299e3de428
Commit message:
First attempt at task 1. Reading LDR, temp, and pressure data into a 120 sample FIFO buffer, using threads.

Changed in this revision

ELEC350-Practicals-FZ429.lib Show diff for this revision Revisions of this file
buffer.cpp Show annotated file Show diff for this revision Revisions of this file
buffer.hpp 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
sd-driver.lib Show diff for this revision Revisions of this file
--- a/ELEC350-Practicals-FZ429.lib	Thu Nov 23 14:03:12 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://os.mbed.com/teams/University-of-Plymouth-Stage-2-and-3/code/ELEC350-Practicals-FZ429/#58ba1a6dbf60
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buffer.cpp	Sun Dec 10 20:17:31 2017 +0000
@@ -0,0 +1,65 @@
+#include "buffer.hpp"
+//#include "sample_hardware.hpp"
+
+//Thread sychronisation primatives
+Semaphore spaceAvailable(BUFFERSIZE); //set to 120 in buffer.hpp
+Semaphore samplesInBuffer(0); //init 0
+Mutex bufferLock; //binary semaphore
+
+//Output buffer
+float buffer[BUFFERSIZE]; // can be used to display buffersize to PuTTY
+unsigned int newestIndex = BUFFERSIZE-1;    //First time it is incremented, it will be 0
+unsigned int oldestIndex = BUFFERSIZE-1;
+
+//Producer
+void addToBuffer(float c) // char c used for char buffer?
+{    
+    //Is there space?
+    int32_t Nspaces = spaceAvailable.wait();
+   
+    //Ok, there is space - take the lock
+    bufferLock.lock();
+    //redLED = 1;       
+        
+    //Update buffer
+    newestIndex = (newestIndex+1) % BUFFERSIZE;  
+    buffer[newestIndex] = c;
+    printf("Added data: %6.4f to buffer, %d spaces available\n", c, Nspaces-1);
+    
+    //Release lock
+    bufferLock.unlock();
+    //redLED = 0;
+    
+    //Signal that a sample has been added
+    samplesInBuffer.release();
+}
+
+//Consumer
+float takeFromBuffer()
+{    
+    //Are thre any samples in the buffer
+    int32_t Nsamples = samplesInBuffer.wait();
+    
+    printf("newestIndex = %i\n", newestIndex);
+    printf("oldestIndex = %i\n", oldestIndex);
+    if (newestIndex == oldestIndex){
+        //Ok, there are samples - take the lock
+        bufferLock.lock();   
+        //yellowLED = 1;
+        
+        //Update buffer - remove oldest
+        oldestIndex = (oldestIndex+1) % BUFFERSIZE;
+        float cc = buffer[oldestIndex];
+        printf("Taking old data (%f) from buffer, %d bytes remaining\n", cc, Nsamples-1);
+        
+        //Release lock
+        bufferLock.unlock();
+        //yellowLED = 0;
+        
+        //Signal there is space in the buffer
+        spaceAvailable.release();
+        
+        //return a copy of the result
+        return cc;
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buffer.hpp	Sun Dec 10 20:17:31 2017 +0000
@@ -0,0 +1,20 @@
+#ifndef __BUFFER__
+#define __BUFFER__
+
+#include "mbed.h"
+
+//Size of the morse character buffer
+#define BUFFERSIZE 10 // CW specified 120 samples
+
+extern Semaphore spaceAvailable;
+extern Semaphore samplesInBuffer;
+extern Mutex bufferLock;
+
+
+extern void addToBuffer(float c);
+extern float takeFromBuffer();
+
+
+
+
+#endif
\ No newline at end of file
--- 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
--- a/sd-driver.lib	Thu Nov 23 14:03:12 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://github.com/ARMmbed/sd-driver/#ae7e7440054c9447f8255bdccbcc523b3f6dffe4