ESP32 SPIFFS, Copercini, Add and Find integers in Flash
Posted: Fri Sep 15, 2017 6:05 am
I include below a variation of copercini's library, eliminating the print statements within the subroutines and using a boolean return to indicate completeness.
I have also added in two crude subroutines to add and find integers in a comma deliminated file. A position integer is linked to the value saved and a 'C' is added to the start of the position string so that it is easy to locate. While crude the add and return subroutines act like a random access storage. unfortunately the file has to be scanned through until the position is found ( or not). As I said, crude, but it works!
#include <SPIFFS.h>
// This variation is based on the work of copercini
//List Directories
// Example:Spiffs_List_Dir(SPIFFS, "/", 0);
// Lists down to number of levels required
//==================================================================
void Spiffs_List_Dir(fs::FS &fs, const char * dirname, uint8_t levels) {
Serial.printf("Listing directory: %s\n", dirname);
File root = fs.open(dirname);
if (!root) {
Serial.println("Failed to open directory");
return;
}
if (!root.isDirectory()) {
Serial.println("Not a directory");
return;
}
File file = root.openNextFile();
while (file) {
if (file.isDirectory()) {
Serial.print(" DIR : ");
Serial.println(file.name());
if (levels) {
Spiffs_List_Dir(fs, file.name(), levels - 1);
}
} else {
Serial.print(" FILE: ");
Serial.print(file.name());
Serial.print(" SIZE: ");
Serial.println(file.size());
}
file = root.openNextFile();
}
}
// Create Directory
// Example: bool y= Spiffs_Create_Dir(SPIFFS, "/mydir");
//================================================
bool Spiffs_Create_Dir(fs::FS &fs, const char * path)
{
bool x;
if (fs.mkdir(path))
{
x=true;
} else {
x=false;
}
return x;
}
// Remove Directory
// Example: bool y= Spiffs_Remove_Dir(SPIFFS, "/mydir")
//================================================
bool Spiffs_Remove_Dir(fs::FS &fs, const char * path) {
bool x;
if (fs.rmdir(path)) {
x=true;
} else {
x=false;
}
return x;
}
// Print a File from a directory
//Example: bool y=Spriffs_Print_File(SPIFFS, "/hello.txt");
//=================================================
bool Spiffs_Print_File(fs::FS &fs, const char * path) {
bool x;
File file = fs.open(path);
if (!file) {
x=false;
return x;
}
while (file.available()) {
Serial.write(file.read());
}
}
// create a File
// Example:bool y= Spiffs_Create_File(SPIFFS, "/hello.txt");
//===============================================================
bool Spiffs_Create_File(fs::FS &fs, const char * path)
{
bool x;
File file = fs.open(path, FILE_WRITE);
if (!file) {
x= false;
return x;
}
}
// Write text to a file in a directory
// Example:bool y= Spiffs_Write_File(SPIFFS, "/hello.txt", "Hello ");
//===============================================================
bool Spiffs_Write_File(fs::FS &fs, const char * path, const char * message)
{
bool x;
File file = fs.open(path, FILE_WRITE);
if (!file) {
x= false;
return x;
}
if (file.print(message)) {
x= true;
} else {
x=false;
}
return x;
}
// Append information to existing file in a directory
// Example: bool y=Spiffs_Append_File(SPIFFS, "/hello.txt", "World!\n");
//===============================================================
bool Spiffs_Append_File(fs::FS &fs, const char * path, const char * message) {
bool x;
File file = fs.open(path, FILE_APPEND);
if (!file) {
x= false;
return x;
}
if (file.print(message)) {
x=true;
} else {
x=false;
}
return x;
}
// Rename a directory
// Example: bool y=Spiffs_Rename_File(SPIFFS, "/hello.txt", "/foo.txt");
//=================================================================
bool Spiffs_Rename_File(fs::FS &fs, const char * path1, const char * path2) {
bool x;
if (fs.rename(path1, path2)) {
x = true;
} else {
x=false;
}
return x;
}
// Delete a directory
// Example: bool y= Spiffs_Delete_File(SPIFFS, "/foo.txt");
//=================================================================
bool Spiffs_Delete_File(fs::FS &fs, const char * path) {
bool x;
if (fs.remove(path)) {
x=true;
} else {
x=false;
}
return x;
}
// Write an Integer to a comma deliminated file with a position indicator.
// Add 'C' to the start of the position indicator so that it can be easily found
// Almost simulates random access!
// Example : Spiffs_Add_Int_Posn(SPIFFS,"/Data.csv",10,1234);
//==========================================================================
void Spiffs_Add_Int_Posn (fs::FS &fs, const char * path,int posn, unsigned int x)
{
String comma = ",";
File file = fs.open(path, FILE_APPEND); // opens file ready for append
String No = 'C'+String(posn)+','+String(x)+',';
file.print(No);
}
//Find the integer associated with a known position identifier in the comma deliminated file
//Example :int z=Spiffs_Return_Int_Posn(SPIFFS, "/Data.csv",3);
//==========================================================================
int Spiffs_Return_Int_Posn(fs::FS &fs, const char * path,int posn)
{
File file = fs.open(path);
while (file.available())
{
if (file.read()=='C') // finds count header
{
if (file.parseInt()==posn) // checks position against that required
{
return file.parseInt(); // returns the value.
}
}
}
return 0;
}
//=================================================================
// Main Program to Test Features
//================================================================
void setup() {
bool y;
Serial.begin(115200);
SPIFFS.begin();
// general add and substract directories and files
Serial.println("List Directories");
Spiffs_List_Dir(SPIFFS, "/", 0);
Serial.println("Create a directory '/mydir'");
y= Spiffs_Create_Dir(SPIFFS, "/mydir");
Serial.println("Create a file '/Hello.txt' with the String 'Hello' ");
y=Spiffs_Write_File(SPIFFS, "/hello.txt", "Hello ");
Serial.println("List Directories");
Spiffs_List_Dir(SPIFFS, "/", 0);
Serial.println("Remove directory '/mydir'");
y=Spiffs_Remove_Dir(SPIFFS, "/mydir");
Serial.println("List Directories");
Spiffs_List_Dir(SPIFFS, "/", 0);
Serial.println("Add 'World' to /hello.txt");
y= Spiffs_Append_File(SPIFFS, "/hello.txt", "World!\n");
Serial.println("Print '/hello.txt'");
y= Spiffs_Print_File(SPIFFS, "/hello.txt");
Serial.print("Delete a fictious file f23.txt: Y=1 or N=0 ");
y=Spiffs_Delete_File(SPIFFS, "/f23.txt"); // Will not remove if file doesn't exist
Serial.println(y);
Serial.println("Rename '/hello.txt' to '/f00.txt'");
y=Spiffs_Rename_File(SPIFFS, "/hello.txt", "/f00.txt");
Serial.println("Print '/f00.txt'");
y= Spiffs_Print_File(SPIFFS, "/f00.txt");
Serial.println("List the directories");
Spiffs_List_Dir(SPIFFS, "/", 0);
// test for integer in and out
y= Spiffs_Create_File(SPIFFS, "/Data1.csv");
for (int i=1;i<100;i++)
{
Spiffs_Add_Int_Posn (SPIFFS,"/Data1.csv",i,i*10);
}
Serial.println("Printing Data1.csv :");
y=Spiffs_Print_File(SPIFFS, "/Data1.csv");
Serial.println("");
Serial.println("Find value associated with posn 27 in Data1.csv :");
int z=Spiffs_Return_Int_Posn(SPIFFS, "/Data1.csv",27); // find integer with 27 as the locator
Serial.println();
Serial.print("Found Value :");
Serial.println(z);
Serial.println("List the directories");
Spiffs_List_Dir(SPIFFS, "/", 0);
SPIFFS.end();
}
void loop() {
}
I have also added in two crude subroutines to add and find integers in a comma deliminated file. A position integer is linked to the value saved and a 'C' is added to the start of the position string so that it is easy to locate. While crude the add and return subroutines act like a random access storage. unfortunately the file has to be scanned through until the position is found ( or not). As I said, crude, but it works!
#include <SPIFFS.h>
// This variation is based on the work of copercini
//List Directories
// Example:Spiffs_List_Dir(SPIFFS, "/", 0);
// Lists down to number of levels required
//==================================================================
void Spiffs_List_Dir(fs::FS &fs, const char * dirname, uint8_t levels) {
Serial.printf("Listing directory: %s\n", dirname);
File root = fs.open(dirname);
if (!root) {
Serial.println("Failed to open directory");
return;
}
if (!root.isDirectory()) {
Serial.println("Not a directory");
return;
}
File file = root.openNextFile();
while (file) {
if (file.isDirectory()) {
Serial.print(" DIR : ");
Serial.println(file.name());
if (levels) {
Spiffs_List_Dir(fs, file.name(), levels - 1);
}
} else {
Serial.print(" FILE: ");
Serial.print(file.name());
Serial.print(" SIZE: ");
Serial.println(file.size());
}
file = root.openNextFile();
}
}
// Create Directory
// Example: bool y= Spiffs_Create_Dir(SPIFFS, "/mydir");
//================================================
bool Spiffs_Create_Dir(fs::FS &fs, const char * path)
{
bool x;
if (fs.mkdir(path))
{
x=true;
} else {
x=false;
}
return x;
}
// Remove Directory
// Example: bool y= Spiffs_Remove_Dir(SPIFFS, "/mydir")
//================================================
bool Spiffs_Remove_Dir(fs::FS &fs, const char * path) {
bool x;
if (fs.rmdir(path)) {
x=true;
} else {
x=false;
}
return x;
}
// Print a File from a directory
//Example: bool y=Spriffs_Print_File(SPIFFS, "/hello.txt");
//=================================================
bool Spiffs_Print_File(fs::FS &fs, const char * path) {
bool x;
File file = fs.open(path);
if (!file) {
x=false;
return x;
}
while (file.available()) {
Serial.write(file.read());
}
}
// create a File
// Example:bool y= Spiffs_Create_File(SPIFFS, "/hello.txt");
//===============================================================
bool Spiffs_Create_File(fs::FS &fs, const char * path)
{
bool x;
File file = fs.open(path, FILE_WRITE);
if (!file) {
x= false;
return x;
}
}
// Write text to a file in a directory
// Example:bool y= Spiffs_Write_File(SPIFFS, "/hello.txt", "Hello ");
//===============================================================
bool Spiffs_Write_File(fs::FS &fs, const char * path, const char * message)
{
bool x;
File file = fs.open(path, FILE_WRITE);
if (!file) {
x= false;
return x;
}
if (file.print(message)) {
x= true;
} else {
x=false;
}
return x;
}
// Append information to existing file in a directory
// Example: bool y=Spiffs_Append_File(SPIFFS, "/hello.txt", "World!\n");
//===============================================================
bool Spiffs_Append_File(fs::FS &fs, const char * path, const char * message) {
bool x;
File file = fs.open(path, FILE_APPEND);
if (!file) {
x= false;
return x;
}
if (file.print(message)) {
x=true;
} else {
x=false;
}
return x;
}
// Rename a directory
// Example: bool y=Spiffs_Rename_File(SPIFFS, "/hello.txt", "/foo.txt");
//=================================================================
bool Spiffs_Rename_File(fs::FS &fs, const char * path1, const char * path2) {
bool x;
if (fs.rename(path1, path2)) {
x = true;
} else {
x=false;
}
return x;
}
// Delete a directory
// Example: bool y= Spiffs_Delete_File(SPIFFS, "/foo.txt");
//=================================================================
bool Spiffs_Delete_File(fs::FS &fs, const char * path) {
bool x;
if (fs.remove(path)) {
x=true;
} else {
x=false;
}
return x;
}
// Write an Integer to a comma deliminated file with a position indicator.
// Add 'C' to the start of the position indicator so that it can be easily found
// Almost simulates random access!
// Example : Spiffs_Add_Int_Posn(SPIFFS,"/Data.csv",10,1234);
//==========================================================================
void Spiffs_Add_Int_Posn (fs::FS &fs, const char * path,int posn, unsigned int x)
{
String comma = ",";
File file = fs.open(path, FILE_APPEND); // opens file ready for append
String No = 'C'+String(posn)+','+String(x)+',';
file.print(No);
}
//Find the integer associated with a known position identifier in the comma deliminated file
//Example :int z=Spiffs_Return_Int_Posn(SPIFFS, "/Data.csv",3);
//==========================================================================
int Spiffs_Return_Int_Posn(fs::FS &fs, const char * path,int posn)
{
File file = fs.open(path);
while (file.available())
{
if (file.read()=='C') // finds count header
{
if (file.parseInt()==posn) // checks position against that required
{
return file.parseInt(); // returns the value.
}
}
}
return 0;
}
//=================================================================
// Main Program to Test Features
//================================================================
void setup() {
bool y;
Serial.begin(115200);
SPIFFS.begin();
// general add and substract directories and files
Serial.println("List Directories");
Spiffs_List_Dir(SPIFFS, "/", 0);
Serial.println("Create a directory '/mydir'");
y= Spiffs_Create_Dir(SPIFFS, "/mydir");
Serial.println("Create a file '/Hello.txt' with the String 'Hello' ");
y=Spiffs_Write_File(SPIFFS, "/hello.txt", "Hello ");
Serial.println("List Directories");
Spiffs_List_Dir(SPIFFS, "/", 0);
Serial.println("Remove directory '/mydir'");
y=Spiffs_Remove_Dir(SPIFFS, "/mydir");
Serial.println("List Directories");
Spiffs_List_Dir(SPIFFS, "/", 0);
Serial.println("Add 'World' to /hello.txt");
y= Spiffs_Append_File(SPIFFS, "/hello.txt", "World!\n");
Serial.println("Print '/hello.txt'");
y= Spiffs_Print_File(SPIFFS, "/hello.txt");
Serial.print("Delete a fictious file f23.txt: Y=1 or N=0 ");
y=Spiffs_Delete_File(SPIFFS, "/f23.txt"); // Will not remove if file doesn't exist
Serial.println(y);
Serial.println("Rename '/hello.txt' to '/f00.txt'");
y=Spiffs_Rename_File(SPIFFS, "/hello.txt", "/f00.txt");
Serial.println("Print '/f00.txt'");
y= Spiffs_Print_File(SPIFFS, "/f00.txt");
Serial.println("List the directories");
Spiffs_List_Dir(SPIFFS, "/", 0);
// test for integer in and out
y= Spiffs_Create_File(SPIFFS, "/Data1.csv");
for (int i=1;i<100;i++)
{
Spiffs_Add_Int_Posn (SPIFFS,"/Data1.csv",i,i*10);
}
Serial.println("Printing Data1.csv :");
y=Spiffs_Print_File(SPIFFS, "/Data1.csv");
Serial.println("");
Serial.println("Find value associated with posn 27 in Data1.csv :");
int z=Spiffs_Return_Int_Posn(SPIFFS, "/Data1.csv",27); // find integer with 27 as the locator
Serial.println();
Serial.print("Found Value :");
Serial.println(z);
Serial.println("List the directories");
Spiffs_List_Dir(SPIFFS, "/", 0);
SPIFFS.end();
}
void loop() {
}