8000 Add initial SPIFFS library · gulusili/arduino-esp32@9c9f924 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9c9f924

Browse files
committed
Add initial SPIFFS library
1 parent a710896 commit 9c9f924

File tree

4 files changed

+292
-0
lines changed

4 files changed

+292
-0
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
#include "FS.h"
2+
#include "SPIFFS.h"
3+
4+
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
5+
Serial.printf("Listing directory: %s\n", dirname);
6+
7+
File root = fs.open(dirname);
8+
if(!root){
9+
Serial.println("Failed to open directory");
10+
return;
11+
}
12+
if(!root.isDirectory()){
13+
Serial.println("Not a directory");
14+
return;
15+
}
16+
17+
File file = root.openNextFile();
18+
while(file){
19+
if(file.isDirectory()){
20+
Serial.print(" DIR : ");
21+
Serial.println(file.name());
22+
if(levels){
23+
listDir(fs, file.name(), levels -1);
24+
}
25+
} else {
26+
Serial.print(" FILE: ");
27+
Serial.print(file.name());
28+
Serial.print(" SIZE: ");
29+
Serial.println(file.size());
30+
}
31+
file = root.openNextFile();
32+
}
33+
}
34+
35+
void readFile(fs::FS &fs, const char * path){
36+
Serial.printf("Reading file: %s\n", path);
37+
38+
File file = fs.open(path);
39+
if(!file){
40+
Serial.println("Failed to open file for reading");
41+
return;
42+
}
43+
44+
Serial.print("Read from file: ");
45+
while(file.available()){
46+
Serial.write(file.read());
47+
}
48+
}
49+
50+
void writeFile(fs::FS &fs, const char * path, const char * message){
51+
Serial.printf("Writing file: %s\n", path);
52+
53+
File file = fs.open(path, FILE_WRITE);
54+
if(!file){
55+
Serial.println("Failed to open file for writing");
56+
return;
57+
}
58+
if(file.print(message)){
59+
Serial.println("File written");
60+
} else {
61+
Serial.println("Write failed");
62+
}
63+
}
64+
65+
void appendFile(fs::FS &fs, const char * path, const char * message){
66+
Serial.printf("Appending to file: %s\n", path);
67+
68+
File file = fs.open(path, FILE_APPEND);
69+
if(!file){
70+
Serial.println("Failed to open file for appending");
71+
return;
72+
}
73+
if(file.print(message)){
74+
Serial.println("Message appended");
75+
} else {
76+
Serial.println("Append failed");
77+
}
78+
}
79+
80+
void renameFile(fs::FS &fs, const char * path1, const char * path2){
81+
Serial.printf("Renaming file %s to %s\n", path1, path2);
82+
if (fs.rename(path1, path2)) {
83+
Serial.println("File renamed");
84+
} else {
85+
Serial.println("Rename failed");
86+
}
87+
}
88+
89+
void deleteFile(fs::FS &fs, const char * path){
90+
Serial.printf("Deleting file: %s\n", path);
91+
if(fs.remove(path)){
92+
Serial.println("File deleted");
93+
} else {
94+
Serial.println("Delete failed");
95+
}
96+
}
97+
98+
void testFileIO(fs::FS &fs, const char * path){
99+
File file = fs.open(path);
100+
static F438 uint8_t buf[512];
101+
size_t len = 0;
102+
uint32_t start = millis();
103+
uint32_t end = start;
104+
if(file){
105+
len = file.size();
106+
size_t flen = len;
107+
start = millis();
108+
while(len){
109+
size_t toRead = len;
110+
if(toRead > 512){
111+
toRead = 512;
112+
}
113+
file.read(buf, toRead);
114+
len -= toRead;
115+
}
116+
end = millis() - start;
117+
Serial.printf("%u bytes read for %u ms\n", flen, end);
118+
file.close();
119+
} else {
120+
Serial.println("Failed to open file for reading");
121+
}
122+
123+
124+
file = fs.open(path, FILE_WRITE);
125+
if(!file){
126+
Serial.println("Failed to open file for writing");
127+
return;
128+
}
129+
130+
size_t i;
131+
start = millis();
132+
for(i=0; i<2048; i++){
133+
file.write(buf, 512);
134+
}
135+
end = millis() - start;
136+
Serial.printf("%u bytes written for %u ms\n", 2048 * 512, end);
137+
file.close();
138+
}
139+
140+
void setup(){
141+
Serial.begin(115200);
142+
if(!SPIFFS.begin()){
143+
Serial.println("SPIFFS Mount Failed");
144+
return;
145+
}
146+
147+
listDir(SPIFFS, "/", 0);
148+
writeFile(SPIFFS, "/hello.txt", "Hello ");
149+
appendFile(SPIFFS, "/hello.txt", "World!\n");
150+
readFile(SPIFFS, "/hello.txt");
151+
deleteFile(SPIFFS, "/foo.txt");
152+
renameFile(SPIFFS, "/hello.txt", "/foo.txt");
153+
readFile(SPIFFS, "/foo.txt");
154+
testFileIO(SPIFFS, "/test.txt");
155+
}
156+
157+
void loop(){
158+
159+
}

