Datasheet
however instead of calling open on a global SD card object you're calling it on a fatfs object created earlier in the
sketch (look at the top after the #define configuration values).
Once the file is opened it's simply a matter of calling print and println functions on the file object to write data inside of
it. This is just like writing data to the serial monitor and you can print out text, numeric, and other types of data. Be
sure to close the file when you're done writing to ensure the data is stored correctly!
Reading and Printing Files
The fatfs_print_file example will open a file (by default the data.csv file created by running the fatfs_datalogging
example above) and print all of its contents to the serial monitor. Open the fatfs_print_file example and load it on your
Feather M0 board, then open the serial monitor at 115200 baud. You should see the sketch print out the contents of
data.csv (if you don't have a file called data.csv on the flash look at running the datalogging example above first).
To understand how to read data from a file look in the setup function of the sketch:
Just like when writing data with the datalogging example you create a File object by calling the open function on a
fatfs object. This time however you pass a file mode of FILE_READ which tells the filesystem you want to read data.
After you open a file for reading you can easily check if data is available by calling the available function on the file,
and then read a single character with the read function. This makes it easy to loop through all of the data in a file by
checking if it's available and reading a character at a time. However there are more advanced read functions you can
use too--see the fatfs_full_usage example or even the Arduino SD library documentation (https://adafru.it/ucu) (the SPI
flash library implements the same functions).
Full Usage Example
For a more complete demonstration of reading and writing files look at the fatfs_full_usage example. This examples
uses every function in the library and demonstrates things like checking for the existence of a file, creating directories,
deleting files, deleting directories, and more.
Remember the SPI flash library is built to have the same functions and interface as the Arduino SD
library (https://adafru.it/ucu) so if you have code or examples that store data on a SD card they should be easy to adapt
to use the SPI flash library, just create a fatfs object like in the examples above and use its open function instead of the
global SD object's open function. Once you have a reference to a file all of the functions and usage should be the
same between the SPI flash and SD libraries!
// Open the file for reading and check that it was successfully opened.
// The FILE_READ mode will open the file for reading.
File dataFile = fatfs.open(FILE_NAME, FILE_READ);
if (dataFile) {
// File was opened, now print out data character by character until at the
// end of the file.
Serial.println("Opened file, printing contents below:");
while (dataFile.available()) {
// Use the read function to read the next character.
// You can alternatively use other functions like readUntil, readString, etc.
// See the fatfs_full_usage example for more details.
char c = dataFile.read();
Serial.print(c);
}
}
© Adafruit Industries
https://learn.adafruit.com/adafruit-feather-m0-express-designed-for-circuit-python-
circuitpython
Page 48 of 199