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

Dependencies:   BMP280

Files at this revision

API Documentation at this revision

Comitter:
mwthewsey
Date:
Mon Jan 08 15:12:23 2018 +0000
Parent:
10:261f2b69c4c7
Child:
12:03589f1d5c30
Commit message:
Sampling, LCD, Serial all working. Commit before SD and network

Changed in this revision

LCD.cpp Show annotated file Show diff for this revision Revisions of this file
LCD.h Show annotated file Show diff for this revision Revisions of this file
SDCard.cpp Show annotated file Show diff for this revision Revisions of this file
SDCard.h Show annotated file Show diff for this revision Revisions of this file
Serial.cpp Show annotated file Show diff for this revision Revisions of this file
Serial.h Show annotated file Show diff for this revision Revisions of this file
TextLCD.lib Show annotated file Show diff for this revision Revisions of this file
TimeInterface.cpp Show annotated file Show diff for this revision Revisions of this file
TimeInterface.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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LCD.cpp	Mon Jan 08 15:12:23 2018 +0000
@@ -0,0 +1,85 @@
+#include "mbed.h"
+#include "LCD.h"
+#include "TextLCD.h"
+#include "rtos.h"
+
+using namespace std;
+ENVDISPLAY::ENVDISPLAY(PinName rsPin, PinName rePin, PinName d4Pin, PinName d5Pin, PinName d6Pin, PinName d7Pin, PinName Button1Pin, PinName Button2Pin) : TextLCD(rsPin, rePin, d4Pin, d5Pin, d6Pin, d7Pin), Button1(Button1Pin), Button2(Button2Pin)
+{
+    //constructor
+    _latestTemp = 0.0;
+    _latestPres = 0.0;
+    _latestLDR = 0.0;
+    _latestTime = 0;
+    _currentState = SCROLLREADINGS;
+}
+void ENVDISPLAY::SendMessage(char sentText[], bool returnToReadings)
+{
+    if (strlen(sentText)>32)
+    {
+        //overflow error
+    } else {
+        std::copy( sentText, sentText+strlen(sentText), _message ); //store  inputted message to _message
+        _AutoQuit = returnToReadings;   //Set AutoQuitState
+        _currentState = MESSAGE;    //Change LCD state to show message
+    }
+}
+
+void ENVDISPLAY::UpdateData(float temp, float pres, float LDR, time_t sampleTime)
+{
+    _latestTemp = temp;
+    _latestPres = pres;
+    _latestLDR = LDR;
+    _latestTime = sampleTime;
+}
+
+void ENVDISPLAY::StateMachine(void)
+{
+    while (true) {
+        cls();
+        switch (_currentState) {
+            case SCROLLREADINGS:
+                tm T = ReturnDateTimeStruct(_latestTime);   //Get a time structure
+                printf("Temp: %5.1fC\n",_latestTemp);   //Print temperature
+                printf("Pres: %5.1fmBar\n",_latestPres);//Print Pressure
+                //while (Button2 == 0) {}; //Spin
+                Thread::wait(SCREENDELAY_MS);   //Hold screen for specified time
+                cls();  //Clear screen
+                printf("LDR: %4.3f\n",_latestLDR);  //Print Light Level
+                printf("%2d/%2d %2d:%2d:%2d\n",T.tm_mday,T.tm_mon,T.tm_hour,T.tm_min,T.tm_sec); //Print time DD/MM HH:mm:ss
+                Thread::wait(SCREENDELAY_MS);   //Hold screen for specified time
+                break;
+
+            case MESSAGE:
+                printf("%s\n",_message);    //Print custom message to display
+                Thread::wait(SCREENDELAY_MS);//Hold screen for specified time
+                if (_AutoQuit) {
+                //If it has been specified to return to scroll readings:
+                    _currentState = SCROLLREADINGS; //Change state
+                    memset(_message, 0, 32*sizeof(char));    //Wipe message
+                }
+                //Else stay in message state.
+                break;
+                
+            case ADJUSTTIME:
+                printf("Adjust the time\n");
+                while (Button2 == 0) {}; //Spin
+                SendMessage("Time Set",true);
+                Button1.rise(this, &ENVDISPLAY::Button1ISR);    //Reattach interrupt
+                break;
+        }
+    }
+}
+
+void ENVDISPLAY::Start(void)
+{
+    LCDThread.start(this, &ENVDISPLAY::StateMachine);
+    this->Button1.rise(this, &ENVDISPLAY::Button1ISR);
+    SendMessage("Starting...",true);
+}
+
+void ENVDISPLAY::Button1ISR(void)
+{
+    _currentState = ADJUSTTIME;
+    Button1.rise(NULL); //Detach IR
+}
\ No newline at end of file
--- a/LCD.h	Sun Jan 07 23:40:10 2018 +0000
+++ b/LCD.h	Mon Jan 08 15:12:23 2018 +0000
@@ -1,6 +1,61 @@
 #ifndef __LCD__
 #define __LCD__
 
