Englebert
3 years ago
10 changed files with 857 additions and 58 deletions
-
1platformio.ini
-
131src/baro.cpp
-
21src/baro.h
-
9src/config.h
-
117src/gyro.cpp
-
94src/gyro.h
-
244src/main.cpp
-
58src/main.h
-
201src/storage.cpp
-
39src/storage.h
@ -0,0 +1,201 @@ |
|||||
|
#include "storage.h"
|
||||
|
|
||||
|
|
||||
|
STORAGE::STORAGE(void) { |
||||
|
// Just some initialization
|
||||
|
} |
||||
|
|
||||
|
|
||||
|
bool STORAGE::format(void) { |
||||
|
if(LITTLEFS.format()) { |
||||
|
return true; |
||||
|
} else { |
||||
|
return false; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
bool STORAGE::exists(const char * path) { |
||||
|
if(LITTLEFS.exists(path)) { |
||||
|
return true; |
||||
|
} else { |
||||
|
return false; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
bool STORAGE::begin(void) { |
||||
|
if(!LITTLEFS.begin(FORMAT_LITTLEFS_IF_FAILED)) { |
||||
|
Serial.println("LITTLEFS mount failed"); |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
// Demo and checking only
|
||||
|
STORAGE::listDir(LITTLEFS, "/", 0); |
||||
|
|
||||
|
if(STORAGE::exists("/config")) { |
||||
|
Serial.println("config FOUND"); |
||||
|
} else { |
||||
|
Serial.println("config NOT FOUND"); |
||||
|
} |
||||
|
|
||||
|
// Check Total storage
|
||||
|
Serial.print("Storage size: "); |
||||
|
Serial.print(LITTLEFS.totalBytes()); |
||||
|
Serial.println(" Bytes"); |
||||
|
|
||||
|
Serial.print("Storage used: " ); |
||||
|
Serial.print(LITTLEFS.usedBytes()); |
||||
|
Serial.println(" Bytes"); |
||||
|
|
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
void STORAGE::listDir(fs::FS &fs, const char * dirname, uint8_t levels){ |
||||
|
Serial.printf("Listing directory: %s\r\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 : "); |
||||
|
|
||||
|
#ifdef CONFIG_LITTLEFS_FOR_IDF_3_2
|
||||
|
Serial.println(file.name()); |
||||
|
#else
|
||||
|
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); |
||||
|
#endif
|
||||
|
|
||||
|
if(levels){ |
||||
|
listDir(fs, file.name(), levels -1); |
||||
|
} |
||||
|
} else { |
||||
|
Serial.print(" FILE: "); |
||||
|
Serial.print(file.name()); |
||||
|
Serial.print(" SIZE: "); |
||||
|
|
||||
|
#ifdef CONFIG_LITTLEFS_FOR_IDF_3_2
|
||||
|
Serial.println(file.size()); |
||||
|
#else
|
||||
|
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); |
||||
|
#endif
|
||||
|
} |
||||
|
file = root.openNextFile(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
void STORAGE::readFile(fs::FS &fs, const char * path){ |
||||
|
Serial.printf("Reading file: %s\r\n", path); |
||||
|
|
||||
|
File file = fs.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 STORAGE::appendFile(fs::FS &fs, const char * path, const char * message){ |
||||
|
Serial.printf("Appending to file: %s\r\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"); |
||||
|
} |
||||
|
file.close(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
void STORAGE::deleteFile(fs::FS &fs, const char * path){ |
||||
|
Serial.printf("Deleting file: %s\r\n", path); |
||||
|
if(fs.remove(path)){ |
||||
|
Serial.println("- file deleted"); |
||||
|
} else { |
||||
|
Serial.println("- delete failed"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
void STORAGE::writeFile(fs::FS &fs, const char * path, const char * message){ |
||||
|
Serial.printf("Writing file: %s\r\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"); |
||||
|
} |
||||
|
|
||||
|
file.close(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
void STORAGE::write_block(uint8_t * data, const char * path, uint32_t len){ |
||||
|
uint32_t i; |
||||
|
fs::FS &fs = LITTLEFS; |
||||
|
|
||||
|
File file = fs.open(path, FILE_APPEND); |
||||
|
if(!file) { |
||||
|
Serial.println("write_block append failed"); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
for(i = 0; i < len; i++){ |
||||
|
// EEPROM.write(address+i, data[i]);
|
||||
|
file.print(data[i]); |
||||
|
} |
||||
|
file.close(); |
||||
|
} |
||||
|
|
||||
|
/****
|
||||
|
268 writeFile(LITTLEFS, "/mydir/hello2.txt", "Hello2"); |
||||
|
|
||||
|
void eeprom_read_block(uint8_t * data, uint32_t address, size_t len){ |
||||
|
int i; |
||||
|
for(i=0; i<len; i++){ |
||||
|
data[i] = EEPROM.read(address+i); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void eeprom_write_block(uint8_t * data, uint32_t address, size_t len){ |
||||
|
int i; |
||||
|
for(i=0; i<len; i++){ |
||||
|
EEPROM.write(address+i, data[i]); |
||||
|
} |
||||
|
EEPROM.commit(); |
||||
|
} |
||||
|
****/ |
@ -0,0 +1,39 @@ |
|||||
|
#ifndef _STORAGE_H |
||||
|
#define _STORAGE_H |
||||
|
|
||||
|
#include "Arduino.h" |
||||
|
#include "main.h" |
||||
|
#include "FS.h" |
||||
|
#include <LITTLEFS.h> |
||||
|
#ifndef CONFIG_LITTLEFS_FOR_IDF_3_2 |
||||
|
#include <time.h> |
||||
|
#endif |
||||
|
|
||||
|
/* You only need to format LITTLEFS the first time you run a |
||||
|
test or else use the LITTLEFS plugin to create a partition |
||||
|
https://github.com/lorol/arduino-esp32littlefs-plugin */ |
||||
|
|
||||
|
#define FORMAT_LITTLEFS_IF_FAILED true |
||||
|
|
||||
|
|
||||
|
class STORAGE { |
||||
|
|
||||
|
public: |
||||
|
STORAGE(void); |
||||
|
bool begin(void); |
||||
|
void listDir(fs::FS &fs, const char * dirname, uint8_t levels); |
||||
|
void readFile(fs::FS &fs, const char * path); |
||||
|
void appendFile(fs::FS &fs, const char * path, const char * message); |
||||
|
void deleteFile(fs::FS &fs, const char * path); |
||||
|
void writeFile(fs::FS &fs, const char * path, const char * message); |
||||
|
|
||||
|
// Emulate EEPROM write to LITTLEFS |
||||
|
void write_block(uint8_t * data, const char * path, uint32_t len); |
||||
|
|
||||
|
bool format(void); |
||||
|
bool exists(const char * path); |
||||
|
|
||||
|
private: |
||||
|
}; |
||||
|
|
||||
|
#endif |
Write
Preview
Loading…
Cancel
Save
Reference in new issue