A collection of examples organized from basics to advanced.
Dependencies: mbed SDFileSystem
Mbed online compiler has no facility to easily manage a lot of programs or organized them in to related folders. This makes creating an examples and sample pack difficult.
This repository contains a single main.cpp file (which does very little), and a BuildOptions.h file. Simply uncomment the example you would like to compile from the build options. Each example is wrapped in a compiler directive.
If the directive does not include a description comment, it likely does not exist yet. If you would like to contribute to the Examples project, please contact me or fork and issue a pull request.
09_Files_SD/SD_Index_File.cpp@2:17a5c34b3a79, 2019-07-09 (annotated)
- Committer:
- epremeaux
- Date:
- Tue Jul 09 02:23:18 2019 +0000
- Revision:
- 2:17a5c34b3a79
Added SD card examples. Had to roll back the MBED library to maintain SDFileSystem compatability
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
epremeaux | 2:17a5c34b3a79 | 1 | /* |
epremeaux | 2:17a5c34b3a79 | 2 | * Opens index.txt, reads a number from the file (as charactor binary value) |
epremeaux | 2:17a5c34b3a79 | 3 | * Closes that file, then creates a file name string by adding one to the index number |
epremeaux | 2:17a5c34b3a79 | 4 | * Creates and opens a new file with the file name string, and writes some text before closing. |
epremeaux | 2:17a5c34b3a79 | 5 | * |
epremeaux | 2:17a5c34b3a79 | 6 | */ |
epremeaux | 2:17a5c34b3a79 | 7 | |
epremeaux | 2:17a5c34b3a79 | 8 | |
epremeaux | 2:17a5c34b3a79 | 9 | #ifdef COMPILE_SD_Index_File |
epremeaux | 2:17a5c34b3a79 | 10 | |
epremeaux | 2:17a5c34b3a79 | 11 | #include "SDFileSystem.h" |
epremeaux | 2:17a5c34b3a79 | 12 | |
epremeaux | 2:17a5c34b3a79 | 13 | SDFileSystem sd(PB_5, PB_4, PB_3, PA_11, "sd"); // MOSI, MISO SCLK, CS, "name" |
epremeaux | 2:17a5c34b3a79 | 14 | |
epremeaux | 2:17a5c34b3a79 | 15 | int last_file_num; |
epremeaux | 2:17a5c34b3a79 | 16 | |
epremeaux | 2:17a5c34b3a79 | 17 | |
epremeaux | 2:17a5c34b3a79 | 18 | int main() { |
epremeaux | 2:17a5c34b3a79 | 19 | FILE *IndexFile = fopen("/sd/index.txt", "r"); // get the last file number from the index file |
epremeaux | 2:17a5c34b3a79 | 20 | last_file_num = fgetc(IndexFile); |
epremeaux | 2:17a5c34b3a79 | 21 | printf("Got: %i \n", last_file_num); |
epremeaux | 2:17a5c34b3a79 | 22 | fclose(IndexFile); |
epremeaux | 2:17a5c34b3a79 | 23 | last_file_num++; |
epremeaux | 2:17a5c34b3a79 | 24 | IndexFile = fopen("/sd/index.txt", "w"); // wrte the new number into the index file |
epremeaux | 2:17a5c34b3a79 | 25 | fputc(last_file_num, IndexFile); |
epremeaux | 2:17a5c34b3a79 | 26 | fclose(IndexFile); |
epremeaux | 2:17a5c34b3a79 | 27 | |
epremeaux | 2:17a5c34b3a79 | 28 | char filename[12]; |
epremeaux | 2:17a5c34b3a79 | 29 | sprintf(filename, "/sd/%i.txt", last_file_num); // build new filename to open |
epremeaux | 2:17a5c34b3a79 | 30 | |
epremeaux | 2:17a5c34b3a79 | 31 | FILE *File = fopen(filename, "w"); |
epremeaux | 2:17a5c34b3a79 | 32 | if(File == NULL) { |
epremeaux | 2:17a5c34b3a79 | 33 | printf("Could not open file to write\n"); |
epremeaux | 2:17a5c34b3a79 | 34 | } |
epremeaux | 2:17a5c34b3a79 | 35 | else { |
epremeaux | 2:17a5c34b3a79 | 36 | printf("SD card file opened: %s\n", filename); |
epremeaux | 2:17a5c34b3a79 | 37 | fprintf(File, "heres some data!"); |
epremeaux | 2:17a5c34b3a79 | 38 | fclose(File); |
epremeaux | 2:17a5c34b3a79 | 39 | printf("closed\n"); |
epremeaux | 2:17a5c34b3a79 | 40 | } |
epremeaux | 2:17a5c34b3a79 | 41 | } |
epremeaux | 2:17a5c34b3a79 | 42 | |
epremeaux | 2:17a5c34b3a79 | 43 | #endif |
epremeaux | 2:17a5c34b3a79 | 44 |