6#define SPIFF_FILE_NAME (char*)"/messages.txt"
16#define FORMAT_SPIFFS_IF_FAILED true
26void listDir(fs::FS &
fs,
const char * dirname, uint8_t levels)
31 SerialDebug.printf(
"Listing directory: %s\r\n", dirname);
33 File root =
fs.open(dirname);
35 SerialDebug.println(
"- failed to open directory");
38 if(!root.isDirectory()){
39 SerialDebug.println(
" - not a directory");
43 File file = root.openNextFile();
45 if(file.isDirectory()){
46 SerialDebug.print(
" DIR : ");
47 SerialDebug.println(file.name());
50 listDir(
fs, file.name(), levels -1);
54 SerialDebug.print(
" FILE: ");
55 SerialDebug.print(file.name());
56 SerialDebug.print(
"\tSIZE: ");
57 SerialDebug.println(file.size());
59 file = root.openNextFile();
64void readFile(fs::FS &
fs,
const char * path)
69 SerialDebug.printf(
"Reading file: %s\r\n", path);
71 File file =
fs.open(path);
72 if(!file || file.isDirectory()){
73 SerialDebug.println(
"- failed to open file for reading");
77 SerialDebug.println(
"- read from file:");
78 while(file.available()){
79 SerialDebug.write(file.read());
85void writeFile(fs::FS &
fs,
const char * path,
const char * message)
90 SerialDebug.printf(
"Writing file: %s\r\n", path);
92 File file =
fs.open(path, FILE_WRITE);
94 SerialDebug.println(
"- failed to open file for writing");
97 if(file.print(message)){
98 SerialDebug.println(
"- file written");
100 SerialDebug.println(
"- write failed");
106void appendFile(fs::FS &
fs,
const char * path,
const char * message)
111 SerialLots.printf(
"Appending to file: %s\r\n", path);
113 File file =
fs.open(path, FILE_APPEND);
115 SerialDebug.println(
"- failed to open file for appending");
118 if(file.print(message)){
119 SerialLots.println(
"- message appended");
121 SerialDebug.println(
"- append failed");
127void renameFile(fs::FS &
fs,
const char * path1,
const char * path2)
132 SerialLots.printf(
"Renaming file %s to %s\r\n", path1, path2);
133 if (
fs.rename(path1, path2)) {
134 SerialLots.println(
"- file renamed");
136 SerialDebug.println(
"- rename failed");
141void deleteFile(fs::FS &
fs,
const char * path)
146 SerialLots.printf(
"Deleting file: %s\r\n", path);
148 SerialLots.println(
"- file deleted");
150 SerialDebug.println(
"- delete failed");
155void testFileIO(fs::FS &
fs,
const char * path)
157 SerialLots.printf(
"Testing file I/O with %s\r\n", path);
159 static uint8_t buf[512];
161 File file =
fs.open(path, FILE_WRITE);
163 SerialDebug.println(
"- failed to open file for writing");
168 SerialDebug.print(
"- writing" );
169 uint32_t start = millis();
170 for(i=0; i<2048; i++){
171 if ((i & 0x001F) == 0x001F){
172 SerialDebug.print(
".");
174 file.write(buf, 512);
176 SerialDebug.println(
"");
177 uint32_t end = millis() - start;
178 SerialDebug.printf(
" - %u bytes written in %u ms\r\n", 2048 * 512, end);
181 file =
fs.open(path);
185 if(file && !file.isDirectory()){
189 SerialDebug.print(
"- reading" );
195 file.read(buf, toRead);
196 if ((i++ & 0x001F) == 0x001F){
197 SerialDebug.print(
".");
201 SerialDebug.println(
"");
202 end = millis() - start;
203 SerialDebug.printf(
"- %u bytes read in %u ms\r\n", flen, end);
206 SerialDebug.println(
"- failed to open file for reading");
238 appendFile(SPIFFS, SPIFF_FILE_NAME,
string);
244 sprintf(str,
"%d",val);
251 deleteFile(SPIFFS, SPIFF_FILE_NAME);
257 readFile(SPIFFS, SPIFF_FILE_NAME);
261int linesInFile_SPIFFModule(fs::FS &
fs,
const char * path)
269 File file =
fs.open(path);
270 if(!file || file.isDirectory()){
271 SerialDebug.println(
"- failed to open file for reading");
275 while(file.available()){
289 SerialDebug.println(
"sendStrings_SPIFFModule");
292 char *path = SPIFF_FILE_NAME;
293 int linesMax = linesInFile_SPIFFModule(SPIFFS, SPIFF_FILE_NAME);
294 SerialDebug.printf(
"linesMax = %d\n", linesMax);
296#ifdef USE_MQTT_NETWORKING
297 if (numberOfLines > 20)
303 int skipLines = linesMax - numberOfLines;
307 File file =
fs.open(path);
308 if(!file || file.isDirectory()){
309 SerialDebug.println(
"- failed to open file for reading");
314 while(file.available() && counter < skipLines){
318#ifdef NOT_WORKING_YET
325 while(file.available() && counter < 5)
327 String line = file.readString();
328 SerialDebug.println(line);
333 strcpy (buffer, (
char*)
"ScottyBoy");
351void writeFB_SPIFFModule(
359 File file = SPIFFS.open(fileName, FILE_WRITE);
361 SerialDebug.println(
"- failed to open file for writing");
364 if(file.write(buf,len)){
365 SerialDebug.println(
"- file written");
367 SerialDebug.println(
"- write failed");
375 SerialDebug.println(
"SPIFFS setup");
379 SerialDebug.println(
"not being used: SPIFFS");
384 if(!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)){
385 SerialDebug.println(
"SPIFFS Mount Failed");
388 listDir(SPIFFS,
"/", 0);
391 boolean test1 =
false;
398 boolean test2 =
false;
422#ifdef M5_CAPTURE_SCREEN
429bool M5Screen2bmp(WiFiClient &client){
430 TFT_eSprite canvas = TFT_eSprite(&M5.Lcd);
432 int image_height = M5.Lcd.height();
433 int image_width = M5.Lcd.width();
436 canvas.createSprite(image_width, image_height);
438 canvas.fillSprite(RED);
439 canvas.fillCircle(100,100,20,GREEN);
440 canvas.pushSprite(0,0,WHITE);
442 const uint pad=(4-(3*image_width)%4)%4;
443 uint filesize=54+(3*image_width+pad)*image_height;
444 unsigned char header[54] = {
461 for(uint i=0; i<4; i++) {
462 header[ 2+i] = (char)((filesize>>(8*i))&255);
463 header[18+i] = (char)((image_width >>(8*i))&255);
464 header[22+i] = (char)((image_height >>(8*i))&255);
467 client.write(header, 54);
470 unsigned char line_data[image_width*3+pad];
472 for(
int i=(image_width-1)*3; i<(image_width*3+pad); i++){
477 for(
int y=image_height; y>0; y--){
480 canvas.readRectRGB(0, y-1, image_width, 1, line_data);
485 for(
int x=0; x<image_width; x++){
486 unsigned char r_buff = line_data[x*3];
487 SerialTemp.print(r_buff);
488 line_data[x*3] = line_data[x*3+2];
489 line_data[x*3+2] = r_buff;
491 SerialTemp.println();
493 client.write(line_data, (image_width*3)+pad);
515bool M5Screen2bmp(fs::FS &
fs,
const char * path){
518 File file =
fs.open(path, FILE_WRITE);
523 int image_height = M5.Lcd.height();
524 int image_width = M5.Lcd.width();
525 SerialDebug.printf(
"Saving screen to bmp file (%d,%d)\n", image_width, image_height);
529 const uint pad=(4-(3*image_width)%4)%4;
533 uint filesize=54+(3*image_width+pad)*image_height;
534 unsigned char header[54] = {
551 for(uint i=0; i<4; i++) {
552 header[ 2+i] = (char)((filesize>>(8*i))&255);
553 header[18+i] = (char)((image_width >>(8*i))&255);
554 header[22+i] = (char)((image_height >>(8*i))&255);
557 file.write(header, 54);
560 unsigned char line_data[image_width*3+pad];
562 for(
int i=(image_width-1)*3; i<(image_width*3+pad); i++){
567 for(
int y=image_height; y>0; y--){
569 M5.Lcd.readRectRGB(0, y-1, image_width, 1, line_data);
573 for(
int x=0; x<image_width; x++){
574 unsigned char r_buff = line_data[x*3];
575 line_data[x*3] = line_data[x*3+2];
576 line_data[x*3+2] = r_buff;
579 file.write(line_data, (image_width*3)+pad);
586 SerialDebug.printf(
" *** Cannot open file: %s\n", path);
597#define SPIFF_SCREEN_FILE_NAME (char*)"/M5Screen.bmp"
599#ifdef M5_CAPTURE_SCREEN
600 boolean linesMax = M5Screen2bmp(SPIFFS, SPIFF_SCREEN_FILE_NAME);
602 listDir(SPIFFS,
"/", 0);
void sendMessageNoChangeMQTT(char *message)
just send a message but without any extras
int getTimeStamp_mainModule()
boolean getPreferenceBoolean_mainModule(int preferenceID)
called to set a preference (which will be an identifier and a string, which can be converted to a num...
#define PREFERENCE_USE_SPIFF_SETTING
8.22.22 to turn on/off SPIFF use
void loop_SPIFFModule()
a loop if anything (nothing right now)
void println_SPIFFModule(char *string)
print a string to spiff (a new line is added)
void printInt_SPIFFModule(int val)
print a int to spiff (NO new line is added)
void sendStrings_SPIFFModule(int numberOfLines)
sends SPIFF module strings over MQTT, starting at the number back specified. This will use the curren...
void printTimestamp_SPIFFModule()
prints a timestamp
void printFile_SPIFFModule()
prints the spiff file to the SerialDebug output
void print_SPIFFModule(char *string)
print a string to spiff (NO new line is added)
void deleteFiles_SPIFFModule()
delete the spiff files..
void setup_SPIFFModule()
the setup for this module
void saveScreen_SPIFFModule()
save the screen to a file on the SPIFF