Plymouth ELEC351 Group T / Mbed OS ELEC351

Dependencies:   BME280 BMP280 TextLCD

Files at this revision

API Documentation at this revision

Comitter:
thomasmorris
Date:
Sun Jan 07 19:21:11 2018 +0000
Parent:
36:a0098306fc58
Child:
38:8d86e0d8a816
Commit message:
Working Revision network working;

Changed in this revision

ELEC350-Coursework-2017.lib 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
SETUP.cpp Show annotated file Show diff for this revision Revisions of this file
SETUP.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
sample_hardware.cpp Show annotated file Show diff for this revision Revisions of this file
sample_hardware.hpp Show annotated file Show diff for this revision Revisions of this file
--- a/ELEC350-Coursework-2017.lib	Sun Jan 07 16:17:24 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://os.mbed.com/teams/Plymouth-ELEC351-Group-T/code/ELEC350-Coursework-2017/#2f8e198841cf
--- a/NETWORK.cpp	Sun Jan 07 16:17:24 2018 +0000
+++ b/NETWORK.cpp	Sun Jan 07 19:21:11 2018 +0000
@@ -1,14 +1,14 @@
 #include "NETWORK.hpp"
-
+#include <string>
 AnalogIn ldr(PA_0);
 
 //Now setup a web server
 TCPServer srv;           //TCP/IP Server
 TCPSocket clt_sock;      //Socket for communication
 SocketAddress clt_addr;  //Address of incoming connection
-
+string GateWay_IP;
 
