aaa

Dependencies:   BSP_B-L475E-IOT01

main.cpp

Committer:
frunzl
Date:
2018-12-06
Revision:
0:11c09159d5c4

File content as of revision 0:11c09159d5c4:


#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 "stm32l475e_iot01_accelero.h"
#include "nvstore.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);*/

DigitalOut led_1(LED1);
DigitalOut led_2(LED2);
DigitalOut led_3(LED3);
Ticker toggle_led_ticker;


EventQueue queue(32 * EVENTS_EVENT_SIZE);
Thread t;

#define BLOCK_SIZE 512
HeapBlockDevice bd(16384, BLOCK_SIZE);

// 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);
    }

    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);
    }
}
static FILE *f;
static uint16_t key_x = 1; 
static uint16_t key_y = 2; 
static uint16_t key_z = 3;
static uint32_t value_x;
static uint32_t value_y;
static uint32_t value_z;
volatile int counter = 0;

void get_data_from_sensors() {
    int16_t pDataXYZ[3] = {0};
    BSP_ACCELERO_AccGetXYZ(pDataXYZ);
    if (abs(pDataXYZ[0]) > 900) {
            fprintf(f, "%d\n", 1);
        } else if (abs(pDataXYZ[1]) > 900) {
            fprintf(f, "%d\n", 2);
        } else if (abs(pDataXYZ[2]) > 900) {
             fprintf(f, "%d\n", 3);
        } else {
             fprintf(f, "%d\n", -1);
        }
    fflush(f);
    fflush(stdout);
}

void read_data_from_file() {
      int led1 = 0;
      int led2 = 0;
      int led3 = 0;
      fflush(stdout);
      fflush(f);
      
      fseek(f, 0, SEEK_SET);
      int number;
      while (!feof(f)) {
        fscanf(f, "%d", &number); 
        if (number == 1) {
            led3 +=1;
        } else if(number == 2) {
            led2 +=1;
        } else if(number == 3) {
            led1 +=1;
        }
      }
      
      NVStore &nvstore = NVStore::get_instance();
      // Z Horizontal LED1, X short LED3, Y long LED2
      if (led1 >= led2 && led1 >= led3) {
            led_1 = 1;
            led_2 = 0;
            led_3 = 0;
            value_x++;
            nvstore.set(key_x, sizeof(value_x), &value_x);
        } else if (led2 >= led1 && led2 >= led3 ) {
            led_1 = 0;
            led_2 = 1;
            led_3 = 0;
            value_y++;
            nvstore.set(key_y, sizeof(value_y), &value_y);
        } else if (led3 >= led1 && led3 >= led2) {
            led_1 = 0;
            led_2 = 0;
            led_3 = 1;
            value_z++;
            nvstore.set(key_z, sizeof(value_z), &value_z);
        }
      
      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);
      }
        
      printf("Mbed OS filesystem example done!\n");
    
}

void toggle_led() {
    queue.call(get_data_from_sensors);
    counter++;
    if (counter == 100*10) {
      toggle_led_ticker.detach();
      queue.call(read_data_from_file);
    }
      
}
 
// Entry point for the example
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();
    printf("Init NVStore. \n");
    
    rc = nvstore.get(key_x, sizeof(value_x), &value_x, actual_len_bytes);
    if (rc == NVSTORE_NOT_FOUND) {
        value_x = 0;
        value_y = 0;
        value_z = 0;
        nvstore.set(key_x, sizeof(value_x), &value_x);
        nvstore.set(key_y, sizeof(value_y), &value_y);
        nvstore.set(key_z, sizeof(value_z), &value_z);
    } else {
        nvstore.get(key_y, sizeof(value_y), &value_y, actual_len_bytes);
        nvstore.get(key_z, sizeof(value_z), &value_z, actual_len_bytes);    
    }
    
    printf("LED1: %d\n LED2: %d\n  LED3: %d\n", value_x, value_y, value_z);
    
    printf("--- Mbed OS AccelleratorReadNV ---\n");

    // Setup the erase event on button press, use the event queue
    // to avoid running in interrupt context
    irq.fall(mbed_event_queue()->event(erase));

    // 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);
        }

//        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 record the acc
    toggle_led_ticker.attach(&toggle_led, 0.01);

    
//    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");
}