Skip to content

Instantly share code, notes, and snippets.

@pfeerick
Created August 8, 2020 06:02
Show Gist options
  • Select an option

  • Save pfeerick/7230ce3b93c35c8b8feb4e97979ac3f7 to your computer and use it in GitHub Desktop.

Select an option

Save pfeerick/7230ce3b93c35c8b8feb4e97979ac3f7 to your computer and use it in GitHub Desktop.
flashlog testing
#include "flashLOG.h"
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;
}
#ifndef FLASH_LOG
#define FLASH_LOG
#include <FS.h>
#include <LittleFS.h>
class flashLOG {
private:
char *_logfilename;
int _log_length;
char _log_array[50][50];
public:
flashLOG(char *filename) ;
void start();
void read();
void write(const char *message);
void append(const char *message);
void postlog(int x);
int sizelog();
};
#endif
#include <FS.h>
#include <LittleFS.h>
#include "flashLOG.h"
char logName[] = "log.txt";
flashLOG logFile(logName);
void setup() {
Serial.begin(115200);
Serial.println("Mount LittleFS");
if (!LittleFS.begin()) {
Serial.println("LittleFS mount failed");
Serial.println("Formatting LittleFS filesystem");
LittleFS.format();
Serial.println("Restarting to try again");
ESP.restart();
delay(100);
return;
}
Serial.println("=== Prior events in flashLOG ===");
logFile.read();
logFile.write("setup complete!");
}
void loop() {
for (int i = 0; i < 5; i++) {
logFile.write("loop!");
delay(500);
}
ESP.restart();
delay(100);
}
ets Jan 8 2013,rst cause:2, boot mode:(3,6)
load 0x4010f000, len 3584, room 16
tail 0
chksum 0xb0
csum 0xb0
v2843a5ac
~ld
Mount LittleFS
Append OK
file size: 55
Append OK
file size: 60
Append OK
file size: 65
Append OK
file size: 70
Append OK
file size: 75
Append OK
file size: 80
ets Jan 8 2013,rst cause:2, boot mode:(3,6)
load 0x4010f000, len 3584, room 16
tail 0
chksum 0xb0
csum 0xb0
v2843a5ac
~ld
Mount LittleFS
Append OK
file size: 95
Append OK
file size: 100
Append OK
file size: 105
Append OK
file size: 110
Append OK
file size: 115
Append OK
file size: 120
ets Jan 8 2013,rst cause:2, boot mode:(3,6)
load 0x4010f000, len 3584, room 16
tail 0
chksum 0xb0
csum 0xb0
v2843a5ac
~ld
Mount LittleFS
Append OK
file size: 135
Append OK
file size: 140
Append OK
file size: 145
Append OK
file size: 150
Append OK
file size: 155
Append OK
file size: 160
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment