assignment

Dependencies:   BSP_B-L475E-IOT01

Files at this revision

API Documentation at this revision

Comitter:
alessioburatti
Date:
Thu Dec 06 16:48:01 2018 +0000
Parent:
26:130bb1173866
Child:
28:cd6b9e13b1fe
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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BSP_B-L475E-IOT01.lib	Thu Dec 06 16:48:01 2018 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/teams/ST/code/BSP_B-L475E-IOT01/#9dfa42666f03
--- a/main.cpp	Fri Nov 23 09:15:36 2018 +0000
+++ b/main.cpp	Thu Dec 06 16:48:01 2018 +0000
@@ -1,85 +1,200 @@
 
 #include "mbed.h"
+
+#include "stm32l475e_iot01_accelero.h"
+
 #include <stdio.h>
 #include <errno.h>
 
-// Block devices
+    // Block devices
 #if COMPONENT_SPIF
 #include "SPIFBlockDevice.h"
 #endif
 
 #if COMPONENT_DATAFLASH
 #include "DataFlashBlockDevice.h"
-#endif 
+#endif
 
 #if COMPONENT_SD
 #include "SDBlockDevice.h"
-#endif 
+#endif
 
 #include "HeapBlockDevice.h"
 
-// File systems
+    // File systems
 #include "LittleFileSystem.h"
 #include "FATFileSystem.h"
+#include "nvstore.h"
 
-// Physical block device, can be any device that supports the BlockDevice API
+    // Physical block device, can be any device that supports the BlockDevice API
 /*SPIFBlockDevice bd(
-        MBED_CONF_SPIF_DRIVER_SPI_MOSI,
-        MBED_CONF_SPIF_DRIVER_SPI_MISO,
-        MBED_CONF_SPIF_DRIVER_SPI_CLK,
-        MBED_CONF_SPIF_DRIVER_SPI_CS);*/
+ MBED_CONF_SPIF_DRIVER_SPI_MOSI,
+ MBED_CONF_SPIF_DRIVER_SPI_MISO,
+ MBED_CONF_SPIF_DRIVER_SPI_CLK,
+ MBED_CONF_SPIF_DRIVER_SPI_CS);*/
+
+#define X_VALUE pDataXYZ[0]
+#define Y_VALUE pDataXYZ[1]
+#define Z_VALUE pDataXYZ[2]
+
+#define HORIZONTAL_INDEX 0
+#define LONG_VERTICAL_INDEX 1
+#define SHORT_VERTICAL_INDEX 2
 
 #define BLOCK_SIZE 512
-HeapBlockDevice bd(2048, BLOCK_SIZE);
+HeapBlockDevice bd(16384, BLOCK_SIZE);
 
-// File system declaration
+    // File system declaration
 LittleFileSystem fs("fs");
 
