assignment

Dependencies:   BSP_B-L475E-IOT01

Files at this revision

API Documentation at this revision

Comitter:
alessioburatti
Date:
Thu Dec 06 17:58:45 2018 +0000
Parent:
1:14aa85a5009a
Commit message:
done

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Dec 06 17:48:24 2018 +0000
+++ b/main.cpp	Thu Dec 06 17:58:45 2018 +0000
@@ -1,10 +1,9 @@
 
 #include "mbed.h"
-
-#include "stm32l475e_iot01_tsensor.h"
-
 #include <stdio.h>
 #include <errno.h>
+#include "stm32l475e_iot01_tsensor.h"
+#include "nvstore.h"
 
     // Block devices
 #if COMPONENT_SPIF
@@ -20,11 +19,15 @@
 #endif
 
 #include "HeapBlockDevice.h"
-
     // File systems
 #include "LittleFileSystem.h"
 #include "FATFileSystem.h"
 
+EventQueue queue(32 * EVENTS_EVENT_SIZE);
+Thread t;
+Ticker tTicker;
+
+
     // Physical block device, can be any device that supports the BlockDevice API
 /*SPIFBlockDevice bd(
  MBED_CONF_SPIF_DRIVER_SPI_MOSI,
@@ -32,53 +35,55 @@
  MBED_CONF_SPIF_DRIVER_SPI_CLK,
  MBED_CONF_SPIF_DRIVER_SPI_CS);*/
 
-
 #define BLOCK_SIZE 512
 HeapBlockDevice bd(16384, BLOCK_SIZE);
 
     // File system declaration
 LittleFileSystem fs("fs");
 
-Ticker tTicker;
-Thread t;
+InterruptIn button(USER_BUTTON);
+
 
 static FILE *f;
 volatile int counter = 0;
-EventQueue queue(32 * EVENTS_EVENT_SIZE);
 
-InterruptIn button(USER_BUTTON);
+void saveTemperature() {
+    float temperature = 0;
+    temperature = BSP_TSENSOR_ReadTemp();
+    fprintf(f, "%f\n", temperature);
+    fflush(f);
+    fflush(stdout);
+}
 
 void printTemperatures() {
     fflush(stdout);
     fflush(f);
     fseek(f, 0, SEEK_SET);
     float temperature;
-    while (!feof(f)) {
+    while(!feof(f)) {
         fscanf(f, "%f", &temperature);
         printf("Temperature: %f\n", temperature);
     }
-    fflush(f);
-}
-
-void getTemperature() {
-    float temperature = BSP_TSENSOR_ReadTemp();
-    fprintf(f, "%f\n", temperature);
-    fflush(f);
     fflush(stdout);
 }
 
-void getTemperatureQueue() {
-    printTemperatures();
+
+void getTempQueueCall() {
+    queue.call(printTemperatures);
+}
+void tickerBlock() {
+    queue.call(saveTemperature);
 }
 
-void tickerBlock() {
-    queue.call(getTemperature);
-}
-
-int main(){
+    // Entry point for the example
+int main() {
     t.start(callback(&queue, &EventQueue::dispatch_forever));
     BSP_TSENSOR_Init();
-    button.rise(&getTemperatureQueue);
+    
+    button.rise(&getTempQueueCall);
+    
+    printf("--- Mbed OS filesystem example ---\n");
+    
         // Try to mount the filesystem
     printf("Mounting the filesystem... ");
     fflush(stdout);
@@ -110,7 +115,16 @@
         if (!f) {
             error("error: %s (%d)\n", strerror(errno), -errno);
         }
+        
+        printf("Seeking file... ");
+        fflush(stdout);
+        err = fseek(f, 0, SEEK_SET);
+        printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
+        if (err < 0) {
+            error("error: %s (%d)\n", strerror(errno), -errno);
+        }
     }
     
     tTicker.attach(&tickerBlock, 60);
+    
 }