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

Dependencies:   BMP280

Files at this revision

API Documentation at this revision

Comitter:
mwthewsey
Date:
Sat Jan 06 16:13:37 2018 +0000
Parent:
8:dbb57b4d5ba4
Child:
10:261f2b69c4c7
Child:
15:e61297f9bae9
Commit message:
Circular buffer working

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
WebUI.cpp Show annotated file Show diff for this revision Revisions of this file
WebUI.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	Thu Jan 04 19:32:30 2018 +0000
+++ b/Sampling.cpp	Sat Jan 06 16:13:37 2018 +0000
@@ -2,16 +2,38 @@
 #include "mbed.h"
 #include "rtos.h"
 
+//Thread Sync Tools
+Mutex tempReadingsLock;
+Mutex presReadingsLock;
+Mutex LDRReadingsLock;
+Mutex timeReadingsLock;
+
+//Buffers with zero inital value
+float tempReadings[BUFFERSIZE] = {};
+float presReadings[BUFFERSIZE] = {};
+float LDRReadings[BUFFERSIZE] = {};
+float timeReadings[BUFFERSIZE] = {};
+
+Thread t1; //Sample Enviromental Sensor
+Thread t2; //Sample LDR Sensor
+
+Ticker sampleRate;
+Timeout SampleLEDTimeout;
+
+bool NewEnvSample;  //Is there new data from the envirom sensor to output?
+bool NewLDRSample;  //Is there new data from the LDR to output?
+
 //Index
-unsigned short newestIndex = BUFFERSIZE-1;
-unsigned short oldestIndex = BUFFERSIZE-1;
+unsigned short nextIndex = 0;
+unsigned short currentIndex = 0;
+unsigned short oldestIndex = 0;
 
 bool firstSample = true;
 
 //Hardware
 BMP280 sensor(D14, D15);
-AnalogIn LDRSensor(PA_0);
-DigitalOut SamplingLED(LED1);
+AnalogIn LDRSensor(A0);
+DigitalOut SamplingLED(PB_10);
 
 void SampleTimerISR(void)
 {
@@ -19,7 +41,7 @@
     t1.signal_set(1);
     t2.signal_set(1);
     SamplingLED = 1;
-    SampleLEDTimeout.attach(&FlipSamplingLED,1); //To turn LED off
+    SampleLEDTimeout.attach(&FlipSamplingLED,0.1); //To turn LED off
 }
 
 void ConfigThreadsAndIR(void)
@@ -30,20 +52,20 @@
     t1.start(&ThreadSampleEnvSensor);
     t2.start(&ThreadSampleLDR);
 
-    sampleRate.attach(&SampleTimerISR, 15); //15 second interval
+    sampleRate.attach(&SampleTimerISR, 1); //15 second interval
 }
 
 void AddTempSample(float temp)
 {
     tempReadingsLock.lock(); //Take the key
-    tempReadings[newestIndex+1] = temp; //Add the sample after the most recent
+    tempReadings[nextIndex] = 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
+    presReadings[nextIndex] = pres; //Add to register
     presReadingsLock.unlock(); //Release the key
 }
 
@@ -63,7 +85,7 @@
 void AddLDRSample(float LDRval)
 {
     LDRReadingsLock.lock(); //Take the key
-    LDRReadings[newestIndex+1] = LDRval; //Add the sample after the most recent
+    LDRReadings[nextIndex] = LDRval; //Add the sample after the most recent
     LDRReadingsLock.unlock(); // Release the key
 }
 
@@ -82,20 +104,32 @@
 
 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;
+    //printf("%d   %d   %d        %2.2f  %2.2f  %2.2f  %2.2f  %2.2f  %2.2f  %2.2f  %2.2f\r",nextIndex, currentIndex, oldestIndex, LDRReadings[0], LDRReadings[1], LDRReadings[2], LDRReadings[3], LDRReadings[4], LDRReadings[5], LDRReadings[6], LDRReadings[7]);
+
+    nextIndex = IndexIncrement(nextIndex); //Increment next index
+    if (firstSample) {
+        firstSample = false; //During first sample, do not increment current or oldest
+    } else {
+        currentIndex = IndexIncrement(currentIndex);
+        if (currentIndex == oldestIndex) { //When current index overflows, start infrementing oldest
+            oldestIndex = IndexIncrement(oldestIndex); 
         }
     }
 }
 
+unsigned short IndexIncrement(unsigned short thisIndex)
+{
+    if (thisIndex+1 == BUFFERSIZE) { 
+        thisIndex = 0; //When index reached buffersize, reset to 0
+    } else {
+        thisIndex++;   //Else increment
+    }
+    return thisIndex;
+}
+
+
 void FlipSamplingLED(void)
 {
     SamplingLED = 0;
 }
+
--- a/Sampling.h	Thu Jan 04 19:32:30 2018 +0000
+++ b/Sampling.h	Sat Jan 06 16:13:37 2018 +0000
@@ -5,34 +5,36 @@
 #include "BMP280.h"
 #include "rtos.h"
 
