littefs working
This commit is contained in:
+132
@@ -0,0 +1,132 @@
|
||||
#include "filesys.h"
|
||||
|
||||
// listDir("/");
|
||||
// deleteFile("/hello.txt");
|
||||
// writeFile("/hello.txt", "Hello ");
|
||||
// appendFile("/hello.txt", "World!\n");
|
||||
// readFile("/hello.txt");
|
||||
// listDir("/");
|
||||
|
||||
FSInfo fsinfo;
|
||||
|
||||
void listDir(const char * dirname) {
|
||||
Serial.printf("Listing directory: %s\n", dirname);
|
||||
Dir root = LittleFS.openDir(dirname);
|
||||
|
||||
while (root.next()) {
|
||||
File file = root.openFile("r");
|
||||
Serial.print(" FILE: ");
|
||||
Serial.print(root.fileName());
|
||||
Serial.print(" SIZE: ");
|
||||
Serial.print(file.size());
|
||||
time_t cr = file.getCreationTime();
|
||||
time_t lw = file.getLastWrite();
|
||||
file.close();
|
||||
struct tm * tmstruct = localtime(&cr);
|
||||
Serial.printf(" CREATION: %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);
|
||||
tmstruct = localtime(&lw);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void readFile(const char * path) {
|
||||
Serial.printf("Reading file: %s\n", path);
|
||||
|
||||
File file = LittleFS.open(path, "r");
|
||||
if (!file) {
|
||||
Serial.println("Failed to open file for reading");
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.print("Read from file: ");
|
||||
while (file.available()) {
|
||||
Serial.write(file.read());
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
|
||||
void writeFile(const char * path, const char * message) {
|
||||
Serial.printf("Writing file: %s\n", path);
|
||||
|
||||
File file = LittleFS.open(path, "w");
|
||||
if (!file) {
|
||||
Serial.println("Failed to open file for writing");
|
||||
return;
|
||||
}
|
||||
if (file.print(message)) {
|
||||
Serial.println("File written");
|
||||
} else {
|
||||
Serial.println("Write failed");
|
||||
}
|
||||
delay(2000); // Make sure the CREATE and LASTWRITE times are different
|
||||
file.close();
|
||||
}
|
||||
|
||||
void appendFile(const char * path, const char * message) {
|
||||
Serial.printf("Appending to file: %s\n", path);
|
||||
|
||||
File file = LittleFS.open(path, "a");
|
||||
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 renameFile(const char * path1, const char * path2) {
|
||||
Serial.printf("Renaming file %s to %s\n", path1, path2);
|
||||
if (LittleFS.rename(path1, path2)) {
|
||||
Serial.println("File renamed");
|
||||
} else {
|
||||
Serial.println("Rename failed");
|
||||
}
|
||||
}
|
||||
|
||||
void deleteFile(const char * path) {
|
||||
Serial.printf("Deleting file: %s\n", path);
|
||||
if (LittleFS.remove(path)) {
|
||||
Serial.println("File deleted");
|
||||
} else {
|
||||
Serial.println("Delete failed");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool mount_fs() {
|
||||
if(!LittleFS.begin()) {
|
||||
Serial.println("[ERROR] LittleFS.info(), reset ...");
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!LittleFS.info(fsinfo)) {
|
||||
Serial.println("[ERROR] LittleFS.info(), reset ...");
|
||||
return false;
|
||||
}
|
||||
|
||||
printf("Filesystem opened: \n");
|
||||
printf("\ttotalBytes:\t%d\n", fsinfo.totalBytes);
|
||||
printf("\tusedBytes:\t%d\n", fsinfo.usedBytes);
|
||||
printf("\tblockSize:\t%d\n", fsinfo.blockSize);
|
||||
printf("\tpageSize:\t%d\n", fsinfo.pageSize);
|
||||
printf("\tmaxOpenFiles:\t%d\n", fsinfo.maxOpenFiles);
|
||||
printf("\tmaxPathLength:\t%d\n", fsinfo.maxPathLength);
|
||||
printf("\n");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool format_fs() {
|
||||
printf("Formatting FS ! \n");
|
||||
return LittleFS.format();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
+10
-8
@@ -19,6 +19,7 @@ extern "C" {
|
||||
#include <WiFiUdp.h>
|
||||
#include <ArduinoOTA.h>
|
||||
|
||||
#include "filesys.h"
|
||||
#include <ESP8266WebServer.h>
|
||||
#include <WiFiClient.h>
|
||||
|
||||
@@ -83,7 +84,6 @@ void handleNotFound() {
|
||||
|
||||
|
||||
void setup_webserver() {
|
||||
|
||||
server.on("/", handleRootGz);
|
||||
server.on("/style.css", handleCssGz);
|
||||
server.on("/favicon.png", handleFaviconGz);
|
||||
@@ -171,6 +171,11 @@ void setup() {
|
||||
|
||||
Serial.println(F("Booting ...."));
|
||||
|
||||
mount_fs();
|
||||
//format_fs();
|
||||
listDir("");
|
||||
readFile("/test.txt");
|
||||
|
||||
pinMode(NODEMCU_LED, OUTPUT);
|
||||
pinMode(ESP12_LED, OUTPUT);
|
||||
|
||||
@@ -214,10 +219,6 @@ void setup() {
|
||||
setup_pwm_pca9685();
|
||||
}
|
||||
|
||||
uint32_t t;
|
||||
#define SP_US(_str,_a) Serial.print(_str); Serial.print(" took: "); Serial.print(_a); Serial.println("µs")
|
||||
#define TIMEIF_US(_str,_f, _l) t=micros(); _f; t=micros()-t; if(t > _l) { SP_US(_str, t); }
|
||||
|
||||
uint8_t softstart_led(uint8_t led, uint16_t startval, uint16_t stopval, uint8_t factor){
|
||||
static uint8_t lastled = 255;
|
||||
static uint8_t current_pwm = 0;
|
||||
@@ -300,11 +301,11 @@ void ledsequence(uint8_t direction, uint8_t onoff, uint8_t factor){
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t t;
|
||||
#define SP_US(_str,_a) Serial.print(_str); Serial.print(" took: "); Serial.print(_a); Serial.println("us")
|
||||
#define TIMEIF_US(_str,_f, _l) t=micros(); _f; t=micros()-t; if(t > _l) { SP_US(_str, t); }
|
||||
|
||||
void loop() {
|
||||
|
||||
|
||||
|
||||
if(millis() - dimmtimer > 2){
|
||||
//ledsequence(direction, onoff, 4);
|
||||
dimmtimer = millis();
|
||||
@@ -316,4 +317,5 @@ void loop() {
|
||||
}
|
||||
TIMEIF_US("OTA", ArduinoOTA.handle(), 1000);
|
||||
TIMEIF_US("HTTP", server.handleClient(), 1000);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user