Leonardo Iandiorio / Mbed OS AccelerationFileNV

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