-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRogueSD.cpp
More file actions
1277 lines (1021 loc) · 22.5 KB
/
RogueSD.cpp
File metadata and controls
1277 lines (1021 loc) · 22.5 KB
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
||
|| @author Brett Hagman <bhagman@wiring.org.co>
|| @url http://wiring.org.co/
|| @url http://roguerobotics.com/
||
|| @description
|| | Rogue Robotics SD Module Library
|| |
|| | This Wiring and Arduino Library works with the following
|| | Rogue Robotics modules:
|| | - uMMC (SD Card Module)
|| | - uMP3 (Industrial MP3 Playback Module)
|| | - rMP3 (Commercial MP3 Playback Module)
|| |
|| | Requires:
|| | uMMC firmware > 102.01
|| | uMP3 firmware > 111.01
|| | rMP3 firmware > 100.00
|| |
|| | See http://www.roguerobotics.com/faq/update_firmware for updating firmware.
|| #
||
|| @license Please see LICENSE.txt for this project.
||
*/
#include <stdint.h>
#include <ctype.h>
#if WIRING
#include <Wiring.h>
#elif ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#include "RogueSD.h"
/*
|| Private Constants
*/
#define UMMC_MIN_FW_VERSION_FOR_NEW_COMMANDS 10201
#define UMP3_MIN_FW_VERSION_FOR_NEW_COMMANDS 11101
#define STATS_SIZE 0
#define STATS_POSITION 1
#define STATS_REMAINING 2
#define ASCII_ESC 0x1b
// Default to 1 second.
#define ROGUESD_DEFAULT_READ_TIMEOUT 100
/*
|| Constructor
*/
RogueSD::RogueSD(Stream &comms)
: lastErrorCode(0),
_promptChar(DEFAULT_PROMPT),
_fwVersion(0),
_moduleType(uMMC),
_fwLevel(0)
{
_comms = &comms;
_prefix = "";
}
/*
|| Public Methods
*/
int8_t RogueSD::sync(bool blocking)
{
// procedure:
// 1. sync (send ESC, clear prompt)
// 2. get version ("v"), and module type
// 3. change settings as needed
// 4. close files
// 0. empty any data in the serial buffer
_comms->flush();
// 1. sync
print((char)ASCII_ESC); // send ESC to clear buffer on uMMC
if (blocking)
{
_readBlocked(); // consume prompt
}
else
{
if (_readTimeout(ROGUESD_DEFAULT_READ_TIMEOUT) < 0)
{
return 0;
}
}
// 2. get version (ignore prompt - just drop it)
_getVersion();
if (_moduleType == uMMC)
_prefix = "";
else
_prefix = "FC";
// 3. change settings as needed
// OLD: write timeout setting = 10 ms timeout
// NEW: listing style = old style (0)
if ((_moduleType == uMMC && _fwVersion < UMMC_MIN_FW_VERSION_FOR_NEW_COMMANDS) ||
(_moduleType == uMP3 && _fwVersion < UMP3_MIN_FW_VERSION_FOR_NEW_COMMANDS))
{
// we need to set the write timeout to allow us to control when a line is finished
// in writeln.
changeSetting('1', 1); // 10 ms timeout
_fwLevel = 0;
}
else
{
// we're using the new version
_fwLevel = 1;
// Let's make sure the Listing Style setting is set to the old style
if (getSetting('L') != 0)
{
changeSetting('L', 0);
}
// get the prompt char
print('S');
if (_moduleType != uMMC) print('T');
print('P');
print('\r'); // get our prompt (if possible)
_promptChar = _getNumber(10);
_readBlocked(); // consume prompt
}
// 4. close files
closeAll(); // ensure all handles are closed
return 1;
}
int32_t RogueSD::cardInfo(uint8_t getSize)
{
int32_t datum = 0;
print(_prefix);
print('Q');
print('\r');
while (!_commAvailable());
if (_commPeek() != 'E')
{
datum = _getNumber(10); // Free space (in KiB)
_readBlocked(); // consume '/' or ' '
if (getSize)
datum = _getNumber(10); // Card size
else
_getNumber(10);
_readBlocked(); // consume prompt
}
else
{
_getResponse();
// if we have an error, return -1
// error code is actually in .lastErrorCode
datum = -1;
}
return datum;
}
int8_t RogueSD::status(int8_t handle)
{
int8_t resp = 0;
print(_prefix);
print('Z');
if (handle > 0)
print((char)('0' + handle));
print('\r'); // Get status
resp = _getResponse();
if (resp)
_readBlocked(); // consume prompt if OK
return resp;
}
int8_t RogueSD::getFreeHandle(void)
{
uint8_t resp;
print(_prefix);
print('F');
print('\r');
resp = _readBlocked();
if (resp != 'E')
resp -= '0'; // got our handle
else
{
lastErrorCode = _getNumber(16);
if (lastErrorCode == ERROR_NO_FREE_FILES)
resp = 0;
else
resp = -1;
}
_readBlocked(); // consume prompt
return resp;
}
int8_t RogueSD::exists(const char *path)
{
// There are a few ways to determine whether path is a file.
// Determining whether path is a directory is a bit more tricky.
// If you use FC L path, the contents of the directory are returned.
// If you use FC LC path, you will get the number of files within the directory, but
// if there is only 1 file within the directory, it can be misleading (and what happens
// if there are NO files in the directory?).
// The current best solution is:
// 1. Use "FC LS path" to "Open Directory" - if no error, then path is a folder.
// 2. If an error was returned, use "FC LC path" - if no error, then path is a file.
if (openDir(path))
{
return TYPE_FOLDER;
}
else
{
if (fileCount(path) == 1)
return TYPE_FILE;
else
return 0;
}
}
fileInfo RogueSD::getFileInfo(int8_t handle)
{
fileInfo fi;
print(_prefix);
print('I');
print((char)('0' + handle));
print('\r');
while (!_commAvailable());
if (_commPeek() != 'E')
{
fi.position = _getNumber(10); // current file position
_readBlocked(); // consume '/' or ' '
fi.size = _getNumber(10); // file size
_readBlocked(); // consume prompt
}
else
{
_getResponse();
// if we have an error, just return 0's
// error code is actually in .lastErrorCode
fi.position = 0;
fi.size = 0;
}
return fi;
}
int32_t RogueSD::size(const char *path)
{
char c;
uint32_t filesize = 0;
int8_t resp;
if (_fwLevel == 0)
{
// old
lastErrorCode = ERROR_NOT_SUPPORTED;
return -1;
}
resp = exists(path);
if (resp == TYPE_FOLDER)
{
// path is a folder
lastErrorCode = ERROR_NOT_A_FILE;
return -1;
}
else if (resp == TYPE_FILE)
{
print(_prefix);
print("L ");
print(path);
print('\r');
if (_getResponse())
{
// we have the file info next
while (!_commAvailable());
filesize = _getNumber(10);
// clear the rest
while ((c = _readBlocked()) != '\r');
_readBlocked(); // consume prompt
return (int32_t) filesize;
}
else
{
// had an error
return -1;
}
}
else
{
// path does not exist
lastErrorCode = ERROR_FILE_DOES_NOT_EXIST;
return -1;
}
}
int8_t RogueSD::changeSetting(char setting, uint8_t value)
{
print('S');
if (_moduleType != uMMC) print('T');
print(setting);
print((int)value, DEC);
print('\r');
return _getResponse();
}
int16_t RogueSD::getSetting(char setting)
{
uint8_t value;
print('S');
if (_moduleType != uMMC) print('T');
print(setting);
print('\r');
while (!_commAvailable());
if (_commPeek() != 'E')
{
value = _getNumber(10);
_readBlocked(); // consume prompt
}
else
{
value = _getResponse(); // get the error
}
return value;
}
void RogueSD::getTime(uint16_t *rtc)
{
if (_fwLevel > 0)
{
print('T');
print('\r');
for (uint8_t i = 0; i < 7; i++)
{
rtc[i] = _getNumber(10);
_readBlocked(); // consume separators, and command prompt (at end)
}
}
else
{
// old
return;
}
}
void RogueSD::setTime(uint16_t rtc[])
{
if (_fwLevel > 0)
{
print('T');
for (uint8_t i = 0; i < 6; i++)
{
print(rtc[i], DEC);
print(' ');
}
print('\r');
// A bug in earlier versions returned the time after setting
while (_readBlocked() != _promptChar); // kludge: read everything including prompt
// _readBlocked(); // consume prompt
}
else
{
// old
return;
}
}
void RogueSD::setTime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second)
{
uint16_t rtc[6];
rtc[0] = year;
rtc[1] = month;
rtc[2] = day;
rtc[3] = hour;
rtc[4] = minute;
rtc[5] = second;
setTime(rtc);
}
int8_t RogueSD::open(const char *path)
{
// defaults to READ
return open(path, OPEN_READ);
}
int8_t RogueSD::open(const char *path, openMode mode)
{
int8_t fh = getFreeHandle();
if (fh > 0)
return _open(fh, path, mode, 0);
else
return fh;
}
int8_t RogueSD::open(int8_t handle, const char *path)
{
// defaults to READ
return _open(handle, path, OPEN_READ, 0);
}
int8_t RogueSD::open(int8_t handle, const char *path, openMode mode)
{
return _open(handle, path, mode, 0);
}
int8_t RogueSD::open_P(const char *path)
{
return open_P(path, OPEN_READ);
}
int8_t RogueSD::open_P(const char *path, openMode mode)
{
int8_t fh = getFreeHandle();
if (fh > 0)
return _open(fh, path, mode, 1);
else
return fh;
}
int8_t RogueSD::open_P(int8_t handle, const char *path)
{
return _open(handle, path, OPEN_READ, 1);
}
int8_t RogueSD::open_P(int8_t handle, const char *path, openMode mode)
{
return _open(handle, path, mode, 1);
}
// private
int8_t RogueSD::_open(int8_t handle, const char *path, openMode mode, int8_t pgmspc)
{
int8_t resp;
print(_prefix);
print('O');
print((char)('0' + handle));
print(' ');
switch (mode)
{
case OPEN_READ:
case OPEN_RW:
print('R');
if (mode == OPEN_READ) break;
case OPEN_WRITE:
print('W');
break;
case OPEN_APPEND:
print('A');
break;
}
print(' ');
if (pgmspc)
print_P(path);
else
print(path);
print('\r');
resp = _getResponse();
return resp ? handle : 0;
}
void RogueSD::close(int8_t handle)
{
print(_prefix);
print('C');
print((char)('0' + handle));
print('\r');
_getResponse();
}
void RogueSD::closeAll(void)
{
if (_fwLevel > 0)
{
// new
print(_prefix);
print('C');
print('\r');
_readBlocked(); // consume prompt
}
else
{
// old
for (uint8_t i = 1; i <= 4; i++) // 4 = Max handles
{
print(_prefix);
print('C');
print((char)('0' + i));
print('\r');
_getResponse();
}
}
}
int32_t RogueSD::fileCount(const char *path, const char *filemask)
{
int32_t fcount = 0;
if (_fwLevel > 0)
{
print(_prefix);
print("LC ");
if (path != NULL)
print(path);
if (filemask != NULL && strlen(filemask) > 0)
{
if (strlen(path) && path[strlen(path) - 1] != '/')
{
print('/');
}
print(filemask);
}
print('\r');
if (_getResponse())
{
fcount = _getNumber(10);
_readBlocked(); // consume prompt
return fcount;
}
else
{
// error occurred with "LC" command
return -1;
}
}
else
{
// old
lastErrorCode = ERROR_NOT_SUPPORTED;
return -1;
}
}
int8_t RogueSD::openDir(const char *dirname)
{
int8_t resp = 0;
if (_fwLevel > 0)
{
// new
print(_prefix);
print("LS ");
print(dirname);
print('\r');
resp = _getResponse();
if (resp)
_readBlocked(); // consume prompt only if OK
return resp;
}
else
{
// old
lastErrorCode = ERROR_NOT_SUPPORTED;
return -1;
}
}
int8_t RogueSD::readDir(char *dest, const char *filemask)
{
// retrieve the next entry from the directory
// returns:
// 0 when no more files/folders (EOF)
// -1 on failure
// 1 if entry is a file
// 2 if entry is a folder/directory
// currently using the original file listing style, i.e. D/fs filename
char c;
int8_t entrytype = TYPE_FILE;
if (_fwLevel > 0)
{
print(_prefix);
print("LI ");
if (filemask)
print(filemask);
else
print('*');
print('\r');
if (_getResponse())
{
// we have the file info next
while (!_commAvailable());
if (_commPeek() == 'D')
{
// we have a directory
_commRead(); // consume 'D'
entrytype = TYPE_FOLDER;
}
else
{
// it's a file, with a file size
_getNumber(10); // discard (for now)
entrytype = TYPE_FILE;
}
_readBlocked(); // consume separator ' '
// now get filename
while ((c = _readBlocked()) != '\r')
{
*dest++ = c;
}
*dest = 0; // terminate string
_readBlocked(); // consume prompt
return entrytype;
}
else
{
// had an error
if (lastErrorCode == ERROR_EOF)
return 0;
else
return -1;
}
}
else
{
// old
lastErrorCode = ERROR_NOT_SUPPORTED;
return -1;
}
}
int8_t RogueSD::entryToFilename(char *dest, uint8_t count, uint16_t entrynum, const char *path, const char *filemask)
{
char c;
int8_t entrytype = TYPE_FILE;
if (_fwLevel > 0)
{
// new
print(_prefix);
print("LE ");
print(entrynum, DEC);
print(' ');
if (path != NULL)
print(path);
if (filemask != NULL && strlen(filemask) > 0)
{
if (strlen(path) == 0 || path[strlen(path) - 1] != '/')
{
print('/');
}
print(filemask);
}
print('\r');
if (_getResponse())
{
// we have the file info next
while (!_commAvailable());
if (_commPeek() == 'D')
{
// we have a directory
_commRead(); // consume 'D'
entrytype = TYPE_FOLDER;
}
else
{
// it's a file, with a file size
_getNumber(10); // discard (for now)
entrytype = TYPE_FILE;
}
_readBlocked(); // consume separator ' '
// now get filename
while ((c = _readBlocked()) != '\r')
{
if (count > 0)
{
count--;
*dest++ = c;
}
}
*dest = 0; // terminate string
_readBlocked(); // consume prompt
return entrytype;
}
else
{
// had an error
return 0;
}
}
else
{
// old
lastErrorCode = ERROR_NOT_SUPPORTED;
return -1;
}
}
int8_t RogueSD::remove(const char *path)
{
print(_prefix);
print('E');
print(path);
print('\r');
return _getResponse();
}
int8_t RogueSD::rename(const char *oldpath, const char *newpath)
{
if (oldpath == NULL || newpath == NULL)
return 0;
print(_prefix);
print('N');
print(oldpath);
print('|');
print(newpath);
print('\r');
return _getResponse();
}
// Read a single byte from the given handle.
int16_t RogueSD::readByte(int8_t handle)
{
uint8_t ch = 0;
print(_prefix);
print('R');
print((char)('0' + handle));
print(' ');
print('1');
print('\r');
// we will get either a space followed by 1 byte, or an error
if (_getResponse())
{
// we have a single byte waiting
ch = _readBlocked();
_readBlocked(); // consume prompt
return (unsigned char) ch;
}
else
{
// had an error
if (lastErrorCode == ERROR_EOF)
return -1;
else
return -2;
}
}
int16_t RogueSD::read(int8_t handle, uint16_t count, char *dest)
{
// read up to count bytes into dest
uint32_t bytesremaining;
uint16_t i;
fileInfo fi = getFileInfo(handle);
// check first how many bytes are remaining
bytesremaining = fi.size - fi.position;
if (bytesremaining > 0)
{
if (count > bytesremaining)
count = bytesremaining;
print(_prefix);
print('R');
print((char)('0' + handle));
print(' ');
print(count, DEC);
print('\r');
}
else
{
return 0;
}
// now read count bytes
if (!_getResponse())
{
if (lastErrorCode == ERROR_EOF)
return -1;
else
return -2;
}
for (i = 0; i < count; i++)
dest[i] = _readBlocked();
_readBlocked(); // consume prompt
return i; // return number of bytes read
}
int16_t RogueSD::readln(int8_t handle, uint16_t maxlength, char *dest)
{
int8_t r;
int16_t i;
if (_fwLevel == 0)
return -1;
print(_prefix);
print("RL"); // Read a line, maxlength chars, maximum
print((char)('0' + handle));
print(' ');
print(maxlength, DEC);
print('\r');
if (!_getResponse())
{
if (lastErrorCode == ERROR_EOF)
// EOF
return -1;
else
return -2;
}
// otherwise, read the data
i = 0;
r = _readBlocked();
while (r != _promptChar) // we could have a blank line
{
dest[i++] = r;
r = _readBlocked();
}
dest[i] = 0; // terminate our string
return i;
}
int8_t RogueSD::write(int8_t handle, uint16_t count, const char *data)
{
print(_prefix);
print('W');
print((char)('0' + handle));
print(' ');
print(count, DEC);
print('\r');
while (count--)
write(*data++);
// after we are done, check the response
return _getResponse();
}
int8_t RogueSD::writeByte(int8_t handle, char data)
{
return write(handle, 1, &data);
}
int8_t RogueSD::write(int8_t handle, const char *data)
{
return write(handle, strlen(data), data);
}
void RogueSD::writelnStart(int8_t handle)
{
// be warned: if using this command with firmware less than 102.xx/111.xx
// you must IMMEDIATELY write data within 10ms or the write time-out will occur
print(_prefix);
print('W');
if (_fwLevel > 0)
{
// new
print('L');
print((char)('0' + handle));
}
else
{
// old
print((char)('0' + handle));
print(" 512");
}
print('\r');
}
int8_t RogueSD::writelnFinish(void)
{
if (_fwLevel > 0)
{
// new
print('\r');