Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of gr-peach-opencv-project-sd-card by
example1.cpp
00001 #include "mbed.h" 00002 #include "FATFileSystem.h" 00003 #include "SDBlockDevice.h" 00004 #include <stdio.h> 00005 #include <errno.h> 00006 /* mbed_retarget.h is included after errno.h so symbols are mapped to 00007 * consistent values for all toolchains */ 00008 #include "platform/mbed_retarget.h" 00009 00010 00011 SDBlockDevice sd(MBED_CONF_APP_SPI_MOSI, MBED_CONF_APP_SPI_MISO, MBED_CONF_APP_SPI_CLK, MBED_CONF_APP_SPI_CS); 00012 FATFileSystem fs("sd", &sd); 00013 00014 void return_error(int ret_val){ 00015 if (ret_val) 00016 printf("Failure. %d\n", ret_val); 00017 else 00018 printf("done.\n"); 00019 } 00020 00021 void errno_error(void* ret_val){ 00022 if (ret_val == NULL) 00023 printf(" Failure. %d \n", errno); 00024 else 00025 printf(" done.\n"); 00026 } 00027 00028 int main() 00029 { 00030 int error = 0; 00031 printf("Welcome to the filesystem example.\n"); 00032 00033 printf("Opening a new file, numbers.txt."); 00034 FILE* fd = fopen("/sd/numbers.txt", "w+"); 00035 errno_error(fd); 00036 00037 for (int i = 0; i < 20; i++){ 00038 printf("Writing decimal numbers to a file (%d/20)\r", i); 00039 fprintf(fd, "%d\n", i); 00040 } 00041 printf("Writing decimal numbers to a file (20/20) done.\n"); 00042 00043 printf("Closing file."); 00044 fclose(fd); 00045 printf(" done.\n"); 00046 00047 printf("Re-opening file read-only."); 00048 fd = fopen("/sd/numbers.txt", "r"); 00049 errno_error(fd); 00050 00051 printf("Dumping file to screen.\n"); 00052 char buff[16] = {0}; 00053 while (!feof(fd)){ 00054 int size = fread(&buff[0], 1, 15, fd); 00055 fwrite(&buff[0], 1, size, stdout); 00056 } 00057 printf("EOF.\n"); 00058 00059 printf("Closing file."); 00060 fclose(fd); 00061 printf(" done.\n"); 00062 00063 printf("Opening root directory."); 00064 DIR* dir = opendir("/sd/"); 00065 errno_error(fd); 00066 00067 struct dirent* de; 00068 printf("Printing all filenames:\n"); 00069 while((de = readdir(dir)) != NULL){ 00070 printf(" %s\n", &(de->d_name)[0]); 00071 } 00072 00073 printf("Closeing root directory. "); 00074 error = closedir(dir); 00075 return_error(error); 00076 printf("Filesystem Demo complete.\n"); 00077 00078 while (true) {} 00079 } 00080
Generated on Tue Jul 12 2022 14:46:35 by
1.7.2
