-
Notifications
You must be signed in to change notification settings - Fork 5
/
fs-teensy.cpp
141 lines (119 loc) · 2.96 KB
/
fs-teensy.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
// Support for LittleFS on the Teensy
#include "c3.h"
#ifdef _TeensyFS_
// The Teensy LittleFS uses NEXT
#ifdef NEXT
#undef NEXT
#endif
#include <LittleFS.h>
LittleFS_Program myFS;
#define NFILES 20
File files[NFILES+1];
void fileInit() {
myFS.begin(1 * 1024 * 1024);
// myFS.quickFormat();
printString("\nLittleFS: initialized\n");
//printStringF("\r\nBytes total: %llu, used: %llu", myFS.totalSize(), myFS.usedSize());
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();
int wr = (mode[0] == 'w') ? 1 : 0;
if (0 < fh) {
files[fh] = myFS.open((char*)fn, (wr) ? FILE_WRITE_BEGIN : FILE_READ);
if (wr) { files[fh].truncate(); }
}
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(buf, num) : -1;
}
// 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) : -1;
}
int readBlock(int blk, char *buf, int sz) {
int num = 0;
File x = myFS.open(getBlockFN(blk), FILE_READ);
if ((bool)x) {
num = (int)x.read(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), FILE_WRITE_BEGIN);
if ((bool)x) {
x.truncate();
num = x.write(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