Redona Kembora / 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 #include "mbed.h"
00002 
00003 #include "stm32l475e_iot01_accelero.h"
00004 
00005 #include <stdio.h>
00006 #include <errno.h>
00007 #include "nvstore.h"
00008 
00009 // Block devices
00010 #if COMPONENT_SPIF
00011 #include "SPIFBlockDevice.h"
00012 #endif
00013 
00014 #if COMPONENT_DATAFLASH
00015 #include "DataFlashBlockDevice.h"
00016 #endif 
00017 
00018 #if COMPONENT_SD
00019 #include "SDBlockDevice.h"
00020 #endif 
00021 
00022 #include "HeapBlockDevice.h"
00023 
00024 // File systems
00025 #include "LittleFileSystem.h"
00026 #include "FATFileSystem.h"
00027 
00028 // Physical block device, can be any device that supports the BlockDevice API
00029 /*SPIFBlockDevice bd(
00030         MBED_CONF_SPIF_DRIVER_SPI_MOSI,
00031         MBED_CONF_SPIF_DRIVER_SPI_MISO,
00032         MBED_CONF_SPIF_DRIVER_SPI_CLK,
00033         MBED_CONF_SPIF_DRIVER_SPI_CS);*/
00034 
00035 #define BLOCK_SIZE 512
00036 HeapBlockDevice bd(16384, BLOCK_SIZE);
00037 
00038 // File system declaration
00039 LittleFileSystem fs("fs");
00040 
00041 DigitalOut led1(LED1);
00042 DigitalOut led2(LED2);
00043 DigitalOut led3(LED3);
00044 
00045 Ticker ticker;
00046 Thread t;
00047 
00048 uint16_t key_led1 = 1; 
00049 uint32_t value_led1;
00050 uint16_t key_led2 = 2; 
00051 uint32_t value_led2;
00052 uint16_t key_led3 = 3;
00053 uint32_t value_led3;
00054 
00055 static FILE *f;
00056 volatile int counter = 0;
00057 EventQueue queue(16 * EVENTS_EVENT_SIZE);
00058 InterruptIn button(USER_BUTTON);
00059 int16_t pDataXYZ[3] = {0};
00060 
00061 
00062 void save_accelero() {
00063     int16_t pDataXYZ[3] = {0};
00064     BSP_ACCELERO_AccGetXYZ(pDataXYZ);
00065     if ((pDataXYZ[0] < -950  && pDataXYZ[0] > -1030) || (pDataXYZ[0] < 1030 && pDataXYZ[0] > 950)){
00066             fprintf(f, "%d\n", 0);
00067         } else if ((pDataXYZ[1] < 1030 && pDataXYZ[1] > 950) || (pDataXYZ[1] < -950 && pDataXYZ[1] > -1030)) {
00068             fprintf(f, "%d\n", 1);
00069         } else if ((pDataXYZ[2] < 1030 && pDataXYZ[2] > 950) || (pDataXYZ[2] < -950 && pDataXYZ[2] > -1030)) {
00070              fprintf(f, "%d\n", 2);
00071         } else {
00072              fprintf(f, "%d\n", -1);
00073         }
00074     fflush(f);
00075     fflush(stdout);
00076 }
00077 
00078 void toggle_led_accelero() {
00079       int horizontal_count = 0;
00080       int long_count = 0;
00081       int short_count = 0;
00082       fflush(stdout);
00083       fflush(f);
00084       
00085       fseek(f, 0, SEEK_SET);
00086       int position;
00087       while (!feof(f)) {
00088         fscanf(f, "%d", &position); 
00089         if (position == 0) {
00090             short_count +=1;
00091         } else if(position == 1) {
00092             long_count +=1;
00093         } else if(position == 2) {
00094             horizontal_count +=1;
00095         }
00096       }
00097      NVStore &nvstore = NVStore::get_instance();
00098       if (short_count >= horizontal_count && short_count >= long_count) {
00099             led1 = 0;
00100             led2 = 0;
00101             led3 = 1;
00102             value_led3+=1;
00103             nvstore.set(key_led3, sizeof(value_led3), &value_led3);
00104             printf("The board stayed on the short edge for most time: %d \n",short_count);
00105         }  else if (long_count >= horizontal_count && long_count >= short_count ) {
00106             led1 = 0;
00107             led2 = 1;
00108             led3 = 0;
00109             value_led2+=1;
00110             nvstore.set(key_led2, sizeof(value_led2), &value_led2);
00111             printf("The board stayed on the long edge for most time: %d \n",long_count);
00112         } else if (horizontal_count >= long_count && horizontal_count >= short_count) {
00113             led1 = 1;
00114             led2 = 0;
00115             led3 = 0;
00116             value_led3+=1;
00117             nvstore.set(key_led3, sizeof(value_led3), &value_led3);
00118             printf("The board stayed horizontal for most time: %d \n",horizontal_count);
00119         }
00120     
00121       fflush(stdout);
00122       int err = fclose(f);
00123       printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00124       if (err < 0) {
00125         error("error: %s (%d)\n", strerror(err), -err);
00126       }
00127       err = fs.unmount();
00128       printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00129       if (err < 0) {
00130           error("error: %s (%d)\n", strerror(-err), err);
00131       }    
00132       
00133 }
00134 
00135 void toggle() {
00136     int16_t pDataXYZ[3] = {0};
00137     BSP_ACCELERO_AccGetXYZ(pDataXYZ);
00138     printf("ACCELERO_X = %d\n", pDataXYZ[0]);
00139     printf("ACCELERO_Y = %d\n", pDataXYZ[1]);
00140     printf("ACCELERO_Z = %d\n\n", pDataXYZ[2]);
00141 }
00142 
00143 void ticker_call() {    
00144     queue.call(save_accelero);
00145     counter++;
00146     if (counter == 1000) {
00147         ticker.detach();
00148       queue.call(toggle_led_accelero);
00149       counter = 0;
00150     }
00151 }
00152 
00153 int main(){
00154     t.start(callback(&queue, &EventQueue::dispatch_forever));
00155     BSP_ACCELERO_Init();
00156     
00157     //Setting up NVStore
00158     NVStore &nvstore = NVStore::get_instance();
00159    
00160     uint16_t actual_len_bytes = 0;
00161     int rc;
00162     
00163     rc = nvstore.init();
00164     printf("Init NVStore. \n");
00165     
00166     rc = nvstore.get(key_led1, sizeof(value_led1), &value_led1, actual_len_bytes);
00167     if (rc == NVSTORE_NOT_FOUND) {
00168         value_led1 = 0;
00169         value_led2 = 0;
00170         value_led3 = 0;
00171         nvstore.set(key_led1, sizeof(value_led1), &value_led1);
00172         nvstore.set(key_led2, sizeof(value_led2), &value_led2);
00173         nvstore.set(key_led3, sizeof(value_led3), &value_led3);
00174     } else {
00175         nvstore.get(key_led2, sizeof(value_led2), &value_led2, actual_len_bytes);
00176         nvstore.get(key_led3, sizeof(value_led3), &value_led3, actual_len_bytes);    
00177     }
00178  
00179     printf("LED1 switched on: %d times\n",value_led1);
00180     printf("LED2 switched on: %d times\n",value_led2);
00181     printf("LED3 switched on: %d times\n",value_led3);    
00182     
00183     // Try to mount the filesystem
00184     printf("Mounting the filesystem... ");
00185     fflush(stdout);
00186     int err = fs.mount(&bd);
00187     printf("%s\n", (err ? "Fail :(" : "OK"));
00188     if (err) {
00189         // Reformat if we can't mount the filesystem
00190         // this should only happen on the first boot
00191         printf("No filesystem found, formatting... ");
00192         fflush(stdout);
00193         err = fs.reformat(&bd);
00194         printf("%s\n", (err ? "Fail :(" : "OK"));
00195         if (err) {
00196             error("error: %s (%d)\n", strerror(-err), err);
00197         }
00198     }
00199     
00200     // Open the numbers file
00201     printf("Opening \"/fs/numbers.txt\"... ");
00202     fflush(stdout);
00203     f = fopen("/fs/numbers.txt", "r+");
00204     printf("%s\n", (!f ? "Fail :(" : "OK"));
00205     if (!f) {
00206         // Create the numbers file if it doesn't exist
00207         printf("No file found, creating a new file... ");
00208         fflush(stdout);
00209         f = fopen("/fs/numbers.txt", "w+");
00210         printf("%s\n", (!f ? "Fail :(" : "OK"));
00211         if (!f) {
00212             error("error: %s (%d)\n", strerror(errno), -errno);
00213         }
00214     }
00215 
00216     ticker.attach(&ticker_call, 0.01);
00217 }