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());
54 SerialDebug.print(
" FILE: ");
55 SerialDebug.print(file.name());
56 SerialDebug.print(
"\tSIZE: ");
57 SerialDebug.println(file.size());
59 file = root.openNextFile();
69 SerialDebug.printf(
"Reading file: %s\r\n", path);
70 SerialDebug.printf(
" *** Done reading ****");
72 File file =
fs.open(path);
73 if(!file || file.isDirectory()){
74 SerialDebug.println(
"- failed to open file for reading");
78 SerialDebug.println(
"- read from file:");
79 while(file.available()){
80 SerialDebug.write(file.read());
86void writeFile(fs::FS &
fs,
const char * path,
const char * message)
91 SerialDebug.printf(
"Writing file: %s\r\n", path);
93 File file =
fs.open(path, FILE_WRITE);
95 SerialDebug.println(
"- failed to open file for writing");
98 if(file.print(message)){
99 SerialDebug.println(
"- file written");
101 SerialDebug.println(
"- write failed");
112 SerialLots.printf(
"Appending to file: %s\r\n", path);
114 File file =
fs.open(path, FILE_APPEND);
116 SerialDebug.println(
"- failed to open file for appending");
119 if(file.print(message)){
120 SerialLots.println(
"- message appended");
122 SerialDebug.println(
"- append failed");
133 SerialMin.printf(
"Renaming file %s to %s\r\n", path1, path2);
134 if (
fs.rename(path1, path2)) {
135 SerialMin.println(
"- file renamed");
137 SerialDebug.println(
"- rename failed");
147 SerialMin.printf(
"Deleting file: %s\r\n", path);
149 SerialMin.println(
"- file deleted");
151 SerialDebug.println(
"- delete failed");
156void testFileIO(fs::FS &
fs,
const char * path)
158 SerialLots.printf(
"Testing file I/O with %s\r\n", path);
160 static uint8_t buf[512];
162 File file =
fs.open(path, FILE_WRITE);
164 SerialDebug.println(
"- failed to open file for writing");
169 SerialDebug.print(
"- writing" );
170 uint32_t start =
millis();
171 for(i=0; i<2048; i++){
172 if ((i & 0x001F) == 0x001F){
173 SerialDebug.print(
".");
175 file.write(buf, 512);
177 SerialDebug.println(
"");
178 uint32_t end =
millis() - start;
179 SerialDebug.printf(
" - %u bytes written in %u ms\r\n", 2048 * 512, end);
182 file =
fs.open(path);
186 if(file && !file.isDirectory()){
190 SerialDebug.print(
"- reading" );
196 file.read(buf, toRead);
197 if ((i++ & 0x001F) == 0x001F){
198 SerialDebug.print(
".");
202 SerialDebug.println(
"");
204 SerialDebug.printf(
"- %u bytes read in %u ms\r\n", flen, end);
207 SerialDebug.println(
"- failed to open file for reading");
245 sprintf(str,
"%d",val);
266 File file =
fs.open(path);
268 if(!file || file.isDirectory()){
269 SerialDebug.println(
"- failed to open file for reading");
274 while(file.available())
276 String line = file.readString();
281 len += line.length();
297 File file =
fs.open(path);
298 if(!file || file.isDirectory()){
299 SerialDebug.println(
"- failed to open file for reading");
303 while(file.available()){
312#define BUFFER_MAX 15000f
325 SerialDebug.printf(
"sendStrings_SPIFFModule (%d)\n", len);
331 SerialDebug.println(
"sendStrings_SPIFFModule");
336 SerialDebug.printf(
"linesMax = %d\n", linesMax);
347 int skipLines = linesMax - numberOfLines;
353 File file =
fs.open(path);
354 if(!file || file.isDirectory()){
355 SerialDebug.println(
"- failed to open file for reading");
360 while(file.available() && counter < skipLines){
364#define POST_AS_BUFFER
369 _buffer = (
char*) calloc(1,BUFFER_MAX);
378 while(file.available() && counter < numberOfLines)
380 String line = file.readString();
381 SerialDebug.println(line);
387 if (strlen(_buffer) > line.length() + 100)
391 SerialDebug.println(
" **** BUFFER too big stopping");
397 strcpy (_buffer, line.c_str());
408 SerialDebug.printf(
"now publishBinary(%d)\n", strlen(_buffer));
409 publishBinaryFile((
char*)
"usersP/bark/images",(uint8_t*) _buffer, strlen(_buffer),
"json");
427 File file = SPIFFS.open(fileName, FILE_WRITE);
429 SerialDebug.println(
"- failed to open file for writing");
432 if(file.write(buf,len)){
433 SerialDebug.println(
"- file written");
435 SerialDebug.println(
"- write failed");
443 SerialDebug.println(
"SPIFFS setup");
447 SerialDebug.println(
"not being used: SPIFFS");
453 SerialDebug.println(
"SPIFFS Mount Failed");
459 boolean test1 =
false;
466 boolean test2 =
false;
517#ifdef M5_CAPTURE_SCREEN
524bool M5Screen2bmp(WiFiClient &client){
525 TFT_eSprite canvas = TFT_eSprite(&M5.Lcd);
527 int image_height = M5.Lcd.height();
528 int image_width = M5.Lcd.width();
531 canvas.createSprite(image_width, image_height);
533 canvas.fillSprite(RED);
534 canvas.fillCircle(100,100,20,GREEN);
535 canvas.pushSprite(0,0,WHITE);
537 const uint pad=(4-(3*image_width)%4)%4;
538 uint filesize=54+(3*image_width+pad)*image_height;
539 unsigned char header[54] = {
556 for(uint i=0; i<4; i++) {
557 header[ 2+i] = (char)((filesize>>(8*i))&255);
558 header[18+i] = (char)((image_width >>(8*i))&255);
559 header[22+i] = (char)((image_height >>(8*i))&255);
562 client.write(header, 54);
565 unsigned char line_data[image_width*3+pad];
567 for(
int i=(image_width-1)*3; i<(image_width*3+pad); i++){
572 for(
int y=image_height; y>0; y--){
575 canvas.readRectRGB(0, y-1, image_width, 1, line_data);
580 for(
int x=0; x<image_width; x++){
581 unsigned char r_buff = line_data[x*3];
582 SerialTemp.print(r_buff);
583 line_data[x*3] = line_data[x*3+2];
584 line_data[x*3+2] = r_buff;
586 SerialTemp.println();
588 client.write(line_data, (image_width*3)+pad);
610bool M5Screen2bmp(fs::FS &
fs,
const char * path){
613 File file =
fs.open(path, FILE_WRITE);
618 int image_height = M5.Lcd.height();
619 int image_width = M5.Lcd.width();
620 SerialDebug.printf(
"Saving screen to bmp file (%d,%d)\n", image_width, image_height);
624 const uint pad=(4-(3*image_width)%4)%4;
628 uint filesize=54+(3*image_width+pad)*image_height;
629 unsigned char header[54] = {
646 for(uint i=0; i<4; i++) {
647 header[ 2+i] = (char)((filesize>>(8*i))&255);
648 header[18+i] = (char)((image_width >>(8*i))&255);
649 header[22+i] = (char)((image_height >>(8*i))&255);
652 file.write(header, 54);
655 unsigned char line_data[image_width*3+pad];
657 for(
int i=(image_width-1)*3; i<(image_width*3+pad); i++){
662 for(
int y=image_height; y>0; y--){
664 M5.Lcd.readRectRGB(0, y-1, image_width, 1, line_data);
668 for(
int x=0; x<image_width; x++){
669 unsigned char r_buff = line_data[x*3];
670 line_data[x*3] = line_data[x*3+2];
671 line_data[x*3+2] = r_buff;
674 file.write(line_data, (image_width*3)+pad);
681 SerialDebug.printf(
" *** Cannot open file: %s\n", path);
692#define SPIFF_SCREEN_FILE_NAME (char*)"/M5Screen.bmp"
694#ifdef M5_CAPTURE_SCREEN
void publishBinaryFile(char *topic, uint8_t *buf, size_t len, String fileExtension)
void publishSPIFFFile_MQTT(char *topic, char *path, int len)
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 (more below..)
#define PREFERENCE_USE_SPIFF_QRATOM_SETTING
For MQTT writing to the QRATOM.
void loop_SPIFFModule()
a loop if anything (nothing right now)
void println_SPIFFModule(char *string)
The SPIFF module is for storing messages that are retrievable later as it stores on a folder area of ...
void printInt_SPIFFModule(int val)
print a int to spiff (NO new line is added)
void appendFile(fs::FS &fs, const char *path, const char *message)
appends the message to the file
void readFile(fs::FS &fs, const char *path)
reads the file name specified. The result is SerialDebug.write
int linesInFile_SPIFFModule(fs::FS &fs, const char *path)
reads the file name specified. returning the number of lines..
void deleteFile(fs::FS &fs, const char *path)
deletes the file specified
#define SPIFF_SCREEN_FILE_NAME
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 time: <time> :
void renameFile(fs::FS &fs, const char *path1, const char *path2)
renames the file
void printFile_SPIFFModule()
prints the spiff file to the SerialDebug output
#define FORMAT_SPIFFS_IF_FAILED
void listDir(fs::FS &fs, const char *dirname, uint8_t levels)
list the directory of the SPIFF
void print_SPIFFModule(char *string)
print a string to spiff (NO new line is added)
int len_SPIFFFile(fs::FS &fs, const char *path)
calculate the length of the SPFF file
boolean useSpiff()
helper for using the spiff or not..
void deleteFiles_SPIFFModule()
delete the spiff files..
void writeFB_SPIFFModule(uint8_t *buf, size_t len, char *fileName)
writes a FB to a file..
void println_SPIFFModule_JSON(char *attribute, char *value)
4.4.24 output a line in JSON format adding timestamp as well
void printSM_SPIFFModule(char *semanticMarker)
void writeFile(fs::FS &fs, const char *path, const char *message)
writes the message to the file specified
void setup_SPIFFModule()
the setup for this module
void saveScreen_SPIFFModule()
save the screen to a file on the SPIFF