-
Notifications
You must be signed in to change notification settings - Fork 1
/
LittleFS.cpp
151 lines (131 loc) · 2.96 KB
/
LittleFS.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "r4.h"
#ifdef __TEENSY_FS__
// LittleFS uses NEXT
#undef NEXT
#include <LittleFS.h>
LittleFS_Program myFS;
#define NFILES 20
File files[NFILES+1];
void fileInit() {
myFS.begin(1 * 1024 * 1024);
// myFS.quickFormat();
printString("\r\nLittleFS: initialized");
printStringF("\r\nBytes total: %llu, used: %llu", myFS.totalSize(), myFS.usedSize());
for (int i=0;i<=NFILES;i++) { files[i] = File(); }
}
int freeFile() {
for (int i=1;i<=NFILES;i++) {
if ((bool)files[i] == false) { return i; }
}
return 0;
}
CELL_T fileOpenI(const char *fn, const char *md) {
int fh = freeFile();
if (0 < fh) {
files[fh] = myFS.open(fn, (md[0]=='w') ? FILE_WRITE_BEGIN : FILE_READ);
if (md[0]=='w') { files[fh].truncate(); }
}
return fh;
}
// fO (fn md -- fh) - FileOpen
void fileOpen() {
char *md = (char *)pop();
char *fn = AOS;
TOS = fileOpenI(fn, md);
}
// fC (fh--) - File Close
void fileClose() {
CELL_T fh = pop();
if (BTWI(fh, 1, NFILES) &&((bool)files[fh])) {
files[fh].close();
}
}
// fD (nm--) - File Delete
void fileDelete() {
char *nm = (char *)pop();
myFS.remove(nm);
}
// fR (fh--c n) - File Read
// fh: File handle, c: char read, n: num chars read
// n=0: End of file or file error
void fileRead() {
int ndx = TOS;
push(0);
NOS = TOS = 0;
if (BTWI(ndx, 1, NFILES)) {
char c;
TOS = files[ndx].read(&c, 1);
NOS = TOS ? c : 0;
}
}
// fW (c fh--n) - File Write
// fh: File handle, c: char to write, n: num chars written
// n=0: File not open or error
void fileWrite() {
int fh = pop();
char c = (char)TOS;
TOS = 0;
if (BTWI(fh, 1, NFILES)) {
TOS = files[fh].write(&c,1);
}
}
int readBlock(int num, char *blk, int sz) {
char fn[32];
sprintf(fn, BLOCK_FN, num);
num = 0;
File x = myFS.open(fn, FILE_READ);
if ((bool)x) {
num = x.read(blk, sz);
x.close();
}
return num;
}
// bR (num addr sz--f)
void readBlock1() {
CELL_T t1 = pop();
char *n1 = (char*)pop();
TOS = readBlock(TOS, (char*)n1, t1);
}
int writeBlock(int num, char *blk, int sz) {
char fn[32];
sprintf(fn, BLOCK_FN, num);
num = 0;
File x = myFS.open(fn, FILE_WRITE_BEGIN);
if ((bool)x) {
x.truncate();
num = x.write(blk, sz);
x.close();
}
return num;
}
// bW (num addr sz--f)
void writeBlock1() {
CELL_T t1 = pop();
char *n1 = (char*)pop();
TOS = writeBlock(TOS, (char*)n1, t1);
}
// fL - File readLine
int fileReadLine(CELL_T fh, char *buf) {
int n = -1;
buf[0] = 0;
if (BTWI(fh, 1, NFILES) && (0 < files[fh].available())) {
n = files[fh].readBytesUntil(10, buf, 256);
buf[n] = 0;
}
return n;
}
// bL - Block Load
void blockLoad(CELL_T num) {
char fn[32];
sprintf(fn, BLOCK_FN, num);
CELL_T fh = fileOpenI(fn, "r");
if (files[fh].available()) {
if (input_fp) {
fpush(input_fp);
}
input_fp = fh;
}
}
// bA - Block load Abort
void loadAbort() {}
#endif // __TEENSY_FS__