Luigi Frunzio / Mbed OS AccelleratorReadNV

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