Lucas Pennati / Mbed OS AccelerationFileNV

Dependencies:   BSP_B-L475E-IOT01

Revision:
1:0b0ad71808ed
Parent:
0:80a623acba5a
--- a/main.cpp	Thu Dec 06 15:33:42 2018 +0000
+++ b/main.cpp	Fri Dec 07 12:12:07 2018 +0000
@@ -78,30 +78,31 @@
 volatile int counter = 0;
 
 int rc; 
+
 uint16_t keyX = 1; 
-uint32_t valueX;
 uint16_t keyY = 2; 
-uint32_t valueY;
 uint16_t keyZ = 3; 
-uint32_t valueZ;
 
-int maximum( int a, int b, int c ) {
-   int max = ( a < b ) ? b : a;
-   return ( ( max < c ) ? c : max );
-}
-
-void readSensors() {
+uint32_t valueX;
+uint32_t valueX;
+uint32_t valueZ;
+void getDataFromSensors() {
     int16_t pDataXYZ[3] = {0};
     BSP_ACCELERO_AccGetXYZ(pDataXYZ);
-    if(pDataXYZ[2] > 900 || pDataXYZ[2] < -900) { //Horizontal State
-            fprintf(file, "%d\n", 1);
-        } else if(pDataXYZ[1] > 900 || pDataXYZ[1] < -900) { //Long Edge State
-            fprintf(file, "%d\n", 2);
-        } else if(pDataXYZ[0] > 900 || pDataXYZ[0] < -900) { // Short Edge State
-            fprintf(file, "%d\n", 3);
-        } else { // all other positions
-            fprintf(file, "%d\n", 4);
-        }
+    
+    bool isHorizontal = pDataXYZ[2] > 900 || pDataXYZ[2] < -900;
+    bool isOnLongEdge = pDataXYZ[1] > 900 || pDataXYZ[1] < -900;
+    bool isOnShortEdge = pDataXYZ[0] > 900 || pDataXYZ[0] < -900;
+    
+    if(isHorizontal) {
+        fprintf(file, "%d\n", 1);
+    } else if(isOnLongEdge) {
+        fprintf(file, "%d\n", 2);
+    } else if(isOnShortEdge) {
+        fprintf(file, "%d\n", 3);
+    } else {
+        fprintf(file, "%d\n", 4);
+    }
     fflush(file);
     fflush(stdout);    
 }
@@ -127,23 +128,26 @@
             countLed3 += 1;    
         }
     }
-    int maxLed = maximum(countLed1, countLed2, countLed3);
-    printf("MAX LED = %d\n", maxLed);
+    
+    bool isLed1 = led1 >= led2 && led1 >= led3;
+    bool isLed2 = led2 >= led1 && led2 >= led3;
+    bool isLed3 = led3 >= led1 && led3 >= led2;
     
     NVStore &nvstore = NVStore::get_instance();
-    if(maxLed == countLed1) {
+
+    if(isLed1) {
         led1 = 1;
         led2 = 0;
         led3 = 0;
         valueX += 1;
         nvstore.set(keyX, sizeof(valueX), &valueX);
-    } else if (maxLed == countLed2) {
+    } else if (isLed2) {
         led1 = 0;
         led2 = 1;
         led3 = 0;
         valueY += 1;
         nvstore.set(keyY, sizeof(valueY), &valueY);
-    } else if (maxLed == countLed3) {
+    } else if (isLed3) {
         led1 = 0;
         led2 = 0;
         led3 = 1;
@@ -151,6 +155,7 @@
         nvstore.set(keyZ, sizeof(valueZ), &valueZ);
     }
     
+    
     fflush(stdout);
     int err = fclose(file);
     printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
@@ -166,7 +171,7 @@
     printf("Mbed OS filesystem example done!\n"); 
 }
 
-void turnOnLed() {
+void toggleLed() {
     queue.call(readSensors);
     counter += 1;
     if(counter == 1000) {
@@ -184,7 +189,7 @@
     NVStore &nvstore = NVStore::get_instance();
     rc = nvstore.init();
     uint16_t actual_len_bytes = 0;
-    rc = nvstore.get(keyZ, sizeof(valueZ), &valueZ, actual_len_bytes);
+    rc = nvstore.get(keyX, sizeof(valueX), &valueX, actual_len_bytes);
     
     if(rc == NVSTORE_NOT_FOUND) {
         valueX = 0;
@@ -194,7 +199,7 @@
         nvstore.set(keyY, sizeof(valueY), &valueY);
         nvstore.set(keyZ, sizeof(valueZ), &valueZ);
     } else {
-        nvstore.get(keyX, sizeof(valueX), &valueX, actual_len_bytes);    
+        nvstore.get(keyZ, sizeof(valueZ), &valueZ, actual_len_bytes);    
         nvstore.get(keyY, sizeof(valueY), &valueY, actual_len_bytes);
     }
     
@@ -238,7 +243,7 @@
         }
     }
     
-    toggle_led.attach(&turnOnLed, 0.01);
+    toggle_led.attach(&toggleLed, 0.01);
     
 
 }