Plymouth ELEC351 Group T / Mbed OS ELEC351

Dependencies:   BME280 BMP280 TextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SD_CARD.cpp Source File

SD_CARD.cpp

00001 #include "SD_CARD.hpp"
00002 FILE* fp; 
00003 InterruptIn SD_CARD_DETECT(D0);
00004    
00005 
00006 bool SD_Write = 1;
00007 void SD_Init()//Initialised the SD CARD
00008 {
00009     
00010 }
00011 void SD_Card_Eject()
00012 {/*
00013     SD_Write = 0;
00014     if(Log_Value==5){pc.printf("Closing File Ejecting SD Card\n");}
00015     //fclose(fp);//Close File
00016     sd.deinit();//Close down
00017     LCD.Display_Clear();
00018     LCD.DDRAM_Address(0x00);
00019     LCD.Write_String("SD Card");
00020     LCD.DDRAM_Address(0x40);
00021     LCD.Write_String("Unmounted");
00022     LCD.DDRAM_Address(0x00);
00023     pc.printf("SD Card Unmounted\n");
00024     */
00025 }
00026 void SD_Card_Write()
00027 {
00028     if(SD_Write == 0)
00029     {
00030         if(Log_Value==5){pc.printf("SD Has Been Ejected Please do a Hardware Restart\n");}
00031         return;   
00032     }
00033     if(Write_Pointer == mailsize - 1)
00034         {
00035             if(Log_Value==5){pc.printf("In SD_Card Thread\n");}
00036             
00037             FATFileSystem fs("sd", &sd);
00038             
00039             
00040             time_t File_Time = Data_Buffer[Write_Pointer - 1].get_time();
00041             tm* File_Time_Pointer = localtime(&File_Time);
00042             int File_temp_day = File_Time_Pointer->tm_mday;
00043             int File_temp_month = (File_Time_Pointer->tm_mon+1);//Set to current month
00044             int File_temp_year = (File_Time_Pointer->tm_year+1900);//Set to current year
00045             
00046             char FileToOpen[50] = {};
00047             snprintf (FileToOpen, sizeof(char) * sizeof(FileToOpen), "/sd/%d_%02d_%02d.csv",File_temp_year,File_temp_month,File_temp_day);
00048             
00049             fp = fopen(FileToOpen,"wb");
00050             if (fp == NULL) 
00051             {
00052                 error("Could not open file for write in SD_Card\n");
00053                 LCD.Display_Clear();
00054                 LCD.Write_String("CANNOT OPEN FILE");
00055                 errorCode(FATAL);
00056             }                             //Open to WRITE
00057             
00058             if(Log_Value==5){pc.printf("File Opened\n");}  
00059                    
00060             for(int SD_Card_Data_Pointer = 0; SD_Card_Data_Pointer != mailsize; SD_Card_Data_Pointer++)
00061             {
00062                 if(Log_Value==5){pc.printf("Copying from address: %d\n", SD_Card_Data_Pointer);}
00063                 
00064                 //ofstream outfile ("Hello Tom.txt");
00065                 time_t Time = Data_Buffer[SD_Card_Data_Pointer].get_time();
00066                 tm* Time_Pointer = localtime(&Time);
00067                 int temp_day = Time_Pointer->tm_mday;
00068                 int temp_month = (Time_Pointer->tm_mon+1);//Set to current month
00069                 int temp_year = (Time_Pointer->tm_year+1900);//Set to current year
00070                     
00071                 int temp_hours = Time_Pointer->tm_hour; 
00072                 int temp_minute = Time_Pointer->tm_min;
00073                 int temp_seconds = Time_Pointer->tm_sec;
00074                     
00075                 float temp_temperature = Data_Buffer[SD_Card_Data_Pointer].get_temperature();
00076                 float temp_pressure = Data_Buffer[SD_Card_Data_Pointer].get_pressure();
00077                 float temp_light = Data_Buffer[SD_Card_Data_Pointer].get_light();
00078                 
00079                 
00080                 fprintf(fp, "Date: %02d/%02d/%d,",temp_day,temp_month,temp_year);//Date
00081                 fprintf(fp, "Time: %02d:%02d:%02d,",temp_hours,temp_minute, temp_seconds);//Time
00082                 fprintf(fp, "Temperature: %1.1f,",temp_temperature);//Temperature
00083                 fprintf(fp, "Pressure: %1.1f,",temp_pressure);//Pressure
00084                 fprintf(fp, "Light: %5.3f,",temp_light);//Light
00085                 fprintf(fp, "End of Data \n");//End of data
00086             }
00087             
00088             //Close File
00089             fclose(fp);
00090             if(Log_Value==5){pc.printf("Dumped data to SD Card\n");}
00091 
00092         }
00093 }
00094