I'm trying to create an even log saved on flash.
I try to make it a class, called flashLOG as shown. Code behaves as expceted, reads, writes and posts it size and entries.
The problem is that after reboot - it seems that the file "disappears".
Code is written based on the Arduino IDE example. First I thought it is a flash problem, but after flashing Arduino's example and tried to run my code on an other MCU - it seems there no physical problem with flash.
I tried to reduce most of my code only to write "events" in order to see what survives reboot - so please ignore commented out lines and some logic in reading text, in code attached.
The only thing came in mind- perhaps it has something to do with being in a class or inside a library.
Appreciate any help finding this bug **EDIT 1: ** Update .h file and .ino fil
.h file
class flashLOG {
private:
char *_logfilename = "/logfile.txt";
const static int _logsize = 5; // entries
const static int _log_length = 20; // chars in each entry
char _log_array[_logsize][_log_length];
public:
flashLOG(char *filename = "/logfile.txt");
void start();
void read();
void write(const char *message);
void append(const char *message);
void postlog(int x);
int sizelog();
};
.cpp file:
flashLOG::flashLOG(char *filename)
{
_logfilename = filename;
}
void flashLOG::start() {
if (!LittleFS.begin())
{
Serial.println("LittleFS mount failed");
}
else {
Serial.println("MOUNTED OK");
}
}
void flashLOG::read() {
int r = 0;
int c = 0;
File file = LittleFS.open(_logfilename, "r");
if (!file)
{
Serial.println("Failed to open file for reading");
}
else {
while (file.available())
{
char tt = file.read();
// if (tt != '\n')
// {
// _log_array[r][c] = tt;
// c++;
// }
// else
// {
// _log_array[r][c] = '\n';
// r++;
// c = 0;
// // Serial.println(_log_array[r-1]);
// }
Serial.write(tt);
}
}
file.close();
// return r;
}
void flashLOG::write(const char *message)
{
char a[_log_length];
// sprintf(a, "%s\n", message);
File file = LittleFS.open(_logfilename, "a");
if (!file)
{
Serial.println("Failed to open file for appending");
}
else {
if (file.print(message)) {
Serial.println("Append OK");
}
else {
Serial.println("Append fail");
}
}
Serial.printf("file size: %d\n", file.size());
delay(2000);
file.close();
}
void flashLOG::append(const char *message)
{
char a[_log_length];
// sprintf(a, "%s\n", message);
File file = LittleFS.open(_logfilename, "a");
if (!file)
{
Serial.println("Failed to open file for appending");
}
else {
if (file.print(message)) {
Serial.println("Append OK");
}
else {
Serial.println("Append fail");
}
}
Serial.printf("file size: %d\n", file.size());
// delay(2000);
file.close();
}
void flashLOG::postlog(int x)
{
int num_lines = 0;//read();
int y = min(x, num_lines);
for (int a = 0; a < y; a++)
{
if (y > 1)
{
char t[_log_length];
sprintf(t, "[#%d] %s", a, _log_array[a]);
Serial.println(t);
}
else
{
Serial.println(_log_array[a]);
}
}
}
int flashLOG::sizelog()
{
File file = LittleFS.open(_logfilename, "r");
int f=file.size();
file.close();
return f;
}
.ino file
#include <myIOT.h>
#include <Arduino.h>
flashLOG flog;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("\nBEGIN!");
flog.start();
// flog.write("HITHERE");
flog.read();
flog.append("HITHERE2");
// flog.read();
}
void loop() {
// put your main code here, to run repeatedly:
}