acceleration file system

Dependencies:   BSP_B-L475E-IOT01

main.cpp

Committer:
iandil
Date:
2018-12-06
Revision:
0:ba1f47c7fdd2

File content as of revision 0:ba1f47c7fdd2:


#include "mbed.h"
#include <stdio.h>
#include <errno.h>
#include "stm32l475e_iot01_accelero.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"

DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
Ticker toggle_led;

EventQueue queue(32 * EVENTS_EVENT_SIZE);
Thread thread;
int maximum( int, int, int);
//int maximum( int a, int b, int c );

// 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(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 *file;
volatile int counter = 0;

int maximum( int a, int b, int c ) {
   int max = ( a < b ) ? b : a;
   return ( ( max < c ) ? c : max );
}

void readSensors() {
    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);
        }
    fflush(file);
    fflush(stdout);    
}

void readFile() {
    
    int countLed1 = 0;
    int countLed2 = 0;
    int countLed3 = 0;
    
    fflush(stdout);
    fflush(file);
    
    fseek(file, 0, SEEK_SET);
    int ledNumber;
    while(!feof(file)) {
        fscanf(file, "%d", &ledNumber); 
        if(ledNumber == 1){
            countLed1 += 1;    
        } else if(ledNumber == 2){
            countLed2 += 1;    
        } else if(ledNumber == 3){
            countLed3 += 1;    
        }
    }
    int maxLed = maximum(countLed1, countLed2, countLed3);
    printf("MAX LED = %d\n", maxLed);
    if(maxLed == countLed1) {
        led1 = 1;
        led2 = 0;
        led3 = 0;
    } else if (maxLed == countLed2) {
        led1 = 0;
        led2 = 1;
        led3 = 0;
    } else if (maxLed == countLed3) {
        led1 = 0;
        led2 = 0;
        led3 = 1;
    }
    
    fflush(stdout);
    int err = fclose(file);
    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 turnOnLed() {
    queue.call(readSensors);
    counter += 1;
    if(counter == 1000) {
        toggle_led.detach();
        queue.call(readFile);
    }
}


// Entry point for the example
int main() {
    thread.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
    // 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);
    file = fopen("/fs/numbers.txt", "r+");
    printf("%s\n", (!file ? "Fail :(" : "OK"));
    if (!file) {
        // Create the numbers file if it doesn't exist
        printf("No file found, creating a new file... ");
        fflush(stdout);
        file = fopen("/fs/numbers.txt", "w+");
        printf("%s\n", (!file ? "Fail :(" : "OK"));
        if (!file) {
            error("error: %s (%d)\n", strerror(errno), -errno);
        }
    }
    
    toggle_led.attach(&turnOnLed, 0.01);
    

}