Skip to content

Commit

Permalink
if we want to co-exist with SD cards we must have a differently named…
Browse files Browse the repository at this point in the history
… File type :( so we put it into a namespace
  • Loading branch information
ladyada committed Apr 27, 2019
1 parent 05a1feb commit d60ae1f
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 22 deletions.
12 changes: 8 additions & 4 deletions Adafruit_SPIFlash_FatFs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ static void build_partition(uint8_t *buf, int boot, int type, uint32_t
buf[15] = num_blocks >> 24;
}

namespace Adafruit_SPIFlash_FAT {

File::File(const char* filepath, uint8_t mode, Adafruit_SPIFlash_FatFs* fatfs):
_opened(false), _file({0}), _fileInfo({0}), _directory({0}), _dirPath(NULL),
_fatfs(fatfs)
Expand Down Expand Up @@ -243,6 +245,8 @@ void File::activate() {
}
}

} // Adafruit_SPIFlash_FAT namespace

bool Adafruit_SPIFlash_FatFs::begin() {
activate();
// Mount the filesystem.
Expand All @@ -254,9 +258,9 @@ bool Adafruit_SPIFlash_FatFs::begin() {
return true;
}

File Adafruit_SPIFlash_FatFs::open(const char *filename, uint8_t mode) {
Adafruit_SPIFlash_FAT::File Adafruit_SPIFlash_FatFs::open(const char *filename, uint8_t mode) {
activate();
return File(filename, mode, this);
return Adafruit_SPIFlash_FAT::File(filename, mode, this);
}

bool Adafruit_SPIFlash_FatFs::exists(const char *filepath) {
Expand Down Expand Up @@ -306,14 +310,14 @@ bool Adafruit_SPIFlash_FatFs::remove(const char *filepath) {
bool Adafruit_SPIFlash_FatFs::rmdir(const char *filepath) {
activate();
// Check the specified path is a directory and can be opened.
File root = open(filepath);
Adafruit_SPIFlash_FAT::File root = open(filepath);
if (!root || !root.isDirectory()) {
FATFS_DEBUG_PRINTLN("rmdir was not given a directory!");
return false;
}
// Walk through all the children, deleting if it's a file and traversing
// down to delete if a subdirectory (depth first search).
File child = root.openNextFile();
Adafruit_SPIFlash_FAT::File child = root.openNextFile();
while (child) {
// Construct full path to the child by concatenating a path separateor
// and the file name.
Expand Down
9 changes: 6 additions & 3 deletions Adafruit_SPIFlash_FatFs.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Adafruit_SPIFlash_FatFs;

#include "utility/flashdisk.h"

namespace Adafruit_SPIFlash_FAT {

#define DEBUG 0
#define DEBUG_PRINTER Serial

Expand All @@ -33,7 +35,6 @@ class Adafruit_SPIFlash_FatFs;
#define FILE_READ FA_READ
#define FILE_WRITE (FA_READ | FA_WRITE | FA_OPEN_APPEND)


class File : public Stream {
public:
File():
Expand Down Expand Up @@ -97,6 +98,8 @@ class File : public Stream {
void activate();
};

}

class Adafruit_SPIFlash_FatFs {
public:
Adafruit_SPIFlash_FatFs(Adafruit_SPIFlash& flash, int flashSectorSize,
Expand All @@ -117,8 +120,8 @@ class Adafruit_SPIFlash_FatFs {

// Functions that are similar to the Arduino SD library:
bool begin();
::File open(const char *filename, uint8_t mode = FILE_READ);
::File open(const String &filename, uint8_t mode = FILE_READ) {
Adafruit_SPIFlash_FAT::File open(const char *filename, uint8_t mode = FILE_READ);
Adafruit_SPIFlash_FAT::File open(const String &filename, uint8_t mode = FILE_READ) {
return open( filename.c_str(), mode );
}
bool exists(const char *filepath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ boolean moveFile(char *file, char *dest) {

pixel.setPixelColor(0, pixel.Color(100,100,0)); pixel.show();

File source = pythonfs.open(file, FILE_READ);
File backup = pythonfs.open(dest, FILE_WRITE);
Adafruit_SPIFlash_FAT::File source = pythonfs.open(file, FILE_READ);
Adafruit_SPIFlash_FAT::File backup = pythonfs.open(dest, FILE_WRITE);
Serial.println("Making backup!");
Serial.println("\n---------------------\n");

Expand Down Expand Up @@ -171,4 +171,4 @@ void error(uint8_t i) {
}
delay(1000);
}
}
}
8 changes: 4 additions & 4 deletions examples/fatfs_circuitpython/fatfs_circuitpython.ino
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void setup() {

// Check if a boot.py exists and print it out.
if (pythonfs.exists("boot.py")) {
File bootPy = pythonfs.open("boot.py", FILE_READ);
Adafruit_SPIFlash_FAT::File bootPy = pythonfs.open("boot.py", FILE_READ);
Serial.println("Printing boot.py...");
while (bootPy.available()) {
char c = bootPy.read();
Expand All @@ -98,7 +98,7 @@ void setup() {

// Check if a main.py exists and print it out:
if (pythonfs.exists("main.py")) {
File mainPy = pythonfs.open("main.py", FILE_READ);
Adafruit_SPIFlash_FAT::File mainPy = pythonfs.open("main.py", FILE_READ);
Serial.println("Printing main.py...");
while (mainPy.available()) {
char c = mainPy.read();
Expand All @@ -113,7 +113,7 @@ void setup() {
// 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);
Adafruit_SPIFlash_FAT::File data = pythonfs.open("data.txt", FILE_WRITE);
if (data) {
// Write a new line to the file:
data.println("Hello CircuitPython from Arduino!");
Expand All @@ -132,4 +132,4 @@ void setup() {
void loop() {
// Nothing to do in the loop.
delay(100);
}
}
2 changes: 1 addition & 1 deletion examples/fatfs_datalogging/fatfs_datalogging.ino
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void setup() {
void loop() {
// Open the datalogging file for writing. The FILE_WRITE mode will open
// the file for appending, i.e. it will add new data to the end of the file.
File dataFile = fatfs.open(FILE_NAME, FILE_WRITE);
Adafruit_SPIFlash_FAT::File dataFile = fatfs.open(FILE_NAME, FILE_WRITE);
// Check that the file opened successfully and write a line to it.
if (dataFile) {
// Take a new data reading from a sensor, etc. For this example just
Expand Down
10 changes: 5 additions & 5 deletions examples/fatfs_full_usage/fatfs_full_usage.ino
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ void setup() {
// using the FatFs f_open modes directly (which can be specified instead
// of FILE_WRITE), see documentation at:
// http://elm-chan.org/fsw/ff/en/open.html
File writeFile = fatfs.open("/test/test.txt", FILE_WRITE);
Adafruit_SPIFlash_FAT::File writeFile = fatfs.open("/test/test.txt", FILE_WRITE);
if (!writeFile) {
Serial.println("Error, failed to open test.txt for writing!");
while(1);
Expand All @@ -133,7 +133,7 @@ void setup() {
Serial.println("Wrote to file /test/test.txt!");

// Now open the same file but for reading.
File readFile = fatfs.open("/test/test.txt", FILE_READ);
Adafruit_SPIFlash_FAT::File readFile = fatfs.open("/test/test.txt", FILE_READ);
if (!readFile) {
Serial.println("Error, failed to open test.txt for reading!");
while(1);
Expand Down Expand Up @@ -175,7 +175,7 @@ void setup() {

// You can open a directory to list all the children (files and directories).
// Just like the SD library the File type represents either a file or directory.
File testDir = fatfs.open("/test");
Adafruit_SPIFlash_FAT::File testDir = fatfs.open("/test");
if (!testDir) {
Serial.println("Error, failed to open test directory!");
while(1);
Expand All @@ -185,7 +185,7 @@ void setup() {
while(1);
}
Serial.println("Listing children of directory /test:");
File child = testDir.openNextFile();
Adafruit_SPIFlash_FAT::File child = testDir.openNextFile();
while (child) {
// Print the file name and mention if it's a directory.
Serial.print("- "); Serial.print(child.name());
Expand All @@ -207,7 +207,7 @@ void setup() {

// Delete a file with the remove command. For example create a test2.txt file
// inside /test/foo and then delete it.
File test2File = fatfs.open("/test/foo/test2.txt", FILE_WRITE);
Adafruit_SPIFlash_FAT::File test2File = fatfs.open("/test/foo/test2.txt", FILE_WRITE);
test2File.close();
Serial.println("Deleting /test/foo/test2.txt...");
if (!fatfs.remove("/test/foo/test2.txt")) {
Expand Down
2 changes: 1 addition & 1 deletion examples/fatfs_print_file/fatfs_print_file.ino
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void setup() {

// 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);
Adafruit_SPIFlash_FAT::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.
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Adafruit SPIFlash
version=1.2.0
version=2.0.0
author=Adafruit
maintainer=Adafruit <[email protected]>
sentence=SPI Flash filesystem support for FAT and CircuitPython FS support from within Arduino
Expand Down

0 comments on commit d60ae1f

Please sign in to comment.