AccellerationFile Assignment

Dependencies:   BSP_B-L475E-IOT01

Files at this revision

API Documentation at this revision

Comitter:
framtk
Date:
Thu Dec 06 20:28:08 2018 +0000
Parent:
26:130bb1173866
Commit message:
done

Changed in this revision

BSP_B-L475E-IOT01.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 130bb1173866 -r 0cf170538518 BSP_B-L475E-IOT01.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BSP_B-L475E-IOT01.lib	Thu Dec 06 20:28:08 2018 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/teams/ST/code/BSP_B-L475E-IOT01/#9dfa42666f03
diff -r 130bb1173866 -r 0cf170538518 main.cpp
--- a/main.cpp	Fri Nov 23 09:15:36 2018 +0000
+++ b/main.cpp	Thu Dec 06 20:28:08 2018 +0000
@@ -1,4 +1,3 @@
-
 #include "mbed.h"
 #include <stdio.h>
 #include <errno.h>
@@ -21,6 +20,7 @@
 // File systems
 #include "LittleFileSystem.h"
 #include "FATFileSystem.h"
+#include "stm32l475e_iot01_accelero.h"
 
 // Physical block device, can be any device that supports the BlockDevice API
 /*SPIFBlockDevice bd(
@@ -29,8 +29,20 @@
         MBED_CONF_SPIF_DRIVER_SPI_CLK,
         MBED_CONF_SPIF_DRIVER_SPI_CS);*/
 
+DigitalOut led_1(LED1);
+DigitalOut led_2(LED2);
+DigitalOut led_3(LED3);
+Ticker check_led_status;
+
+EventQueue queue(32 * EVENTS_EVENT_SIZE);
+Thread t;
+
 #define BLOCK_SIZE 512
-HeapBlockDevice bd(2048, BLOCK_SIZE);
+HeapBlockDevice bd(32768, BLOCK_SIZE);
+
+#define LED_1_VALUE 1
+#define LED_2_VALUE 2
+#define LED_3_VALUE 3
 
 // File system declaration
 LittleFileSystem fs("fs");
@@ -63,9 +75,109 @@
     }
 }
 
+static FILE *f;
+volatile int counter = 0;
+
+void read_acc() {
+    int16_t pDataXYZ[3] = {0};
+    BSP_ACCELERO_AccGetXYZ(pDataXYZ);
+    if (abs(pDataXYZ[2]) > 900) { // board laying down
+            fprintf(f, "%d\n", LED_1_VALUE);
+        } else if (abs(pDataXYZ[1]) > 900) { // long side
+            fprintf(f, "%d\n", LED_2_VALUE);
+        } else if (abs(pDataXYZ[0]) > 900) { // short side
+             fprintf(f, "%d\n", LED_3_VALUE);
+        } else { // all other positions
+             fprintf(f, "%d\n", -1);
+        }
+        
+    fflush(f);
+    fflush(stdout);
+}
+
+void read_data_from_file() {
+      int led1_counter = 0;
+      int led2_counter = 0;
+      int led3_counter = 0;
+      fflush(stdout);
+      fflush(f);
+      
+      // read file and count occurrences of position numbers
+      fseek(f, 0, SEEK_SET);
+      int number;
+      while (!feof(f)) {
+        fscanf(f, "%d", &number);
+        
+        switch (number){
+        case LED_1_VALUE:
+            led1_counter += 1;
+            break;
+        case LED_2_VALUE:
+            led2_counter += 1;
+            break;
+        case LED_3_VALUE:
+            led3_counter += 1;
+            break;
+        default:
+            break; 
+        }
+      }
+      
+      // 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;
+        } else if (led2_counter > led1_counter && led2_counter > led3_counter ) {
+            led_1 = 0;
+            led_2 = 1;
+            led_3 = 0;
+        } else if (led3_counter > led1_counter && led3_counter > led2_counter) {
+            led_1 = 0;
+            led_2 = 0;
+            led_3 = 1;
+        }
+      
+      fflush(stdout);
+      int err = fclose(f);
+      printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
+      if (err < 0) {
+        error("error: %s (%d)\n", strerror(err), -err);
+      }
+      err = fs.unmount();
+      printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
+      if (err < 0) {
+          error("error: %s (%d)\n", strerror(-err), err);
+      }
+        
+      if (led_1){  
+        printf("Lighting up corresponding LED 1!\n");
+      } else if (led_2){
+        printf("Lighting up corresponding LED 2!\n");
+      } else if (led_3){
+        printf("Lighting up corresponding LED 3!\n");
+      } else {
+        printf("No valid position detected during the sampling time, no LED lighting up!\n");
+      }
+    
+}
+
+void toggle_led() {
+    queue.call(read_acc);
+    counter++;
+    if (counter == 100*10) {
+      check_led_status.detach();
+      queue.call(read_data_from_file);
+    }
+      
+}
 
 // Entry point for the example
 int main() {
+    t.start(callback(&queue, &EventQueue::dispatch_forever));
+    BSP_ACCELERO_Init();
+    
+    
     printf("--- Mbed OS filesystem example ---\n");
 
     // Setup the erase event on button press, use the event queue
@@ -92,7 +204,7 @@
     // Open the numbers file
     printf("Opening \"/fs/numbers.txt\"... ");
     fflush(stdout);
-    FILE *f = fopen("/fs/numbers.txt", "r+");
+    f = fopen("/fs/numbers.txt", "r +");
     printf("%s\n", (!f ? "Fail :(" : "OK"));
     if (!f) {
         // Create the numbers file if it doesn't exist
@@ -104,17 +216,6 @@
             error("error: %s (%d)\n", strerror(errno), -errno);
         }
 
-        for (int i = 0; i < 10; i++) {
-            printf("\rWriting numbers (%d/%d)... ", i, 10);
-            fflush(stdout);
-            err = fprintf(f, "    %d\n", i);
-            if (err < 0) {
-                printf("Fail :(\n");
-                error("error: %s (%d)\n", strerror(errno), -errno);
-            }
-        }
-        printf("\rWriting numbers (%d/%d)... OK\n", 10, 10);
-
         printf("Seeking file... ");
         fflush(stdout);
         err = fseek(f, 0, SEEK_SET);
@@ -123,99 +224,6 @@
             error("error: %s (%d)\n", strerror(errno), -errno);
         }
     }
