Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include “SPIFFS.h”
- #include “FS.h”
- /mode = “r”, “w”, “a”, “r+”, “w+”, “a+” (text file) or “rb”, “wb”, “ab”, “rb+”, “wb+”, “ab+” (binary)/
- //void listFilesInDir(File dir, int numTabs = 1);
- String command;
- int incomingByte = 0;
- unsigned int totalBytes = 0;
- unsigned int usedBytes = 0;
- unsigned int availBytes = 0;
- void setup() {
- Serial.begin(115200);
- Serial.flush();
- delay(500);
- Serial.println(F(“Inizializing FS…”));
- if (SPIFFS.begin()) {
- Serial.println(F(“SPIFFS mounted correctly.”));
- File dir = SPIFFS.open(“/”);
- listDir(SPIFFS, “/”, 0);
- // writeFile(SPIFFS, “/hello.txt”, “Hello “);
- // appendFile(SPIFFS, “/hello.txt”, “World!\r\n”);
- // readFile(SPIFFS, “/hello.txt”);
- // renameFile(SPIFFS, “/hello.txt”, “/foo.txt”);
- // readFile(SPIFFS, “/foo.txt”);
- // deleteFile(SPIFFS, “/foo.txt”);
- // testFileIO(SPIFFS, “/test.txt”);
- //deleteFile(SPIFFS, “/test.txt”);
- //readFile(SPIFFS, “/logos/brightnessup.bmp”);
- } else {
- Serial.println(F(“!An error occurred during SPIFFS mounting”));
- }
- }
- /* ———– MAIN LOOP ———-/
- void loop() {
- if (Serial.available()) {
- command = Serial.readStringUntil(‘\n’);
- command.trim();// elimina LF e CR
- Serial.println(“Command received ” + command);
- if (command.startsWith(“DEL”, 0)) {/ ———- DELETE ——– /
- long posizione = command.indexOf(“/”);
- if (posizione != -1) {
- String nome = command.substring(posizione);
- // Serial.println(“>>>” + nome + “<<<“);
- int lung = nome.length();
- char Buf [lung + 1];
- nome.toCharArray(Buf, lung + 1);
- // Serial.println(“>>>” + (String) Buf + “<<<“);
- deleteFile(SPIFFS, Buf);
- }
- } else if (command.startsWith(“DIR”, 0)) {/ ———- DIRECTORY ——– /
- listDir(SPIFFS, “/”, 0);
- } else if (command.startsWith(“REA”, 0)) {/ ———- READ FILE ——– /
- long posizione = command.indexOf(“/”);
- if (posizione != -1) {
- String nome = command.substring(posizione);
- int lung = nome.length();
- char Buf [lung + 1];
- nome.toCharArray(Buf, lung + 1);
- readFile(SPIFFS, Buf);
- }
- } else if (command.startsWith(“REH”, 0)) {/ ———- READ FILE BINARY TO HEX——– /
- long posizione = command.indexOf(“/”);
- if (posizione != -1) {
- String nome = command.substring(posizione);
- int lung = nome.length();
- char Buf [lung + 1];
- nome.toCharArray(Buf, lung + 1);
- readFileH(SPIFFS, Buf);
- }
- } else if (command.startsWith(“CDI”, 0)) {/ ———- CREATE DIRECTORY ——– /
- long posizione = command.indexOf(“/”);
- if (posizione != -1) {
- String nome = command.substring(posizione);
- int lung = nome.length();
- char Buf [lung + 1];
- nome.toCharArray(Buf, lung + 1);
- createDir(SPIFFS, Buf);
- }
- } else if (command.startsWith(“RDI”, 0)) {/ ———- REMOVE DIRECTORY ——– /
- long posizione = command.indexOf(“/”);
- if (posizione != -1) {
- String nome = command.substring(posizione);
- int lung = nome.length();
- char Buf [lung + 1];
- nome.toCharArray(Buf, lung + 1);
- removeDir(SPIFFS, Buf);
- }
- } else if (command.startsWith(“FOR”, 0)) {/ ———- FORMAT——– /
- formatto();
- } else if (command.startsWith(“WRI”, 0)) {/ ———- WRITE FILE ——– */
- long posizione = command.indexOf(“/”);
- if (posizione != -1) {
- String nome = command.substring(posizione);
- int lung = nome.length();
- char Buf [lung + 1];
- nome.toCharArray(Buf, lung + 1);
- writeFile(SPIFFS, Buf, “”);
- }
- } else {
- Serial.println(” Invalid command”);
- Comandi();
- }
- }
- }
- /* ———– END LOOP ———-*/
- /* ———– LIST DIR ———-/
- void listDir(fs::FS &fs, const char * dirname, uint8_t levels) {
- totalBytes = SPIFFS.totalBytes();
- usedBytes = SPIFFS.usedBytes();
- availBytes = totalBytes – usedBytes;
- Serial.println(“===== FILE SYSTEM INFO =====”);
- Serial.print(“Total space: “); Serial.print(totalBytes); Serial.println(” byte”);
- Serial.print(“Total space used: “); Serial.print(usedBytes); Serial.println(” byte”);
- Serial.print(“FREE SPACE : “); Serial.print(availBytes); Serial.println(” byte”);
- Serial.println();
- Serial.printf(“>>>>> LISTING DIRECTORY: %s\r\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) {
- listDir(fs, file.name(), levels – 1);
- }
- } else {
- Serial.print(“>>>>> FILE: “);
- Serial.print(file.name());
- Serial.print(“\tSIZE: “);
- Serial.println(file.size());
- }
- file = root.openNextFile();
- }
- file.close();
- Comandi();
- }
- / ————————— COMANDI ——————————/
- void Comandi() {
- Serial.println(“”);
- Serial.println(“========== LIST OF COMMANDS ==========”);
- Serial.println(“DIR << Directory List”);
- Serial.println(“DEL /dir/name << Delete file”);
- Serial.println(“REA /dir/name << Read file”);
- Serial.println(“REH /dir/name << Read file Binary TO HEX”);
- Serial.println(“WRI << Write a file (example: ‘WRI /Pippo/test.txt’ for create a file test.txt into directory Pippo, or just for create a directory)”);
- Serial.println(“CDI /dir/name << Create DIR”);
- Serial.println(“RDI /dir/name << Remove DIR”);
- Serial.println(“FOR << Format SPIFF”);
- Serial.println(“======================================”);
- }
- / ———– READ FILE ———-/
- /***************************************************************************
- Load configuration data from the SPIFF file, return 0 if OK
- SPIFFS open function needs path and mode:
- mode = “r”, “w”, “a”, “r+”, “w+”, “a+” (text file) or “rb”, “wb”, “ab”, “rb+”, “wb+”, “ab+” (binary)
- where r = read, w = write, a = append
- + means file is open for update (read and write)
- b means the file os open for binary operations
- Returns 0 if OK else:
- -1 = No SPIFFS file system
- -2 = File does not exist
- -3 = File too short
- -4 = Checksum does not compare
- ***************************************************************************/
- void readFile(fs::FS &fs, const char * path) {
- size_t len = 0;
- size_t i = 0;
- Serial.printf(“Reading file: %s\r\n”, path);
- File file = fs.open(path, “r”);
- file.setTimeout(0);
- if (file && !file.isDirectory()) {
- len = file.size();
- size_t flen = len;
- Serial.println(“- reading” );
- uint32_t sstart = millis();
- uint32_t eend = sstart;
- for (i = 0; i < len; i++) {
- incomingByte = file.read();
- Serial.write(incomingByte);
- }
- eend = millis() – sstart;
- Serial.println(“”);
- file.close();
- Serial.printf(“- %u bytes read in %u ms\r\n”, flen, eend);
- Serial.println(“—– FINISH —–“);
- } else {
- Serial.println(“- failed to open file for reading”);
- }
- }
- / ———– READ FILE BINARY TO HEX———-/
- void readFileH(fs::FS &fs, const char * path) {
- size_t len = 0;
- size_t i = 0;
- Serial.printf(“Reading file: %s\r\n”, path);
- File file = fs.open(path, “rb”);
- file.setTimeout(0);
- if (file && !file.isDirectory()) {
- len = file.size();
- size_t flen = len;
- Serial.println(“- reading” );
- uint32_t sstart = millis();
- uint32_t eend = sstart;
- for (i = 0; i < len; i++) {
- incomingByte = file.read();
- if ( incomingByte < 16) {/ need put a 0 more or we get a 5 and not a 05 in hex result when byte is from 0 to 15/
- Serial.print(“0”);
- }
- Serial.print(incomingByte, HEX);
- }
- eend = millis() – sstart;
- Serial.println(“”);
- file.close();
- Serial.printf(“- %u bytes read in %u ms\r\n”, flen, eend);
- Serial.println(“—– FINISH —–“);
- } else {
- Serial.println(“- failed to open file for reading”);
- }
- }
- / ———– WRITE FILE ———-/
- void writeFile(fs::FS &fs, const char * path, const char * message) {
- Serial.printf(“Writing file: %s\r\n”, path);
- File file = fs.open(path, “w”);
- delay(2000);
- if (!file) {
- Serial.println(“− failed to open file for writing”);
- return;
- }
- if (file.print(message)) {
- Serial.println(“− file written”);
- } else {
- Serial.println(“− write failed”);
- }
- file.close();
- }
- / ———– APPEND FILE ———-/
- void appendFile(fs::FS &fs, const char * path, const char * message) {
- Serial.printf(“Appending to file: %s\r\n”, path);
- File file = fs.open(path, FILE_APPEND);
- if (!file) {
- Serial.println(“− failed to open file for appending”);
- return;
- }
- if (file.print(message)) {
- Serial.println(“− message appended”);
- } else {
- Serial.println(“− append failed”);
- }
- file.close();
- }
- / ———– RENAME FILE ———-/
- void renameFile(fs::FS &fs, const char * path1, const char * path2) {
- Serial.printf(“Renaming file %s to %s\r\n”, path1, path2);
- if (fs.rename(path1, path2)) {
- Serial.println(“− file renamed”);
- } else {
- Serial.println(“− rename failed”);
- }
- }
- / ———– DELETE FILE ———-/
- void deleteFile(fs::FS &fs, const char * path) {
- Serial.printf(“Deleting file: %s\r\n”, path);
- Serial.println(“>>>” + (String) path + “<<<“);
- if (fs.remove(path)) {
- Serial.println(“− file deleted”);
- } else {
- Serial.println(“− delete failed”);
- }
- }
- / ———– FORMAT ———-/
- void formatto() {
- Serial.println(“Formatto SPIFF”);
- SPIFFS.format();
- SPIFFS.begin();
- Serial.println(“SPIFF FORMATTATO”);
- }
- / ———– CREATE DIR ———-/
- void createDir(fs::FS &fs, const char * path) {
- Serial.printf(“Creating Dir: %s\n”, path);
- if (fs.mkdir(path)) {
- Serial.println(“Dir created”);
- } else {
- Serial.println(“mkdir failed”);
- }
- }
- / ———– REMOVE DIR ———-/
- void removeDir(fs::FS &fs, const char * path) {
- Serial.printf(“Removing Dir: %s\n”, path);
- if (fs.rmdir(path)) {
- Serial.println(“Dir removed”);
- } else {
- Serial.println(“rmdir failed”);
- }
- }
- /// ———– TEST FILE ———-*/
- //void testFileIO(fs::FS &fs, const char * path) {
- // Serial.printf(“Testing file I/O with %s\r\n”, path);
- // static uint8_t buf[512];
- // size_t len = 0;
- // File file = fs.open(path, FILE_WRITE);
- // if (!file) {
- // Serial.println(“- failed to open file for writing”);
- // return;
- // }
- // size_t i;
- // Serial.print(“- writing” );
- // uint32_t start = millis();
- // for (i = 0; i < 512; i++) {
- // if ((i & 0x001F) == 0x001F) {
- // Serial.print(“.”);
- // }
- // file.write(buf, 512);
- // }
- // Serial.println(“”);
- // uint32_t end = millis() – start;
- // Serial.printf(” – %u bytes written in %u ms\r\n”, 512 * 512, end);
- // file.close();
- // file = fs.open(path);
- // start = millis();
- // end = start;
- // i = 0;
- // if (file && !file.isDirectory()) {
- // len = file.size();
- // size_t flen = len;
- // start = millis();
- // Serial.print(“- reading” );
- // while (len) {
- // size_t toRead = len;
- // if (toRead > 512) {
- // toRead = 512;
- // }
- // file.read(buf, toRead);
- // if ((i++ & 0x001F) == 0x001F) {
- // Serial.print(“.”);
- // }
- // len -= toRead;
- // }
- // Serial.println(“”);
- // end = millis() – start;
- // Serial.printf(“- %u bytes read in %u ms\r\n”, flen, end);
- // file.close();
- // } else {
- // Serial.println(“- failed to open file for reading”);
- // }
- //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement