diajzifsajoifajsoifaj

Dependencies:   BMP280 LGLCD

Fork of ELEC350-CWTEMPLATE-2017 by University of Plymouth - Stages 1, 2 and 3

Files at this revision

API Documentation at this revision

Comitter:
liam_grazier
Date:
Sun Jan 07 21:29:12 2018 +0000
Parent:
6:f0601ebd2423
Commit message:
working LCD, SD, (NON FIFO) , webpage, all threaded

Changed in this revision

components.cpp Show annotated file Show diff for this revision Revisions of this file
components.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
network.cpp Show annotated file Show diff for this revision Revisions of this file
network.hpp Show annotated file Show diff for this revision Revisions of this file
--- a/components.cpp	Fri Jan 05 17:56:59 2018 +0000
+++ b/components.cpp	Sun Jan 07 21:29:12 2018 +0000
@@ -1,19 +1,21 @@
 #include "mbed.h"
 #include "components.hpp"
 #include "lglcd.h"
-
-//#include "Networkbits.hpp"
+#include "stdio.h"
 #define RED_DONE 1
 #define YELLOW_DONE 2
-
 //Digital outputs
+DigitalIn onBoardSwitch(USER_BUTTON);
 DigitalOut onBoardLED(LED1);
 DigitalOut redLED(PE_15);
 DigitalOut yellowLED(PB_10);
 DigitalOut greenLED(PB_11);
-
+SDBlockDevice sd(PB_5, D12, D13, D10);// miso, sclk, cs 
+Mutex Lock1;
+Mutex Lock2;
+Mutex Remove;
 //Inputs
-DigitalIn  onBoardSwitch(USER_BUTTON);
+
 DigitalIn  SW1(PE_12);
 DigitalIn  SW2(PE_14);
 //Serial pc(USBTX, USBRX);
@@ -32,7 +34,137 @@
 {
     //posttest 
 }