-
-    // Go through and increment the numbers
-    for (int i = 0; i < 10; i++) {
-        printf("\rIncrementing numbers (%d/%d)... ", i, 10);
-        fflush(stdout);
-
-        // Get current stream position
-        long pos = ftell(f);
-
-        // Parse out the number and increment
-        int32_t number;
-        fscanf(f, "%d", &number);
-        number += 1;
-
-        // Seek to beginning of number
-        fseek(f, pos, SEEK_SET);
     
-        // Store number
-        fprintf(f, "    %d\n", number);
-
-        // Flush between write and read on same file
-        fflush(f);
-    }
-    printf("\rIncrementing numbers (%d/%d)... OK\n", 10, 10);
-
-    // Close the file which also flushes any cached writes
-    printf("Closing \"/fs/numbers.txt\"... ");
-    fflush(stdout);
-    err = fclose(f);
-    printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
-    if (err < 0) {
-        error("error: %s (%d)\n", strerror(errno), -errno);
-    }
-    
-    // Display the root directory
-    printf("Opening the root directory... ");
-    fflush(stdout);
-    DIR *d = opendir("/fs/");
-    printf("%s\n", (!d ? "Fail :(" : "OK"));
-    if (!d) {
-        error("error: %s (%d)\n", strerror(errno), -errno);
-    }
-
-    printf("root directory:\n");
-    while (true) {
-        struct dirent *e = readdir(d);
-        if (!e) {
-            break;
-        }
-
-        printf("    %s\n", e->d_name);
-    }
-
-    printf("Closing the root directory... ");
-    fflush(stdout);
-    err = closedir(d);
-    printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
-    if (err < 0) {
-        error("error: %s (%d)\n", strerror(errno), -errno);
-    }
-
-    // Display the numbers file
-    printf("Opening \"/fs/numbers.txt\"... ");
-    fflush(stdout);
-    f = fopen("/fs/numbers.txt", "r");
-    printf("%s\n", (!f ? "Fail :(" : "OK"));
-    if (!f) {
-        error("error: %s (%d)\n", strerror(errno), -errno);
-    }
-
-    printf("numbers:\n");
-    while (!feof(f)) {
-        int c = fgetc(f);
-        printf("%c", c);
-    }
-
-    printf("\rClosing \"/fs/numbers.txt\"... ");
-    fflush(stdout);
-    err = fclose(f);
-    printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
-    if (err < 0) {
-        error("error: %s (%d)\n", strerror(errno), -errno);
-    }
-
-    // Tidy up
-    printf("Unmounting... ");
-    fflush(stdout);
-    err = fs.unmount();
-    printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
-    if (err < 0) {
-        error("error: %s (%d)\n", strerror(-err), err);
-    }
-        
-    printf("Mbed OS filesystem example done!\n");
-}
-
+    check_led_status.attach(&toggle_led, 0.01);
+}
\ No newline at end of file