AccellerationFileNV Assignment

Dependencies:   BSP_B-L475E-IOT01

Revision:
28:6afd8979a690
Parent:
27:0cf170538518
--- a/main.cpp	Thu Dec 06 20:28:08 2018 +0000
+++ b/main.cpp	Thu Dec 06 20:47:20 2018 +0000
@@ -21,6 +21,7 @@
 #include "LittleFileSystem.h"
 #include "FATFileSystem.h"
 #include "stm32l475e_iot01_accelero.h"
+#include "nvstore.h"
 
 // Physical block device, can be any device that supports the BlockDevice API
 /*SPIFBlockDevice bd(
@@ -76,6 +77,15 @@
 }
 
 static FILE *f;
+
+static uint16_t key_led1 = 1; 
+static uint16_t key_led2 = 2; 
+static uint16_t key_led3 = 3;
+
+static uint32_t value_led1;
+static uint32_t value_led2;
+static uint32_t value_led3;
+
 volatile int counter = 0;
 
 void read_acc() {
@@ -123,19 +133,26 @@
         }
       }
       
+      NVStore &nvstore = NVStore::get_instance();
       // check which position is the one present the most
       if (led1_counter > led2_counter && led1_counter > led3_counter) {
             led_1 = 1;
             led_2 = 0;
             led_3 = 0;
+            value_led1 += 1;
+            nvstore.set(key_led1, sizeof(value_led1), &value_led1);
         } else if (led2_counter > led1_counter && led2_counter > led3_counter ) {
             led_1 = 0;
             led_2 = 1;
             led_3 = 0;
+            value_led2 += 1;
+            nvstore.set(key_led2, sizeof(value_led2), &value_led2);
         } else if (led3_counter > led1_counter && led3_counter > led2_counter) {
             led_1 = 0;
             led_2 = 0;
             led_3 = 1;
+            value_led3 += 1;
+            nvstore.set(key_led3, sizeof(value_led3), &value_led3);
         }
       
       fflush(stdout);
@@ -177,6 +194,27 @@
     t.start(callback(&queue, &EventQueue::dispatch_forever));
     BSP_ACCELERO_Init();
     
+    NVStore &nvstore = NVStore::get_instance();
+    
+    uint16_t actual_len_bytes = 0;
+    int rc;
+    
+    rc = nvstore.init();
+    
+    rc = nvstore.get(key_led1, sizeof(value_led1), &value_led1, actual_len_bytes);
+    if (rc == NVSTORE_NOT_FOUND) {
+        value_led1 = 0;
+        value_led2 = 0;
+        value_led3 = 0;
+        nvstore.set(key_led1, sizeof(value_led1), &value_led1);
+        nvstore.set(key_led2, sizeof(value_led2), &value_led2);
+        nvstore.set(key_led3, sizeof(value_led3), &value_led3);
+    } else {
+        nvstore.get(key_led2, sizeof(value_led2), &value_led2, actual_len_bytes);
+        nvstore.get(key_led3, sizeof(value_led3), &value_led3, actual_len_bytes);    
+    }
+    
+    printf("\nLED1 has been on: %d times\nLED2 has been on: %d times\nLED3 has been on: %d times\n", value_led1, value_led2, value_led3);
     
     printf("--- Mbed OS filesystem example ---\n");