SD card functionality

Dependents:   ELEC350_Project2 SDcard

Revision:
2:9a5eea2adbf8
Parent:
1:c3af0c26ded2
Child:
3:c6e5dd2faa22
--- a/SDCard.cpp	Wed Nov 07 19:56:55 2018 +0000
+++ b/SDCard.cpp	Thu Nov 08 21:29:38 2018 +0000
@@ -3,21 +3,77 @@
 void SDcard(void)
 {
         
-        //puts("SD Card alive");
-        //Thread::wait(3000);
+        
+    //REQUIRES IMPROVEMENTS, SEE ONENOTE
         
-        //Initialisation stuff
-               
-        //printf("SD card queue error, unreachable code");
-        //SDQueue.dispatch();
+    //Initialise the SD card
+    if ( sd.init() != 0) {
+        printf("Init failed \n");
+        lcd.cls();
+        lcd.printf("CANNOT INIT SD");        
+        errorCode(FATAL);
+    } 
+    
+    //Create a filing system for SD Card
+    FATFileSystem fs("sd", &sd);     
+
+    //Open to WRITE
+    FILE* fp = fopen("/sd/test.csv","a");
+    if (fp == NULL) {
+        error("Could not open file for write\n");
+        lcd.cls();
+        lcd.printf("CANNOT OPEN FILE\n\n");
+        errorCode(FATAL);
+    }
+    
+    //Last message before sampling begins
+    lcd.cls();
+    lcd.printf("READY\n\n");
+    
+    //move?   
+    //Press either switch to unmount
+    while ((SW1 == 0) && (SW2 == 0)) {
+        
+        //Base loop delay
+        wait(1.0);
+        
+        //Read environmental sensors
+        double temp = sensor.getTemperature();
+        double pressure = sensor.getPressure();
+        
+        //Write new data to LCD (not fast!)
+        lcd.cls();
+        lcd.printf("Temp   Pressure\n"); 
+        lcd.printf("%6.1f ",temp);
+        lcd.printf("%.2f\n",pressure);
+        
+        //Write to SD (potentially slow)
+        //fprintf(fp, "%6.1f,%.2f\n\r", temp, pressure);
+    }
+    
+    //Close File
+    fclose(fp);
+    
+    //Close down
+    sd.deinit();
+    printf("Unmounted...\n");
+    lcd.cls();
+    lcd.printf("Unmounted...\n\n");
 }
 
 void SDread(int n)
 {
-    printf("Read %d files\n", n);
+    if (n == -1) puts("Received command to read all");
+    else printf("Received command to read %d\n", n);
+}
+
+void SDdelete(int n)
+{
+    if (n == -1) puts("Received command to delete all");
+    else printf("Received command to delete %d\n", n);   
 }
 
 void SDalive(void)
 {
-    puts("Im alive\n");   
+    //puts("SD THREAD ALIVE\n");   
 }
\ No newline at end of file