Luca Mottola / Mbed OS IOTAtelier1819-FileSystem

Dependents:   TemperatureButtonFile AccelleratorRead

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 
00025 // Physical block device, can be any device that supports the BlockDevice API
00026 /*SPIFBlockDevice bd(
00027         MBED_CONF_SPIF_DRIVER_SPI_MOSI,
00028         MBED_CONF_SPIF_DRIVER_SPI_MISO,
00029         MBED_CONF_SPIF_DRIVER_SPI_CLK,
00030         MBED_CONF_SPIF_DRIVER_SPI_CS);*/
00031 
00032 #define BLOCK_SIZE 512
00033 HeapBlockDevice bd(2048, BLOCK_SIZE);
00034 
00035 // File system declaration
00036 LittleFileSystem fs("fs");
00037 
00038 // Set up the button to trigger an erase
00039 InterruptIn irq(BUTTON1);
00040 void erase() {
00041     printf("Initializing the block device... ");
00042     fflush(stdout);
00043     int err = bd.init();
00044     printf("%s\n", (err ? "Fail :(" : "OK"));
00045     if (err) {
00046         error("error: %s (%d)\n", strerror(-err), err);
00047     }
00048 
00049     printf("Erasing the block device... ");
00050     fflush(stdout);
00051     err = bd.erase(0, bd.size());
00052     printf("%s\n", (err ? "Fail :(" : "OK"));
00053     if (err) {
00054         error("error: %s (%d)\n", strerror(-err), err);
00055     }
00056 
00057     printf("Deinitializing the block device... ");
00058     fflush(stdout);
00059     err = bd.deinit();
00060     printf("%s\n", (err ? "Fail :(" : "OK"));
00061     if (err) {
00062         error("error: %s (%d)\n", strerror(-err), err);
00063     }
00064 }
00065 
00066 
00067 // Entry point for the example
00068 int main() {
00069     printf("--- Mbed OS filesystem example ---\n");
00070 
00071     // Setup the erase event on button press, use the event queue
00072     // to avoid running in interrupt context
00073     irq.fall(mbed_event_queue()->event(erase));
00074 
00075     // Try to mount the filesystem
00076     printf("Mounting the filesystem... ");
00077     fflush(stdout);
00078     int err = fs.mount(&bd);
00079     printf("%s\n", (err ? "Fail :(" : "OK"));
00080     if (err) {
00081         // Reformat if we can't mount the filesystem
00082         // this should only happen on the first boot
00083         printf("No filesystem found, formatting... ");
00084         fflush(stdout);
00085         err = fs.reformat(&bd);
00086         printf("%s\n", (err ? "Fail :(" : "OK"));
00087         if (err) {
00088             error("error: %s (%d)\n", strerror(-err), err);
00089         }
00090     }
00091 
00092     // Open the numbers file
00093     printf("Opening \"/fs/numbers.txt\"... ");
00094     fflush(stdout);
00095     FILE *f = fopen("/fs/numbers.txt", "r+");
00096     printf("%s\n", (!f ? "Fail :(" : "OK"));
00097     if (!f) {
00098         // Create the numbers file if it doesn't exist
00099         printf("No file found, creating a new file... ");
00100         fflush(stdout);
00101         f = fopen("/fs/numbers.txt", "w+");
00102         printf("%s\n", (!f ? "Fail :(" : "OK"));
00103         if (!f) {
00104             error("error: %s (%d)\n", strerror(errno), -errno);
00105         }
00106 
00107         for (int i = 0; i < 10; i++) {
00108             printf("\rWriting numbers (%d/%d)... ", i, 10);
00109             fflush(stdout);
00110             err = fprintf(f, "    %d\n", i);
00111             if (err < 0) {
00112                 printf("Fail :(\n");
00113                 error("error: %s (%d)\n", strerror(errno), -errno);
00114             }
00115         }
00116         printf("\rWriting numbers (%d/%d)... OK\n", 10, 10);
00117 
00118         printf("Seeking file... ");
00119         fflush(stdout);
00120         err = fseek(f, 0, SEEK_SET);
00121         printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00122         if (err < 0) {
00123             error("error: %s (%d)\n", strerror(errno), -errno);
00124         }
00125     }
00126 
00127     // Go through and increment the numbers
00128     for (int i = 0; i < 10; i++) {
00129         printf("\rIncrementing numbers (%d/%d)... ", i, 10);
00130         fflush(stdout);
00131 
00132         // Get current stream position
00133         long pos = ftell(f);
00134 
00135         // Parse out the number and increment
00136         int32_t number;
00137         fscanf(f, "%d", &number);
00138         number += 1;
00139 
00140         // Seek to beginning of number
00141         fseek(f, pos, SEEK_SET);
00142     
00143         // Store number
00144         fprintf(f, "    %d\n", number);
00145 
00146         // Flush between write and read on same file
00147         fflush(f);
00148     }
00149     printf("\rIncrementing numbers (%d/%d)... OK\n", 10, 10);
00150 
00151     // Close the file which also flushes any cached writes
00152     printf("Closing \"/fs/numbers.txt\"... ");
00153     fflush(stdout);
00154     err = fclose(f);
00155     printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00156     if (err < 0) {
00157         error("error: %s (%d)\n", strerror(errno), -errno);
00158     }
00159     
00160     // Display the root directory
00161     printf("Opening the root directory... ");
00162     fflush(stdout);
00163     DIR *d = opendir("/fs/");
00164     printf("%s\n", (!d ? "Fail :(" : "OK"));
00165     if (!d) {
00166         error("error: %s (%d)\n", strerror(errno), -errno);
00167     }
00168 
00169     printf("root directory:\n");
00170     while (true) {
00171         struct dirent *e = readdir(d);
00172         if (!e) {
00173             break;
00174         }
00175 
00176         printf("    %s\n", e->d_name);
00177     }
00178 
00179     printf("Closing the root directory... ");
00180     fflush(stdout);
00181     err = closedir(d);
00182     printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00183     if (err < 0) {
00184         error("error: %s (%d)\n", strerror(errno), -errno);
00185     }
00186 
00187     // Display the numbers file
00188     printf("Opening \"/fs/numbers.txt\"... ");
00189     fflush(stdout);
00190     f = fopen("/fs/numbers.txt", "r");
00191     printf("%s\n", (!f ? "Fail :(" : "OK"));
00192     if (!f) {
00193         error("error: %s (%d)\n", strerror(errno), -errno);
00194     }
00195 
00196     printf("numbers:\n");
00197     while (!feof(f)) {
00198         int c = fgetc(f);
00199         printf("%c", c);
00200     }
00201 
00202     printf("\rClosing \"/fs/numbers.txt\"... ");
00203     fflush(stdout);
00204     err = fclose(f);
00205     printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00206     if (err < 0) {
00207         error("error: %s (%d)\n", strerror(errno), -errno);
00208     }
00209 
00210     // Tidy up
00211     printf("Unmounting... ");
00212     fflush(stdout);
00213     err = fs.unmount();
00214     printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00215     if (err < 0) {
00216         error("error: %s (%d)\n", strerror(-err), err);
00217     }
00218         
00219     printf("Mbed OS filesystem example done!\n");
00220 }
00221