-// Set up the button to trigger an erase
-InterruptIn irq(BUTTON1);
-void erase() {
-    printf("Initializing the block device... ");
-    fflush(stdout);
-    int err = bd.init();
-    printf("%s\n", (err ? "Fail :(" : "OK"));
-    if (err) {
-        error("error: %s (%d)\n", strerror(-err), err);
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+
+Ticker tTicker;
+Thread t;
+
+uint32_t horizontalCount;
+uint32_t longVerticalCount;
+uint32_t shortVerticalCount;
+
+static FILE *f;
+volatile int countedPositionsd = 0;
+EventQueue queue(32 * EVENTS_EVENT_SIZE);
+
+InterruptIn button(USER_BUTTON);
+
+int max3(int a, int b, int c) {
+    if (a > b) {
+        return a > c ? HORIZONTAL_INDEX : SHORT_VERTICAL_INDEX;
     }
+    return b > c ? LONG_VERTICAL_INDEX : SHORT_VERTICAL_INDEX;
+}
+
+void turnOffLeds() {
+    led1 = 0;
+    led2 = 0;
+    led3 = 0;
+}
+
+void turnOnLedAndUpdateStore(DigitalOut * led, int key, uint32_t * currentCount) {
+    *led = 1;
+    NVStore &nvstore = NVStore::get_instance();
+    *currentCount++;
+    nvstore.set(key, sizeof(*currentCount), currentCount);
+}
 
-    printf("Erasing the block device... ");
+void toggleLed() {
+    int16_t occurencies[3] = {0};
     fflush(stdout);
-    err = bd.erase(0, bd.size());
-    printf("%s\n", (err ? "Fail :(" : "OK"));
-    if (err) {
-        error("error: %s (%d)\n", strerror(-err), err);
+    fflush(f);
+    
+    fseek(f, 0, SEEK_SET);
+    
+    printf("Start reading\n");
+    
+    int number;
+    while (!feof(f)) {
+        fscanf(f, "%d", &number);
+        if (number == HORIZONTAL_INDEX || number == LONG_VERTICAL_INDEX || number == SHORT_VERTICAL_INDEX) {
+            occurencies[number] = occurencies[number] + 1;
+        }
     }
-
-    printf("Deinitializing the block device... ");
+    
+    printf("%d %d %d\n", occurencies[HORIZONTAL_INDEX], occurencies[LONG_VERTICAL_INDEX], occurencies[SHORT_VERTICAL_INDEX]);
+    
+    int mostOccurentValue = max3(occurencies[0], occurencies[1], occurencies[2]);
+    
+    printf("Max %d\n", mostOccurentValue);
+    
+    turnOffLeds();
+    
+    if (mostOccurentValue == HORIZONTAL_INDEX) {
+        turnOnLedAndUpdateStore(&led1, HORIZONTAL_INDEX, &horizontalCount);
+    } else if (mostOccurentValue == LONG_VERTICAL_INDEX) {
+        turnOnLedAndUpdateStore(&led2, LONG_VERTICAL_INDEX, &longVerticalCount);
+    } else if (mostOccurentValue == SHORT_VERTICAL_INDEX) {
+        turnOnLedAndUpdateStore(&led3, SHORT_VERTICAL_INDEX, &shortVerticalCount);
+    }
+    
     fflush(stdout);
-    err = bd.deinit();
-    printf("%s\n", (err ? "Fail :(" : "OK"));
-    if (err) {
+    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);
     }
 }
 
+bool isInRange100(int val) {
+    return val < 100 && val > -100;
+}
 
-// Entry point for the example
-int main() {
-    printf("--- Mbed OS filesystem example ---\n");
+bool isInRange900_1000(int val) {
+    return (val < 1050 && val > 950) || (val < -950 && val > -1050);
+}
+
+void savePosition() {
+    int16_t pDataXYZ[3] = {0};
+    BSP_ACCELERO_AccGetXYZ(pDataXYZ);
+    if (isInRange100(X_VALUE) && isInRange100(Y_VALUE) && isInRange900_1000(Z_VALUE)) {
+        fprintf(f, "%d\n", HORIZONTAL_INDEX);
+    } else if (isInRange100(X_VALUE) && isInRange900_1000(Y_VALUE) && isInRange100(Z_VALUE)) {
+        fprintf(f, "%d\n", LONG_VERTICAL_INDEX);
+    } else if (isInRange900_1000(X_VALUE) && isInRange100(Y_VALUE) && isInRange100(Z_VALUE)) {
+        fprintf(f, "%d\n", SHORT_VERTICAL_INDEX);
+    }
+    fflush(f);
+    fflush(stdout);
+}
+
 
-    // Setup the erase event on button press, use the event queue
-    // to avoid running in interrupt context
-    irq.fall(mbed_event_queue()->event(erase));
+void tickerBlock() {
+    queue.call(savePosition);
+    countedPositionsd++;
+    if (countedPositionsd == 1000) {
+        tTicker.detach();
+        queue.call(toggleLed);
+        countedPositionsd = 0;
+    }
+}
 
-    // Try to mount the filesystem
+int main(){
+    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(HORIZONTAL_INDEX, sizeof(horizontalCount), &horizontalCount, actual_len_bytes);
+    if (rc == NVSTORE_NOT_FOUND) {
+        horizontalCount = 0;
+        shortVerticalCount = 0;
+        longVerticalCount = 0;
+        nvstore.set(HORIZONTAL_INDEX, sizeof(horizontalCount), &horizontalCount);
+        nvstore.set(SHORT_VERTICAL_INDEX, sizeof(shortVerticalCount), &shortVerticalCount);
+        nvstore.set(LONG_VERTICAL_INDEX, sizeof(longVerticalCount), &longVerticalCount);
+    } else {
+        nvstore.get(SHORT_VERTICAL_INDEX, sizeof(shortVerticalCount), &shortVerticalCount, actual_len_bytes);
+        nvstore.get(LONG_VERTICAL_INDEX, sizeof(longVerticalCount), &longVerticalCount, actual_len_bytes);
+    }
+    
+    printf("Led 1: %d\nLed 2: %d\nLed 3: %d\n", horizontalCount, longVerticalCount, shortVerticalCount);
+        // Try to mount the filesystem
     printf("Mounting the filesystem... ");
     fflush(stdout);
     int err = fs.mount(&bd);
     printf("%s\n", (err ? "Fail :(" : "OK"));
     if (err) {
-        // Reformat if we can't mount the filesystem
-        // this should only happen on the first boot
+            // Reformat if we can't mount the filesystem
+            // this should only happen on the first boot
         printf("No filesystem found, formatting... ");
         fflush(stdout);
         err = fs.reformat(&bd);
@@ -88,14 +203,14 @@
             error("error: %s (%d)\n", strerror(-err), err);
         }
     }
-
-    // Open the numbers file
+    
+        // 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
+            // Create the numbers file if it doesn't exist
         printf("No file found, creating a new file... ");
         fflush(stdout);
         f = fopen("/fs/numbers.txt", "w+");
@@ -103,119 +218,7 @@
         if (!f) {
             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);
-        printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
-        if (err < 0) {
-            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");
+    tTicker.attach(&tickerBlock, 0.01);
 }
-