Marco Calzana / 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 
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 "nvstore.h"
00025 
00026 #include "stm32l475e_iot01_accelero.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(4096, 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 // Set up the button to trigger an erase
00046 InterruptIn irq(BUTTON1);
00047 void erase() {
00048     printf("Initializing the block device... ");
00049     fflush(stdout);
00050     int err = bd.init();
00051     printf("%s\n", (err ? "Fail :(" : "OK"));
00052     if (err) {
00053         error("error: %s (%d)\n", strerror(-err), err);
00054     }
00055 
00056     printf("Erasing the block device... ");
00057     fflush(stdout);
00058     err = bd.erase(0, bd.size());
00059     printf("%s\n", (err ? "Fail :(" : "OK"));
00060     if (err) {
00061         error("error: %s (%d)\n", strerror(-err), err);
00062     }
00063 
00064     printf("Deinitializing the block device... ");
00065     fflush(stdout);
00066     err = bd.deinit();
00067     printf("%s\n", (err ? "Fail :(" : "OK"));
00068     if (err) {
00069         error("error: %s (%d)\n", strerror(-err), err);
00070     }
00071 }
00072 
00073 
00074 // Entry point for the example
00075 
00076 Ticker toggle_write_to_file;
00077 EventQueue queue(16 * EVENTS_EVENT_SIZE);
00078 Thread t;
00079 static FILE *f;
00080 volatile int counter = 0;
00081 time_t begin = time(NULL);
00082 
00083 
00084 
00085 void writeAccToFile(){
00086     int16_t pDataXYZ[3] = {0};
00087     BSP_ACCELERO_AccGetXYZ(pDataXYZ);
00088     int x = abs(pDataXYZ[0]);
00089     int y = abs(pDataXYZ[1]);
00090     int z = abs(pDataXYZ[2]);
00091     char c;
00092     if(z > 950 && z < 1050){
00093         c = 'z';
00094     } else if(y > 950 && y < 1050){
00095         c = 'y'; 
00096     }else if(x > 950 && x < 1050){
00097         c = 'x';   
00098     }else{
00099         c = 'o';
00100     }
00101     fprintf(f, "%c", c);
00102     fflush(stdout); 
00103 }
00104 
00105 void readNVStoreLedCount(uint16_t key){
00106     NVStore &nvstore = NVStore::get_instance();
00107     int rc;
00108     uint32_t value;
00109     uint16_t actual_len_bytes = 0;
00110     rc = nvstore.get(key, sizeof(value), &value, actual_len_bytes);
00111     if(rc == NVSTORE_NOT_FOUND){
00112         printf("Led %d turn on 0 times\n", key);
00113     }else{
00114        printf("Led %d turn on %d times\n", key, value);
00115     }
00116 }
00117 
00118 void addNVStoreLedCount(uint16_t key){
00119     NVStore &nvstore = NVStore::get_instance();
00120     int rc;
00121     uint32_t value;
00122     uint16_t actual_len_bytes = 0;
00123     rc = nvstore.get(key, sizeof(value), &value, actual_len_bytes);
00124     if(rc == NVSTORE_NOT_FOUND){
00125         value = 1;
00126     }else{
00127         value++;
00128     }
00129     rc = nvstore.set(key, sizeof(value), &value);
00130 }
00131 
00132 void readAccFile(){
00133     printf("end sampling \n");
00134     
00135     // Close the file which also flushes any cached writes
00136     printf("Closing \"/fs/numbers.txt\"... ");
00137     fflush(stdout);
00138     int err = fclose(f);
00139     printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00140     if (err < 0) {
00141         error("error: %s (%d)\n", strerror(errno), -errno);
00142     }
00143     
00144     printf("Opening \"/fs/numbers.txt\"... ");
00145     fflush(stdout);
00146     f = fopen("/fs/numbers.txt", "r");
00147     printf("%s\n", (!f ? "Fail :(" : "OK"));
00148     if (!f) {
00149         error("error: %s (%d)\n", strerror(errno), -errno);
00150     }
00151     
00152     int xCount = 0;
00153     int yCount = 0;
00154     int zCount = 0;
00155     
00156     while (!feof(f)) {
00157         char c = fgetc(f);
00158         if(c == 'x'){
00159             xCount++;
00160         }else if(c == 'y'){
00161             yCount++;
00162         }else if(c == 'z'){
00163             zCount++;
00164         }
00165     }
00166     printf("Values: x: %d, y:%d, z: %d \n", xCount, yCount, zCount);
00167     
00168     //Switch on the LED. x is default Led.
00169     if(xCount >= yCount && xCount >= zCount){
00170         uint16_t key = 3;
00171         led3 = 1;
00172         addNVStoreLedCount(key);
00173     }else if(yCount > xCount && yCount > zCount){
00174         uint16_t key = 2; 
00175         addNVStoreLedCount(key);
00176         led2 = 1;
00177     }else{
00178         uint16_t key = 1;
00179         addNVStoreLedCount(key); 
00180         led1 = 1;
00181     }
00182 
00183     printf("\rClosing \"/fs/numbers.txt\"... ");
00184     fflush(stdout);
00185     err = fclose(f);
00186     printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00187     if (err < 0) {
00188         error("error: %s (%d)\n", strerror(errno), -errno);
00189     }
00190 
00191     // Tidy up
00192     printf("Unmounting... ");
00193     fflush(stdout);
00194     err = fs.unmount();
00195     printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00196     if (err < 0) {
00197         error("error: %s (%d)\n", strerror(-err), err);
00198     } 
00199     printf("Mbed OS filesystem example done!\n");
00200     
00201 }
00202 
00203 void readSensorsInterupt() {
00204     queue.call(writeAccToFile);
00205     counter++;
00206     if (counter == 1000) {
00207       toggle_write_to_file.detach();
00208       queue.call(readAccFile);
00209     }
00210 }
00211 
00212 int main() {
00213     // Setup the erase event on button press, use the event queue
00214     // to avoid running in interrupt context
00215     irq.fall(mbed_event_queue()->event(erase));
00216     
00217     //Settings for nvstore. 
00218     NVStore &nvstore = NVStore::get_instance();
00219     uint16_t key; 
00220     nvstore.init();
00221     printf("Init NVStore... ");
00222     
00223     printf("NVStore size is %d.\n", nvstore.size()); 
00224     printf("NVStore max number of keys is %d (out of %d possible ones in this flash configuration).\n", 
00225         nvstore.get_max_keys(), nvstore.get_max_possible_keys());
00226     printf("--- LED PREV. RESULTS---\n");
00227     key = 1;
00228     readNVStoreLedCount(key);
00229     key = 2;
00230     readNVStoreLedCount(key);
00231     key = 3;
00232     readNVStoreLedCount(key);
00233 
00234     // Try to mount the filesystem
00235     printf("Mounting the filesystem... ");
00236     fflush(stdout);
00237     int err = fs.mount(&bd);
00238     printf("%s\n", (err ? "Fail :(" : "OK"));
00239     if (err) {
00240         // Reformat if we can't mount the filesystem
00241         // this should only happen on the first boot
00242         printf("No filesystem found, formatting... ");
00243         fflush(stdout);
00244         err = fs.reformat(&bd);
00245         printf("%s\n", (err ? "Fail :(" : "OK"));
00246         if (err) {
00247             error("error: %s (%d)\n", strerror(-err), err);
00248         }
00249     }
00250 
00251     // Open the numbers file
00252     printf("Opening \"/fs/numbers.txt\"... ");
00253     fflush(stdout);
00254     f = fopen("/fs/numbers.txt", "r +");
00255     printf("%s\n", (!f ? "Fail :(" : "OK"));
00256     if (!f) {
00257         // Create the numbers file if it doesn't exist
00258         printf("No file found, creating a new file... ");
00259         fflush(stdout);
00260         f = fopen("/fs/numbers.txt", "w+");
00261         printf("%s\n", (!f ? "Fail :(" : "OK"));
00262         if (!f) {
00263             error("error: %s (%d)\n", strerror(errno), -errno);
00264         }
00265         
00266         printf("Seeking file... ");
00267         fflush(stdout);
00268         err = fseek(f, 0, SEEK_SET);
00269         printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00270         if (err < 0) {
00271             error("error: %s (%d)\n", strerror(errno), -errno);
00272         }
00273     }
00274     
00275     t.start(callback(&queue, &EventQueue::dispatch_forever));
00276     BSP_ACCELERO_Init();
00277     led1 = 0;
00278     led2 = 0;
00279     led3 = 0;
00280     printf("Start sampling... \n");
00281     begin = time(NULL);
00282     toggle_write_to_file.attach(&readSensorsInterupt, 0.01);
00283 }