Implement a SD card into HW6

Dependencies:   SDFileSystem mbed

Fork of shomberg_hw_6 by Russell Shomberg

Revision:
20:fc1690076f87
Parent:
19:1f4dd59bbe6b
--- a/main.cpp	Wed Oct 31 23:32:01 2018 +0000
+++ b/main.cpp	Thu Nov 01 18:03:25 2018 +0000
@@ -12,7 +12,7 @@
     @revised 2018-10-31
     @version 0.0
 
-    Issues:
+    Issues: SD Card Writing functions need to be in a seperate file
 
 */
 
@@ -36,9 +36,10 @@
 char file_name [20];
 Timer t;
 FILE *fp;
+unsigned char c;                          // a single byte buffer
 
 // Initialize SDFileSystem
-SDFileSystem sd(p12,p13,p14,p15,"sd");
+SDFileSystem sd(p5,p6,p7,p8,"sd");
 
 
 
@@ -48,10 +49,12 @@
     t.start();
 
     // Mount the sd and
-    printf("Mounting SD Card\n\r");
+    printf("\n\r\n\rMounting SD Card\n\r");
     sd.mount();
 
     while(1) {
+        while(!read_switch()) {}
+
         while(read_switch()) {  // skip everything if switch is off
 
             // Initialize a file
@@ -70,8 +73,9 @@
 
                 // start a loop that logging
                 // exiting this loop will initialize data save procedures
+                printf("Logging data....\n\r");
                 while(read_switch()) { // Main loop for logging
-                    printf("Logging data....\n\r");
+
                     // Get data for logging
                     current_time = t.read();
                     current_voltage = read_sensor();
@@ -81,28 +85,35 @@
 
                     wait(1);
                 }
+
+                // This code runs when the switch is toggled off
+                // Close out file
+                printf("Logging complete! \n\r Saving file \n\r");
+                fclose(fp);
+
+                // Print to USB serial
+                printf("Uploading data to USB \n\r");
+                //
+                fp = fopen(file_name,"r");
+                if (fp!=NULL) { // Don't procede if there is an issue
+                    while (!feof(fp)) {                       // while not end of file
+                        c=fgetc(fp);                         // get a character/byte from the file
+                        printf("%c",c); // and show it in hex format
+                    }
+                } else { // runs if there was a issue opening the file
+                    printf("Issue with opening file\n\r");
+                    fclose(fp);
+                }
+
+
             } else { // runs if there was a issue opening the file
                 printf("Issue with opening file\n\r");
             }
-
-            // This code runs when the switch is toggled off
-            // Close out file
-            printf("Logging complete! \n\r Saving file \n\r");
-            fclose(fp);
+        }
 
-            // Print to USB serial
-            printf("Uploading data to USB");
-            //
-            fp = fopen(file_name,"r");
-            if (fp!=NULL) { // Don't procede if there is an issue
-                printf("I don't know how to do this");
-            } else { // runs if there was a issue opening the file
-                printf("Issue with opening file\n\r");
-                fclose(fp);
-                file_counter++;
-            }
-        }
         // Re-enter main while loop
         //continuously check for switch to turn on again
+        file_counter++;
+        printf("waiting...");
     }
 }
\ No newline at end of file