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

Dependencies:   BMP280

Files at this revision

API Documentation at this revision

Comitter:
mwthewsey
Date:
Tue Jan 09 08:57:57 2018 +0000
Parent:
11:b538e73841ae
Child:
13:41c394fa932c
Commit message:
Sampling,LCD,Serial, SD working. Stability problem exists with RXISR!

Changed in this revision

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
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
Serial.cpp 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/LCD.h	Mon Jan 08 15:12:23 2018 +0000
+++ b/LCD.h	Tue Jan 09 08:57:57 2018 +0000
@@ -20,7 +20,7 @@
 *lcd.Start() must be run to begin operation.
 */
 
-#define SCREENDELAY_MS 2000
+#define SCREENDELAY_MS 2707
 
 class ENVDISPLAY : TextLCD
 {
@@ -56,6 +56,6 @@
     //Pass in new enviromental data
     
 };
-
+extern ENVDISPLAY lcd;
 
 #endif
\ No newline at end of file
--- a/SDCard.cpp	Mon Jan 08 15:12:23 2018 +0000
+++ b/SDCard.cpp	Tue Jan 09 08:57:57 2018 +0000
@@ -0,0 +1,145 @@
+#include "mbed.h"
+#include "Sampling.h"
+#include "SDCard.h"
+#include "SDBlockDevice.h"
+#include "FATFileSystem.h"
+#include "LCD.h"
+
+DigitalOut GreenLED(PB_11);
+DigitalOut SDCardStatusLED(LED3);
+InterruptIn SD_WP(PE_10);
+InterruptIn UserButton(USER_BUTTON);
+SDBlockDevice sd(PB_5, D12, D13, D10);
+
+enum SDCardStates {INSERTED,REMOVED,DISMOUNTREQUEST};
+bool SDCardPresent;
+bool SDCardMounted;
+unsigned short SDinternalIndex;
+
+FILE* fp; //File pointer type
+SDCardStates SDCurrentState;
+
+Thread SDCardThread;
+FATFileSystem fs;
+
+
+void SDCardInit(void)
+{
+    SDCardThread.start(&SDCardCode);   //Start thread running
+    SD_WP.rise(&SDCardRemovedISR);     //SD removed
+    SD_WP.fall(&SDCardInsertedISR);    //SD inserted
+    UserButton.fall(&SDCardButtonISR); //Button released
+
+    if (SD_WP == 1) {
+        SDCardPresent = false;
+        SDCurrentState = REMOVED;
+        SDCardMounted = false;
+    } else {
+        SDCardPresent = true;
+        SDCurrentState = INSERTED;
+        SDCardMounted = false;
+    }
+}
+
+
+//-----------------------------------------------------------------//
+//ISRs
+void SDCardRemovedISR(void)
+{
+    SDCardPresent = false;
+    SDCurrentState = REMOVED;
+    SDCardThread.signal_set(1);
+}
+
+void SDCardInsertedISR(void)
+{
+    SDCardPresent = true;
+    SDCurrentState = INSERTED;
+    SDCardThread.signal_set(1);
+
+}
+
+void SDCardButtonISR(void)
+{
+    SDCurrentState = DISMOUNTREQUEST;
+    SDCardThread.signal_set(1);
+}
+//-----------------------------------------------------------------//
+
+
+void SDCardCode(void)
+{
+
+    //set bool state. if sd card is in or not. (if sd card is already in)
+    if (SD_WP) {
+        SDCurrentState = REMOVED;
+    } else {
+        SDCurrentState = INSERTED;
+    }
+
+    while(true) {
+        Thread::signal_wait(1);    //Wait untill signal set
+
+        if (SDCurrentState == REMOVED) {
+
+            if (SDCardMounted) {
+                //print error message file currupt
+                lcd.SendMessage("SD CARD REMOVED!  FILE CORRUPT",true);
+            }
+            SDCardStatusLED = 0;
+            GreenLED = 0;
+
+
+
+        } else if (SDCurrentState == INSERTED) {
+
+            Thread::wait(1000); //wait 1 seccond to allow the SD card to be pluged in.
+            
+            if ( sd.init() != 0) { //Initalises the SDCard and checks if it succeeded
+                //SDInit failed. Reinsert the SDCard
+            }
+
+            //Create a filing system for SD Card
+            FATFileSystem fs("sd", &sd);////////////////////////////////////////////////////moved to init. dont know if this works.
+
+            //Open to WRITE
+            fp = fopen("/sd/test.csv","w");
+            if (fp == NULL) {
+                lcd.SendMessage("CANNOT OPEN FILE/sd/test.csv",true);
+                //error
+            }
+            SDCardMounted = true;
+            
+            Sampling(false);         //Stop sampling
+            SDinternalIndex = currentIndex; //InternalIndex for incrementing out data
+            TakeKeys(true); //Take keys
+
+            for (short i = 0; i < BUFFERSIZE; i++) { //For loop of length buffersize
+                tm T = ReturnDateTimeStruct(timeReadings[SDinternalIndex]);
+                //InternalIndex was set as newest. We will now decrement to display oldest to newest
+                fprintf(fp," %4d/%2d/%2d,%2d:%2d:%2d,%2.2f,%2.2f,%2.2f\n",T.tm_year,T.tm_mon,T.tm_mday,T.tm_hour,T.tm_min,T.tm_sec,tempReadings[SDinternalIndex],presReadings[SDinternalIndex],LDRReadings[SDinternalIndex]);
+                SDinternalIndex = IndexDecrement(SDinternalIndex); //Decrement internal index
+            }
+            
+            TakeKeys(false); //Return keys
+            Sampling(true);  //Start sampling
+            
+
+            fclose(fp);
+            SDCardStatusLED = 1;
+
+
+
+        } else if (SDCurrentState == DISMOUNTREQUEST) {
+
+            sd.deinit();
+            SDCardMounted = false;
+            lcd.SendMessage("REMOVE SD CARD",true);
+            GreenLED = 1;
+
+        }
+    }
+}
+
+
+
--- a/SDCard.h	Mon Jan 08 15:12:23 2018 +0000
+++ b/SDCard.h	Tue Jan 09 08:57:57 2018 +0000
@@ -1,16 +1,19 @@
-#ifndef __Sampling__
-#define __Sampling__
-
-
-
-
+#ifndef __SDCard__
+#define __SDCard__
 
 
-
-
+enum SDCardStates;
+extern bool SDCardPresent;
+extern bool SDCardMounted;
+extern unsigned short SDinternalIndex;
+extern SDCardStates SDCurrentState;
 
