Solution

Dependencies:   ELEC350-Practicals-FZ429

Fork of Task680-mbed-os-FZ429ZI by University of Plymouth - Stages 1, 2 and 3

Revision:
2:fad34c30dcc4
Parent:
0:1ce5a958aaf8
Child:
3:13716f14b257
--- a/main.cpp	Fri Oct 20 11:30:34 2017 +0000
+++ b/main.cpp	Wed Nov 22 15:18:28 2017 +0000
@@ -3,39 +3,79 @@
  #include "mbed.h"
  #include "SDBlockDevice.h"
  #include "FATFileSystem.h"
+ #include "sample_hardware.hpp"
  
+ //SD Card Object
  SDBlockDevice sd(D11, D12, D13, D10); // mosi, miso, sclk, cs
 
  uint8_t block[512] = "Hello World!\n";
  int main()
 {
-    printf("Initialise and write to a file\n");
+    //POWER ON SELF TEST
+    post();
+    
+    printf("Initialise\n");
     //FileSystemLike(*sd);
 
-
     // call the SDBlockDevice instance initialisation method.
-    if ( 0 != sd.init()) {
+    if ( sd.init() != 0) {
         printf("Init failed \n");
-        return -1;
-    }
-    //FileSystemLike(*sd);
+        errorCode(FATAL);
+    }    
     
+    //Create a filing system for SD Card
     FATFileSystem fs("sd", &sd);
     
-    
-    FILE *fp = fopen("/sd/test.txt","w");
-        if(fp == NULL) {
+    // *************
+    // Open to WRITE
+    // *************
+    printf("Write to a file\n");
+    FILE* fp = fopen("/sd/test.txt","w");
+    //Check file handle (stream)
+    if (fp == NULL) {
         error("Could not open file for write\n");
+        errorCode(FATAL);
     }
     
     //Put some text in the file...
-    fprintf(fp, "Martin Says Hi! One for the good guys\n");
+    fprintf(fp, "Welcome to ELEC350\n");
+    
+    //Close the file
+    fclose(fp);
     
-    //Tidy up here
+    // ************
+    // Open to READ
+    // ************
+    printf("Read a file\n");
+    fp = fopen("/sd/test.txt","r");
+    if (fp == NULL) {
+        error("Could not open file for read\n");
+        errorCode(FATAL);
+    }   
+    
+    //Read in three strings
+    char s1[64], s2[64], s3[64];
+    fscanf(fp, "%s %s %s", s1,s2,s3);
+    //To read a whole line, use: fgets(s1, sizeof(s1), fp);
+    printf("READ BACK: %s %s %s\n", s1, s2, s3);
+    
+    //Close File
     fclose(fp);
+    
+    //Close down
     sd.deinit();
     printf("All done...\n");
+    errorCode(OK);
     
+    //Flash to indicate goodness
+    while(true) {
+        greenLED = 1;
+        wait(0.5);
+        greenLED = 0;
+        wait(0.1);    
+    }
+}
+
    /* 
     printf("sd size: %llu\n",         sd.size());
     printf("sd read size: %llu\n",    sd.get_read_size());
@@ -63,5 +103,4 @@
     // call the SDBlockDevice instance de-initialisation method.
     
     sd.deinit();
-    */
-}
\ No newline at end of file
+    */
\ No newline at end of file