-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsdcard.cpp
209 lines (191 loc) · 4.47 KB
/
sdcard.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
// Documentation on defining PXT blocks:
// https://www.pxt.io/defining-blocks
#include "pxt.h"
#include "SDFileSystem.h"
using namespace pxt;
SDFileSystem *sd;
char buf[50];
struct stat buffer;
FILE *f;
DIR *dir;
struct dirent *ent;
enum class Pins{
P0= 3,
P1= 2,
P2= 1,
P3= 4,
P4= 5,
P5= 17,
P6= 12,
P7= 11,
P8= 18,
P9= 10,
P10= 6,
P11= 26,
P12= 20,
P13= 23,
P14= 22,
P15= 21,
P16= 16,
P19= 0,
P20= 30};
//% color=70 weight=80
namespace SD {
/**
* more secret stuff!
*/
//% blockId=sd_open_filehandle
//% block="open a filehandle for %file| for reading? %read|"
void fileHandle(StringData *file, bool read){
sprintf(buf, "/sd/%s", file->data);
if(read) f = fopen(buf, "r");
else f= fopen(buf, "a");
}
/**
* secret stuff!
*/
//% blockId=sd_read_sixteen
//% block="16 bytes from the filehandle"
StringData *readAligned(){
memset(buf, 0, sizeof buf);
fread(buf, sizeof(unsigned char), 16, f);
return ManagedString(buf).leakData();
}
/**
*more secret stuffffff
*/
//% blockId=sd_read_byte
//% block="1 byte from filehandle"
int readSingleByte(){
fread(buf, sizeof(char), 1, f);
return (int)((256 + buf[0]) % 256);
}
/**
* final secret item
*/
//% blockId=sd_close_filehandle
//% block="close filehandle"
void closeFile(){
fclose(f);
free(f);
}
/**
* i guess it's not so secret
*/
//% blockId=sd_write_filehandle
//% block="write %str| to filehandle"
void writeFileHandle(StringData *str){
fwrite(str->data, sizeof(char), strlen(str->data), f);
}
/**
* reads a file from the sd card.
*/
//% blockId=sd_read_file
//% block="the contents of |%file"
StringData *readFile(StringData *file){
sprintf(buf, "/sd/%s", file->data);
f = fopen(buf, "r");
fseek(f, 0, SEEK_END);
int fsize = ftell(f);
rewind(f);
memset(buf, 0, sizeof buf);
fread(buf, sizeof(char), min(fsize, 50), f);
fclose(f);
free(f);
return ManagedString(buf).leakData();
}
/**
* creates a file on the sd card.
*/
//% blockId=sd_create_file
//% block="create a file called |%file"
void createFile(StringData *file){
sprintf(buf, "/sd/%s", file->data);
f = fopen(buf, "w");
fclose(f);
free(f);
}
/**
* creates a folder on the sd card.
*/
//% blockId=sd_create_folder
//% block="create a folder called |%folder"
void createFolder(StringData *file){
sprintf(buf, "/sd/%s", file->data);
mkdir(buf, 0777);
}
/**
* checks if a file exists (will return false on folder as well)
*/
//% blockId=sd_file_exists
//% block="the file %file| exists"
bool fileExists(StringData *file){
sprintf(buf, "/sd/%s", file->data);
if(stat (buf, &buffer) == 0) return true;
return false;
}
/**
* checks if a folder exists
*/
//% blockId=sd_folder_exists
//% block="the folder %folder| exists"
bool folderExists(StringData *folder){
sprintf(buf, "/sd/%s", folder->data);
dir = opendir(buf);
if(dir == NULL) return false;
closedir(dir);
return true;
}
/**
* returns the amount of stuff in a folder
*/
//% blockId=sd_lscount
//% block="the number of items in folder %folder|"
int contents(StringData *folder){
int ret = 0;
sprintf(buf, "/sd/%s", folder->data);
dir = opendir(buf);
if(dir == NULL) return ret;
while(ent = readdir(dir)) ret++;
closedir(dir);
return ret;
}
/**
* gets the i-th item in a folder
*/
//% blockId=sd_ls
//% block="the %count|th item in %folder|"
StringData *getItem(int count, StringData *folder){
sprintf(buf, "/sd/%s", folder->data);
dir = opendir(buf);
if(dir == NULL) return ManagedString("failed").leakData();
struct dirent *ent;
for(int i=0;i<count;i++){
ent = readdir(dir);
if(ent == NULL) break;
}
if(ent == NULL) {closedir(dir); return ManagedString("failed").leakData();}
else {closedir(dir); return ManagedString(ent->d_name).leakData();}
}
/**
* writes a string to a file.
*/
//% blockId=sd_write_file
//% block="write %stuff| to %file|"
void writeFile(StringData *stuff, StringData *file){
sprintf(buf, "/sd/%s", file->data);
f = fopen(buf, "a");
fwrite(stuff->data, sizeof(char), strlen(stuff->data), f);
fclose(f);
free(f);
}
/**
* initialises the sd card.
*/
//% blockId=sd_init
//% block="connected SD to |%pin"
void connect(Pins pin){
printf("%i != %i?\r\n", pin, (PinName)pin);
sd = new SDFileSystem(MOSI, MISO, SCK, (PinName)pin, "sd");
}
}