Skip to content

Commit

Permalink
เพิ่มตัวอย่าง LittleFS สำหรับ ArduinoIDE และ PlatformIO แยกกัน
Browse files Browse the repository at this point in the history
  • Loading branch information
BlynkGO committed Feb 12, 2024
1 parent c223dc0 commit 2a352e0
Show file tree
Hide file tree
Showing 15 changed files with 424 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
aaa
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bbb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
aaa
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bbb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

This directory is intended for project header files.

A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.

```src/main.c

#include "header.h"

int main (void)
{
...
}
```

Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.

In C, the usual convention is to give header files names that end with `.h'.
It is most portable to use only letters, digits, dashes, and underscores in
header file names, and at most one dot.

Read more about using header files in official GCC documentation:

* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes

https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into executable file.

The source code of each library should be placed in a an own separate directory
("lib/your_library_name/[here are source files]").

For example, see a structure of the following two libraries `Foo` and `Bar`:

|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c

and a contents of `src/main.c`:
```
#include <Foo.h>
#include <Bar.h>

int main (void)
{
...
}

```

PlatformIO Library Dependency Finder will find automatically dependent
libraries scanning project source files.

More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:beenext_2_4c]
platform = espressif32
board = beenext_2_4c
framework = arduino
upload_port = COM6
monitor_speed = 115200
lib_deps =
blynkgo/BlynkGOv3@^3.0.10
blynkgo/[email protected]
board_build.filesystem = littlefs

Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
#include <LittleFS.h>
#include <time.h>
#include "project.h"

#define FORMAT_LITTLEFS_IF_FAILED true

void LITTLEFS::begin() {
if (!LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED)) {
Serial.println("LittleFS Mount Failed");
return;
}
}

bool LITTLEFS::format(){
return LittleFS.format();
}

void LITTLEFS::listDir(const char * dirname, uint8_t levels) {
Serial.printf("Listing directory: %s\r\n", dirname);

File root = LittleFS.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.print(file.name());
time_t t = file.getLastWrite();
struct tm * tmstruct = localtime(&t);
Serial.printf(" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, ( tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour , tmstruct->tm_min, tmstruct->tm_sec);

if (levels) {
listDir(file.name(), levels - 1);
}
} else {
Serial.print(" FILE: ");
Serial.print(file.name());
Serial.print(" SIZE: ");

Serial.print(file.size());
time_t t = file.getLastWrite();
struct tm * tmstruct = localtime(&t);
Serial.printf(" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, ( tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour , tmstruct->tm_min, tmstruct->tm_sec);
}
file = root.openNextFile();
}
}

void LITTLEFS::createDir(const char * path) {
Serial.printf("Creating Dir: %s\n", path);
if (LittleFS.mkdir(path)) {
Serial.println("Dir created");
} else {
Serial.println("mkdir failed");
}
}

void LITTLEFS::removeDir(const char * path) {
Serial.printf("Removing Dir: %s\n", path);
if (LittleFS.rmdir(path)) {
Serial.println("Dir removed");
} else {
Serial.println("rmdir failed");
}
}

void LITTLEFS::readFile(const char * path) {
Serial.printf("Reading file: %s\r\n", path);

File file = LittleFS.open(path);
if (!file || file.isDirectory()) {
Serial.println("- failed to open file for reading");
return;
}

Serial.println("- read from file:");
while (file.available()) {
Serial.write(file.read());
}
file.close();
}

void LITTLEFS::writeFile(const char * path, const char * message) {
Serial.printf("Writing file: %s\r\n", path);

File file = LittleFS.open(path, FILE_WRITE);
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();
}

void LITTLEFS::appendFile(const char * path, const char * message) {
Serial.printf("Appending to file: %s\r\n", path);

File file = LittleFS.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();
}

void LITTLEFS::renameFile(const char * path1, const char * path2) {
Serial.printf("Renaming file %s to %s\r\n", path1, path2);
if (LittleFS.rename(path1, path2)) {
Serial.println("- file renamed");
} else {
Serial.println("- rename failed");
}
}

void LITTLEFS::deleteFile(const char * path) {
Serial.printf("Deleting file: %s\r\n", path);
if (LittleFS.remove(path)) {
Serial.println("- file deleted");
} else {
Serial.println("- delete failed");
}
}

// SPIFFS-like write and delete file

// See: https://github.com/esp8266/Arduino/blob/master/libraries/LittleFS/src/LittleFS.cpp#L60
void LITTLEFS::writeFile2(const char * path, const char * message) {
if (!LittleFS.exists(path)) {
if (strchr(path, '/')) {
Serial.printf("Create missing folders of: %s\r\n", path);
char *pathStr = strdup(path);
if (pathStr) {
char *ptr = strchr(pathStr, '/');
while (ptr) {
*ptr = 0;
LittleFS.mkdir(pathStr);
*ptr = '/';
ptr = strchr(ptr + 1, '/');
}
}
free(pathStr);
}
}

Serial.printf("Writing file to: %s\r\n", path);
File file = LittleFS.open(path, FILE_WRITE);
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();
}

// See: https://github.com/esp8266/Arduino/blob/master/libraries/LittleFS/src/LittleFS.h#L149
void LITTLEFS::deleteFile2(const char * path) {
Serial.printf("Deleting file and empty folders on path: %s\r\n", path);

if (LittleFS.remove(path)) {
Serial.println("- file deleted");
} else {
Serial.println("- delete failed");
}

char *pathStr = strdup(path);
if (pathStr) {
char *ptr = strrchr(pathStr, '/');
if (ptr) {
Serial.printf("Removing all empty folders on path: %s\r\n", path);
}
while (ptr) {
*ptr = 0;
LittleFS.rmdir(pathStr);
ptr = strrchr(pathStr, '/');
}
free(pathStr);
}
}

void LITTLEFS::testFileIO(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 = LittleFS.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 < 2048; 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", 2048 * 512, end);
file.close();

file = LittleFS.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");
}
}
Loading

0 comments on commit 2a352e0

Please sign in to comment.