Leonardo Iandiorio / Mbed OS AccelerationFile

Dependencies:   BSP_B-L475E-IOT01

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 #include "mbed.h"
00003 #include <stdio.h>
00004 #include <errno.h>
00005 #include "stm32l475e_iot01_accelero.h"
00006 
00007 // Block devices
00008 #if COMPONENT_SPIF
00009 #include "SPIFBlockDevice.h"
00010 #endif
00011 
00012 #if COMPONENT_DATAFLASH
00013 #include "DataFlashBlockDevice.h"
00014 #endif 
00015 
00016 #if COMPONENT_SD
00017 #include "SDBlockDevice.h"
00018 #endif 
00019 
00020 #include "HeapBlockDevice.h"
00021 // File systems
00022 #include "LittleFileSystem.h"
00023 #include "FATFileSystem.h"
00024 
00025 DigitalOut led1(LED1);
00026 DigitalOut led2(LED2);
00027 DigitalOut led3(LED3);
00028 Ticker toggle_led;
00029 
00030 EventQueue queue(32 * EVENTS_EVENT_SIZE);
00031 Thread thread;
00032 int maximum( int, int, int);
00033 //int maximum( int a, int b, int c );
00034 
00035 // Physical block device, can be any device that supports the BlockDevice API
00036 /*SPIFBlockDevice bd(
00037         MBED_CONF_SPIF_DRIVER_SPI_MOSI,
00038         MBED_CONF_SPIF_DRIVER_SPI_MISO,
00039         MBED_CONF_SPIF_DRIVER_SPI_CLK,
00040         MBED_CONF_SPIF_DRIVER_SPI_CS);*/
00041 
00042 #define BLOCK_SIZE 512
00043 HeapBlockDevice bd(16384, BLOCK_SIZE);
00044 
00045 // File system declaration
00046 LittleFileSystem fs("fs");
00047 
00048 // Set up the button to trigger an erase
00049 InterruptIn irq(BUTTON1);
00050 void erase() {
00051     printf("Initializing the block device... ");
00052     fflush(stdout);
00053     int err = bd.init();
00054     printf("%s\n", (err ? "Fail :(" : "OK"));
00055     if (err) {
00056         error("error: %s (%d)\n", strerror(-err), err);
00057     }
00058 
00059     printf("Erasing the block device... ");
00060     fflush(stdout);
00061     err = bd.erase(0, bd.size());
00062     printf("%s\n", (err ? "Fail :(" : "OK"));
00063     if (err) {
00064         error("error: %s (%d)\n", strerror(-err), err);
00065     }
00066 
00067     printf("Deinitializing the block device... ");
00068     fflush(stdout);
00069     err = bd.deinit();
00070     printf("%s\n", (err ? "Fail :(" : "OK"));
00071     if (err) {
00072         error("error: %s (%d)\n", strerror(-err), err);
00073     }
00074 }
00075 
00076 static FILE *file;
00077 volatile int counter = 0;
00078 
00079 int maximum( int a, int b, int c ) {
00080    int max = ( a < b ) ? b : a;
00081    return ( ( max < c ) ? c : max );
00082 }
00083 
00084 void readSensors() {
00085     int16_t pDataXYZ[3] = {0};
00086     BSP_ACCELERO_AccGetXYZ(pDataXYZ);
00087     if(pDataXYZ[2] > 900 || pDataXYZ[2] < -900) { //Horizontal State
00088             fprintf(file, "%d\n", 1);
00089         } else if(pDataXYZ[1] > 900 || pDataXYZ[1] < -900) { //Long Edge State
00090             fprintf(file, "%d\n", 2);
00091         } else if(pDataXYZ[0] > 900 || pDataXYZ[0] < -900) { // Short Edge State
00092             fprintf(file, "%d\n", 3);
00093         } else { // all other positions
00094             fprintf(file, "%d\n", 4);
00095         }
00096     fflush(file);
00097     fflush(stdout);    
00098 }
00099 
00100 void readFile() {
00101     
00102     int countLed1 = 0;
00103     int countLed2 = 0;
00104     int countLed3 = 0;
00105     
00106     fflush(stdout);
00107     fflush(file);
00108     
00109     fseek(file, 0, SEEK_SET);
00110     int ledNumber;
00111     while(!feof(file)) {
00112         fscanf(file, "%d", &ledNumber); 
00113         if(ledNumber == 1){
00114             countLed1 += 1;    
00115         } else if(ledNumber == 2){
00116             countLed2 += 1;    
00117         } else if(ledNumber == 3){
00118             countLed3 += 1;    
00119         }
00120     }
00121     int maxLed = maximum(countLed1, countLed2, countLed3);
00122     printf("MAX LED = %d\n", maxLed);
00123     if(maxLed == countLed1) {
00124         led1 = 1;
00125         led2 = 0;
00126         led3 = 0;
00127     } else if (maxLed == countLed2) {
00128         led1 = 0;
00129         led2 = 1;
00130         led3 = 0;
00131     } else if (maxLed == countLed3) {
00132         led1 = 0;
00133         led2 = 0;
00134         led3 = 1;
00135     }
00136     
00137     fflush(stdout);
00138     int err = fclose(file);
00139     printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00140     if (err < 0) {
00141       error("error: %s (%d)\n", strerror(err), -err);
00142     }
00143     err = fs.unmount();
00144     printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00145     if (err < 0) {
00146         error("error: %s (%d)\n", strerror(-err), err);
00147     }
00148       
00149     printf("Mbed OS filesystem example done!\n"); 
00150 }
00151 
00152 void turnOnLed() {
00153     queue.call(readSensors);
00154     counter += 1;
00155     if(counter == 1000) {
00156         toggle_led.detach();
00157         queue.call(readFile);
00158     }
00159 }
00160 
00161 
00162 // Entry point for the example
00163 int main() {
00164     thread.start(callback(&queue, &EventQueue::dispatch_forever));
00165     BSP_ACCELERO_Init();
00166     printf("--- Mbed OS filesystem example ---\n");
00167 
00168     // Setup the erase event on button press, use the event queue
00169     // to avoid running in interrupt context
00170     irq.fall(mbed_event_queue()->event(erase));
00171 
00172     // Try to mount the filesystem
00173     printf("Mounting the filesystem... ");
00174     fflush(stdout);
00175     int err = fs.mount(&bd);
00176     printf("%s\n", (err ? "Fail :(" : "OK"));
00177     if (err) {
00178         // Reformat if we can't mount the filesystem
00179         // this should only happen on the first boot
00180         printf("No filesystem found, formatting... ");
00181         fflush(stdout);
00182         err = fs.reformat(&bd);
00183         printf("%s\n", (err ? "Fail :(" : "OK"));
00184         if (err) {
00185             error("error: %s (%d)\n", strerror(-err), err);
00186         }
00187     }
00188 
00189     // Open the numbers file
00190     printf("Opening \"/fs/numbers.txt\"... ");
00191     fflush(stdout);
00192     file = fopen("/fs/numbers.txt", "r+");
00193     printf("%s\n", (!file ? "Fail :(" : "OK"));
00194     if (!file) {
00195         // Create the numbers file if it doesn't exist
00196         printf("No file found, creating a new file... ");
00197         fflush(stdout);
00198         file = fopen("/fs/numbers.txt", "w+");
00199         printf("%s\n", (!file ? "Fail :(" : "OK"));
00200         if (!file) {
00201             error("error: %s (%d)\n", strerror(errno), -errno);
00202         }
00203     }
00204     
00205     toggle_led.attach(&turnOnLed, 0.01);
00206     
00207 
00208 }