Plymouth ELEC351 Group T / Mbed OS ELEC351_Group_T

Fork of ELEC351 by Plymouth ELEC351 Group T

Committer:
thomasmorris
Date:
Tue Jan 09 00:37:01 2018 +0000
Revision:
48:244d6d81bb52
Parent:
47:6d128e500875
Child:
49:d51f96a46cc3
HOLY SHIT IT WORKS

Who changed what in which revision?

UserRevisionLine numberNew contents of line
thomasmorris 47:6d128e500875 1 #include "SD_CARD.hpp"
thomasmorris 48:244d6d81bb52 2 FILE* fp;
thomasmorris 48:244d6d81bb52 3 InterruptIn SD_CARD_DETECT(D0);
thomasmorris 48:244d6d81bb52 4
thomasmorris 48:244d6d81bb52 5 bool SD_Write = 1;
thomasmorris 47:6d128e500875 6 void SD_Init()//Initialised the SD CARD
thomasmorris 47:6d128e500875 7 {
thomasmorris 47:6d128e500875 8
thomasmorris 47:6d128e500875 9 }
thomasmorris 48:244d6d81bb52 10 void SD_Card_Eject()
thomasmorris 48:244d6d81bb52 11 {
thomasmorris 48:244d6d81bb52 12 SD_Write = 0;
thomasmorris 48:244d6d81bb52 13 if(Log_Value==5){pc.printf("Closing File Ejecting SD Card\n");}
thomasmorris 48:244d6d81bb52 14 fclose(fp);//Close File
thomasmorris 48:244d6d81bb52 15 sd.deinit();//Close down
thomasmorris 48:244d6d81bb52 16 LCD.Display_Clear();
thomasmorris 48:244d6d81bb52 17 LCD.DDRAM_Address(0x00);
thomasmorris 48:244d6d81bb52 18 LCD.Write_String("SD Card");
thomasmorris 48:244d6d81bb52 19 LCD.DDRAM_Address(0x40);
thomasmorris 48:244d6d81bb52 20 LCD.Write_String("Unmounted");
thomasmorris 48:244d6d81bb52 21 LCD.DDRAM_Address(0x00);
thomasmorris 48:244d6d81bb52 22 pc.printf("SD Card Unmounted\n");
thomasmorris 48:244d6d81bb52 23 }
thomasmorris 47:6d128e500875 24 void SD_Card_Write()
thomasmorris 47:6d128e500875 25 {
thomasmorris 48:244d6d81bb52 26 if(SD_Write == 0)
thomasmorris 48:244d6d81bb52 27 {
thomasmorris 48:244d6d81bb52 28 if(Log_Value==5){pc.printf("SD Has Been Ejected Please do a Hardware Restart\n");}
thomasmorris 48:244d6d81bb52 29 return;
thomasmorris 48:244d6d81bb52 30 }
thomasmorris 47:6d128e500875 31 if(Write_Pointer == mailsize - 1)
thomasmorris 47:6d128e500875 32 {
thomasmorris 48:244d6d81bb52 33 if(Log_Value==5){pc.printf("In SD_Card Thread\n");}
thomasmorris 47:6d128e500875 34
thomasmorris 47:6d128e500875 35 //Open to WRITE
thomasmorris 47:6d128e500875 36 FILE* fp = fopen("/sd/test.csv","a");
thomasmorris 48:244d6d81bb52 37 if(Log_Value==5){pc.printf("File Opened\n");}
thomasmorris 47:6d128e500875 38
thomasmorris 47:6d128e500875 39 for(int SD_Card_Data_Pointer = 0; SD_Card_Data_Pointer != mailsize; SD_Card_Data_Pointer++)
thomasmorris 47:6d128e500875 40 {
thomasmorris 47:6d128e500875 41 time_t Time = Data_Buffer[SD_Card_Data_Pointer].get_time();
thomasmorris 47:6d128e500875 42 tm* Time_Pointer = localtime(&Time);
thomasmorris 47:6d128e500875 43 int temp_day = Time_Pointer->tm_mday;
thomasmorris 47:6d128e500875 44 int temp_month = (Time_Pointer->tm_mon+1);//Set to current month
thomasmorris 47:6d128e500875 45 int temp_year = (Time_Pointer->tm_year+1900);//Set to current year
thomasmorris 47:6d128e500875 46
thomasmorris 47:6d128e500875 47 int temp_hours = Time_Pointer->tm_hour;
thomasmorris 47:6d128e500875 48 int temp_minute = Time_Pointer->tm_min;
thomasmorris 47:6d128e500875 49 int temp_seconds = Time_Pointer->tm_sec;
thomasmorris 47:6d128e500875 50
thomasmorris 47:6d128e500875 51 float temp_temperature = Data_Buffer[SD_Card_Data_Pointer].get_temperature();
thomasmorris 47:6d128e500875 52 float temp_pressure = Data_Buffer[SD_Card_Data_Pointer].get_pressure();
thomasmorris 47:6d128e500875 53 float temp_light = Data_Buffer[SD_Card_Data_Pointer].get_light();
thomasmorris 47:6d128e500875 54
thomasmorris 47:6d128e500875 55 fprintf(fp, "Date: %02d/%02d/%d,",temp_day,temp_month,temp_year);//Date
thomasmorris 47:6d128e500875 56 fprintf(fp, "Time: %02d:%02d:%02d,",temp_hours,temp_minute, temp_seconds);//Time
thomasmorris 47:6d128e500875 57 fprintf(fp, "Temperature: %1.1f,",temp_temperature);//Temperature
thomasmorris 47:6d128e500875 58 fprintf(fp, "Pressure: %1.1f,",temp_pressure);//Pressure
thomasmorris 47:6d128e500875 59 fprintf(fp, "Light: %5.3f,",temp_light);//Light
thomasmorris 47:6d128e500875 60 fprintf(fp, "End of Data \n");//End of data
thomasmorris 47:6d128e500875 61 }
thomasmorris 48:244d6d81bb52 62 if(Log_Value==5){pc.printf("Dumped data to SD Card\n");}
thomasmorris 47:6d128e500875 63 //Close File
thomasmorris 47:6d128e500875 64 fclose(fp);
thomasmorris 47:6d128e500875 65 }
thomasmorris 47:6d128e500875 66 }