Plymouth ELEC351 Group T / Mbed OS ELEC351_Group_T

Fork of ELEC351 by Plymouth ELEC351 Group T

Revision:
51:47f5db68500b
Parent:
50:3d61ca637399
Child:
52:99915f5240b2
--- a/SD_CARD.cpp	Tue Jan 09 12:19:12 2018 +0000
+++ b/SD_CARD.cpp	Tue Jan 09 15:15:08 2018 +0000
@@ -1,93 +1,102 @@
 #include "SD_CARD.hpp"
-FILE* fp; 
-InterruptIn SD_CARD_DETECT(D0);
+FILE* fp;                           //Declaration of FILE pointer named fp
+InterruptIn SD_CARD_DETECT(D0);     //Declaration of Interrupt to detect if the SD Card is present
    
 
-bool SD_Write = 1;
-void SD_Init()//Initialised the SD CARD
+bool SD_Write = 1;                  //Declare global variable SD_Write and initialise it to 0
+void SD_Init()                      //Function to initialise the SD Card
 {
     
 }
-void SD_Card_Eject()
-{/*
-    SD_Write = 0;
-    if(Log_Value==5){pc.printf("Closing File Ejecting SD Card\n");}
-    //fclose(fp);//Close File
-    sd.deinit();//Close down
-    LCD.Display_Clear();
-    LCD.DDRAM_Address(0x00);
-    LCD.Write_String("SD Card");
-    LCD.DDRAM_Address(0x40);
-    LCD.Write_String("Unmounted");
-    LCD.DDRAM_Address(0x00);
-    pc.printf("SD Card Unmounted\n");
-    */
+
+void SD_Card_Eject()                                                        //Function to eject the SD Card
+{
+    SD_Write = 0;                                                           //Set global variable SD_Write to 0
+    if(Log_Value==5){pc.printf("Closing File Ejecting SD Card\n");}         //If logging is enabled, print debugging statement
+    fclose(fp);                                                             //Close the opened file
+    sd.deinit();                                                            //Deinitialise the SD card
+    LCD.Display_Clear();                                                    //Clear the LCD Display
+    LCD.DDRAM_Address(0x00);                                                //Set the LCD Write Address to 0x00
+    LCD.Write_String("SD Card");                                            //Write "SD Card" to the first line of the LCD
+    LCD.DDRAM_Address(0x40);                                                //Set the LCD Write Address to 0x40
+    LCD.Write_String("Unmounted");                                          //Write "Unmounted" to the second line of the LCD
+    LCD.DDRAM_Address(0x00);                                                //Set the LCD Write Address to 0x00
+    pc.printf("SD Card Unmounted\n");                                       //Write "SD Card Unmounted" to the terminal
+    
 }
-void SD_Card_Write()
+void SD_Write_Control(const char* SD_Write_FileToOpen, string Write_Append_Select)                  //Function to write data to a file
 {
-    if(SD_Write == 0)
+    if      (Write_Append_Select == "Append"){fp = fopen(SD_Write_FileToOpen,"a");}                 //If "Append" is selected, continue writing to existing file
+    else if (Write_Append_Select == "Write" ){fp = fopen(SD_Write_FileToOpen, "w");}                //If "Write" is selected, create and write to new file
+    
+    if(fp == NULL)                                                                                  //If file cannot be opened
+    {
+        error("Could not open file for write in SD_Card\n");                                        //Print error message to terminal
+        LCD.Display_Clear();                                                                        //Clear the LCD display
+        LCD.Write_String("CANNOT OPEN FILE");                                                       //Write error message to LCD
+        errorCode(FATAL);                                                                           //Run error function - toggles red LED
+    }                             
+                
+    if(Log_Value==5){pc.printf("File Opened\n");}                                                   //If logging is enabled, print debug statement  
+                       
+    for(int SD_Card_Data_Pointer = 0; SD_Card_Data_Pointer != mailsize; SD_Card_Data_Pointer++)     //For all addresses in the DATA buffer
     {
-        if(Log_Value==5){pc.printf("SD Has Been Ejected Please do a Hardware Restart\n");}
-        return;   
+        if(Log_Value==5){pc.printf("Copying from address: %d\n", SD_Card_Data_Pointer);}            //If logging is enabled, print debug statement
+                        
+        time_t Time = Data_Buffer[SD_Card_Data_Pointer].get_time();                                 //Store the time from the DATA buffer 
+        tm* Time_Pointer = localtime(&Time);                                                        //Create pointer to stored time
+        int temp_day = Time_Pointer->tm_mday;                                                       //Assign the day to a temporary value
+        int temp_month = (Time_Pointer->tm_mon+1);                                                  //Assign the month to a temporary value
+        int temp_year = (Time_Pointer->tm_year+1900);                                               //Assign the year to a temporary value
+        int temp_hours = Time_Pointer->tm_hour;                                                     //Assign the hour to a temporary value 
+        int temp_minute = Time_Pointer->tm_min;                                                     //Assign the minute to a temporary value
+        int temp_seconds = Time_Pointer->tm_sec;                                                    //Assign the second to a temporary value
+        float temp_temperature = Data_Buffer[SD_Card_Data_Pointer].get_temperature();               //Assign the temperature to a temporary value            
+        float temp_pressure = Data_Buffer[SD_Card_Data_Pointer].get_pressure();                     //Assign the pressure to a temporary value
+        float temp_light = Data_Buffer[SD_Card_Data_Pointer].get_light();                           //Assign the light to a temporary value
+                        
+        fprintf(fp, "Date: %02d/%02d/%d,",temp_day,temp_month,temp_year);                           //Print the date into column 1
+        fprintf(fp, "Time: %02d:%02d:%02d,",temp_hours,temp_minute, temp_seconds);                  //Print the time into column 2
+        fprintf(fp, "Temperature: %1.1f,",temp_temperature);                                        //Print the temperature into column 3
+        fprintf(fp, "Pressure: %1.1f,",temp_pressure);                                              //Print the pressure into column 4
+        fprintf(fp, "Light: %5.3f,",temp_light);                                                    //Print the light into column 5
+    }            
+    fclose(fp);                                                                                     //Close the file
+     
+}
+void SD_Card_Write()                                                                        //Function to write to the SD card
+{
+    if(SD_Write == 0)                                                                       //If the SD card is not present
+    {
+        if(Log_Value==5){pc.printf("SD Has Been Ejected Please do a Hardware Restart\n");}  //If logging is enabled, print debug statement        
+        return;                                                                             //Exit function
     }
-    if(Write_Pointer == mailsize - 1)
+    if(Write_Pointer == mailsize - 1)                                                       //If the end of the buffer is being written to
         {
-            if(Log_Value==5){pc.printf("In SD_Card Thread\n");}
+            if(Log_Value==5){pc.printf("In SD_Card Thread\n");}                             //If logging is enabled, print debug statement
+    
+            FATFileSystem fs("sd", &sd);                                                    //Sets working directory as "sd"
             
-            FATFileSystem fs("sd", &sd);
-            
+            time_t File_Time = Data_Buffer[Write_Pointer - 1].get_time();                   //Store the time from the data buffer
+            tm* File_Time_Pointer = localtime(&File_Time);                                  //Create pointer to stored time
+            int File_temp_day = File_Time_Pointer->tm_mday;                                 //Assign the day to a temporary value
+            int File_temp_month = (File_Time_Pointer->tm_mon+1);                            //Assign the month to a temporary value
+            int File_temp_year = (File_Time_Pointer->tm_year+1900);                         //Assign the year to a temporary value
             
-            time_t File_Time = Data_Buffer[Write_Pointer - 1].get_time();
-            tm* File_Time_Pointer = localtime(&File_Time);
-            int File_temp_day = File_Time_Pointer->tm_mday;
-            int File_temp_month = (File_Time_Pointer->tm_mon+1);//Set to current month
-            int File_temp_year = (File_Time_Pointer->tm_year+1900);//Set to current year
-            
+            //Create a const char* using year, month and day and create a file using that name
             char FileToOpen[50] = {};
             snprintf (FileToOpen, sizeof(char) * sizeof(FileToOpen), "/sd/%d_%02d_%02d.csv",File_temp_year,File_temp_month,File_temp_day);
             
-            fp = fopen(FileToOpen,"wb");
-            if (fp == NULL) 
-            {
-                error("Could not open file for write in SD_Card\n");
-                LCD.Display_Clear();
-                LCD.Write_String("CANNOT OPEN FILE");
-                errorCode(FATAL);
-            }                             //Open to WRITE
-            
-            if(Log_Value==5){pc.printf("File Opened\n");}  
-                   
-            for(int SD_Card_Data_Pointer = 0; SD_Card_Data_Pointer != mailsize; SD_Card_Data_Pointer++)
+            if(fp = fopen(FileToOpen,"r"))                                                  //If the file already exists
             {
-                if(Log_Value==5){pc.printf("Copying from address: %d\n", SD_Card_Data_Pointer);}
-                
-                //ofstream outfile ("Hello Tom.txt");
-                time_t Time = Data_Buffer[SD_Card_Data_Pointer].get_time();
-                tm* Time_Pointer = localtime(&Time);
-                int temp_day = Time_Pointer->tm_mday;
-                int temp_month = (Time_Pointer->tm_mon+1);//Set to current month
-                int temp_year = (Time_Pointer->tm_year+1900);//Set to current year
-                    
-                int temp_hours = Time_Pointer->tm_hour; 
-                int temp_minute = Time_Pointer->tm_min;
-                int temp_seconds = Time_Pointer->tm_sec;
-                    
-                float temp_temperature = Data_Buffer[SD_Card_Data_Pointer].get_temperature();
-                float temp_pressure = Data_Buffer[SD_Card_Data_Pointer].get_pressure();
-                float temp_light = Data_Buffer[SD_Card_Data_Pointer].get_light();
-                
-                
-                fprintf(fp, "Date: %02d/%02d/%d,",temp_day,temp_month,temp_year);//Date
-                fprintf(fp, "Time: %02d:%02d:%02d,",temp_hours,temp_minute, temp_seconds);//Time
-                fprintf(fp, "Temperature: %1.1f,",temp_temperature);//Temperature
-                fprintf(fp, "Pressure: %1.1f,",temp_pressure);//Pressure
-                fprintf(fp, "Light: %5.3f,",temp_light);//Light
-                fprintf(fp, "End of Data \n");//End of data
+                fclose(fp);                                                                 //Close the file
+                SD_Write_Control(FileToOpen, "Append");                                     //Append the existing file
             }
-            
-            //Close File
-            fclose(fp);
-            if(Log_Value==5){pc.printf("Dumped data to SD Card\n");}
+            else                                                                            //If the file doesn't exist
+            {
+                SD_Write_Control(FileToOpen, "Write");                                      //Create a new file
+            }
+            if(Log_Value==5){pc.printf("Dumped data to SD Card\n");}                        //If logging is enabled, print debug statement
 
         }
 }