From 1b555e7b52b073fd8010259871c953088cb282dd Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 1 Mar 2017 13:08:53 +0700 Subject: [PATCH] Nffs add mkdir_p add ListFiles example --- .../nffs/examples/ListFiles/ListFiles.ino | 126 ++++++++++++++++++ .../nffs/examples/ReadWrite/ReadWrite.ino | 4 +- libraries/nffs/library.properties | 2 +- libraries/nffs/src/NewtNffs.cpp | 28 ++++ libraries/nffs/src/NewtNffs.h | 7 +- libraries/nffs/src/NffsDir.cpp | 9 +- libraries/nffs/src/NffsDir.h | 1 + libraries/nffs/src/NffsDirEntry.cpp | 2 +- libraries/nffs/src/NffsDirEntry.h | 2 +- 9 files changed, 173 insertions(+), 8 deletions(-) create mode 100644 libraries/nffs/examples/ListFiles/ListFiles.ino diff --git a/libraries/nffs/examples/ListFiles/ListFiles.ino b/libraries/nffs/examples/ListFiles/ListFiles.ino new file mode 100644 index 00000000..6bb8d8aa --- /dev/null +++ b/libraries/nffs/examples/ListFiles/ListFiles.ino @@ -0,0 +1,126 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ + +#include +#include + +/* This example print out NFFS content up to + * MAX_LEVEL level of directories (including root) + */ + +/* + * WARNING: This example uses recursive call to print out directory tree + * be extra careful, high number of MAX_LEVEL can cause memory overflow + */ +#define MAX_LEVEL 2 + +// Current Working Directory Name +char cwd[256] = { 0 }; + +// the setup function runs once when you press reset or power the board +void setup() +{ + Serial.begin(115200); + Serial.println("Nffs List Files Example"); + + Nffs.begin(); + + // Print whole directory tree of root whose level is 0 + printTreeDir("/", 0); + + // Print prompt + Serial.println(); + Serial.println("Enter anything to print directory tree (again):"); +} + +// the loop function runs over and over again forever +void loop() +{ + digitalToggle(LED_BUILTIN); + delay(1000); +} + +/**************************************************************************/ +/*! + @brief Print out whole directory tree of an folder + until the level reach MAX_LEVEL + + @note Recursive call +*/ +/**************************************************************************/ +void printTreeDir(const char* path, uint8_t level) +{ + // Open the input folder + NffsDir dir(path); + + // Print root + if (level == 0) Serial.println("root"); + + // File Entry Information which hold file attribute and name + NffsDirEntry dirEntry; + + // Loop through the directory + while( dir.read(&dirEntry) ) + { + // Indentation according to dir level + for(int i=0; i 50 + Serial.print(' '); + + // Print size in bytes + NffsFile file(eName); + + Serial.print( file.size() ); + Serial.println( " Bytes"); + + file.close(); + } + } + + dir.close(); + + // Change back to Parent if not root + if (level != 0) + { + char* slash = strrchr(cwd, '/'); + *slash = 0; + } +} diff --git a/libraries/nffs/examples/ReadWrite/ReadWrite.ino b/libraries/nffs/examples/ReadWrite/ReadWrite.ino index 173737ef..3b3fdab8 100644 --- a/libraries/nffs/examples/ReadWrite/ReadWrite.ino +++ b/libraries/nffs/examples/ReadWrite/ReadWrite.ino @@ -12,7 +12,6 @@ any redistribution *********************************************************************/ -#include #include #define FILENAME "/adafruit.txt" @@ -24,9 +23,8 @@ NffsFile file; void setup() { Serial.begin(115200); - Serial.println("nffs example"); + Serial.println("Nffs Read Write File Example"); - Bluefruit.begin(); Nffs.begin(); file.open(FILENAME, FS_ACCESS_READ); diff --git a/libraries/nffs/library.properties b/libraries/nffs/library.properties index 21e69b73..d12b5243 100644 --- a/libraries/nffs/library.properties +++ b/libraries/nffs/library.properties @@ -7,4 +7,4 @@ paragraph=Newt Newtron Flash Filesystem category=Communication url=https://github.com/adafruit/Adafruit_nRF52_Arduino architectures=* -includes=nffs/include/nffs/nffs.h +includes=NewtNffs.h diff --git a/libraries/nffs/src/NewtNffs.cpp b/libraries/nffs/src/NewtNffs.cpp index 3045135a..e570ae3b 100644 --- a/libraries/nffs/src/NewtNffs.cpp +++ b/libraries/nffs/src/NewtNffs.cpp @@ -54,6 +54,34 @@ bool NewtNffs::mkdir(const char* path) return (errnum == FS_EOK); } +/** + * Make a directory (and its parents if not existed) a.k.a mkdir -p + * @param path Absolute path of directory, must start with '/' + * @return + */ +bool NewtNffs::mkdir_p(const char* path) +{ + char* parent = (char*) rtos_malloc(strlen(path)+1); + const char* slash = path+1; // skip root '/' + + memclr(parent, strlen(path)+1); + + while( NULL != (slash = strchr(slash, '/')) ) + { + memcpy(parent, path, slash-path); + + // make parent, ignore return value (OK or Already Existed) + fs_mkdir(parent); + + slash++; + } + errnum = fs_mkdir(path); + + rtos_free(parent); + + return (errnum == FS_EOK); +} + bool NewtNffs::remove(const char* path) { errnum = fs_unlink(path); diff --git a/libraries/nffs/src/NewtNffs.h b/libraries/nffs/src/NewtNffs.h index 1099f189..4da26383 100644 --- a/libraries/nffs/src/NewtNffs.h +++ b/libraries/nffs/src/NewtNffs.h @@ -36,6 +36,8 @@ #ifndef NEWTNFFS_H_ #define NEWTNFFS_H_ +#include + #include "nffs/nffs.h" #include "fs/fsutil.h" @@ -52,9 +54,12 @@ public: void begin(void); - // Make a directory (and its parents if not existed) a.k.a mkdir -p + // Make a directory, All intermediate directories must already exist bool mkdir(const char* path); + // Make a directory and its parents if not existed a.k.a "mkdir -p" + bool mkdir_p(const char* path); + // Remove a file or an empty directory bool remove(const char* path); bool rm (const char* path) { return remove(path); } diff --git a/libraries/nffs/src/NffsDir.cpp b/libraries/nffs/src/NffsDir.cpp index 900dc4d9..1337ad32 100644 --- a/libraries/nffs/src/NffsDir.cpp +++ b/libraries/nffs/src/NffsDir.cpp @@ -93,9 +93,16 @@ bool NffsDir::existed(void) NffsDirEntry NffsDir::read(void) { - struct fs_dirent* dirent; + struct fs_dirent* dirent = NULL ; errnum = fs_readdir(_dir, &dirent); return NffsDirEntry(dirent); } + +bool NffsDir::read(NffsDirEntry* entry) +{ + *entry = read(); + + return entry->existed(); +} diff --git a/libraries/nffs/src/NffsDir.h b/libraries/nffs/src/NffsDir.h index afd8dc31..c7028b50 100644 --- a/libraries/nffs/src/NffsDir.h +++ b/libraries/nffs/src/NffsDir.h @@ -60,6 +60,7 @@ public: bool existed(void); NffsDirEntry read(void); + bool read(NffsDirEntry* entry); }; diff --git a/libraries/nffs/src/NffsDirEntry.cpp b/libraries/nffs/src/NffsDirEntry.cpp index 61c9fe44..dc24a66f 100644 --- a/libraries/nffs/src/NffsDirEntry.cpp +++ b/libraries/nffs/src/NffsDirEntry.cpp @@ -46,7 +46,7 @@ NffsDirEntry::NffsDirEntry(struct fs_dirent* entry) _dirent = entry; } -NffsDirEntry& NffsDirEntry::operator+(const NffsDirEntry& rhs) +NffsDirEntry& NffsDirEntry::operator=(const NffsDirEntry& rhs) { _dirent = rhs._dirent; diff --git a/libraries/nffs/src/NffsDirEntry.h b/libraries/nffs/src/NffsDirEntry.h index 5954769d..2a6af157 100644 --- a/libraries/nffs/src/NffsDirEntry.h +++ b/libraries/nffs/src/NffsDirEntry.h @@ -47,7 +47,7 @@ public: NffsDirEntry(void); NffsDirEntry(struct fs_dirent* entry); - NffsDirEntry& operator+(const NffsDirEntry& rhs); + NffsDirEntry& operator=(const NffsDirEntry& rhs); bool existed(void);