-
Notifications
You must be signed in to change notification settings - Fork 0
/
BayEOSBufferSPIFFS.cpp
137 lines (120 loc) · 2.93 KB
/
BayEOSBufferSPIFFS.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "BayEOSBufferSPIFFS.h"
BayEOSBufferSPIFFS::BayEOSBufferSPIFFS(unsigned long max_length, uint8_t append,
const char *f) :
BayEOSBuffer() {
strncpy(_filename, f, 12);
_filename[12] = 0;
#if SERIAL_DEBUG
Serial.println(_filename);
Serial.println("open file:");
delay(100);
#endif
_f = SPIFFS.open(_filename, "a+");
_max_length = max_length;
#if SERIAL_DEBUG
Serial.println(_f.size());
#endif
if (append)
set(_f.size());
else
reset();
}
void BayEOSBufferSPIFFS::resetStorage(void) {
_f.close();
SPIFFS.remove(_filename);
_f = SPIFFS.open(_filename, "w+");
// Serial.println("SPIFFS reset");
}
uint8_t BayEOSBufferSPIFFS::write(const uint8_t b) {
return _f.write(b);
}
uint8_t BayEOSBufferSPIFFS::write(const uint8_t *b, uint8_t length) {
// Serial.print("SPIFFS.write:");
// Serial.println(_f.size());
return _f.write(b, length);
}
uint8_t BayEOSBufferSPIFFS::seek(unsigned long pos) {
return _f.seek(pos, SeekSet);
}
int BayEOSBufferSPIFFS::read(void) {
return _f.read();
}
int BayEOSBufferSPIFFS::read(uint8_t *dest, int length) {
return _f.read(dest, length);
}
void BayEOSBufferSPIFFS::flush(void) {
_f.flush();
// Serial.println(_f.size());
}
BayEOSBufferSPIFFS2::BayEOSBufferSPIFFS2(unsigned long max_length) :
BayEOSBuffer() {
_max_length = max_length;
}
void BayEOSBufferSPIFFS2::init(void) {
_old_size=0;
if (SPIFFS.exists("bayeos.odb")) {
// Serial.println("existing file");
_o = SPIFFS.open("bayeos.odb","r");
_old_size=_o.size();
}
_f = SPIFFS.open("bayeos.db", "a+");
set(_old_size+_f.size());
// Serial.printf("init %d",_old_size);
_end=0;
}
void BayEOSBufferSPIFFS2::resetStorage(void) {
_f.close();
_o.close();
SPIFFS.remove("bayeos.db");
SPIFFS.remove("bayeos.odb");
init();
// Serial.println("SPIFFS reset");
}
uint8_t BayEOSBufferSPIFFS2::write(const uint8_t b) {
return _f.write(b);
}
uint8_t BayEOSBufferSPIFFS2::write(const uint8_t *b, uint8_t length) {
// Serial.print("SPIFFS.write:");
// Serial.println(_f.size());
return _f.write(b, length);
}
uint8_t BayEOSBufferSPIFFS2::seek(unsigned long pos) {
if(pos>=_old_size)
return _f.seek(pos - _old_size, SeekSet);
else
return _o.seek(pos);
}
int BayEOSBufferSPIFFS2::read(void) {
if(_pos>=_old_size)
return _f.read();
else
return _o.read();
}
int BayEOSBufferSPIFFS2::read(uint8_t *dest, int length) {
if(_pos>=_old_size)
return _f.read(dest, length);
else
return _o.read(dest, length);
}
void BayEOSBufferSPIFFS2::flush(void) {
_f.flush();
if(_pos>=(_max_length/2 - BayEOS_MAX_PAYLOAD) ){
_o.close();
if(_read_pos<_old_size){
_read_pos=0;
} else {
_read_pos-=_old_size;
}
_f.close();
SPIFFS.remove("bayeos.odb");
SPIFFS.rename("bayeos.db","bayeos.odb");
//Serial.println("existing file");
_o = SPIFFS.open("bayeos.odb","r");
_old_size=_o.size();
_f = SPIFFS.open("bayeos.db", "w+");
_write_pos=_old_size;
_end=0;
_pos=_old_size;
}
// Serial.printf("pos: %d - size %d\n",_f.position(),_f.size());
}