Francesco Arrigo / Mbed OS AccellerationFileNV

Dependencies:   BSP_B-L475E-IOT01

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include <stdio.h>
00003 #include <errno.h>
00004 
00005 // Block devices
00006 #if COMPONENT_SPIF
00007 #include "SPIFBlockDevice.h"
00008 #endif
00009 
00010 #if COMPONENT_DATAFLASH
00011 #include "DataFlashBlockDevice.h"
00012 #endif 
00013 
00014 #if COMPONENT_SD
00015 #include "SDBlockDevice.h"
00016 #endif 
00017 
00018 #include "HeapBlockDevice.h"
00019 
00020 // File systems
00021 #include "LittleFileSystem.h"
00022 #include "FATFileSystem.h"
00023 #include "stm32l475e_iot01_accelero.h"
00024 #include "nvstore.h"
00025 
00026 // Physical block device, can be any device that supports the BlockDevice API
00027 /*SPIFBlockDevice bd(
00028         MBED_CONF_SPIF_DRIVER_SPI_MOSI,
00029         MBED_CONF_SPIF_DRIVER_SPI_MISO,
00030         MBED_CONF_SPIF_DRIVER_SPI_CLK,
00031         MBED_CONF_SPIF_DRIVER_SPI_CS);*/
00032 
00033 DigitalOut led_1(LED1);
00034 DigitalOut led_2(LED2);
00035 DigitalOut led_3(LED3);
00036 Ticker check_led_status;
00037 
00038 EventQueue queue(32 * EVENTS_EVENT_SIZE);
00039 Thread t;
00040 
00041 #define BLOCK_SIZE 512
00042 HeapBlockDevice bd(32768, BLOCK_SIZE);
00043 
00044 #define LED_1_VALUE 1
00045 #define LED_2_VALUE 2
00046 #define LED_3_VALUE 3
00047 
00048 // File system declaration
00049 LittleFileSystem fs("fs");
00050 
00051 // Set up the button to trigger an erase
00052 InterruptIn irq(BUTTON1);
00053 void erase() {
00054     printf("Initializing the block device... ");
00055     fflush(stdout);
00056     int err = bd.init();
00057     printf("%s\n", (err ? "Fail :(" : "OK"));
00058     if (err) {
00059         error("error: %s (%d)\n", strerror(-err), err);
00060     }
00061 
00062     printf("Erasing the block device... ");
00063     fflush(stdout);
00064     err = bd.erase(0, bd.size());
00065     printf("%s\n", (err ? "Fail :(" : "OK"));
00066     if (err) {
00067         error("error: %s (%d)\n", strerror(-err), err);
00068     }
00069 
00070     printf("Deinitializing the block device... ");
00071     fflush(stdout);
00072     err = bd.deinit();
00073     printf("%s\n", (err ? "Fail :(" : "OK"));
00074     if (err) {
00075         error("error: %s (%d)\n", strerror(-err), err);
00076     }
00077 }
00078 
00079 static FILE *f;
00080 
00081 static uint16_t key_led1 = 1; 
00082 static uint16_t key_led2 = 2; 
00083 static uint16_t key_led3 = 3;
00084 
00085 static uint32_t value_led1;
00086 static uint32_t value_led2;
00087 static uint32_t value_led3;
00088 
00089 volatile int counter = 0;
00090 
00091 void read_acc() {
00092     int16_t pDataXYZ[3] = {0};
00093     BSP_ACCELERO_AccGetXYZ(pDataXYZ);
00094     if (abs(pDataXYZ[2]) > 900) { // board laying down
00095             fprintf(f, "%d\n", LED_1_VALUE);
00096         } else if (abs(pDataXYZ[1]) > 900) { // long side
00097             fprintf(f, "%d\n", LED_2_VALUE);
00098         } else if (abs(pDataXYZ[0]) > 900) { // short side
00099              fprintf(f, "%d\n", LED_3_VALUE);
00100         } else { // all other positions
00101              fprintf(f, "%d\n", -1);
00102         }
00103         
00104     fflush(f);
00105     fflush(stdout);
00106 }
00107 
00108 void read_data_from_file() {
00109       int led1_counter = 0;
00110       int led2_counter = 0;
00111       int led3_counter = 0;
00112       fflush(stdout);
00113       fflush(f);
00114       
00115       // read file and count occurrences of position numbers
00116       fseek(f, 0, SEEK_SET);
00117       int number;
00118       while (!feof(f)) {
00119         fscanf(f, "%d", &number);
00120         
00121         switch (number){
00122         case LED_1_VALUE:
00123             led1_counter += 1;
00124             break;
00125         case LED_2_VALUE:
00126             led2_counter += 1;
00127             break;
00128         case LED_3_VALUE:
00129             led3_counter += 1;
00130             break;
00131         default:
00132             break; 
00133         }
00134       }
00135       
00136       NVStore &nvstore = NVStore::get_instance();
00137       // check which position is the one present the most
00138       if (led1_counter > led2_counter && led1_counter > led3_counter) {
00139             led_1 = 1;
00140             led_2 = 0;
00141             led_3 = 0;
00142             value_led1 += 1;
00143             nvstore.set(key_led1, sizeof(value_led1), &value_led1);
00144         } else if (led2_counter > led1_counter && led2_counter > led3_counter ) {
00145             led_1 = 0;
00146             led_2 = 1;
00147             led_3 = 0;
00148             value_led2 += 1;
00149             nvstore.set(key_led2, sizeof(value_led2), &value_led2);
00150         } else if (led3_counter > led1_counter && led3_counter > led2_counter) {
00151             led_1 = 0;
00152             led_2 = 0;
00153             led_3 = 1;
00154             value_led3 += 1;
00155             nvstore.set(key_led3, sizeof(value_led3), &value_led3);
00156         }
00157       
00158       fflush(stdout);
00159       int err = fclose(f);
00160       printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00161       if (err < 0) {
00162         error("error: %s (%d)\n", strerror(err), -err);
00163       }
00164       err = fs.unmount();
00165       printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00166       if (err < 0) {
00167           error("error: %s (%d)\n", strerror(-err), err);
00168       }
00169         
00170       if (led_1){  
00171         printf("Lighting up corresponding LED 1!\n");
00172       } else if (led_2){
00173         printf("Lighting up corresponding LED 2!\n");
00174       } else if (led_3){
00175         printf("Lighting up corresponding LED 3!\n");
00176       } else {
00177         printf("No valid position detected during the sampling time, no LED lighting up!\n");
00178       }
00179     
00180 }
00181 
00182 void toggle_led() {
00183     queue.call(read_acc);
00184     counter++;
00185     if (counter == 100*10) {
00186       check_led_status.detach();
00187       queue.call(read_data_from_file);
00188     }
00189       
00190 }
00191 
00192 // Entry point for the example
00193 int main() {
00194     t.start(callback(&queue, &EventQueue::dispatch_forever));
00195     BSP_ACCELERO_Init();
00196     
00197     NVStore &nvstore = NVStore::get_instance();
00198     
00199     uint16_t actual_len_bytes = 0;
00200     int rc;
00201     
00202     rc = nvstore.init();
00203     
00204     rc = nvstore.get(key_led1, sizeof(value_led1), &value_led1, actual_len_bytes);
00205     if (rc == NVSTORE_NOT_FOUND) {
00206         value_led1 = 0;
00207         value_led2 = 0;
00208         value_led3 = 0;
00209         nvstore.set(key_led1, sizeof(value_led1), &value_led1);
00210         nvstore.set(key_led2, sizeof(value_led2), &value_led2);
00211         nvstore.set(key_led3, sizeof(value_led3), &value_led3);
00212     } else {
00213         nvstore.get(key_led2, sizeof(value_led2), &value_led2, actual_len_bytes);
00214         nvstore.get(key_led3, sizeof(value_led3), &value_led3, actual_len_bytes);    
00215     }
00216     
00217     printf("\nLED1 has been on: %d times\nLED2 has been on: %d times\nLED3 has been on: %d times\n", value_led1, value_led2, value_led3);
00218     
00219     printf("--- Mbed OS filesystem example ---\n");
00220 
00221     // Setup the erase event on button press, use the event queue
00222     // to avoid running in interrupt context
00223     irq.fall(mbed_event_queue()->event(erase));
00224 
00225     // Try to mount the filesystem
00226     printf("Mounting the filesystem... ");
00227     fflush(stdout);
00228     int err = fs.mount(&bd);
00229     printf("%s\n", (err ? "Fail :(" : "OK"));
00230     if (err) {
00231         // Reformat if we can't mount the filesystem
00232         // this should only happen on the first boot
00233         printf("No filesystem found, formatting... ");
00234         fflush(stdout);
00235         err = fs.reformat(&bd);
00236         printf("%s\n", (err ? "Fail :(" : "OK"));
00237         if (err) {
00238             error("error: %s (%d)\n", strerror(-err), err);
00239         }
00240     }
00241 
00242     // Open the numbers file
00243     printf("Opening \"/fs/numbers.txt\"... ");
00244     fflush(stdout);
00245     f = fopen("/fs/numbers.txt", "r +");
00246     printf("%s\n", (!f ? "Fail :(" : "OK"));
00247     if (!f) {
00248         // Create the numbers file if it doesn't exist
00249         printf("No file found, creating a new file... ");
00250         fflush(stdout);
00251         f = fopen("/fs/numbers.txt", "w+");
00252         printf("%s\n", (!f ? "Fail :(" : "OK"));
00253         if (!f) {
00254             error("error: %s (%d)\n", strerror(errno), -errno);
00255         }
00256 
00257         printf("Seeking file... ");
00258         fflush(stdout);
00259         err = fseek(f, 0, SEEK_SET);
00260         printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00261         if (err < 0) {
00262             error("error: %s (%d)\n", strerror(errno), -errno);
00263         }
00264     }
00265     
00266     check_led_status.attach(&toggle_led, 0.01);
00267 }