+void runanalysis(void){
+    while(1){     
+Lock1.lock();
+double temp = sensor.getTemperature();
+double pressure = sensor.getPressure();
+double lightin = adcIn;
+char TEM[6];
+char PRE[5];  
+char LIGHT[6];
+printf("REFRESH SENSORS\n\r");
+sprintf(TEM,"%.2f", temp);
+sprintf(PRE,"%.2f", pressure);
+sprintf(LIGHT,"%.2f", lightin);
+mylcd.setline(1,1);
+mylcd.write("L:");
+if(lightin > 0.7 && lightin < 0.9)
+{
+mylcd.setline(1,4);
+ mylcd.write("|||||||||MAX");    
+}
+else if(lightin > 0.55 && lightin < 0.69)
+{
+mylcd.setline(1,4);
+ mylcd.write("|||||||     ");
+}
+else if(lightin > 0.5 && lightin < 0.54)
+{
+mylcd.setline(1,4);
+ mylcd.write("|||||       ");
+}
+else if(lightin > 0.4 && lightin < 0.54)
+{
+mylcd.setline(1,4);
+ mylcd.write("|||         ");
+}
+else if(lightin > 0.3 && lightin < 0.39)
+{
+mylcd.setline(1,4);
+ mylcd.write("||          ");
+} 
+else if(lightin > 0.06 && lightin < 0.29)
+{
+mylcd.setline(1,4);
+ mylcd.write("LOW LIGHT    ");
+} 
+else if(lightin < 0.05)
+{
+mylcd.setline(1,4);
+mylcd.write("             ");
+mylcd.setline(1,4);
+mylcd.write("disconnected");
+}
+mylcd.setline(2,1);
+mylcd.write("P:");
+mylcd.write(PRE);
+mylcd.setline(2,10);
+mylcd.write("T:");
+mylcd.write(TEM);
+wait(0.01);
+Lock1.unlock();
+Thread::signal_wait(SIG_READY);
+}
+}
+void sdwrite(void)
+{
+    while(1)
+    {
+        Lock2.lock();
+        FATFileSystem fs("sd", &sd);
+FILE* fp = fopen("/sd/test.csv","a");
+    if (fp == NULL) {
+        errorCode(FATAL);
+        printf("SD FAIL\n\r");
+        mylcd.clear();
+        mylcd.setline(1,0);
+        mylcd.write("SD FAIL");
+    }
+    if (fp != NULL){
+    printf("SD Success\n\r");
+    fprintf(fp, "hello,hello,hello\n\r");
+    wait(0.01);
+    }
+      fclose(fp);      
+      Lock2.unlock(); 
+      Thread::signal_wait(SIG_READY2);   
 
+}
+}
+void sdrun(void)
+{
+if ( sd.init() != 0) {
+printf("Init failed \n");
+mylcd.clear();
+mylcd.setline(1,1);
+mylcd.write("CANNOT INIT SD");        
+errorCode(FATAL);
+}
+if( sd.init() == 0){
+printf("Init Success \n");
+mylcd.clear();
+mylcd.setline(1,1);
+mylcd.write("SD GOOD MAN");
+wait(0.5); //flash the SD error / good code! 
+}
+}
+void sdcheck(void){
+if ( sd.init() != 0) {
+printf("Init failed \n\r");
+mylcd.clear();
+mylcd.setline(1,1);
+mylcd.write("CANNOT INIT SD\n\r");        
+errorCode(FATAL);
+}
+if( sd.init() == 0){
+printf("SD Good\n\r");
+}
+}
+void sdremove(void)
+{
+    Thread::signal_wait(SIG_REMOVE); 
+    mylcd.clear();  
+    Remove.lock();
+                sd.deinit();
+                mylcd.clear();
+                mylcd.setline(1,0);
+                greenLED = 1;
+                mylcd.write("R");
+                printf("SD REMOVED");
+                errorCode(FATAL);
+    Remove.unlock();
+}
 void errorCode(ELEC350_ERROR_CODE err)
 {
     switch (err) {
--- a/components.hpp	Fri Jan 05 17:56:59 2018 +0000
+++ b/components.hpp	Sun Jan 07 21:29:12 2018 +0000
@@ -1,14 +1,22 @@
 #ifndef __sample_hardware__
 #define __sample_hardware__
-
+#define SIG_READY 1
+#define SIG_READY2 1
+#define SIG_REMOVE 1
+#define SIG_NET 1
 //#define BME
 #ifdef BME
 #include "BME280.h"
 #else
 #include "BMP280.h"
 #endif
-
-
+#include "SDBlockDevice.h"
+#include "FATFileSystem.h"
+void runanalysis(void);
+void sdrun(void);
+void sdcheck(void);
+void sdwrite(void);
+void sdremove(void);
 enum ELEC350_ERROR_CODE {OK, FATAL};
 extern DigitalOut onBoardLED;
 extern DigitalOut redLED;
--- a/main.cpp	Fri Jan 05 17:56:59 2018 +0000
+++ b/main.cpp	Sun Jan 07 21:29:12 2018 +0000
@@ -1,81 +1,54 @@
 #include "components.hpp"
-#include "SDBlockDevice.h"
-#include "FATFileSystem.h"
-//#include "Networkbits.hpp"
 #include "mbed.h"
+#include "network.hpp"
 #include "lglcd.h"
-#include "stdio.h"
+InterruptIn sw1(USER_BUTTON);
+Thread samplesThread(osPriorityNormal);
+Thread sdThread(osPriorityLow);
+Thread sdRemoveThread(osPriorityLow);
+//Thread rtThread;
+//Thread stxThread;
+Thread netThread(osPriorityRealtime);
+void inter1();
+void inter2();
+void inter3();
+void inter4();
 // This is a very short demo that demonstrates all the hardware used in the coursework.
 // You will need a network connection set up (covered elsewhere). The host PC should have the address 10.0.0.1
-
-//Threads
-Thread nwrkThread;
+Ticker samples;
+Ticker remov;
+InterruptIn sdex(USER_BUTTON);
+Ticker storage;
+Ticker netTick;
+void inter1(){
+ sdcheck();
+ samplesThread.signal_set(SIG_READY);          
+}
+void inter2(){
+ sdThread.signal_set(SIG_READY2);  
+}
+void inter3(){
+ printf("CHECK REMOVE BUTTON\n\r");
+ if(sdex == 1){
+ sdRemoveThread.signal_set(SIG_REMOVE);
+ }
+ }
+ void inter4(){
+ netThread.signal_set(SIG_NET);
+}
 int main(){
+sdrun();
 lglcd mylcd(D7,D6,D5,D4,D3,D2);
 mylcd.clear();
-while(true){        
-double temp = sensor.getTemperature();
-double pressure = sensor.getPressure();
-double lightin = adcIn;
-char TEM[6];
-char PRE[5];  
-sprintf(TEM,"%.2f", temp);
-sprintf(PRE,"%.2f", pressure);
-printf(TEM);
-printf(PRE);
-mylcd.setline(1,1);
-mylcd.write("P:");
-mylcd.write(PRE);
-mylcd.setline(1,10);
-mylcd.write("T:");
-mylcd.write(TEM);
-mylcd.setline(2,1);
-mylcd.write("L:");
-if(lightin > 0.8 && lightin < 0.9)
-{
-mylcd.setline(2,4);
-mylcd.write("            ");
-mylcd.setline(2,4);
- mylcd.write("|||||||||");    
-}
-else if(lightin > 0.51 && lightin < 0.9)
-{
- mylcd.setline(2,4);
-mylcd.write("            ");
-mylcd.setline(2,4);
- mylcd.write("|||||");
-}
-else if(lightin > 0.4 && lightin < 0.5)
-{
- mylcd.setline(2,4);
-mylcd.write("            ");
-mylcd.setline(2,4);
- mylcd.write("|||");
-}
-else if(lightin > 0.2 && lightin < 0.39)
-{
- mylcd.setline(2,4);
-mylcd.write("            ");
-mylcd.setline(2,4);
- mylcd.write("LOW");
-} 
-else if(lightin < 0.19)
-{
-mylcd.setline(2,4);
-mylcd.write("            ");
-mylcd.setline(2,4);
-mylcd.write("disconnected");
-}
-
-//mylcd.write(light);
-//printf(adcIn);
-wait(0.4);
-      
-    }
-    
-    //Flash to indicate goodness
-  
-}
+sdThread.start(sdwrite);
+storage.attach(&inter2,10); //runs send to sd  based on last number (5) in this case (every 5 seconds)
+samplesThread.start(runanalysis); 
+samples.attach(&inter1,1);   //runs analysis based on last number (1) in this case (every 1 second)
+sdRemoveThread.start(sdremove);
+remov.attach(&inter3,3);//checks sd remove key every second
+netThread.start(networksend);
+netTick.attach(&inter4,0.01);
 
 
-    
+ 
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/network.cpp	Sun Jan 07 21:29:12 2018 +0000
@@ -0,0 +1,135 @@
+#if !FEATURE_LWIP
+    #error [NOT_SUPPORTED] LWIP not supported for this target
+#endif
+
+#include "mbed.h"
+#include "EthernetInterface.h"
+#include "TCPServer.h"
+#include "TCPSocket.h"
+#include <iostream>
+#include <string> 
+#include "BMP280.h"
+#include "components.hpp"
+#include "network.hpp"
+Mutex Net;
+#define HTTP_STATUS_LINE "HTTP/1.0 200 OK"
+#define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8"
+#define HTTP_MESSAGE_BODY1 ""                                    \
+"<html>" "\r\n"                                                  \
+"  <body style=\"display:flex;text-align:center\">" "\r\n"       \
+"    <div style=\"margin:auto\">" "\r\n"                         \
+"      <h1>Sensor Data</h1>" "\r\n"                              \
+"      <p>Light Level LDR value: " "\r\n" 
+                 
+#define HTTP_Pressure1 ""                                        \
+"<html>" "\r\n"                                                  \
+"  <body style=\"display:flex;text-align:center\">" "\r\n"       \
+"    <div style=\"margin:auto\">" "\r\n"                         \
+"      <h1> </h1>" "\r\n"                                        \
+"      <p>Pressure(mbar): " "\r\n"                
+
+#define HTTP_Pressure2 ""                                        \
+       "</p>" "\r\n"                                             \
+"    </div>" "\r\n"                                              \
+"  </body>" "\r\n"                                               \
+"</html>"
+
+                 
+#define HTTP_Temperature1 ""                                     \
+"<html>" "\r\n"                                                  \
+"  <body style=\"display:flex;text-align:center\">" "\r\n"       \
+"    <div style=\"margin:auto\">" "\r\n"                         \
+"      <h1> </h1>" "\r\n"                                        \
+"      <p>Temperature(Degrees Celcius): " "\r\n"       
+
+#define HTTP_Temperature2 ""                                     \
+       "</p>" "\r\n"                                             \
+"    </div>" "\r\n"                                              \
+"  </body>" "\r\n"                                               \
+"</html>"
+
+#define HTTP_MESSAGE_BODY2 ""                                    \
+       "</p>" "\r\n"                                             \
+"    </div>" "\r\n"                                              \
+"  </body>" "\r\n"                                               \
+"</html>"
+
+#define HTTP_RESPONSE HTTP_STATUS_LINE "\r\n"   \
+                      HTTP_HEADER_FIELDS "\r\n" \
+                      "\r\n"                    \
+                      HTTP_MESSAGE_BODY "\r\n"
+
+#define IP        "10.0.0.10"
+#define NETMASK   "255.0.0.0"
+#define GATEWAY   "10.0.0.1"
+
+AnalogIn pres();
+AnalogIn temp();
+
+void networksend(void)
+{   
+    // interrupt routine setup
+    printf("Basic HTTP server example\n");//n
+    
+    //Configure an ethernet connection
+    EthernetInterface eth;
+    eth.set_network(IP, NETMASK, GATEWAY);
+    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);
+    
+    /* Bind the HTTP port (TCP 80) to the server */
+    srv.bind(eth.get_ip_address(), 80);
+    
+    /* Can handle 5 simultaneous connections */
+    srv.listen(5);
+    
+    while (true) {
+        Net.lock();
+        using namespace std;
+        //Block and wait on an incoming connection
+        srv.accept(&clt_sock, &clt_addr);
+        printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port());
+        
+        double temp = sensor.getTemperature();
+        double pres = sensor.getPressure();
+        //Uses a C++ string to make it easier to concatinate
+        string response;
+        //This is a C string
+        char adcIn_str[64];
+        char pres_str[64];
+        char temp_str[64];
+               
+        //Read the LDR value
+        float u = adcIn;
+        float b = pres;
+        float a = temp;
+        
+        //Convert to a C String
+        sprintf(adcIn_str, "%5.3f", u );
+        sprintf(pres_str, "%4.2f", b);
+        sprintf(temp_str, "%3.1f", a);
+        printf("LDR: %5.3f\n\r", u);
+        
+        //Build the C++ string response
+        response = HTTP_MESSAGE_BODY1;
+        response += adcIn_str;
+        response += HTTP_Temperature1;
+        response += temp_str;
+        response += HTTP_Temperature2;
+        response += HTTP_Pressure1;
+        response += pres_str;
+        response += HTTP_Pressure2;
+        response += HTTP_MESSAGE_BODY2;       
+        //Send static HTML response (as a C string)
+        clt_sock.send(response.c_str(), response.size()+6); 
+        Net.unlock();   
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/network.hpp	Sun Jan 07 21:29:12 2018 +0000
@@ -0,0 +1,4 @@
+#ifndef __network__
+#define __network__
+void networksend(void);
+#endif
\ No newline at end of file