Exercise2

Dependencies:   BSP_B-L475E-IOT01

Files at this revision

API Documentation at this revision

Comitter:
mcalzana
Date:
Thu Dec 06 16:11:10 2018 +0000
Commit message:
Exrcise2

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
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
mbed_app.json 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:11:10 2018 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/teams/ST/code/BSP_B-L475E-IOT01/#9dfa42666f03
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Dec 06 16:11:10 2018 +0000
@@ -0,0 +1,283 @@
+
+#include "mbed.h"
+#include <stdio.h>
+#include <errno.h>
+
+// Block devices
+#if COMPONENT_SPIF
+#include "SPIFBlockDevice.h"
+#endif
+
+#if COMPONENT_DATAFLASH
+#include "DataFlashBlockDevice.h"
+#endif 
+
+#if COMPONENT_SD
+#include "SDBlockDevice.h"
+#endif 
+
+#include "HeapBlockDevice.h"
+
+// File systems
+#include "LittleFileSystem.h"
+#include "FATFileSystem.h"
+#include "nvstore.h"
+
+#include "stm32l475e_iot01_accelero.h"
+
+// 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);*/
+
+#define BLOCK_SIZE 512
+HeapBlockDevice bd(4096, BLOCK_SIZE);
+
+// File system declaration
+LittleFileSystem fs("fs");
+
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+
+// 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);
+    }
+
+    printf("Erasing the block device... ");
+    fflush(stdout);
+    err = bd.erase(0, bd.size());
+    printf("%s\n", (err ? "Fail :(" : "OK"));
+    if (err) {
+        error("error: %s (%d)\n", strerror(-err), err);
+    }
+
+    printf("Deinitializing the block device... ");
+    fflush(stdout);
+    err = bd.deinit();
+    printf("%s\n", (err ? "Fail :(" : "OK"));
+    if (err) {
+        error("error: %s (%d)\n", strerror(-err), err);
+    }
+}
+
+
+// Entry point for the example
+
+Ticker toggle_write_to_file;
+EventQueue queue(16 * EVENTS_EVENT_SIZE);
+Thread t;
+static FILE *f;
+volatile int counter = 0;
+time_t begin = time(NULL);
+
+
+
+void writeAccToFile(){
+    int16_t pDataXYZ[3] = {0};
+    BSP_ACCELERO_AccGetXYZ(pDataXYZ);
+    int x = abs(pDataXYZ[0]);
+    int y = abs(pDataXYZ[1]);
+    int z = abs(pDataXYZ[2]);
+    char c;
+    if(z > 950 && z < 1050){
+        c = 'z';
+    } else if(y > 950 && y < 1050){
+        c = 'y'; 
+    }else if(x > 950 && x < 1050){
+        c = 'x';   
+    }else{
+        c = 'o';
+    }
+    fprintf(f, "%c", c);
+    fflush(stdout); 
+}
+
+void readNVStoreLedCount(uint16_t key){
+    NVStore &nvstore = NVStore::get_instance();
+    int rc;
+    uint32_t value;
+    uint16_t actual_len_bytes = 0;
+    rc = nvstore.get(key, sizeof(value), &value, actual_len_bytes);
+    if(rc == NVSTORE_NOT_FOUND){
+        printf("Led %d turn on 0 times\n", key);
+    }else{
+       printf("Led %d turn on %d times\n", key, value);
+    }
+}
+
+void addNVStoreLedCount(uint16_t key){
+    NVStore &nvstore = NVStore::get_instance();
+    int rc;
+    uint32_t value;
+    uint16_t actual_len_bytes = 0;
+    rc = nvstore.get(key, sizeof(value), &value, actual_len_bytes);
+    if(rc == NVSTORE_NOT_FOUND){
+        value = 1;
+    }else{
+        value++;
+    }
+    rc = nvstore.set(key, sizeof(value), &value);
+}
+
+void readAccFile(){
+    printf("end sampling \n");
+    
+    // Close the file which also flushes any cached writes
+    printf("Closing \"/fs/numbers.txt\"... ");
+    fflush(stdout);
+    int err = fclose(f);
+    printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
+    if (err < 0) {
+        error("error: %s (%d)\n", strerror(errno), -errno);
+    }
+    
+    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);
+    }
+    
+    int xCount = 0;
+    int yCount = 0;
+    int zCount = 0;
+    
+    while (!feof(f)) {
+        char c = fgetc(f);
+        if(c == 'x'){
+            xCount++;
+        }else if(c == 'y'){
+            yCount++;
+        }else if(c == 'z'){
+            zCount++;
+        }
+    }
+    printf("Values: x: %d, y:%d, z: %d \n", xCount, yCount, zCount);
+    
+    //Switch on the LED. x is default Led.
+    if(xCount >= yCount && xCount >= zCount){
+        uint16_t key = 3;
+        led3 = 1;
+        addNVStoreLedCount(key);
+    }else if(yCount > xCount && yCount > zCount){
+        uint16_t key = 2; 
+        addNVStoreLedCount(key);
+        led2 = 1;
+    }else{
+        uint16_t key = 1;
+        addNVStoreLedCount(key); 
+        led1 = 1;
+    }
+
+    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");
+    
+}
+
+void readSensorsInterupt() {
+    queue.call(writeAccToFile);
+    counter++;
+    if (counter == 1000) {
+      toggle_write_to_file.detach();
+      queue.call(readAccFile);
+    }
+}
+
+int main() {
+    // Setup the erase event on button press, use the event queue
+    // to avoid running in interrupt context
+    irq.fall(mbed_event_queue()->event(erase));
+    
+    //Settings for nvstore. 
+    NVStore &nvstore = NVStore::get_instance();
+    uint16_t key; 
+    nvstore.init();
+    printf("Init NVStore... ");
+    
+    printf("NVStore size is %d.\n", nvstore.size()); 
+    printf("NVStore max number of keys is %d (out of %d possible ones in this flash configuration).\n", 
+        nvstore.get_max_keys(), nvstore.get_max_possible_keys());
+    printf("--- LED PREV. RESULTS---\n");
+    key = 1;
+    readNVStoreLedCount(key);
+    key = 2;
+    readNVStoreLedCount(key);
+    key = 3;
+    readNVStoreLedCount(key);
+
+    // 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
+        printf("No filesystem found, formatting... ");
+        fflush(stdout);
+        err = fs.reformat(&bd);
+        printf("%s\n", (err ? "Fail :(" : "OK"));
+        if (err) {
+            error("error: %s (%d)\n", strerror(-err), err);
+        }
+    }
+
+    // Open the numbers file
+    printf("Opening \"/fs/numbers.txt\"... ");
+    fflush(stdout);
+    f = fopen("/fs/numbers.txt", "r +");
+    printf("%s\n", (!f ? "Fail :(" : "OK"));
+    if (!f) {
+        // 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+");
+        printf("%s\n", (!f ? "Fail :(" : "OK"));
+        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);
+        }
+    }
+    
+    t.start(callback(&queue, &EventQueue::dispatch_forever));
+    BSP_ACCELERO_Init();
+    led1 = 0;
+    led2 = 0;
+    led3 = 0;
+    printf("Start sampling... \n");
+    begin = time(NULL);
+    toggle_write_to_file.attach(&readSensorsInterupt, 0.01);
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Thu Dec 06 16:11:10 2018 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#610e35ddc6d59f153173c1e7b2748cf96d6c9bcd
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed_app.json	Thu Dec 06 16:11:10 2018 +0000
@@ -0,0 +1,7 @@
+{
+    "target_overrides": {
+        "*": {
+            "platform.stdio-convert-newlines": true
+        }
+    }
+}