forked from euphy/polargraph_server_a1
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sd.ino
344 lines (292 loc) · 8.38 KB
/
sd.ino
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/**
* Polargraph Server for ATMEGA1280+
* Written by Sandy Noble
* Released under GNU License version 3.
* http://www.polargraph.co.uk
* https://github.com/euphy/polargraph_server_polarshield
Specific features for Polarshield / arduino mega.
SD.
For initialising, reading and writing the SD card data store.
*/
/* ==============================================================
Processing the SD card if there is one attached.
=================================================================*/
#if MICROCONTROLLER == MC_MEGA
void sd_initSD()
{
currentlyDrawingFromFile = false;
currentlyDrawingFilename = "";
cardPresent = false;
cardInit = false;
commandFilename = "";
// sd_alternativeInit();
sd_simpleInit();
}
void sd_simpleInit() {
pinMode(chipSelect, OUTPUT); // necessary for SD card reading to work
// see if the card is present and can be initialized:
int initValue = 0;
initValue = SD.begin(chipSelect);
if (initValue == 0) {
Serial.println("Card failed, or not present");
}
else {
Serial.println("Successfully beginned.");
cardPresent = true;
}
if (cardPresent) {
Serial.println("card initialized.");
root = SD.open("/", FILE_READ);
// File entry = root.openNextFile();
// entry.close();
cardInit = true;
sd_printDirectory(root, 0);
Serial.println("done!");
}
}
void sd_alternativeInit() {
Sd2Card card;
SdVolume volume;
SdFile sdFile;
if (!card.init(SPI_HALF_SPEED, chipSelect))
{
Serial.println("initialization failed. Things to check:");
Serial.println("* is a card is inserted?");
Serial.println("* Is your wiring correct?");
Serial.println("* did you change the chipSelect pin to match your shield or module?");
return;
}
else
{
Serial.println("Wiring is correct and a card is present.");
}
// print the type of card
Serial.print("\nCard type: ");
switch(card.type()) {
case SD_CARD_TYPE_SD1:
Serial.println("SD1");
break;
case SD_CARD_TYPE_SD2:
Serial.println("SD2");
break;
case SD_CARD_TYPE_SDHC:
Serial.println("SDHC");
break;
default:
Serial.println("Unknown");
}
// Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
if (!volume.init(card)) {
Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
return;
}
// print the type and size of the first FAT-type volume
uint32_t volumesize;
Serial.print("\nVolume type is FAT");
Serial.println(volume.fatType(), DEC);
Serial.println();
volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
volumesize *= volume.clusterCount(); // we'll have a lot of clusters
volumesize *= 512; // SD card blocks are always 512 bytes
Serial.print("Volume size (bytes): ");
Serial.println(volumesize);
Serial.print("Volume size (Kbytes): ");
volumesize /= 1024;
Serial.println(volumesize);
Serial.print("Volume size (Mbytes): ");
volumesize /= 1024;
Serial.println(volumesize);
// Serial.println("\nFiles found on the card (name, date and size in bytes): ");
sdFile.openRoot(volume);
// list all files in the card with date and size
sdFile.ls(LS_R | LS_DATE | LS_SIZE);
cardPresent = true;
cardInit = true;
}
/*
http://stackoverflow.com/questions/18158136/why-cant-i-pass-typedef-or-enum-in-arduino
Using struct File here so that the SD library does NOT need to be included.
*/
void sd_printDirectory(struct File dir, int numTabs) {
while(true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
Serial.println("...");
break;
}
for (uint8_t i=0; i<numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println("/");
sd_printDirectory(entry, numTabs+1);
} else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}
void sd_storeCommand(String command)
{
// delete file if it exists
char filename[commandFilename.length()+1];
commandFilename.toCharArray(filename, commandFilename.length()+1);
File storeFile = SD.open(filename, FILE_WRITE);
// if the file opened okay, write to it:
if (storeFile)
{
Serial.print("Writing to file ");
Serial.println(commandFilename);
storeFile.println(command);
// close the file:
storeFile.close();
Serial.println("done.");
}
else
{
// if the file didn't open, print an error:
Serial.print("error opening ");
Serial.println(commandFilename);
}
}
/**
* Most of this bmp image opening / handling stuff only slightly adapted from
* Adafruit's marvellous stuff.
https://github.com/adafruit/TFTLCD-Library/blob/master/examples/tftbmp/tftbmp.pde
*/
boolean sd_openPbm(String pbmFilename)
{
char filename[pbmFilename.length()+1];
pbmFilename.toCharArray(filename, pbmFilename.length()+1);
pbmFile = SD.open(filename, FILE_READ);
if (! pbmFile)
{
Serial.println("didnt find image");
return false;
}
if (! sd_pbmReadHeader())
{
Serial.println("bad pbm");
return false;
}
pbmFileLength = pbmFile.size();
return true;
}
byte sd_getBrightnessAtPixel(int x, int y)
{
Serial.print("Pixel x:");
Serial.print(x);
Serial.print(", y:");
Serial.println(y);
Serial.print("PbmImageOffset:");
Serial.println(pbmImageoffset);
Serial.print("pbmWidth:");
Serial.println(pbmWidth);
long addressToSeek = (pbmImageoffset + (y * pbmWidth) + x);
Serial.print("Address:");
Serial.print(addressToSeek);
if (addressToSeek > pbmFileLength)
{
return -1;
}
else
{
pbmFile.seek(addressToSeek);
byte pixelValue = pbmFile.read();
Serial.print(", Pixel value:");
Serial.println(pixelValue);
return pixelValue;
}
}
boolean sd_pbmReadHeader()
{
pbmFile.seek(0);
// read header
char buf;
String magicNumber = " ";
buf = pbmFile.read();
magicNumber[0] = buf;
buf = pbmFile.read();
magicNumber[1] = buf;
if (magicNumber != "P5")
{
Serial.print("This isn't a P5 file. It's a ");
Serial.print(magicNumber);
Serial.println(" file, and that's no good.");
return false;
}
else
Serial.println("This is a very good file Herr Doktor!");
buf = pbmFile.read(); // this is a blank
// get image width
String numberString = "";
buf = pbmFile.read();
// photoshop puts a linebreak (0A) inbetween the width & height,
// GIMP puts a space (20).
while (buf != 0x0A && buf != 0x20)
{
// check for comments, these start with a # - hex 23
if (buf == 0x23)
{
while (buf != 0x0A)
buf = pbmFile.read(); // just loop through until we get to the end of the comment
}
numberString = numberString + buf;
buf = pbmFile.read();
}
Serial.print("PBM width:");
Serial.println(numberString);
char paramChar[numberString.length() + 1];
numberString.toCharArray(paramChar, numberString.length() + 1);
pbmWidth = atoi(paramChar);
if (pbmWidth < 10)
{
Serial.println(F("PBM image must be at least 10 pixels wide."));
return false;
}
// get image height
numberString = "";
buf = pbmFile.read();
while (buf != 0x0A)
{
// check for comments, these start with a # - hex 23
if (buf == 0x23)
{
while (buf != 0x0A)
buf = pbmFile.read(); // just loop through until we get to the end of the comment
}
numberString = numberString + buf;
buf = pbmFile.read();
}
Serial.print("PBM height:");
Serial.println(numberString);
paramChar[numberString.length() + 1];
numberString.toCharArray(paramChar, numberString.length() + 1);
pbmHeight = atoi(paramChar);
// work out aspect ratio
pbmAspectRatio = float(pbmHeight) / float(pbmWidth);
Serial.print("PBM aspect ratio:");
Serial.println(pbmAspectRatio);
// get image depth
numberString = "";
buf = pbmFile.read();
while (buf != 0x0A)
{
numberString = numberString + buf;
buf = pbmFile.read();
}
Serial.print("Numberstring depth:");
Serial.println(numberString);
paramChar[numberString.length() + 1];
numberString.toCharArray(paramChar, numberString.length() + 1);
pbmDepth = atoi(paramChar);
pbmImageoffset = pbmFile.position();
Serial.print("Image offset:");
Serial.println(pbmImageoffset);
return true;
}
#endif