c

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 void getDataFromSensors() {
00080     int16_t pDataXYZ[3] = {0};
00081     BSP_ACCELERO_AccGetXYZ(pDataXYZ);
00082     
00083     bool isHorizontal = pDataXYZ[2] > 900 || pDataXYZ[2] < -900;
00084     bool isOnLongEdge = pDataXYZ[1] > 900 || pDataXYZ[1] < -900;
00085     bool isOnShortEdge = pDataXYZ[0] > 900 || pDataXYZ[0] < -900;
00086     
00087     if(isHorizontal) {
00088         fprintf(file, "%d\n", 1);
00089     } else if(isOnLongEdge) {
00090         fprintf(file, "%d\n", 2);
00091     } else if(isOnShortEdge) {
00092         fprintf(file, "%d\n", 3);
00093     } else {
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     
00122     bool isLed1 = led1 >= led2 && led1 >= led3;
00123     bool isLed2 = led2 >= led1 && led2 >= led3;
00124     bool isLed3 = led3 >= led1 && led3 >= led2;
00125 
00126     if(isLed1) {
00127         led1 = 1;
00128         led2 = 0;
00129         led3 = 0;
00130     } else if (isLed2) {
00131         led1 = 0;
00132         led2 = 1;
00133         led3 = 0;
00134     } else if (isLed3) {
00135         led1 = 0;
00136         led2 = 0;
00137         led3 = 1;
00138     }
00139     
00140     fflush(stdout);
00141     int err = fclose(file);
00142     printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00143     if (err < 0) {
00144       error("error: %s (%d)\n", strerror(err), -err);
00145     }
00146     err = fs.unmount();
00147     printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00148     if (err < 0) {
00149         error("error: %s (%d)\n", strerror(-err), err);
00150     }
00151       
00152     printf("Mbed OS filesystem example done!\n"); 
00153 }
00154 
00155 void toggleLed() {
00156     queue.call(getDataFromSensors);
00157     counter += 1;
00158     if(counter == 1000) {
00159         toggle_led.detach();
00160         queue.call(readFile);
00161     }
00162 }
00163 
00164 
00165 // Entry point for the example
00166 int main() {
00167     thread.start(callback(&queue, &EventQueue::dispatch_forever));
00168     BSP_ACCELERO_Init();
00169     printf("--- Mbed OS filesystem example ---\n");
00170 
00171     // Setup the erase event on button press, use the event queue
00172     // to avoid running in interrupt context
00173     irq.fall(mbed_event_queue()->event(erase));
00174 
00175     // Try to mount the filesystem
00176     printf("Mounting the filesystem... ");
00177     fflush(stdout);
00178     int err = fs.mount(&bd);
00179     printf("%s\n", (err ? "Fail :(" : "OK"));
00180     if (err) {
00181         // Reformat if we can't mount the filesystem
00182         // this should only happen on the first boot
00183         printf("No filesystem found, formatting... ");
00184         fflush(stdout);
00185         err = fs.reformat(&bd);
00186         printf("%s\n", (err ? "Fail :(" : "OK"));
00187         if (err) {
00188             error("error: %s (%d)\n", strerror(-err), err);
00189         }
00190     }
00191 
00192     // Open the numbers file
00193     printf("Opening \"/fs/numbers.txt\"... ");
00194     fflush(stdout);
00195     file = fopen("/fs/numbers.txt", "r+");
00196     printf("%s\n", (!file ? "Fail :(" : "OK"));
00197     if (!file) {
00198         // Create the numbers file if it doesn't exist
00199         printf("No file found, creating a new file... ");
00200         fflush(stdout);
00201         file = fopen("/fs/numbers.txt", "w+");
00202         printf("%s\n", (!file ? "Fail :(" : "OK"));
00203         if (!file) {
00204             error("error: %s (%d)\n", strerror(errno), -errno);
00205         }
00206     }
00207     
00208     toggle_led.attach(&toggleLed, 0.01);
00209     
00210 
00211 }