Time: 17:33 Date: 10/12/2017 Description: Task 1,7,8 Currently Functioning
Dependencies: BME280 BMP280 TextLCD
Working Repository
SD_CARD.cpp@50:3d61ca637399, 2018-01-09 (annotated)
- Committer:
- thomasmorris
- Date:
- Tue Jan 09 12:19:12 2018 +0000
- Revision:
- 50:3d61ca637399
- Parent:
- 49:d51f96a46cc3
Dynamic Dating Working
Who changed what in which revision?
User | Revision | Line number | New 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 | 49:d51f96a46cc3 | 4 | |
thomasmorris | 49:d51f96a46cc3 | 5 | |
thomasmorris | 48:244d6d81bb52 | 6 | bool SD_Write = 1; |
thomasmorris | 47:6d128e500875 | 7 | void SD_Init()//Initialised the SD CARD |
thomasmorris | 47:6d128e500875 | 8 | { |
thomasmorris | 47:6d128e500875 | 9 | |
thomasmorris | 47:6d128e500875 | 10 | } |
thomasmorris | 48:244d6d81bb52 | 11 | void SD_Card_Eject() |
thomasmorris | 49:d51f96a46cc3 | 12 | {/* |
thomasmorris | 48:244d6d81bb52 | 13 | SD_Write = 0; |
thomasmorris | 48:244d6d81bb52 | 14 | if(Log_Value==5){pc.printf("Closing File Ejecting SD Card\n");} |
thomasmorris | 49:d51f96a46cc3 | 15 | //fclose(fp);//Close File |
thomasmorris | 48:244d6d81bb52 | 16 | sd.deinit();//Close down |
thomasmorris | 48:244d6d81bb52 | 17 | LCD.Display_Clear(); |
thomasmorris | 48:244d6d81bb52 | 18 | LCD.DDRAM_Address(0x00); |
thomasmorris | 48:244d6d81bb52 | 19 | LCD.Write_String("SD Card"); |
thomasmorris | 48:244d6d81bb52 | 20 | LCD.DDRAM_Address(0x40); |
thomasmorris | 48:244d6d81bb52 | 21 | LCD.Write_String("Unmounted"); |
thomasmorris | 48:244d6d81bb52 | 22 | LCD.DDRAM_Address(0x00); |
thomasmorris | 48:244d6d81bb52 | 23 | pc.printf("SD Card Unmounted\n"); |
thomasmorris | 49:d51f96a46cc3 | 24 | */ |
thomasmorris | 48:244d6d81bb52 | 25 | } |
thomasmorris | 47:6d128e500875 | 26 | void SD_Card_Write() |
thomasmorris | 47:6d128e500875 | 27 | { |
thomasmorris | 48:244d6d81bb52 | 28 | if(SD_Write == 0) |
thomasmorris | 48:244d6d81bb52 | 29 | { |
thomasmorris | 48:244d6d81bb52 | 30 | if(Log_Value==5){pc.printf("SD Has Been Ejected Please do a Hardware Restart\n");} |
thomasmorris | 48:244d6d81bb52 | 31 | return; |
thomasmorris | 48:244d6d81bb52 | 32 | } |
thomasmorris | 47:6d128e500875 | 33 | if(Write_Pointer == mailsize - 1) |
thomasmorris | 47:6d128e500875 | 34 | { |
thomasmorris | 48:244d6d81bb52 | 35 | if(Log_Value==5){pc.printf("In SD_Card Thread\n");} |
thomasmorris | 50:3d61ca637399 | 36 | |
thomasmorris | 50:3d61ca637399 | 37 | FATFileSystem fs("sd", &sd); |
thomasmorris | 50:3d61ca637399 | 38 | |
thomasmorris | 47:6d128e500875 | 39 | |
thomasmorris | 50:3d61ca637399 | 40 | time_t File_Time = Data_Buffer[Write_Pointer - 1].get_time(); |
thomasmorris | 50:3d61ca637399 | 41 | tm* File_Time_Pointer = localtime(&File_Time); |
thomasmorris | 50:3d61ca637399 | 42 | int File_temp_day = File_Time_Pointer->tm_mday; |
thomasmorris | 50:3d61ca637399 | 43 | int File_temp_month = (File_Time_Pointer->tm_mon+1);//Set to current month |
thomasmorris | 50:3d61ca637399 | 44 | int File_temp_year = (File_Time_Pointer->tm_year+1900);//Set to current year |
thomasmorris | 50:3d61ca637399 | 45 | |
thomasmorris | 50:3d61ca637399 | 46 | char FileToOpen[50] = {}; |
thomasmorris | 50:3d61ca637399 | 47 | snprintf (FileToOpen, sizeof(char) * sizeof(FileToOpen), "/sd/%d_%02d_%02d.csv",File_temp_year,File_temp_month,File_temp_day); |
thomasmorris | 50:3d61ca637399 | 48 | |
thomasmorris | 50:3d61ca637399 | 49 | fp = fopen(FileToOpen,"wb"); |
thomasmorris | 49:d51f96a46cc3 | 50 | if (fp == NULL) |
thomasmorris | 49:d51f96a46cc3 | 51 | { |
thomasmorris | 50:3d61ca637399 | 52 | error("Could not open file for write in SD_Card\n"); |
thomasmorris | 50:3d61ca637399 | 53 | LCD.Display_Clear(); |
thomasmorris | 50:3d61ca637399 | 54 | LCD.Write_String("CANNOT OPEN FILE"); |
thomasmorris | 50:3d61ca637399 | 55 | errorCode(FATAL); |
thomasmorris | 50:3d61ca637399 | 56 | } //Open to WRITE |
thomasmorris | 50:3d61ca637399 | 57 | |
thomasmorris | 48:244d6d81bb52 | 58 | if(Log_Value==5){pc.printf("File Opened\n");} |
thomasmorris | 47:6d128e500875 | 59 | |
thomasmorris | 47:6d128e500875 | 60 | for(int SD_Card_Data_Pointer = 0; SD_Card_Data_Pointer != mailsize; SD_Card_Data_Pointer++) |
thomasmorris | 47:6d128e500875 | 61 | { |
thomasmorris | 49:d51f96a46cc3 | 62 | if(Log_Value==5){pc.printf("Copying from address: %d\n", SD_Card_Data_Pointer);} |
thomasmorris | 49:d51f96a46cc3 | 63 | |
thomasmorris | 49:d51f96a46cc3 | 64 | //ofstream outfile ("Hello Tom.txt"); |
thomasmorris | 47:6d128e500875 | 65 | time_t Time = Data_Buffer[SD_Card_Data_Pointer].get_time(); |
thomasmorris | 47:6d128e500875 | 66 | tm* Time_Pointer = localtime(&Time); |
thomasmorris | 47:6d128e500875 | 67 | int temp_day = Time_Pointer->tm_mday; |
thomasmorris | 47:6d128e500875 | 68 | int temp_month = (Time_Pointer->tm_mon+1);//Set to current month |
thomasmorris | 47:6d128e500875 | 69 | int temp_year = (Time_Pointer->tm_year+1900);//Set to current year |
thomasmorris | 47:6d128e500875 | 70 | |
thomasmorris | 49:d51f96a46cc3 | 71 | int temp_hours = Time_Pointer->tm_hour; |
thomasmorris | 47:6d128e500875 | 72 | int temp_minute = Time_Pointer->tm_min; |
thomasmorris | 47:6d128e500875 | 73 | int temp_seconds = Time_Pointer->tm_sec; |
thomasmorris | 47:6d128e500875 | 74 | |
thomasmorris | 47:6d128e500875 | 75 | float temp_temperature = Data_Buffer[SD_Card_Data_Pointer].get_temperature(); |
thomasmorris | 47:6d128e500875 | 76 | float temp_pressure = Data_Buffer[SD_Card_Data_Pointer].get_pressure(); |
thomasmorris | 47:6d128e500875 | 77 | float temp_light = Data_Buffer[SD_Card_Data_Pointer].get_light(); |
thomasmorris | 47:6d128e500875 | 78 | |
thomasmorris | 50:3d61ca637399 | 79 | |
thomasmorris | 47:6d128e500875 | 80 | fprintf(fp, "Date: %02d/%02d/%d,",temp_day,temp_month,temp_year);//Date |
thomasmorris | 47:6d128e500875 | 81 | fprintf(fp, "Time: %02d:%02d:%02d,",temp_hours,temp_minute, temp_seconds);//Time |
thomasmorris | 47:6d128e500875 | 82 | fprintf(fp, "Temperature: %1.1f,",temp_temperature);//Temperature |
thomasmorris | 47:6d128e500875 | 83 | fprintf(fp, "Pressure: %1.1f,",temp_pressure);//Pressure |
thomasmorris | 47:6d128e500875 | 84 | fprintf(fp, "Light: %5.3f,",temp_light);//Light |
thomasmorris | 47:6d128e500875 | 85 | fprintf(fp, "End of Data \n");//End of data |
thomasmorris | 47:6d128e500875 | 86 | } |
thomasmorris | 50:3d61ca637399 | 87 | |
thomasmorris | 47:6d128e500875 | 88 | //Close File |
thomasmorris | 47:6d128e500875 | 89 | fclose(fp); |
thomasmorris | 50:3d61ca637399 | 90 | if(Log_Value==5){pc.printf("Dumped data to SD Card\n");} |
thomasmorris | 50:3d61ca637399 | 91 | |
thomasmorris | 47:6d128e500875 | 92 | } |
thomasmorris | 47:6d128e500875 | 93 | } |
thomasmorris | 49:d51f96a46cc3 | 94 |