-//These functions manage writing to the LCD
+#include "mbed.h"
+#include "TextLCD.h"
+#include "rtos.h"
+#include "TimeInterface.h"
+#include <string>
+/*
+*The ENVDISPLAY class enhances the abilities of the TextLCD class to be
+*relevant for an ENVIRONMENTAL sensor.
+*Temperature, pressure, LDR and time readings are passed into the object using
+*the UpdateData function. A custom message can also be displayed using
+*SendMessage, with the option of returning to enviromental data.
+*
+*Two buttons are required for time setting functions. Pressing button 1 enters
+*the lcd into a set_time state.
+*
+*Device initialised with: ENVDISPLAY lcd(D9, D8, D7, D6, D4, D2,PE_12, PE_14);
+*lcd.Start() must be run to begin operation.
+*/
+
+#define SCREENDELAY_MS 2000
+
+class ENVDISPLAY : TextLCD
+{
+private:
+    char _message[32];  //32 characters on display
+    bool _AutoQuit; //After a message, should the LCD return to enviromental?
+    float _latestTemp;
+    float _latestPres;
+    float _latestLDR;
+    time_t _latestTime;
+    
+    enum ThreadState{MESSAGE,SCROLLREADINGS,ADJUSTTIME};
+    
+    ThreadState _currentState;
+    
+    InterruptIn Button1;
+    DigitalIn Button2;
+    Thread LCDThread;
+    
+    void Button1ISR(void);
+    //Called when button 1 is pressed. 
+    void StateMachine(void);
+    //Gets attached to LCDThread
+    
+public:
+    ENVDISPLAY(PinName rsPin, PinName rePin, PinName d4Pin, PinName d5Pin, PinName d6Pin, PinName d7Pin, PinName Button1Pin, PinName Button2Pin);
+    //constructor
+    void SendMessage(char sentText[], bool returnToReadings);
+    //Display a custom message on the display with option to resume displaying readings
+    void Start(void);
+    //Inititates LCD activity
+    void UpdateData(float temp, float pres, float LDR, time_t sampleTime);
+    //Pass in new enviromental data
+    
+};
+
 
 #endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDCard.h	Mon Jan 08 15:12:23 2018 +0000
@@ -0,0 +1,16 @@
+#ifndef __Sampling__
+#define __Sampling__
+
+
+
+
+
+
+
+
+
+
+
+
+
+#endif
\ No newline at end of file
--- a/Serial.cpp	Sun Jan 07 23:40:10 2018 +0000
+++ b/Serial.cpp	Mon Jan 08 15:12:23 2018 +0000
@@ -30,19 +30,6 @@
     PC.attach(NULL, Serial::RxIrq); //Disable interrupt
 }
 
-void UpdateSerialData(void)
-{
-
-    TakeKeys(true); //Take keys
-
-    *tempReadingsSerial = *tempReadings; //Copy samples to serial variables
-    *presReadingsSerial = *presReadings;
-    *LDRReadingsSerial = *LDRReadings;
-    *timeReadingsSerial = *timeReadings;
-
-    TakeKeys(false); //Return keys
-}
-
 
 void SerialCode(void)
 {
@@ -67,14 +54,16 @@
             internalIndex = currentIndex; //InternalIndex for incrementing out data
             PC.printf("Printing all %d reccords\n\r",BUFFERSIZE);
             PC.printf("  Date  |  Time  | Temp | Pressure | Light\n\r");
-            tm T = ReturnDateTimeStruct(timeReadingsSerial[internalIndex]);
             TakeKeys(true); //Take keys
 
             for (short i = 0; i < BUFFERSIZE; i++) { //For loop of length buffersize
+                tm T = ReturnDateTimeStruct(timeReadings[internalIndex]);
                 //InternalIndex was set as newest. We will now decrement to display oldest to newest
                 PC.printf(" %d/%d/%d    %d:%d:%d    %2.2f     %2.2f     %2.2f\n\r",T.tm_year,T.tm_mon,T.tm_mday,T.tm_hour,T.tm_min,T.tm_sec,tempReadings[internalIndex],presReadings[internalIndex],LDRReadings[internalIndex]);
+                //PC.printf("%d\n\r",T.tm_year);
                 internalIndex = IndexDecrement(internalIndex); //Decrement internal index
             }
+            
             TakeKeys(false); //Return keys
 
 
@@ -85,10 +74,10 @@
 
             TakeKeys(true); //Take keys
 
-            memset(tempReadings, 0, BUFFERSIZE); //Fill array with 0s
-            memset(presReadings, 0, BUFFERSIZE);
-            memset(LDRReadings, 0, BUFFERSIZE);
-            memset(timeReadings, 0, BUFFERSIZE);
+            memset(tempReadings, 0.0, BUFFERSIZE * sizeof(float)); //Fill array with 0s
+            memset(presReadings, 0.0, BUFFERSIZE * sizeof(float));
+            memset(LDRReadings, 0.0, BUFFERSIZE * sizeof(float));
+            memset(timeReadings, 0.0, BUFFERSIZE * sizeof(float));
 
             nextIndex = 0;      //Reset Index
             currentIndex = 0;
@@ -141,6 +130,7 @@
                 tempReadings[oldestIndex] = 0; //Fill array with 0s
                 presReadings[oldestIndex] = 0;
                 LDRReadings[oldestIndex] = 0;
+                timeReadings[oldestIndex] = 0;
 
                 oldestIndex = IndexIncrement(oldestIndex);
             }