-void Network_Init()
+int Network_Init()
 { 
     printf("Basic HTTP server example\n");
     //Configure an ethernet connection
@@ -16,7 +16,13 @@
     eth.set_network(IP, NETMASK, GATEWAY);
     eth.connect();
     printf("The target IP address is '%s'\n", eth.get_ip_address());
-    //t1.start(Network_Thread);
+    GateWay_IP = eth.get_ip_address();
+    
+    if(GateWay_IP != "10.0.0.10")
+    {
+        return 1;
+        //Error code here   
+    }
     /* Open the server on ethernet stack */
     srv.open(&eth);
     
@@ -25,6 +31,7 @@
     
     /* Can handle 5 simultaneous connections */
     srv.listen(5);
+    return 0;
 }
 void Networking(int network_day, int network_month, int network_year, int network_hours, int network_minute, float network_temperature, float network_pressure, float network_light)
 {    
--- a/NETWORK.hpp	Sun Jan 07 16:17:24 2018 +0000
+++ b/NETWORK.hpp	Sun Jan 07 19:21:11 2018 +0000
@@ -40,7 +40,7 @@
 #define GATEWAY   "10.0.0.1"
 
 
-void Network_Init();
+int Network_Init();
 void Networking(int network_day, int network_month, int network_year, int network_hours, int network_minute, float network_temperature, float network_pressure, float network_light);
 
 #endif
\ No newline at end of file
--- a/SETUP.hpp	Sun Jan 07 16:17:24 2018 +0000
+++ b/SETUP.hpp	Sun Jan 07 19:21:11 2018 +0000
@@ -22,6 +22,9 @@
 #define EDGE_FALLEN 0
 #define mailsize 5
 #include <iostream>
+
+LCD LCD(D8,D9,D4,A0,A1,A2,A3,D7,D6,D3,D1); 
+
 Serial pc(USBTX, USBRX);
 //SW1+SW2 are declared as interrupt ins in sample hardware.cpp
 //Forward Declarations
@@ -29,7 +32,7 @@
 void Serial_Comms();
 void Sample_signal_set();
 void Console_Output_ISR();    
-void Serial_Comms_Stealth_Mode();
+void Serial_Comms_Data_Get(); 
 
 float Sample_Rate;
 //Thread IDs
@@ -48,7 +51,6 @@
 LED Yellow_led(PB_10);
 LED Green_led(PB_11);
 
-LCD LCD(D8,D9,D4,A0,A1,A2,A3,D7,D6,D3,D1); 
 char LCD_buffer [50];
 int LCD_sprintf;
 
@@ -82,6 +84,7 @@
 void SW1RisingEdge();
 void SW2RisingEdge();
 
+
 DATA Data_Active;
 DATA Data_Buffer[mailsize];
 
--- a/main.cpp	Sun Jan 07 16:17:24 2018 +0000
+++ b/main.cpp	Sun Jan 07 19:21:11 2018 +0000
@@ -1,7 +1,7 @@
 /*
 ELEC 351 Group T
 Team Members : Christopher Hills, Thomas Morris
-Current Verision 17
+Current Verision 18
 Overiew: Working Tasks 1,2,3,5,6,7,8,9,10,11,12,13
 
 Last Revision: Added Mail Box to serial
@@ -20,7 +20,7 @@
     while(1)
     {
         Thread::wait(5000);//Waits 5 seconds
-        cout << "In Network Thread" << endl;   
+        if(Log_Value==1){cout << "In Network Thread" << endl;} //Log this  
         
         time_t Time = Data_Active.get_time();
         tm* Time_Pointer = localtime(&Time);
@@ -42,10 +42,12 @@
 void LCD_Write_Year()
 {
         Time_Lock_Main.lock();
+        if(Log_Value == 1){cout<<"In LCD_Write_Year Time lock taken"<<endl;}
         time_t Time = time(NULL);
         tm* Time_Pointer = localtime(&Time);
         int Years = 1900 + Time_Pointer->tm_year;
         Time_Lock_Main.unlock();
+        if(Log_Value == 1){cout<<"In LCD_Write_Year Time lock released"<<endl;}
         stringstream ss;
         ss << Years;
         string Year_String = ss.str();
@@ -58,10 +60,12 @@
 void LCD_Write_Month()
 {
         Time_Lock_Main.lock();
+        if(Log_Value == 1){cout<<"In LCD_Write_Month Time lock taken"<<endl;}
         time_t Time = time(NULL);
         tm* Time_Pointer = localtime(&Time);
         int Months = 1 + Time_Pointer->tm_mon;
         Time_Lock_Main.unlock();
+        if(Log_Value == 1){cout<<"In LCD_Write_Month Time lock released"<<endl;}
         stringstream ss;
         ss << Months;
         string Month_String = ss.str();
@@ -73,10 +77,12 @@
 void LCD_Write_Day()
 {
         Time_Lock_Main.lock();
+        if(Log_Value == 1){cout<<"In LCD_Write_Day Time lock taken"<<endl;}
         time_t Time = time(NULL);
         tm* Time_Pointer = localtime(&Time);
         int Days = Time_Pointer->tm_mday;
         Time_Lock_Main.unlock();
+        if(Log_Value == 1){cout<<"In LCD_Write_Day Time lock released"<<endl;}
         stringstream ss;
         ss << Days;
         string Day_String = ss.str();
@@ -90,10 +96,12 @@
 void LCD_Write_Hour()
 {
         Time_Lock_Main.lock();
+        if(Log_Value == 1){cout<<"In LCD_Write_Hour Time lock taken"<<endl;}
         time_t Time = time(NULL);
         tm* Time_Pointer = localtime(&Time);
         int Hours = Time_Pointer->tm_hour;
         Time_Lock_Main.unlock();
+        if(Log_Value == 1){cout<<"In LCD_Write_Hour Time lock released"<<endl;}
         stringstream ss;
         ss << Hours;
         string Hour_String = ss.str();
@@ -105,10 +113,12 @@
 void LCD_Write_Minute()
 {
         Time_Lock_Main.lock();
+        if(Log_Value == 1){cout<<"In LCD_Write_Minute Time lock taken"<<endl;}
         time_t Time = time(NULL);
         tm* Time_Pointer = localtime(&Time);
         int Minutes = Time_Pointer->tm_min;
         Time_Lock_Main.unlock();
+        if(Log_Value == 1){cout<<"In LCD_Write_Minute Time lock released"<<endl;}
         stringstream ss;
         ss << Minutes;
         string Minute_String = ss.str();
@@ -120,10 +130,12 @@
 void LCD_Write_Seconds()
 {
         Time_Lock_Main.lock();
+        if(Log_Value == 1){cout<<"In LCD_Write_Seconds Time lock taken"<<endl;}
         time_t Time = time(NULL);
         tm* Time_Pointer = localtime(&Time);
         int Seconds = Time_Pointer->tm_sec;
         Time_Lock_Main.unlock();
+        if(Log_Value == 1){cout<<"In LCD_Write_Seconds Time lock released"<<endl;}
         stringstream ss;
         ss << Seconds;
         string Second_String = ss.str();
@@ -140,10 +152,10 @@
         Thread::wait(10);//Dont Delete
         if(mode == 0)//Default mode
         { 
-            if(Log_Value == 1){cout << "In mode 0 " << endl;}
+            if(Log_Value == 1){cout << "In mode 0 " << endl;}//Log this 
             
             Thread::wait(Default_Mode_Toggle_Time);
-    
+            if(Log_Value == 1){cout<<"Writing Data to LCD"<<endl;}
             sprintf (LCD_buffer, "%1.1f %1.1f %1.1f",Data_Active.get_temperature(),Data_Active.get_pressure(),Data_Active.get_light());//Used for converting to a sting  
              
             LCD.DDRAM_Address(0x00);
@@ -152,7 +164,7 @@
             LCD.Write_String(LCD_buffer);
 
             Thread::wait(Default_Mode_Toggle_Time);    
-            
+            if(Log_Value == 1){cout<<"Writing Time and Date to LCD"<<endl;}
             Time_Lock_Main.lock();
             time_t Time = Data_Active.get_time();
             tm* Time_Pointer = localtime(&Time);
@@ -165,6 +177,7 @@
             LCD.DDRAM_Address(0x40);
             LCD.Write_String(LCD_buffer);
             
+            if(Log_Value == 1){cout<<"Checking Switches for next mode"<<endl;}
             if(SW1.read() & SW2.read() == 1)
             {
                 mode = 1;   
@@ -181,14 +194,16 @@
                 LCD.DDRAM_Address(0x40);
                 LCD.Write_String("<              >");
                 Thread::wait(1000);
-                if(SW1.read() == 1)
+                if(Log_Value == 1){cout<<"Checking SW1 to go to Date setting"<<endl;}
+                if(SW1.read() == 1 & SW2.read() == 0)
                 {
-                    mode = 2;
+                    mode = 2;//Date Setting
                     break;   
                 }
-                if(SW2.read() == 1)
+                if(Log_Value == 1){cout<<"Checking SW2 to go to Time setting"<<endl;}
+                if(SW2.read() == 1 & SW1.read() == 0)
                 {
-                    mode = 5;
+                    mode = 5;//Time Setting
                     break;
                 }
             }
@@ -201,15 +216,18 @@
             {          
                 LCD_Write_Year();
                 Thread::wait(1000);
+                 if(Log_Value == 1){cout<<"Checking SW1 and SW2 to go to Month setting"<<endl;}
                 if(SW1.read() & SW2.read() == 1)
                 {
                     mode = 3;
                     break;   
                 }
+                if(Log_Value == 1){cout<<"Checking SW1 to add Year"<<endl;}
                 if(SW1.read() == 1 & SW2.read() == 0)
                 {
                     Add_Year();
                 }
+                if(Log_Value == 1){cout<<"Checking SW2 to subtract Year"<<endl;}
                 else if(SW2.read() == 1 & SW1.read() == 0)
                 {
                     Subtract_Year();   
@@ -225,15 +243,18 @@
             {
                 LCD_Write_Month();
                 Thread::wait(1000);
+                if(Log_Value == 1){cout<<"Checking SW1 and SW2 to go to Day setting"<<endl;}
                 if(SW1.read() & SW2.read() == 1)
                 {
                     mode = 4;
                     break;   
-                }                
+                }
+                if(Log_Value == 1){cout<<"Checking SW1 to add Month"<<endl;}                
                 if(SW1.read() == 1 & SW2.read() == 0)
                 {
                     Add_Month();
                 }
+                if(Log_Value == 1){cout<<"Checking SW2 to subtract Month"<<endl;}  
                 else if(SW2.read() == 1 & SW1.read() == 0)
                 {
                     Subtract_Month();   
@@ -249,15 +270,18 @@
             {      
                 LCD_Write_Day();
                 Thread::wait(1000);
+                if(Log_Value == 1){cout<<"Checking SW1 and SW2 to go Default setting"<<endl;}
                 if(SW1.read() & SW2.read() == 1)
                 {
                     mode = 0;   
                     break;
                 }
+                if(Log_Value == 1){cout<<"Checking SW1 to add Day"<<endl;}
                 if(SW1.read() == 1 & SW2.read() == 0)
                 {
                     Add_Day();
                 }
+                if(Log_Value == 1){cout<<"Checking SW2 to subtract Day"<<endl;}
                 else if(SW2.read() == 1 & SW1.read() == 0)
                 {
                     Subtract_Day();   
@@ -272,15 +296,18 @@
             {
                 LCD_Write_Hour();
                 Thread::wait(1000);
+                if(Log_Value == 1){cout<<"Checking SW1 and SW2 to go Minute setting"<<endl;}
                 if(SW1.read() & SW2.read() == 1)
                 {
                     mode = 6;  
                     break; 
-                }                
+                }
+                if(Log_Value == 1){cout<<"Checking SW1 to add Hour"<<endl;}                
                 if(SW1.read() == 1 & SW2.read() == 0)
                 {
                     Add_Hour();
                 }
+                if(Log_Value == 1){cout<<"Checking SW2 to subtract Hour"<<endl;} 
                 else if(SW2.read() == 1 & SW1.read() == 0)
                 {
                     Subtract_Hour();   
@@ -295,15 +322,18 @@
             {
                 LCD_Write_Minute();
                 Thread::wait(1000);
+                if(Log_Value == 1){cout<<"Checking SW1 and SW2 to go Seconds setting"<<endl;}
                 if(SW1.read() & SW2.read() == 1)
                 {
                     mode = 7;  
                     break; 
                 }                
+                if(Log_Value == 1){cout<<"Checking SW1 to add Minute"<<endl;}
                 if(SW1.read() == 1 & SW2.read() == 0)
                 {
                     Add_Minute();
                 }
+                if(Log_Value == 1){cout<<"Checking SW2 to subtract Minute"<<endl;}
                 else if(SW2.read() == 1 & SW1.read() == 0)
                 {
                     Subtract_Minute();   
@@ -318,15 +348,18 @@
             {
                 LCD_Write_Seconds();
                 Thread::wait(1000);
+                if(Log_Value == 1){cout<<"Checking SW1 and SW2 to go Default setting"<<endl;}
                 if(SW1.read() & SW2.read() == 1)
                 {
                     mode = 0;  
                     break; 
-                }                
+                }
+                if(Log_Value == 1){cout<<"Checking SW1 to add Second"<<endl;}                
                 if(SW1.read() == 1 & SW2.read() == 0)
                 {
                     Add_Second();
                 }
+                if(Log_Value == 1){cout<<"Checking SW1 to subtract Second"<<endl;}   
                 else if(SW2.read() == 1 & SW1.read() == 0)
                 {
                     Subtract_Second();   
@@ -335,17 +368,20 @@
         }
         else
         {
+            if(Log_Value == 1){cout<<"Mode Error occured mode now set to 0"<<endl;}   
             mode = 0;
         }
     }
 }
 void Serial_Commands()//Used for getting input from the user to determine the opperations to perform
 {
+    
     string Serial_Input;
-    int A,B,C;
+    int A,B,C;//Place a thread wait here?
     while(1) {
+        if(Log_Value == 1){cout<<"In Serial_Commands"<<endl;}   
         Serial_Input = "Blank";
-        cout << "Please type in a command" << endl;
+        cout << "Please type in a command" << endl;//Log this 
         
         cin >> Serial_Input >> A >> B >> C;
 
@@ -364,7 +400,7 @@
         {   
             Sampling_Timer.detach();
             Console_Output_Timer.detach();
-            for (int x = 0; x < mailsize; x++)
+            for (int x = 0; x < mailsize; x++)//cout deleted or something
             {
     
             }
@@ -375,7 +411,7 @@
         {
             for (int x = 0; x < A; x++)
             {
-                Serial_Comms_Stealth_Mode();
+                Serial_Comms_Data_Get();
                 if(Log_Value == 1){cout << "Read " << x << " samples" << endl;}
             }
         } 
@@ -412,7 +448,7 @@
         } 
         else if(Serial_Input == "LOGGING")
         {
-            Log_Value = A;
+            Log_Value = A;//Look into a nicer way of doing this
         } 
         else if(Serial_Input == "HELP")// Use this to display all of the availble commands
         { 
@@ -422,18 +458,15 @@
         {
             Console_Output_Timer.detach(); //Stops Sampling   
         }
-         else if(Serial_Input == "Blank Blank")
-        {
-           cout << "Blank Blank complete" << endl;
-        }
         else 
         {
             cout << "Please enter an acceptable command" << endl;
         }
     }
 }
-void Serial_Comms_Stealth_Mode()                                                        //Change this name
+void Serial_Comms_Data_Get()                                                        //Change this name
 {
+    if(Log_Value == 1){cout<<"Getting Data"<<endl;} 
     time_t Time = Data_Active.get_time();
     tm* Time_Pointer = localtime(&Time);
     strftime(scom_time_buffer, 32, "%I:%M %p\t", Time_Pointer);                         //Format time as a string
@@ -448,9 +481,11 @@
 }
 void Serial_Comms()//Thread for Serial Communications
 {
-    while(1) {
+    while(1) 
+    {
         Thread::signal_wait(SerialCommsTime);//Stupid 0 Bug
-        Serial_Comms_Stealth_Mode();
+        Serial_Comms_Data_Get();
+        if(Log_Value == 1){cout<<"In Serial Comms"<<endl;} 
     }
 }
 
@@ -463,7 +498,7 @@
     while(1) 
     {
         Thread::signal_wait(SamplingTime);      //Set the time between samples
-
+        if(Log_Value == 1){cout<<"Sample Time"<<endl;} 
         Data_Active.set_temperature(sensor.getTemperature());   //Read Temperature
         Data_Active.set_pressure(sensor.getPressure());         //Read Pressure
         Data_Active.set_light(adcIn.read());                    //Read Light
@@ -476,15 +511,12 @@
 
         Red_led.Toggle();
         t1.signal_set(NotSamplingTime);
+        if(Log_Value == 1){cout<<"New Sample avaliable"<<endl;} 
     }
 }
 int main()
 {
-    //Greeting
-    pc.printf("Test Start");
-    pc.printf("\n\r");
-
-    set_time(1512940530);               //Set RTC time to December 10 2017
+    set_time(1515352584);               //Set RTC time to December 10 2017
     
     pc.baud(9600);                      //Sets the Serial Comms Baud Rate
 
@@ -508,9 +540,18 @@
     if (fp == NULL) {
         error("Could not open file for write\n");
         LCD.Display_Clear();
-        LCD.Write_String("CANNOT OPEN FILE\n\n");//Change me
+        LCD.Write_String("CANNOT OPEN FILE");
         errorCode(FATAL);
     }
+    int network_temp;
+    network_temp = Network_Init();
+    if(network_temp == 1)//Sets up the network and checks if the network cable is not pluged in
+    {
+        error("Could not access network");
+        LCD.Display_Clear();
+        LCD.Write_String("Could not access network");
+        errorCode(FATAL);   
+    }
     //Last message before sampling begins
     LCD.Display_Clear();
     LCD.Write_String("READY     PLAYER");
@@ -518,7 +559,6 @@
     LCD.Write_String("      One     ");
     LCD.DDRAM_Address(0x00);
 
-    Network_Init();//Sets up the network
     Sample_Rate = TimerInterval;
     //Run interrupt
     Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sample_hardware.cpp	Sun Jan 07 19:21:11 2018 +0000
@@ -0,0 +1,101 @@
+#include "mbed.h"
+#include "sample_hardware.hpp"
+#include "LCD.hpp"
+#define RED_DONE 1
+#define YELLOW_DONE 2
+
+//Digital outputs
+DigitalOut onBoardLED(LED1);
+DigitalOut redLED(PE_15);
+DigitalOut yellowLED(PB_10);
+DigitalOut greenLED(PB_11);
+
+//Inputs
+DigitalIn  onBoardSwitch(USER_BUTTON);
+InterruptIn  SW1(PE_12);
+InterruptIn  SW2(PE_14);
+//Serial pc(USBTX, USBRX);
+AnalogIn adcIn(PA_0);
+
+//Environmental Sensor driver
+#ifdef BME
+BME280 sensor(D14, D15);
+#else
+BMP280 sensor(D14, D15);
+#endif
+
+//SD Card
+SDBlockDevice sd(PB_5, D12, D13, D10); // mosi, miso, sclk, cs 
+
+//POWER ON SELF TEST
+void post() 
+{
+    //POWER ON TEST (POT)
+    puts("**********STARTING POWER ON SELF TEST (POST)**********");
+    
+    //Test LEDs
+    puts("ALL LEDs should be blinking");
+    for (unsigned int n=0; n<10; n++) {
+        redLED    = 1;
+        yellowLED = 1;
+        greenLED  = 1;
+        wait(0.05);
+        redLED    = 0;
+        yellowLED = 0;
+        greenLED  = 0;     
+        wait(0.05);         
+    } 
+    
+    //Output the switch states (hold them down to test)
+    printf("SW1: %d\tSW2: %d\n\r", SW1.read(), SW2.read());    
+    printf("USER: %d\n\r", onBoardSwitch.read()); 
+    
+    //Output the ADC
+    printf("ADC: %f\n\r", adcIn.read());
+    
+    //Read Sensors (I2C)
+    float temp = sensor.getTemperature();
+    float pressure = sensor.getPressure();
+    #ifdef BME
+    float humidity = sensor.getHumidity();
+    #endif
+   
+    //Display in PuTTY
+    printf("Temperature: %5.1f\n", temp);
+    printf("Pressure: %5.1f\n", pressure);
+    #ifdef BME
+    printf("Pressure: %5.1f\n", humidity);
+    #endif
+    
+    //Display on LCD
+    redLED = 1;
+    //LCD.Display_Clear();
+    //LCD.DDRAM_Address(0x00);
+    //LCD.Write_String("LCD TEST...");
+    wait(0.5);
+    redLED = 0;
+    
+    //Network test (if BOTH switches are held down)
+    //networktest();
+    
+    puts("**********POST END**********");
+ 
+}
+
+void errorCode(ELEC350_ERROR_CODE err)
+{
+    switch (err) {
+      case OK:
+        greenLED = 1;
+        wait(1.0);
+        greenLED = 0;
+        return;                
+      case FATAL:
+        while(1) {
+            redLED = 1;
+            wait(0.1);
+            redLED = 0;
+            wait(0.1);                
+        }
+    };
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sample_hardware.hpp	Sun Jan 07 19:21:11 2018 +0000
@@ -0,0 +1,36 @@
+#ifndef __sample_hardware__
+#define __sample_hardware__
+//#define BME
+#ifdef BME
+#include "BME280.h"
+#else
+#include "BMP280.h"
+#endif
+#include "SDBlockDevice.h"
+#include "FATFileSystem.h"
+#include "LCD.hpp"
+
+enum ELEC350_ERROR_CODE {OK, FATAL};
+
+extern DigitalOut onBoardLED;
+extern DigitalOut redLED;
+extern DigitalOut yellowLED;
+extern DigitalOut greenLED;
+
+extern DigitalIn  onBoardSwitch;
+extern InterruptIn  SW1;
+extern InterruptIn  SW2;
+//extern Serial pc;
+extern AnalogIn adcIn;
+
+#ifdef BME
+extern BME280 sensor;
+#else
+extern BMP280 sensor;
+#endif
+extern SDBlockDevice sd;
+
+extern void post();
+extern void errorCode(ELEC350_ERROR_CODE err);
+
+#endif
\ No newline at end of file