libraries/SPIFFS/library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=SPIFFS
2+
version=1.0
3+
author=Hristo Gochkov, Ivan Grokhtkov
4+
maintainer=Hristo Gochkov <hristo@espressif.com>
5+
sentence=ESP32 SPIFFS File System
6+
paragraph=
7+
category=Data Storage
8+
url=
9+
architectures=esp32

libraries/SPIFFS/src/SPIFFS.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "vfs_api.h"
16+
17+
extern "C" {
18+
#include <sys/unistd.h>
19+
#include <sys/stat.h>
20+
#include <dirent.h>
21+
#include "esp_spiffs.h"
22+
}
23+
#include "SPIFFS.h"
24+
25+
using namespace fs;
26+
27+
SPIFFSFS::SPIFFSFS(FSImplPtr impl)
28+
: FS(impl)
29+
{}
30+
31+
bool SPIFFSFS::begin(bool formatOnFail, const char * basePath, uint8_t maxOpenFiles)
32+
{
33+
if(esp_spiffs_mounted()){
34+
log_w("SPIFFS Already Mounted!");
35+
return true;
36+
}
37+
esp_err_t err = esp_vfs_spiffs_register(basePath, maxOpenFiles, formatOnFail);
38+
if(err){
39+
log_e("Mounting SPIFFS failed! Error: %d", err);
40+
return false;
41+
}
42+
_impl->mountpoint(basePath);
43+
return true;
44+
}
45+
46+
void SPIFFSFS::end()
47+
{
48+
if(esp_spiffs_mounted()){
49+
esp_err_t err = esp_vfs_spiffs_unregister();
50+
if(err){
51+
log_e("Unmounting SPIFFS failed! Error: %d", err);
52+
return;
53+
}
54+
_impl->mountpoint(NULL);
55+
}
56+
}
57+
58+
bool SPIFFSFS::format()
59+
{
60+
esp_err_t err = esp_spiffs_format();
61+
if(err){
62+
log_e("Formatting SPIFFS failed! Error: %d", err);
63+
return false;
64+
}
65+
return true;
66+
}
67+
68+
size_t SPIFFSFS::totalBytes()
69+
{
70+
size_t total,used;
71+
if(esp_spiffs_info(&total, &used)){
72+
return 0;
73+
}
74+
return total;
75+
}
76+
77+
size_t SPIFFSFS::usedBytes()
78+
{
79+
size_t total,used;
80+
if(esp_spiffs_info(&total, &used)){
81+
return 0;
82+
}
83+
return used;
84+
}
85+
86+
87+
SPIFFSFS SPIFFS = SPIFFSFS(FSImplPtr(new VFSImpl()));

libraries/SPIFFS/src/SPIFFS.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
#ifndef _SPIFFS_H_
15+
#define _SPIFFS_H_
16+
17+
#include "FS.h"
18+
19+
namespace fs
20+
{
21+
22+
class SPIFFSFS : public FS
23+
{
24+
public:
25+
SPIFFSFS(FSImplPtr impl);
26+
bool begin(bool formatOnFail=false, const char * basePath="/spiffs", uint8_t maxOpenFiles=10);
27+
bool format();
28+
size_t totalBytes();
29+
size_t usedBytes();
30+
void end();
31+
};
32+
33+
}
34+
35+
extern fs::SPIFFSFS SPIFFS;
36+
37+
#endif /* _SPIFFS_H_ */

0 commit comments

Comments
 (0)
0