--- a/Serial.h	Sun Jan 07 23:40:10 2018 +0000
+++ b/Serial.h	Mon Jan 08 15:12:23 2018 +0000
@@ -12,10 +12,6 @@
 extern char InputBufferText[128];
 extern unsigned short InputBufferNum;
 extern unsigned short internalIndex;
-extern float tempReadingsSerial[BUFFERSIZE];
-extern float presReadingsSerial[BUFFERSIZE];
-extern float LDRReadingsSerial[BUFFERSIZE];
-extern time_t timeReadingsSerial[BUFFERSIZE];
 
 //Objects
 extern Thread SampleThread; //
@@ -32,9 +28,6 @@
 void SerialCode(void);
 //Code that runs in the SerialThread
 
-void UpdateSerialData(void);
-//Copies new sample data to serial variables.
-
 
 
 #endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TextLCD.lib	Mon Jan 08 15:12:23 2018 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/simon/code/TextLCD/#308d188a2d3a
--- a/TimeInterface.cpp	Sun Jan 07 23:40:10 2018 +0000
+++ b/TimeInterface.cpp	Mon Jan 08 15:12:23 2018 +0000
@@ -43,6 +43,7 @@
 struct tm ReturnDateTimeStruct(time_t seconds)
 {
     tm* ltm = localtime(&seconds);  //Turn given time integer into time structure pointer
-    //ltm.tm_mon += 1;    //adjust from the standard of 0-11 for months to 1-12
+    ltm->tm_mon += 1;    //adjust from the standard of 0-11 for months to 1-12
+    ltm->tm_year += 1900;
     return *ltm;    //Return the data held at this address.
 }
\ No newline at end of file
--- a/TimeInterface.h	Sun Jan 07 23:40:10 2018 +0000
+++ b/TimeInterface.h	Mon Jan 08 15:12:23 2018 +0000
@@ -21,7 +21,7 @@
 *   int tm_hour;  // hours of day from 0 to 24
 *   int tm_mday;  // day of month from 1 to 31
 *   int tm_mon;   // month of year from 1 to 12 (customised)
-*   int tm_year;  // year since 1900
+*   int tm_year;  // year since 1900 (customised)gives acctual year
 *   int tm_wday;  // days since sunday
 *   int tm_yday;  // days since January 1st
 *   int tm_isdst; // hours of daylight savings time
--- a/main.cpp	Sun Jan 07 23:40:10 2018 +0000
+++ b/main.cpp	Mon Jan 08 15:12:23 2018 +0000
@@ -3,13 +3,15 @@
 #include "Serial.h"
 #include "Sampling.h"
 #include "LCD.h"
+#include "SDCard.h"
 #include "SDBlockDevice.h"
 #include "FATFileSystem.h"
 
 
-
 //SD Card Object
 SDBlockDevice sd(D11, D12, D13, D10); // mosi, miso, sclk, cs
+//LCD Object
+ENVDISPLAY lcd(D9, D8, D7, D6, D4, D2,PE_12, PE_14);
 //File pointer for the SD card
 FILE* fp;
 
@@ -23,6 +25,7 @@
     
     //Initialise interrupts and times
     SerialStart();
+    lcd.Start();
     ConfigThreadsAndIR();
     firstSample = true; //Set only at start of program
     
@@ -36,8 +39,7 @@
             //LCD Update Function
             NewEnvSample = false;
             NewLDRSample = false;
-            //Serial update
-            UpdateSerialData();
+            lcd.UpdateData(tempReadings[currentIndex],presReadings[currentIndex],LDRReadings[currentIndex],timeReadings[currentIndex]);
 
         }