Datasheet
Again the open function is used but this time it's told to open the file for writing. In this mode the file will be opened for
appending (i.e. data added to the end of it) if it exists, or it will be created if it doesn't exist. Once the file is open print
functions like print and println can be used to write data to the file (just like writing to the serial monitor). Be sure to
close the file when finished writing!
That's all there is to basic file reading and writing. Check out the fatfs_full_usage example for examples of even more
functions like creating directories, deleting files & directories, checking the size of files, and more! Remember though
to interact with CircuitPython files you need to use the Adafruit_Feather_M0_CircuitPython class as shown in the
fatfs_circuitpython example above!
Format Flash Memory
The fatfs_format example will format the SPI flash with a new blank filesystem. Be warned this sketch will delete all
data on the flash memory, including any Python code or other data you might have stored! The format sketch is
useful if you'd like to wipe everything away and start fresh, or to help get back in a good state if the memory should
get corrupted for some reason.
Be aware too the fatfs_format and examples below are not compatible with a CircuitPython-formatted flash chip! If
you need to share data between Arduino & CircuitPython check out the fatfs_circuitpython example above.
To run the format sketch load it in the Arduino IDE and upload it to the M0 board. Then open the serial monitor at
115200 baud. You should see the serial monitor display a message asking you to confirm formatting the flash. If you
don't see this message then close the serial monitor, press the board's reset button, and open the serial monitor again.
// Create or append to a data.txt file and add a new line
// to the end of it. CircuitPython code can later open and
// see this file too!
File data = pythonfs.open("data.txt", FILE_WRITE);
if (data) {
// Write a new line to the file:
data.println("Hello CircuitPython from Arduino!");
data.close();
// See the other fatfs examples like fatfs_full_usage and fatfs_datalogging
// for more examples of interacting with files.
Serial.println("Wrote a new line to the end of data.txt!");
}
else {
Serial.println("Error, failed to open data file for writing!");
}
© Adafruit Industries
https://learn.adafruit.com/adafruit-feather-m0-express-designed-for-circuit-python-
circuitpython
Page 46 of 199