Add initial SPIFFS library (#627)
* Add initial SPIFFS library * Fix Deep Sleep Examples * Missed one example
This commit is contained in:
parent
39fb8c3044
commit
29a253ac98
@ -30,9 +30,9 @@ Method to print the reason by which ESP32
|
||||
has been awaken from sleep
|
||||
*/
|
||||
void print_wakeup_reason(){
|
||||
esp_deep_sleep_wakeup_cause_t wakeup_reason;
|
||||
esp_sleep_wakeup_cause_t wakeup_reason;
|
||||
|
||||
wakeup_reason = esp_deep_sleep_get_wakeup_cause();
|
||||
wakeup_reason = esp_sleep_get_wakeup_cause();
|
||||
|
||||
switch(wakeup_reason)
|
||||
{
|
||||
@ -66,7 +66,7 @@ void setup(){
|
||||
Note that using internal pullups/pulldowns also requires
|
||||
RTC peripherals to be turned on.
|
||||
*/
|
||||
esp_deep_sleep_enable_ext0_wakeup(GPIO_NUM_33,1); //1 = High, 0 = Low
|
||||
esp_sleep_enable_ext0_wakeup(GPIO_NUM_33,1); //1 = High, 0 = Low
|
||||
|
||||
//If you were to use ext1, you would use it like
|
||||
//esp_deep_sleep_enable_ext1_wakeup(BUTTON_PIN_BITMASK,ESP_EXT1_WAKEUP_ANY_HIGH);
|
||||
|
@ -29,9 +29,9 @@ Method to print the reason by which ESP32
|
||||
has been awaken from sleep
|
||||
*/
|
||||
void print_wakeup_reason(){
|
||||
esp_deep_sleep_wakeup_cause_t wakeup_reason;
|
||||
esp_sleep_wakeup_cause_t wakeup_reason;
|
||||
|
||||
wakeup_reason = esp_deep_sleep_get_wakeup_cause();
|
||||
wakeup_reason = esp_sleep_get_wakeup_cause();
|
||||
|
||||
switch(wakeup_reason)
|
||||
{
|
||||
@ -59,7 +59,7 @@ void setup(){
|
||||
First we configure the wake up source
|
||||
We set our ESP32 to wake up every 5 seconds
|
||||
*/
|
||||
esp_deep_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
|
||||
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
|
||||
Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) +
|
||||
" Seconds");
|
||||
|
||||
|
@ -20,9 +20,9 @@ Method to print the reason by which ESP32
|
||||
has been awaken from sleep
|
||||
*/
|
||||
void print_wakeup_reason(){
|
||||
esp_deep_sleep_wakeup_cause_t wakeup_reason;
|
||||
esp_sleep_wakeup_cause_t wakeup_reason;
|
||||
|
||||
wakeup_reason = esp_deep_sleep_get_wakeup_cause();
|
||||
wakeup_reason = esp_sleep_get_wakeup_cause();
|
||||
|
||||
switch(wakeup_reason)
|
||||
{
|
||||
@ -42,7 +42,7 @@ has been awaken from sleep
|
||||
void print_wakeup_touchpad(){
|
||||
touch_pad_t pin;
|
||||
|
||||
touchPin = esp_deep_sleep_get_touchpad_wakeup_status();
|
||||
touchPin = esp_sleep_get_touchpad_wakeup_status();
|
||||
|
||||
switch(touchPin)
|
||||
{
|
||||
@ -80,7 +80,7 @@ void setup(){
|
||||
touchAttachInterrupt(T3, callback, Threshold);
|
||||
|
||||
//Configure Touchpad as wakeup source
|
||||
esp_deep_sleep_enable_touchpad_wakeup();
|
||||
esp_sleep_enable_touchpad_wakeup();
|
||||
|
||||
//Go to sleep now
|
||||
Serial.println("Going to sleep now");
|
||||
|
@ -77,7 +77,7 @@ void setup() {
|
||||
|
||||
// Set ESP32 to go to deep sleep to see a variation
|
||||
// in the reset reason. Device will sleep for 5 seconds.
|
||||
esp_deep_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF);
|
||||
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF);
|
||||
Serial.println("Going to sleep");
|
||||
esp_deep_sleep(5 * uS_TO_S_FACTOR);
|
||||
}
|
||||
|
159
libraries/SPIFFS/examples/SPIFFS_Test/SPIFFS_Test.ino
Normal file
159
libraries/SPIFFS/examples/SPIFFS_Test/SPIFFS_Test.ino
Normal file
@ -0,0 +1,159 @@
|
||||
#include "FS.h"
|
||||
#include "SPIFFS.h"
|
||||
|
||||
void listDir(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){
|
||||
listDir(fs, file.name(), levels -1);
|
||||
}
|
||||
} else {
|
||||
Serial.print(" FILE: ");
|
||||
Serial.print(file.name());
|
||||
Serial.print(" SIZE: ");
|
||||
Serial.println(file.size());
|
||||
}
|
||||
file = root.openNextFile();
|
||||
}
|
||||
}
|
||||
|
||||
void readFile(fs::FS &fs, const char * path){
|
||||
Serial.printf("Reading file: %s\n", path);
|
||||
|
||||
File file = fs.open(path);
|
||||
if(!file){
|
||||
Serial.println("Failed to open file for reading");
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.print("Read from file: ");
|
||||
while(file.available()){
|
||||
Serial.write(file.read());
|
||||
}
|
||||
}
|
||||
|
||||
void writeFile(fs::FS &fs, const char * path, const char * message){
|
||||
Serial.printf("Writing file: %s\n", path);
|
||||
|
||||
File file = fs.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");
|
||||
}
|
||||
}
|
||||
|
||||
void appendFile(fs::FS &fs, const char * path, const char * message){
|
||||
Serial.printf("Appending to file: %s\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");
|
||||
}
|
||||
}
|
||||
|
||||
void renameFile(fs::FS &fs, const char * path1, const char * path2){
|
||||
Serial.printf("Renaming file %s to %s\n", path1, path2);
|
||||
if (fs.rename(path1, path2)) {
|
||||
Serial.println("File renamed");
|
||||
} else {
|
||||
Serial.println("Rename failed");
|
||||
}
|
||||
}
|
||||
|
||||
void deleteFile(fs::FS &fs, const char * path){
|
||||
Serial.printf("Deleting file: %s\n", path);
|
||||
if(fs.remove(path)){
|
||||
Serial.println("File deleted");
|
||||
} else {
|
||||
Serial.println("Delete failed");
|
||||
}
|
||||
}
|
||||
|
||||
void testFileIO(fs::FS &fs, const char * path){
|
||||
File file = fs.open(path);
|
||||
static uint8_t buf[512];
|
||||
size_t len = 0;
|
||||
uint32_t start = millis();
|
||||
uint32_t end = start;
|
||||
if(file){
|
||||
len = file.size();
|
||||
size_t flen = len;
|
||||
start = millis();
|
||||
while(len){
|
||||
size_t toRead = len;
|
||||
if(toRead > 512){
|
||||
toRead = 512;
|
||||
}
|
||||
file.read(buf, toRead);
|
||||
len -= toRead;
|
||||
}
|
||||
end = millis() - start;
|
||||
Serial.printf("%u bytes read for %u ms\n", flen, end);
|
||||
file.close();
|
||||
} else {
|
||||
Serial.println("Failed to open file for reading");
|
||||
}
|
||||
|
||||
|
||||
file = fs.open(path, FILE_WRITE);
|
||||
if(!file){
|
||||
Serial.println("Failed to open file for writing");
|
||||
return;
|
||||
}
|
||||
|
||||
size_t i;
|
||||
start = millis();
|
||||
for(i=0; i<2048; i++){
|
||||
file.write(buf, 512);
|
||||
}
|
||||
end = millis() - start;
|
||||
Serial.printf("%u bytes written for %u ms\n", 2048 * 512, end);
|
||||
file.close();
|
||||
}
|
||||
|
||||
void setup(){
|
||||
Serial.begin(115200);
|
||||
if(!SPIFFS.begin()){
|
||||
Serial.println("SPIFFS Mount Failed");
|
||||
return;
|
||||
}
|
||||
|
||||
listDir(SPIFFS, "/", 0);
|
||||
writeFile(SPIFFS, "/hello.txt", "Hello ");
|
||||
appendFile(SPIFFS, "/hello.txt", "World!\n");
|
||||
readFile(SPIFFS, "/hello.txt");
|
||||
deleteFile(SPIFFS, "/foo.txt");
|
||||
renameFile(SPIFFS, "/hello.txt", "/foo.txt");
|
||||
readFile(SPIFFS, "/foo.txt");
|
||||
testFileIO(SPIFFS, "/test.txt");
|
||||
}
|
||||
|
||||
void loop(){
|
||||
|
||||
}
|
9
libraries/SPIFFS/library.properties
Normal file
9
libraries/SPIFFS/library.properties
Normal file
@ -0,0 +1,9 @@
|
||||
name=SPIFFS
|
||||
version=1.0
|
||||
author=Hristo Gochkov, Ivan Grokhtkov
|
||||
maintainer=Hristo Gochkov <hristo@espressif.com>
|
||||
sentence=ESP32 SPIFFS File System
|
||||
paragraph=
|
||||
category=Data Storage
|
||||
url=
|
||||
architectures=esp32
|
106
libraries/SPIFFS/src/SPIFFS.cpp
Normal file
106
libraries/SPIFFS/src/SPIFFS.cpp
Normal file
@ -0,0 +1,106 @@
|
||||
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "vfs_api.h"
|
||||
|
||||
extern "C" {
|
||||
#include <sys/unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <dirent.h>
|
||||
#include "esp_spiffs.h"
|
||||
}
|
||||
#include "SPIFFS.h"
|
||||
|
||||
using namespace fs;
|
||||
|
||||
SPIFFSFS::SPIFFSFS(FSImplPtr impl)
|
||||
: FS(impl)
|
||||
{}
|
||||
|
||||
bool SPIFFSFS::begin(bool formatOnFail, const char * basePath, uint8_t maxOpenFiles)
|
||||
{
|
||||
if(esp_spiffs_mounted(NULL)){
|
||||
log_w("SPIFFS Already Mounted!");
|
||||
return true;
|
||||
}
|
||||
|
||||
esp_vfs_spiffs_conf_t conf = {
|
||||
.base_path = basePath,
|
||||
.partition_label = NULL,
|
||||
.max_files = maxOpenFiles,
|
||||
.format_if_mount_failed = formatOnFail
|
||||
};
|
||||
|
||||
esp_err_t err = esp_vfs_spiffs_register(&conf);
|
||||
if(err){
|
||||
log_e("Mounting SPIFFS failed! Error: %d", err);
|
||||
return false;
|
||||
}
|
||||
_impl->mountpoint(basePath);
|
||||
return true;
|
||||
}
|
||||
|
||||
void SPIFFSFS::end()
|
||||
{
|
||||
if(esp_spiffs_mounted(NULL)){
|
||||
esp_err_t err = esp_vfs_spiffs_unregister(NULL);
|
||||
if(err){
|
||||
log_e("Unmounting SPIFFS failed! Error: %d", err);
|
||||
return;
|
||||
}
|
||||
_impl->mountpoint(NULL);
|
||||
}
|
||||
}
|
||||
|
||||
bool SPIFFSFS::format()
|
||||
{
|
||||
esp_err_t err = esp_spiffs_format(NULL);
|
||||
if(err){
|
||||
log_e("Formatting SPIFFS failed! Error: %d", err);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t SPIFFSFS::totalBytes()
|
||||
{
|
||||
size_t total,used;
|
||||
if(esp_spiffs_info(NULL, &total, &used)){
|
||||
return 0;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
size_t SPIFFSFS::usedBytes()
|
||||
{
|
||||
size_t total,used;
|
||||
if(esp_spiffs_info(NULL, &total, &used)){
|
||||
return 0;
|
||||
}
|
||||
return used;
|
||||
}
|
||||
|
||||
bool SPIFFSFS::exists(const char* path)
|
||||
{
|
||||
File f = open(path, "r");
|
||||
return (f == true) && !f.isDirectory();
|
||||
}
|
||||
|
||||
bool SPIFFSFS::exists(const String& path)
|
||||
{
|
||||
return exists(path.c_str());
|
||||
}
|
||||
|
||||
|
||||
SPIFFSFS SPIFFS = SPIFFSFS(FSImplPtr(new VFSImpl()));
|
39
libraries/SPIFFS/src/SPIFFS.h
Normal file
39
libraries/SPIFFS/src/SPIFFS.h
Normal file
@ -0,0 +1,39 @@
|
||||
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
#ifndef _SPIFFS_H_
|
||||
#define _SPIFFS_H_
|
||||
|
||||
#include "FS.h"
|
||||
|
||||
namespace fs
|
||||
{
|
||||
|
||||
class SPIFFSFS : public FS
|
||||
{
|
||||
public:
|
||||
SPIFFSFS(FSImplPtr impl);
|
||||
bool begin(bool formatOnFail=false, const char * basePath="/spiffs", uint8_t maxOpenFiles=10);
|
||||
bool format();
|
||||
size_t totalBytes();
|
||||
size_t usedBytes();
|
||||
void end();
|
||||
bool exists(const char* path);
|
||||
bool exists(const String& path);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
extern fs::SPIFFSFS SPIFFS;
|
||||
|
||||
#endif /* _SPIFFS_H_ */
|
Loading…
Reference in New Issue
Block a user