-
Notifications
You must be signed in to change notification settings - Fork 5
/
fs-pico.cpp
139 lines (118 loc) · 2.84 KB
/
fs-pico.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
// Support for LittleFS on the Pico
#include "c3.h"
#ifdef _PicoFS_
#include <LittleFS.h>
#define myFS LittleFS
#define NFILES 20
File files[NFILES+1];
void fileInit() {
printString("\r\nLittleFS: begin ...");
myFS.begin();
printString("done.\r\n");
FSInfo fsinfo;
LittleFS.info(fsinfo);
printString("LittleFS: initialized\r\n");
//printStringF("\r\nBytes total: %llu, used: %llu", fsinfo.totalBytes, fsinfo.usedBytes);
input_fp = input_sp = 0;
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 fOpen(const char *fn, const char *mode) {
int fh = freeFile();
if (0 < fh) {
files[fh] = myFS.open((char*)fn, mode);
if (files[fh].name() == nullptr) { return 0; }
if (mode[0] == 'w') { files[fh].truncate(0); }
}
return fh;
}
// fO (fn md -- fh) - FileOpen
void fOpenF() {
char *md = (char *)pop();
char *fn = (char *)pop();
push(fOpen(fn, md));
}
// fC (fh--) - File Close
void fClose(CELL_T fh) {
if (BTW(fh, 1, NFILES) &&((bool)files[fh])) {
files[fh].close();
}
}
// fD (nm--) - File Delete
void fDelete() {
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
int fRead(char *buf, int num, int sz, CELL_T fh) {
return (BTW(fh, 1, NFILES)) ? (int)files[fh].read((uint8_t*)buf, num*sz) : 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
int fWrite(char *buf, int num, int sz, CELL_T fh) {
return (BTW(fh, 1, NFILES)) ? (int)files[fh].write(buf, num*sz) : 0;
}
int readBlock(int blk, char *buf, int sz) {
int num = 0;
File x = myFS.open(getBlockFN(blk), "r");
if ((bool)x) {
num = (int)x.read((uint8_t*)buf, sz);
x.close();
}
return num;
}
// bR (blk buf sz--f)
void readBlock1() {
CELL_T sz = pop();
char *buf = (char*)pop();
CELL_T blk = pop();
push(readBlock(blk, (char*)buf, sz));
}
int writeBlock(int blk, char *buf, int sz) {
int num = 0;
File x = myFS.open(getBlockFN(blk), "w");
if ((bool)x) {
x.truncate(0);
num = x.write((uint8_t*)buf, sz);
x.close();
}
return num;
}
// bW (blk buf sz--f)
void writeBlock1() {
CELL_T sz = pop();
char *buf = (char*)pop();
CELL_T blk = pop();
push(writeBlock(blk, buf, sz));
}
// fL - File readLine
int fGets(CELL_T fh, char *buf, int sz) {
int n = -1;
buf[0] = 0;
if (BTW(fh, 1, NFILES) && (0 < files[fh].available())) {
n = files[fh].readBytesUntil(10, buf, sz);
buf[n] = 0;
}
return n;
}
// bL - Block Load
void blockLoad(int blk) {
CELL_T fh = fOpen(getBlockFN(blk), "r");
if (files[fh].available()) {
if (input_fp) {
fpush(input_fp);
}
input_fp = fh;
}
}
// bA - Block load Abort
void loadAbort() {}
#endif