Redona Kembora / 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 #include "mbed.h"
00002 
00003 #include "stm32l475e_iot01_accelero.h"
00004 
00005 #include <stdio.h>
00006 #include <errno.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 
00023 // File systems
00024 #include "LittleFileSystem.h"
00025 #include "FATFileSystem.h"
00026 
00027 // Physical block device, can be any device that supports the BlockDevice API
00028 /*SPIFBlockDevice bd(
00029         MBED_CONF_SPIF_DRIVER_SPI_MOSI,
00030         MBED_CONF_SPIF_DRIVER_SPI_MISO,
00031         MBED_CONF_SPIF_DRIVER_SPI_CLK,
00032         MBED_CONF_SPIF_DRIVER_SPI_CS);*/
00033 
00034 #define BLOCK_SIZE 512
00035 HeapBlockDevice bd(16384, BLOCK_SIZE);
00036 
00037 // File system declaration
00038 LittleFileSystem fs("fs");
00039 
00040 DigitalOut led1(LED1);
00041 DigitalOut led2(LED2);
00042 DigitalOut led3(LED3);
00043 
00044 Ticker ticker;
00045 Thread t;
00046 
00047 static FILE *f;
00048 volatile int counter = 0;
00049 EventQueue queue(16 * EVENTS_EVENT_SIZE);
00050 InterruptIn button(USER_BUTTON);
00051 int16_t pDataXYZ[3] = {0};
00052 
00053 
00054 void save_accelero() {
00055     int16_t pDataXYZ[3] = {0};
00056     BSP_ACCELERO_AccGetXYZ(pDataXYZ);
00057     if ((pDataXYZ[0] < -950  && pDataXYZ[0] > -1030) || (pDataXYZ[0] < 1030 && pDataXYZ[0] > 950)){
00058             fprintf(f, "%d\n", 0);
00059         } else if ((pDataXYZ[1] < 1030 && pDataXYZ[1] > 950) || (pDataXYZ[1] < -950 && pDataXYZ[1] > -1030)) {
00060             fprintf(f, "%d\n", 1);
00061         } else if ((pDataXYZ[2] < 1030 && pDataXYZ[2] > 950) || (pDataXYZ[2] < -950 && pDataXYZ[2] > -1030)) {
00062              fprintf(f, "%d\n", 2);
00063         } else {
00064              fprintf(f, "%d\n", -1);
00065         }
00066     fflush(f);
00067     fflush(stdout);
00068 }
00069 
00070 void toggle_led_accelero() {
00071       int horizontal_count = 0;
00072       int long_count = 0;
00073       int short_count = 0;
00074       fflush(stdout);
00075       fflush(f);
00076       
00077       fseek(f, 0, SEEK_SET);
00078       int position;
00079       while (!feof(f)) {
00080         fscanf(f, "%d", &position); 
00081         if (position == 0) {
00082             short_count +=1;
00083         } else if(position == 1) {
00084             long_count +=1;
00085         } else if(position == 2) {
00086             horizontal_count +=1;
00087         }
00088       }
00089     
00090       if (short_count >= horizontal_count && short_count >= long_count) {
00091             led1 = 0;
00092             led2 = 0;
00093             led3 = 1;
00094             printf("The board stayed on the short edge for most time: %d \n",short_count);
00095         }  else if (long_count >= horizontal_count && long_count >= short_count ) {
00096             led1 = 0;
00097             led2 = 1;
00098             led3 = 0;
00099             printf("The board stayed on the long edge for most time: %d \n",long_count);
00100         } else if (horizontal_count >= long_count && horizontal_count >= short_count) {
00101             led1 = 1;
00102             led2 = 0;
00103             led3 = 0;
00104             printf("The board stayed horizontal for most time: %d \n",horizontal_count);
00105         }
00106     
00107       fflush(stdout);
00108       int err = fclose(f);
00109       printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00110       if (err < 0) {
00111         error("error: %s (%d)\n", strerror(err), -err);
00112       }
00113       err = fs.unmount();
00114       printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00115       if (err < 0) {
00116           error("error: %s (%d)\n", strerror(-err), err);
00117       }    
00118       
00119 }
00120 
00121 void toggle() {
00122     int16_t pDataXYZ[3] = {0};
00123     BSP_ACCELERO_AccGetXYZ(pDataXYZ);
00124     printf("ACCELERO_X = %d\n", pDataXYZ[0]);
00125     printf("ACCELERO_Y = %d\n", pDataXYZ[1]);
00126     printf("ACCELERO_Z = %d\n\n", pDataXYZ[2]);
00127 }
00128 
00129 void ticker_call() {    
00130     queue.call(save_accelero);
00131     counter++;
00132     if (counter == 1000) {
00133     ticker.detach();
00134       queue.call(toggle_led_accelero);
00135       counter = 0;
00136     }
00137 }
00138 
00139 int main(){
00140     t.start(callback(&queue, &EventQueue::dispatch_forever));
00141     BSP_ACCELERO_Init();
00142     
00143     // Try to mount the filesystem
00144     printf("Mounting the filesystem... ");
00145     fflush(stdout);
00146     int err = fs.mount(&bd);
00147     printf("%s\n", (err ? "Fail :(" : "OK"));
00148     if (err) {
00149         // Reformat if we can't mount the filesystem
00150         // this should only happen on the first boot
00151         printf("No filesystem found, formatting... ");
00152         fflush(stdout);
00153         err = fs.reformat(&bd);
00154         printf("%s\n", (err ? "Fail :(" : "OK"));
00155         if (err) {
00156             error("error: %s (%d)\n", strerror(-err), err);
00157         }
00158     }
00159     
00160     // Open the numbers file
00161     printf("Opening \"/fs/numbers.txt\"... ");
00162     fflush(stdout);
00163     f = fopen("/fs/numbers.txt", "r+");
00164     printf("%s\n", (!f ? "Fail :(" : "OK"));
00165     if (!f) {
00166         // Create the numbers file if it doesn't exist
00167         printf("No file found, creating a new file... ");
00168         fflush(stdout);
00169         f = fopen("/fs/numbers.txt", "w+");
00170         printf("%s\n", (!f ? "Fail :(" : "OK"));
00171         if (!f) {
00172             error("error: %s (%d)\n", strerror(errno), -errno);
00173         }
00174     }
00175 
00176     ticker.attach(&ticker_call, 0.01);
00177 }