-#define BUFFERSIZE 120
+#define BUFFERSIZE 8
 
 
 //Thread Sync Tools
-Mutex tempReadingsLock;
-Mutex presReadingsLock;
-Mutex LDRReadingsLock;
-Mutex timeReadingsLock;
+extern Mutex tempReadingsLock;
+extern Mutex presReadingsLock;
+extern Mutex LDRReadingsLock;
+extern Mutex timeReadingsLock;
 
 //Buffers
-float tempReadings[BUFFERSIZE];
-float presReadings[BUFFERSIZE];
-float LDRReadings[BUFFERSIZE];
-float timeReadings[BUFFERSIZE];
+extern float tempReadings[BUFFERSIZE];
+extern float presReadings[BUFFERSIZE];
+extern float LDRReadings[BUFFERSIZE];
+extern float timeReadings[BUFFERSIZE];
 
-extern unsigned short newestIndex;
+extern unsigned short currentIndex;
 //Position in the buffer of the newest sample
+extern unsigned short nextIndex;
+//Position in the buffer where the next sample needs to be writen to
 extern unsigned short oldestIndex;
 //Position in the buffer of the oldest sample
 
 extern 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
+extern Thread t1; //Sample Enviromental Sensor
+extern Thread t2; //Sample LDR Sensor
 
-Ticker sampleRate;
-Timeout SampleLEDTimeout;
+extern Ticker sampleRate;
+extern Timeout SampleLEDTimeout;
 extern BMP280 sensor;
 extern AnalogIn LDRSensor; //Input pin for LDR
 extern DigitalOut SamplingLED; //Onboard LED showing when a sample happens
@@ -43,8 +45,8 @@
 extern float fLatestPres;
 */
 
-bool NewEnvSample;  //Is there new data from the envirom sensor to output?
-bool NewLDRSample;  //Is there new data from the LDR to output?
+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?
 
 void SampleTimerISR(void);
 //Called by ticker. Calls the sample funcs of each device by flagging the threads
@@ -77,4 +79,7 @@
 void FlipSamplingLED(void);
 //Called by timeout, turns of LED
 
+unsigned short IndexIncrement(unsigned short thisIndex);
+//Incrementing the index with respect for the buffersize
+
 #endif
\ No newline at end of file
--- a/WebUI.cpp	Thu Jan 04 19:32:30 2018 +0000
+++ b/WebUI.cpp	Sat Jan 06 16:13:37 2018 +0000
@@ -10,6 +10,11 @@
 #include <iostream>
 #include <string> 
 
+//Now setup a web server
+TCPServer srv;           //TCP/IP Server
+TCPSocket clt_sock;      //Socket for communication
+SocketAddress clt_addr;  //Address of incoming connection
+
 #define HTTP_STATUS_LINE "HTTP/1.0 200 OK"
 #define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8"
 #define HTTP_MESSAGE_BODY1 ""                                    \
@@ -43,10 +48,7 @@
     eth.connect();
     printf("The target IP address is '%s'\n", eth.get_ip_address());
     
-    //Now setup a web server
-    TCPServer srv;           //TCP/IP Server
-    TCPSocket clt_sock;      //Socket for communication
-    SocketAddress clt_addr;  //Address of incoming connection
+    
     
     /* Open the server on ethernet stack */
     srv.open(&eth);
@@ -58,7 +60,7 @@
     srv.listen(5);
 }
 
-void WebUIUpdate(string& value){
+void WebUIUpdate(float ldr){
     while (true) {
         using namespace std;
         //Block and wait on an incoming connection
--- a/WebUI.h	Thu Jan 04 19:32:30 2018 +0000
+++ b/WebUI.h	Sat Jan 06 16:13:37 2018 +0000
@@ -1,8 +1,19 @@
 #ifndef __WebUI__
 #define __WebUI__
 
+#include <string>
+#include "EthernetInterface.h"
+#include "TCPServer.h"
+#include "TCPSocket.h"
+#include <iostream> 
+
+//Now setup a web server
+extern TCPServer srv;           //TCP/IP Server
+extern TCPSocket clt_sock;      //Socket for communication
+extern SocketAddress clt_addr;  //Address of incoming connection
+
 void WebUISetup(void);  //Configures the TCP server
 
-void WebUIUpdate(string& Value); //Might be called as a string, but does have blocking currently.
+void WebUIUpdate(float ldr); //Might be called as a string, but does have blocking currently.
 
 #endif
\ No newline at end of file
--- a/main.cpp	Thu Jan 04 19:32:30 2018 +0000
+++ b/main.cpp	Sat Jan 06 16:13:37 2018 +0000
@@ -16,7 +16,7 @@
 int main()
 {
     //Initialise devices
-    WebUISetup();
+////WebUISetup();
     
     
     //Hardware Self Test
@@ -35,6 +35,8 @@
             //LCD Update Function
             NewEnvSample = false;
             NewLDRSample = false;
+
         }
+        
     }   
 }
\ No newline at end of file