-
+extern Thread SDCardThread;
 
-
+void SDCardInit(void);
+void SDCardRemovedISR(void);
+void SDCardInsertedISR(void);
+void SDCardButtonISR(void);
+void SDCardCode(void);
 
 #endif
\ No newline at end of file
--- a/Sampling.cpp	Mon Jan 08 15:12:23 2018 +0000
+++ b/Sampling.cpp	Tue Jan 09 08:57:57 2018 +0000
@@ -31,11 +31,6 @@
 
 bool firstSample = true;
 
-//Hardware
-BMP280 sensor(D14, D15);
-AnalogIn LDRSensor(A0);
-DigitalOut SamplingLED(PB_10);
-
 void SampleTimerISR(void)
 {
     //Flag Threads
--- a/Sampling.h	Mon Jan 08 15:12:23 2018 +0000
+++ b/Sampling.h	Tue Jan 09 08:57:57 2018 +0000
@@ -5,8 +5,8 @@
 #include "BMP280.h"
 #include "rtos.h"
 
-#define BUFFERSIZE 16
-#define SAMPLERATE 1
+#define BUFFERSIZE 120
+#define SAMPLERATE 7
 
 
 //Thread Sync Tools
@@ -36,6 +36,7 @@
 
 extern Ticker sampleRate;
 extern Timeout SampleLEDTimeout;
+//Hardware
 extern BMP280 sensor;
 extern AnalogIn LDRSensor; //Input pin for LDR
 extern DigitalOut SamplingLED; //Onboard LED showing when a sample happens
--- a/Serial.cpp	Mon Jan 08 15:12:23 2018 +0000
+++ b/Serial.cpp	Tue Jan 09 08:57:57 2018 +0000
@@ -2,9 +2,9 @@
 #include "mbed.h"
 #include "Sampling.h"
 #include "TimeInterface.h"
+
 #include <string>
 
-Serial PC(USBTX, USBRX);
 Thread SerialThread;
 
 int DateTimeVar[3];
@@ -20,7 +20,8 @@
 void SerialStart(void)
 {
     SerialThread.start(&SerialCode);            //Start thread running
-    PC.attach(&RXInterruptISR, Serial::RxIrq);  //Attach interrupt function to hardware interrupt
+    //PC.attach(&RXInterruptISR, Serial::RxIrq);  //Attach interrupt function to hardware interrupt
+    PC.attach(&RXInterruptISR); //Fix
 }
 
 
@@ -51,6 +52,7 @@
 
 
         } else if (InputBufferString == "readall") {
+            Sampling(false);         //Stop sampling
             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");
@@ -59,12 +61,13 @@
             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(" %4d/%2d/%2d    %2d:%2d:%2d    %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
+            Sampling(true);  //Start sampling
 
 
         } else if (InputBufferString == "deleteall") {
@@ -109,8 +112,9 @@
             PC.printf("  Date  |  Time  | Temp | Pressure | Light\n\r");
             TakeKeys(true); //Take keys
 
-            for (short i = 0; i < InputBufferNum; i++) { //For loop of length InputBufferNum
-                PC.printf(" %d   %d    %2.2f     %2.2f     %2.2f\n\r",150496,165700,tempReadings[internalIndex],presReadings[internalIndex],LDRReadings[internalIndex]);
+            for (short i = 0; i < InputBufferNum; i++) { //For loop of length InputBufferNum                
+                tm T = ReturnDateTimeStruct(timeReadings[internalIndex]);
+                PC.printf(" %4d/%2d/%2d    %2d:%2d:%2d    %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]);
                 internalIndex = IndexDecrement(internalIndex); //Decrement internal index
             }
             TakeKeys(false); //Return keys
@@ -194,6 +198,7 @@
 
 
         } else if (InputBufferString == "state") {
+            
 
 
         } else if (InputBufferString == "logging") {
--- a/main.cpp	Mon Jan 08 15:12:23 2018 +0000
+++ b/main.cpp	Tue Jan 09 08:57:57 2018 +0000
@@ -5,27 +5,35 @@
 #include "LCD.h"
 #include "SDCard.h"
 #include "SDBlockDevice.h"
-#include "FATFileSystem.h"
+
 
 
+//Hardware setup
+BMP280 sensor(D14, D15);
+AnalogIn LDRSensor(A0);
+DigitalOut SamplingLED(PB_10);
+
+Serial PC(USBTX, USBRX);
 //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;
+//FILE* fp;
 
 int main()
 {
     //Initialise devices
 ////WebUISetup();
-    
-    
+       
     //Hardware Self Test
     
     //Initialise interrupts and times
     SerialStart();
     lcd.Start();
+    SDCardInit();
+
+    
     ConfigThreadsAndIR();
     firstSample = true; //Set only at start of program