Luigi Frunzio / Mbed OS AccelleratorRead

Dependencies:   IOTAtelier1819-FileSystem 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 
00006 // Block devices
00007 #if COMPONENT_SPIF
00008 #include "SPIFBlockDevice.h"
00009 #endif
00010 
00011 #if COMPONENT_DATAFLASH
00012 #include "DataFlashBlockDevice.h"
00013 #endif 
00014 
00015 #if COMPONENT_SD
00016 #include "SDBlockDevice.h"
00017 #endif 
00018 
00019 #include "HeapBlockDevice.h"
00020 
00021 // File systems
00022 #include "LittleFileSystem.h"
00023 #include "FATFileSystem.h"
00024 #include "stm32l475e_iot01_accelero.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 toggle_led_ticker;
00037 
00038 EventQueue queue(32 * EVENTS_EVENT_SIZE);
00039 Thread t;
00040 
00041 #define BLOCK_SIZE 512
00042 HeapBlockDevice bd(16384, BLOCK_SIZE);
00043 
00044 // File system declaration
00045 LittleFileSystem fs("fs");
00046 
00047 // Set up the button to trigger an erase
00048 InterruptIn irq(BUTTON1);
00049 void erase() {
00050     printf("Initializing the block device... ");
00051     fflush(stdout);
00052     int err = bd.init();
00053     printf("%s\n", (err ? "Fail :(" : "OK"));
00054     if (err) {
00055         error("error: %s (%d)\n", strerror(-err), err);
00056     }
00057 
00058     printf("Erasing the block device... ");
00059     fflush(stdout);
00060     err = bd.erase(0, bd.size());
00061     printf("%s\n", (err ? "Fail :(" : "OK"));
00062     if (err) {
00063         error("error: %s (%d)\n", strerror(-err), err);
00064     }
00065 
00066     printf("Deinitializing the block device... ");
00067     fflush(stdout);
00068     err = bd.deinit();
00069     printf("%s\n", (err ? "Fail :(" : "OK"));
00070     if (err) {
00071         error("error: %s (%d)\n", strerror(-err), err);
00072     }
00073 }
00074 
00075 static FILE *f;
00076 volatile int counter = 0;
00077 
00078 void get_data_from_sensors() {
00079     int16_t pDataXYZ[3] = {0};
00080     BSP_ACCELERO_AccGetXYZ(pDataXYZ);
00081     if (abs(pDataXYZ[0]) > 900) {
00082             fprintf(f, "%d\n", 1);
00083         } else if (abs(pDataXYZ[1]) > 900) {
00084             fprintf(f, "%d\n", 2);
00085         } else if (abs(pDataXYZ[2]) > 900) {
00086              fprintf(f, "%d\n", 3);
00087         } else {
00088              fprintf(f, "%d\n", -1);
00089         }
00090     fflush(f);
00091     fflush(stdout);
00092 }
00093 
00094 void read_data_from_file() {
00095       int led1 = 0;
00096       int led2 = 0;
00097       int led3 = 0;
00098       fflush(stdout);
00099       fflush(f);
00100       
00101       fseek(f, 0, SEEK_SET);
00102       int number;
00103       while (!feof(f)) {
00104         fscanf(f, "%d", &number); 
00105         if (number == 1) {
00106             led3 +=1;
00107         } else if(number == 2) {
00108             led2 +=1;
00109         } else if(number == 3) {
00110             led1 +=1;
00111         }
00112       }
00113       
00114       // Z Horizontal LED1, X short LED3, Y long LED2
00115       if (led1 >= led2 && led1 >= led3) {
00116             led_1 = 1;
00117             led_2 = 0;
00118             led_3 = 0;
00119         } else if (led2 >= led1 && led2 >= led3 ) {
00120             led_1 = 0;
00121             led_2 = 1;
00122             led_3 = 0;
00123         } else if (led3 >= led1 && led3 >= led2) {
00124             led_1 = 0;
00125             led_2 = 0;
00126             led_3 = 1;
00127         }
00128       
00129       fflush(stdout);
00130       int err = fclose(f);
00131       printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00132       if (err < 0) {
00133         error("error: %s (%d)\n", strerror(err), -err);
00134       }
00135       err = fs.unmount();
00136       printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00137       if (err < 0) {
00138           error("error: %s (%d)\n", strerror(-err), err);
00139       }
00140         
00141       printf("Mbed OS filesystem example done!\n");
00142     
00143 }
00144 
00145 void toggle_led() {
00146     queue.call(get_data_from_sensors);
00147     counter++;
00148     if (counter == 100*10) {
00149       toggle_led_ticker.detach();
00150       queue.call(read_data_from_file);
00151     }
00152       
00153 }
00154 
00155 // Entry point for the example
00156 int main() {
00157     t.start(callback(&queue, &EventQueue::dispatch_forever));
00158     BSP_ACCELERO_Init();
00159     
00160     
00161     printf("--- Mbed OS filesystem example ---\n");
00162 
00163     // Setup the erase event on button press, use the event queue
00164     // to avoid running in interrupt context
00165     irq.fall(mbed_event_queue()->event(erase));
00166 
00167     // Try to mount the filesystem
00168     printf("Mounting the filesystem... ");
00169     fflush(stdout);
00170     int err = fs.mount(&bd);
00171     printf("%s\n", (err ? "Fail :(" : "OK"));
00172     if (err) {
00173         // Reformat if we can't mount the filesystem
00174         // this should only happen on the first boot
00175         printf("No filesystem found, formatting... ");
00176         fflush(stdout);
00177         err = fs.reformat(&bd);
00178         printf("%s\n", (err ? "Fail :(" : "OK"));
00179         if (err) {
00180             error("error: %s (%d)\n", strerror(-err), err);
00181         }
00182     }
00183 
00184     // Open the numbers file
00185     printf("Opening \"/fs/numbers.txt\"... ");
00186     fflush(stdout);
00187     f = fopen("/fs/numbers.txt", "r +");
00188     printf("%s\n", (!f ? "Fail :(" : "OK"));
00189     if (!f) {
00190         // Create the numbers file if it doesn't exist
00191         printf("No file found, creating a new file... ");
00192         fflush(stdout);
00193         f = fopen("/fs/numbers.txt", "w+");
00194         printf("%s\n", (!f ? "Fail :(" : "OK"));
00195         if (!f) {
00196             error("error: %s (%d)\n", strerror(errno), -errno);
00197         }
00198 
00199      //   for (int i = 0; i < 10; i++) {
00200 //            printf("\rWriting numbers (%d/%d)... ", i, 10);
00201 //            fflush(stdout);
00202 //            err = fprintf(f, "    %d\n", i);
00203 //            if (err < 0) {
00204 //                printf("Fail :(\n");
00205 //                error("error: %s (%d)\n", strerror(errno), -errno);
00206 //            }
00207 //        }
00208 //        printf("\rWriting numbers (%d/%d)... OK\n", 10, 10);
00209 
00210         printf("Seeking file... ");
00211         fflush(stdout);
00212         err = fseek(f, 0, SEEK_SET);
00213         printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00214         if (err < 0) {
00215             error("error: %s (%d)\n", strerror(errno), -errno);
00216         }
00217     }
00218     
00219     // Go through and record the acc
00220     toggle_led_ticker.attach(&toggle_led, 0.01);
00221 
00222     
00223 //    for (int i = 0; i < 10; i++) {
00224 //        printf("\rIncrementing numbers (%d/%d)... ", i, 10);
00225 //        fflush(stdout);
00226 //
00227 //        // Get current stream position
00228 //        long pos = ftell(f);
00229 //
00230 //        // Parse out the number and increment
00231 //        int32_t number;
00232 //        fscanf(f, "%d", &number);
00233 //        number += 1;
00234 //
00235 //        // Seek to beginning of number
00236 //        fseek(f, pos, SEEK_SET);
00237 //    
00238 //        // Store number
00239 //        fprintf(f, "    %d\n", number);
00240 //
00241 //        // Flush between write and read on same file
00242 //        fflush(f);
00243 //    }
00244 //    printf("\rIncrementing numbers (%d/%d)... OK\n", 10, 10);
00245 
00246     // Close the file which also flushes any cached writes
00247     //printf("Closing \"/fs/numbers.txt\"... ");
00248 //    fflush(stdout);
00249 //    err = fclose(f);
00250 //    printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00251 //    if (err < 0) {
00252 //        error("error: %s (%d)\n", strerror(errno), -errno);
00253 //    }
00254 //    
00255 //    // Display the root directory
00256 //    printf("Opening the root directory... ");
00257 //    fflush(stdout);
00258 //    DIR *d = opendir("/fs/");
00259 //    printf("%s\n", (!d ? "Fail :(" : "OK"));
00260 //    if (!d) {
00261 //        error("error: %s (%d)\n", strerror(errno), -errno);
00262 //    }
00263 //
00264 //    printf("root directory:\n");
00265 //    while (true) {
00266 //        struct dirent *e = readdir(d);
00267 //        if (!e) {
00268 //            break;
00269 //        }
00270 //
00271 //        printf("    %s\n", e->d_name);
00272 //    }
00273 //
00274 //    printf("Closing the root directory... ");
00275 //    fflush(stdout);
00276 //    err = closedir(d);
00277 //    printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00278 //    if (err < 0) {
00279 //        error("error: %s (%d)\n", strerror(errno), -errno);
00280 //    }
00281 //
00282 //    // Display the numbers file
00283 //    printf("Opening \"/fs/numbers.txt\"... ");
00284 //    fflush(stdout);
00285 //    f = fopen("/fs/numbers.txt", "r");
00286 //    printf("%s\n", (!f ? "Fail :(" : "OK"));
00287 //    if (!f) {
00288 //        error("error: %s (%d)\n", strerror(errno), -errno);
00289 //    }
00290 //
00291 //    printf("numbers:\n");
00292 //    while (!feof(f)) {
00293 //        int c = fgetc(f);
00294 //        printf("%c", c);
00295 //    }
00296 //
00297 //    printf("\rClosing \"/fs/numbers.txt\"... ");
00298 //    fflush(stdout);
00299 //    err = fclose(f);
00300 //    printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00301 //    if (err < 0) {
00302 //        error("error: %s (%d)\n", strerror(errno), -errno);
00303 //    }
00304 //
00305 //    // Tidy up
00306 //    printf("Unmounting... ");
00307 //    fflush(stdout);
00308 //    err = fs.unmount();
00309 //    printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00310 //    if (err < 0) {
00311 //        error("error: %s (%d)\n", strerror(-err), err);
00312 //    }
00313 //        
00314 //    printf("Mbed OS filesystem example done!\n");
00315 }
00316