fuck this
Dependencies: BMP280
SDCard.cpp
00001 #include "mbed.h" 00002 #include "Sampling.h" 00003 #include "SDCard.h" 00004 #include "SDBlockDevice.h" 00005 #include "FATFileSystem.h" 00006 #include "LCD.h" 00007 00008 00009 //Hardware 00010 DigitalOut SDCardStatusLED(LED3); 00011 00012 //Variables 00013 bool SDCardPresent; 00014 bool SDCardMounted; 00015 unsigned short SDinternalIndex; 00016 00017 FILE* fp; //File pointer type 00018 SDCardStates SDCurrentState; //Create state machine state 00019 00020 Timeout antiBounce; 00021 Thread SDCardThread; 00022 FATFileSystem fs; 00023 00024 00025 void SDCardInit(void) 00026 { 00027 SDCardThread.start(&SDCardCode); //Start thread running 00028 SD_WP.rise(&SDCardRemovedISR); //SD removed 00029 SD_WP.fall(&SDCardInsertedISR); //SD inserted 00030 UserButton.fall(&SDCardButtonISR); //Button released 00031 00032 if (SD_WP == 1) { //Checks if card is inserted on power up and sets states 00033 SDCardPresent = false; 00034 SDCurrentState = REMOVED; 00035 SDCardMounted = false; 00036 } else { 00037 SDCardPresent = true; 00038 SDCurrentState = INSERTED; 00039 SDCardMounted = false; 00040 } 00041 } 00042 00043 00044 //-----------------------------------------------------------------// 00045 //ISRs 00046 void SDCardRemovedISR(void) 00047 { 00048 SDCardPresent = false; 00049 SDCurrentState = REMOVED; 00050 antiBounce.attach(&antiBounceISR,1); 00051 } 00052 00053 void SDCardInsertedISR(void) 00054 { 00055 SDCardPresent = true; 00056 SDCurrentState = INSERTED; 00057 antiBounce.attach(&antiBounceISR,1); 00058 00059 } 00060 00061 void SDCardButtonISR(void) 00062 { 00063 SDCurrentState = DISMOUNTREQUEST; 00064 antiBounce.attach(&antiBounceISR,1); 00065 } 00066 //-----------------------------------------------------------------// 00067 00068 void antiBounceISR(void) 00069 { 00070 SDCardThread.signal_set(1); 00071 } 00072 00073 00074 void SDCardCode(void) 00075 { 00076 while(true) { 00077 Thread::signal_wait(1); //Wait untill signal set 00078 00079 if (SDCurrentState == REMOVED) { 00080 00081 if (SDCardMounted) { 00082 //print error message file currupt 00083 lcd.SendMessage("SD CARD REMOVED! FILE CORRUPT",true); 00084 SDCardMounted = 0; 00085 } 00086 LogEvent(Log_SDRemoved); 00087 SDCardStatusLED = 0; //Set LEDs 00088 GreenLED = 0; 00089 00090 00091 00092 } else if (SDCurrentState == INSERTED) { 00093 LogEvent(Log_SDInserted); 00094 SDCardStatusLED = 0; //Set LEDs 00095 GreenLED = 0; 00096 00097 if ( sd.init() != 0) { //Initalises the SDCard and checks if it succeeded 00098 //SDInit failed. Reinsert the SDCard 00099 lcd.SendMessage("REINSERT SDCARD",true); 00100 LogEvent(Log_SDInitFail); 00101 00102 } else { 00103 00104 //Create a filing system for SD Card 00105 FATFileSystem fs("sd", &sd); 00106 00107 //Open to WRITE 00108 fp = fopen("/sd/samples.csv","w"); 00109 00110 00111 if (fp == NULL) { 00112 //error 00113 lcd.SendMessage("CANNOT OPEN FILE/sd/samples.csv",true); 00114 LogEvent(Log_SDFileOpenFail); 00115 00116 } else { 00117 SDCardMounted = true; //SD card is now mounted 00118 00119 Sampling(false); //Stop sampling 00120 SDinternalIndex = currentIndex; //SDinternalIndex for incrementing out data 00121 TakeKeys(true); //Take keys 00122 00123 fprintf(fp,"Date,Time,Temperature,Pressure,Light\n"); 00124 for (short i = 0; i < BUFFERSIZE; i++) { //For loop of length buffersize 00125 tm T = ReturnDateTimeStruct(timeReadings[SDinternalIndex]); 00126 //SDinternalIndex was set as newest. We will now decrement to display oldest to newest 00127 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]); 00128 SDinternalIndex = IndexDecrement(SDinternalIndex); //Decrement internal index 00129 } 00130 } 00131 TakeKeys(false); //Return keys 00132 Sampling(true); //Start sampling 00133 00134 00135 fclose(fp); //Close file 00136 SDCardStatusLED = 1;//Set light to indicate SD card file transfer complete. 00137 } 00138 00139 } else if (SDCurrentState == DISMOUNTREQUEST) { 00140 00141 if (SDCardMounted) { 00142 sd.deinit(); //De initalise SD card 00143 SDCardMounted = false; //Card now not mounted 00144 lcd.SendMessage("REMOVE SD CARD",true); //SD card can be removed 00145 GreenLED = 1; //Indication LED 00146 LogEvent(Log_SDDeInit); 00147 } else { 00148 lcd.SendMessage("NO SD CARD TO REMOVE",true); 00149 LogEvent(Log_SDDismountFail); 00150 } 00151 00152 } 00153 } 00154 } 00155 00156 00157
Generated on Tue Jul 26 2022 07:29:23 by 1.7.2