Solution to Task 690

Dependencies:   BMP280 ELEC350-Practicals-FZ429 BME280

Fork of Task690solution-mbed-os-FZ429ZI_ by University of Plymouth - Stages 1, 2 and 3

Revision:
4:17fe5b63cd8a
Parent:
3:22e5460a8b42
Child:
5:4959019b2904
--- a/main.cpp	Thu Nov 23 10:54:42 2017 +0000
+++ b/main.cpp	Thu Nov 23 14:01:42 2017 +0000
@@ -4,11 +4,57 @@
  #include "SDBlockDevice.h"
  #include "FATFileSystem.h"
  #include "sample_hardware.hpp"
+ #include "mbed_events.h"
  
  //SD Card Object
- SDBlockDevice sd(D11, D12, D13, D10); // mosi, miso, sclk, cs
+SDBlockDevice sd(D11, D12, D13, D10); // mosi, miso, sclk, cs
+
+//Event queue for main
+EventQueue queue;
+
+//File pointer for the SD card
+FILE* fp;
+
+//Flag to indicate if the user has ejected the SD card
+bool ejected = false;
 
- uint8_t block[512] = "Hello World!\n";
+//This is called every 5 seconds by the main event queue (main thread) 
+void takeSample()
+{
+    //If the card is unmounted, just return
+    if (ejected == true) {
+        return;    
+    }
+    
+    //Read Sensors (I2C)
+    float temp     = sensor.getTemperature();
+    float pressure = sensor.getPressure();
+    
+    //Write to SD Card
+    fprintf(fp, "%f,%f\n", temp, pressure);
+    
+    //Flash to indicate the sampling is alive
+    redLED = 1;
+    Thread::wait(100);
+    redLED = 0;
+    
+    //Chheck switches - press both to eject
+    if ((SW1 == 1) && (SW2 == 1)) {
+        //Close File
+        fclose(fp);
+        
+        //Close down SD card (flush buffers)
+        sd.deinit();
+        
+        //Let user know     
+        puts("You can now remove the SD Card\n");
+        yellowLED = 1;
+        
+        //Flag to record the event
+        ejected = true;
+    }
+}
+ 
  int main()
 {
     //POWER ON SELF TEST
@@ -26,83 +72,21 @@
     //Create a filing system for SD Card
     FATFileSystem fs("sd", &sd);
     
-    // *************
-    // Open to WRITE
-    // *************
-    printf("Write to a file\n");
-    FILE* fp = fopen("/sd/test.txt","a");
+    // **************
+    // Open to APPEND
+    // **************
+    fp = fopen("/sd/test.txt","a");
+    
     //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, "Welcome to ELEC350\n");
-    
-    //Close the file
-    fclose(fp);
-    
-    // ************
-    // 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 back all strings
-    char s1[64];
-    while (fscanf(fp, "%s", s1) == 1) {
-        printf("READ BACK: %s\n", s1);
-    }
-    //To read a whole line, use: fgets(s1, sizeof(s1), fp);
-    
-    
-    //Close File
-    fclose(fp);
-    
-    //Close down
-    sd.deinit();
-    printf("All done...\n");
-    errorCode(OK);
+    //Set up tasks on the main thread
+    queue.call_every(5000, takeSample);
     
-    //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());
-    printf("sd program size: %llu\n", sd.get_program_size());
-    printf("sd erase size: %llu\n",   sd.get_erase_size());
+    //Main queue event loop
+    queue.dispatch();
 
-    // set the frequency
-    if ( 0 != sd.frequency(5000000)) {
-        printf("Error setting frequency \n");
-    }
-
-    if ( 0 != sd.erase(0, sd.get_erase_size())) {
-        printf("Error Erasing block \n");
-    }
-
-    // Write some the data block to the device
-    if ( 0 == sd.program(block, 0, 512)) {
-        // read the data block from the device
-        if ( 0 == sd.read(block, 0, 512)) {
-            // print the contents of the block
-            printf("%s", block);
-        }
-    }
-
-    // call the SDBlockDevice instance de-initialisation method.
-    
-    sd.deinit();
-    */
\ No newline at end of file
+}