This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
files.cpp
253 lines (224 loc) · 5.98 KB
/
files.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include "pxt.h"
#include "MicroBitFile.h"
#include "MicroBitFileSystem.h"
using namespace pxt;
// v0 backward compat support
#ifndef PXT_BUFFER_DATA
#define PXT_BUFFER_DATA(buffer) buffer->payload
#endif
/**
* File seek offset modifier
*/
enum FileSystemSeekFlags {
//% block=set
Set = MB_SEEK_SET,
//% block=current
Current = MB_SEEK_CUR,
//% block=end
End = MB_SEEK_END
};
/**
* File system operations
*/
//% weight=5 color=#002050 icon="\uf0a0"
namespace files
{
// Initializes file system. Must be called before any FS operation.
// built-in size computation for file system
// does not take into account size changes
// for compiled code
void initFileSystem()
{
if (MicroBitFileSystem::defaultFileSystem == NULL)
{
new MicroBitFileSystem(pxt::afterProgramPage());
}
}
/**
* Appends text and a new line to a file
* @param filename file name, eg: "output.txt"
* @param text the string to append to the end of the file
*/
//% blockId="files_append_line" block="file %filename|append line %text"
//% blockExternalInputs=1 weight=90 blockGap=8
void appendLine(StringData *filename, StringData *text)
{
initFileSystem();
ManagedString fn(filename);
ManagedString t(text);
MicroBitFile f(fn);
f.append(t);
f.append("\r\n");
f.close();
}
/**
* Appends text to a file
* @param filename file name, eg: "output.txt"
* @param text the string to append to the end of the file
*/
//% blockId="fs_append_string" block="file %filename|append string %text"
//% blockExternalInputs=1 weight=86 blockGap=8
void appendString(StringData *filename, StringData *text)
{
initFileSystem();
ManagedString fn(filename);
ManagedString t(text);
MicroBitFile f(fn);
f.append(t);
f.close();
}
/**
* Reads the content of the file to send it to serial
* @param filename file name, eg: "output.txt"
*/
//% blockId="fs_write_to_serial" block="file %filename|read to serial"
//% weight=80
void readToSerial(StringData* filename) {
initFileSystem();
ManagedString fn(filename);
MicroBitFile f(fn);
char buf[32];
int read = 0;
while((read = f.read(buf, sizeof(buf) * sizeof(char))) > 0) {
uBit.serial.send((uint8_t*)buf, read * sizeof(char), SYNC_SPINWAIT);
}
f.close();
}
/**
* Removes the file. There is no undo for this operation.
* @param filename name of the file to remove, eg: "output.txt"
*/
//% blockId="fs_remove" block="file remove %filename"
//% weight=80 advanced=true blockGap=8
void remove(StringData *filename)
{
initFileSystem();
ManagedString fn(filename);
MicroBitFileSystem::defaultFileSystem->remove(fn.toCharArray());
}
/**
* Creates a directory
* @param name full qualified path to the new directory
*/
//% advanced=true weight=10
//% blockId=files_create_directory block="files create directory %name"
void createDirectory(StringData* name) {
initFileSystem();
ManagedString fn(name);
MicroBitFileSystem::defaultFileSystem->createDirectory(fn.toCharArray());
}
/**
* Reads a number settings, -1 if not found.
* @param name name of the settings, must be filename compatible, e.g.: setting
*/
//% blockId=settings_read_number block="settings read number %name"
//% weight=19
int settingsReadNumber(StringData* name) {
initFileSystem();
MicroBitFileSystem::defaultFileSystem->createDirectory("settings");
MicroBitFile f("settings/" + ManagedString(name), MB_READ);
if (!f.isValid())
return -1;
ManagedString v;
ManagedString buff;
do {
buff = f.read(32);
v = v + buff;
} while(buff.length() > 0);
return atoi(v.toCharArray());
}
/**
*
*/
//% weight=0 advanced=true
int fsOpen(StringData* path) {
initFileSystem();
ManagedString fn(path);
return MicroBitFileSystem::defaultFileSystem->open(fn.toCharArray(), MB_READ|MB_WRITE|MB_CREAT);
}
/**
*
*/
//% weight=0 advanced=true
int fsFlush(int fd) {
if (fd < 0) return MICROBIT_NOT_SUPPORTED;
initFileSystem();
return MicroBitFileSystem::defaultFileSystem->flush(fd);
}
/**
*
*/
//% weight=0 advanced=true
int fsClose(int fd) {
if (fd < 0) return MICROBIT_NOT_SUPPORTED;
initFileSystem();
return MicroBitFileSystem::defaultFileSystem->close(fd);
}
/**
*
*/
//% weight=0 advanced=true
int fsRemove(StringData* name) {
initFileSystem();
ManagedString fn(name);
return MicroBitFileSystem::defaultFileSystem->remove(fn.toCharArray());
}
/**
*
*/
//% weight=0 advanced=true
int fsSeek(int fd, int offset, int flags) {
if (fd < 0) return MICROBIT_NOT_SUPPORTED;
if (offset < 0) return MICROBIT_INVALID_PARAMETER;
initFileSystem();
return MicroBitFileSystem::defaultFileSystem->seek(fd, offset, flags);
}
/**
*
*/
//% weight=0 advanced=true
int fsWriteString(int fd, StringData* text) {
if (fd < 0) return MICROBIT_NOT_SUPPORTED;
initFileSystem();
ManagedString s(text);
return MicroBitFileSystem::defaultFileSystem->write(fd, (uint8_t*)s.toCharArray(), s.length());
}
/**
*
*/
//% weight=0 advanced=true
int fsWriteBuffer(int fd, Buffer buffer) {
if (fd < 0) return MICROBIT_NOT_SUPPORTED;
initFileSystem();
return MicroBitFileSystem::defaultFileSystem->write(fd, PXT_BUFFER_DATA(buffer), buffer->length);
}
/**
*/
//% weight=0 advanced=true
Buffer fsReadBuffer(int fd, int length) {
if (fd < 0 || length < 0)
return mkBuffer(NULL, 0);
initFileSystem();
Buffer buf = mkBuffer(NULL, length);
int ret = MicroBitFileSystem::defaultFileSystem->read(fd, PXT_BUFFER_DATA(buf), buf->length);
if (ret < 0) return mkBuffer(NULL, 0);
else if (ret != length) {
auto sbuf = mkBuffer(PXT_BUFFER_DATA(buf), ret);
decrRC(buf);
return sbuf;
}
else return buf;
}
/**
*
*/
//% weight=0 advanced=true
int fsRead(int fd) {
if (fd < 0) return MICROBIT_NOT_SUPPORTED;
initFileSystem();
char c[1];
int ret = MicroBitFileSystem::defaultFileSystem->read(fd, (uint8_t*)&c, 1);
if (ret != 1) return ret;
else return c[0];
}
}