|
@@ -1,329 +0,0 @@
|
1
|
|
-/* Arduino SdFat Library
|
2
|
|
- * Copyright (C) 2009 by William Greiman
|
3
|
|
- *
|
4
|
|
- * This file is part of the Arduino SdFat Library
|
5
|
|
- *
|
6
|
|
- * This Library is free software: you can redistribute it and/or modify
|
7
|
|
- * it under the terms of the GNU General Public License as published by
|
8
|
|
- * the Free Software Foundation, either version 3 of the License, or
|
9
|
|
- * (at your option) any later version.
|
10
|
|
- *
|
11
|
|
- * This Library is distributed in the hope that it will be useful,
|
12
|
|
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
|
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14
|
|
- * GNU General Public License for more details.
|
15
|
|
- *
|
16
|
|
- * You should have received a copy of the GNU General Public License
|
17
|
|
- * along with the Arduino SdFat Library. If not, see
|
18
|
|
- * <http://www.gnu.org/licenses/>.
|
19
|
|
- */
|
20
|
|
-#include "SdFat.h"
|
21
|
|
-#include "SdFatUtil.h"
|
22
|
|
-//------------------------------------------------------------------------------
|
23
|
|
-/** Change a volume's working directory to root
|
24
|
|
- *
|
25
|
|
- * Changes the volume's working directory to the SD's root directory.
|
26
|
|
- * Optionally set the current working directory to the volume's
|
27
|
|
- * working directory.
|
28
|
|
- *
|
29
|
|
- * \param[in] set_cwd Set the current working directory to this volume's
|
30
|
|
- * working directory if true.
|
31
|
|
- *
|
32
|
|
- * \return The value one, true, is returned for success and
|
33
|
|
- * the value zero, false, is returned for failure.
|
34
|
|
- */
|
35
|
|
-bool SdFat::chdir(bool set_cwd) {
|
36
|
|
- if (set_cwd) SdBaseFile::cwd_ = &vwd_;
|
37
|
|
- vwd_.close();
|
38
|
|
- return vwd_.openRoot(&vol_);
|
39
|
|
-}
|
40
|
|
-//------------------------------------------------------------------------------
|
41
|
|
-/** Change a volume's working directory
|
42
|
|
- *
|
43
|
|
- * Changes the volume working directory to the \a path subdirectory.
|
44
|
|
- * Optionally set the current working directory to the volume's
|
45
|
|
- * working directory.
|
46
|
|
- *
|
47
|
|
- * Example: If the volume's working directory is "/DIR", chdir("SUB")
|
48
|
|
- * will change the volume's working directory from "/DIR" to "/DIR/SUB".
|
49
|
|
- *
|
50
|
|
- * If path is "/", the volume's working directory will be changed to the
|
51
|
|
- * root directory
|
52
|
|
- *
|
53
|
|
- * \param[in] path The name of the subdirectory.
|
54
|
|
- *
|
55
|
|
- * \param[in] set_cwd Set the current working directory to this volume's
|
56
|
|
- * working directory if true.
|
57
|
|
- *
|
58
|
|
- * \return The value one, true, is returned for success and
|
59
|
|
- * the value zero, false, is returned for failure.
|
60
|
|
- */
|
61
|
|
-bool SdFat::chdir(const char *path, bool set_cwd) {
|
62
|
|
- SdBaseFile dir;
|
63
|
|
- if (path[0] == '/' && path[1] == '\0') return chdir(set_cwd);
|
64
|
|
- if (!dir.open(&vwd_, path, O_READ)) goto fail;
|
65
|
|
- if (!dir.isDir()) goto fail;
|
66
|
|
- vwd_ = dir;
|
67
|
|
- if (set_cwd) SdBaseFile::cwd_ = &vwd_;
|
68
|
|
- return true;
|
69
|
|
-
|
70
|
|
- fail:
|
71
|
|
- return false;
|
72
|
|
-}
|
73
|
|
-//------------------------------------------------------------------------------
|
74
|
|
-/** Set the current working directory to a volume's working directory.
|
75
|
|
- *
|
76
|
|
- * This is useful with multiple SD cards.
|
77
|
|
- *
|
78
|
|
- * The current working directory is changed to this volume's working directory.
|
79
|
|
- *
|
80
|
|
- * This is like the Windows/DOS \<drive letter>: command.
|
81
|
|
- */
|
82
|
|
-void SdFat::chvol() {
|
83
|
|
- SdBaseFile::cwd_ = &vwd_;
|
84
|
|
-}
|
85
|
|
-//------------------------------------------------------------------------------
|
86
|
|
-/** %Print any SD error code and halt. */
|
87
|
|
-void SdFat::errorHalt() {
|
88
|
|
- errorPrint();
|
89
|
|
- while (1);
|
90
|
|
-}
|
91
|
|
-//------------------------------------------------------------------------------
|
92
|
|
-/** %Print msg, any SD error code, and halt.
|
93
|
|
- *
|
94
|
|
- * \param[in] msg Message to print.
|
95
|
|
- */
|
96
|
|
-void SdFat::errorHalt(char const* msg) {
|
97
|
|
- errorPrint(msg);
|
98
|
|
- while (1);
|
99
|
|
-}
|
100
|
|
-//------------------------------------------------------------------------------
|
101
|
|
-/** %Print msg, any SD error code, and halt.
|
102
|
|
- *
|
103
|
|
- * \param[in] msg Message in program space (flash memory) to print.
|
104
|
|
- */
|
105
|
|
-void SdFat::errorHalt_P(PGM_P msg) {
|
106
|
|
- errorPrint_P(msg);
|
107
|
|
- while (1);
|
108
|
|
-}
|
109
|
|
-//------------------------------------------------------------------------------
|
110
|
|
-/** %Print any SD error code. */
|
111
|
|
-void SdFat::errorPrint() {
|
112
|
|
- if (!card_.errorCode()) return;
|
113
|
|
- PgmPrint("SD errorCode: 0X");
|
114
|
|
- Serial.println(card_.errorCode(), HEX);
|
115
|
|
-}
|
116
|
|
-//------------------------------------------------------------------------------
|
117
|
|
-/** %Print msg, any SD error code.
|
118
|
|
- *
|
119
|
|
- * \param[in] msg Message to print.
|
120
|
|
- */
|
121
|
|
-void SdFat::errorPrint(char const* msg) {
|
122
|
|
- PgmPrint("error: ");
|
123
|
|
- Serial.println(msg);
|
124
|
|
- errorPrint();
|
125
|
|
-}
|
126
|
|
-//------------------------------------------------------------------------------
|
127
|
|
-/** %Print msg, any SD error code.
|
128
|
|
- *
|
129
|
|
- * \param[in] msg Message in program space (flash memory) to print.
|
130
|
|
- */
|
131
|
|
-void SdFat::errorPrint_P(PGM_P msg) {
|
132
|
|
- PgmPrint("error: ");
|
133
|
|
- SerialPrintln_P(msg);
|
134
|
|
- errorPrint();
|
135
|
|
-}
|
136
|
|
-//------------------------------------------------------------------------------
|
137
|
|
-/**
|
138
|
|
- * Test for the existence of a file.
|
139
|
|
- *
|
140
|
|
- * \param[in] name Name of the file to be tested for.
|
141
|
|
- *
|
142
|
|
- * \return true if the file exists else false.
|
143
|
|
- */
|
144
|
|
-bool SdFat::exists(const char* name) {
|
145
|
|
- return vwd_.exists(name);
|
146
|
|
-}
|
147
|
|
-//------------------------------------------------------------------------------
|
148
|
|
-/**
|
149
|
|
- * Initialize an SdFat object.
|
150
|
|
- *
|
151
|
|
- * Initializes the SD card, SD volume, and root directory.
|
152
|
|
- *
|
153
|
|
- * \param[in] sckRateID value for SPI SCK rate. See Sd2Card::init().
|
154
|
|
- * \param[in] chipSelectPin SD chip select pin. See Sd2Card::init().
|
155
|
|
- *
|
156
|
|
- * \return The value one, true, is returned for success and
|
157
|
|
- * the value zero, false, is returned for failure.
|
158
|
|
- */
|
159
|
|
-bool SdFat::init(uint8_t sckRateID, uint8_t chipSelectPin) {
|
160
|
|
- return card_.init(sckRateID, chipSelectPin) && vol_.init(&card_) && chdir(1);
|
161
|
|
-}
|
162
|
|
-//------------------------------------------------------------------------------
|
163
|
|
-/** %Print error details and halt after SdFat::init() fails. */
|
164
|
|
-void SdFat::initErrorHalt() {
|
165
|
|
- initErrorPrint();
|
166
|
|
- while (1);
|
167
|
|
-}
|
168
|
|
-//------------------------------------------------------------------------------
|
169
|
|
-/**Print message, error details, and halt after SdFat::init() fails.
|
170
|
|
- *
|
171
|
|
- * \param[in] msg Message to print.
|
172
|
|
- */
|
173
|
|
-void SdFat::initErrorHalt(char const *msg) {
|
174
|
|
- Serial.println(msg);
|
175
|
|
- initErrorHalt();
|
176
|
|
-}
|
177
|
|
-//------------------------------------------------------------------------------
|
178
|
|
-/**Print message, error details, and halt after SdFat::init() fails.
|
179
|
|
- *
|
180
|
|
- * \param[in] msg Message in program space (flash memory) to print.
|
181
|
|
- */
|
182
|
|
-void SdFat::initErrorHalt_P(PGM_P msg) {
|
183
|
|
- SerialPrintln_P(msg);
|
184
|
|
- initErrorHalt();
|
185
|
|
-}
|
186
|
|
-//------------------------------------------------------------------------------
|
187
|
|
-/** Print error details after SdFat::init() fails. */
|
188
|
|
-void SdFat::initErrorPrint() {
|
189
|
|
- if (card_.errorCode()) {
|
190
|
|
- PgmPrintln("Can't access SD card. Do not reformat.");
|
191
|
|
- if (card_.errorCode() == SD_CARD_ERROR_CMD0) {
|
192
|
|
- PgmPrintln("No card, wrong chip select pin, or SPI problem?");
|
193
|
|
- }
|
194
|
|
- errorPrint();
|
195
|
|
- } else if (vol_.fatType() == 0) {
|
196
|
|
- PgmPrintln("Invalid format, reformat SD.");
|
197
|
|
- } else if (!vwd_.isOpen()) {
|
198
|
|
- PgmPrintln("Can't open root directory.");
|
199
|
|
- } else {
|
200
|
|
- PgmPrintln("No error found.");
|
201
|
|
- }
|
202
|
|
-}
|
203
|
|
-//------------------------------------------------------------------------------
|
204
|
|
-/**Print message and error details and halt after SdFat::init() fails.
|
205
|
|
- *
|
206
|
|
- * \param[in] msg Message to print.
|
207
|
|
- */
|
208
|
|
-void SdFat::initErrorPrint(char const *msg) {
|
209
|
|
- Serial.println(msg);
|
210
|
|
- initErrorPrint();
|
211
|
|
-}
|
212
|
|
-//------------------------------------------------------------------------------
|
213
|
|
-/**Print message and error details after SdFat::init() fails.
|
214
|
|
- *
|
215
|
|
- * \param[in] msg Message in program space (flash memory) to print.
|
216
|
|
- */
|
217
|
|
-void SdFat::initErrorPrint_P(PGM_P msg) {
|
218
|
|
- SerialPrintln_P(msg);
|
219
|
|
- initErrorHalt();
|
220
|
|
-}
|
221
|
|
-//------------------------------------------------------------------------------
|
222
|
|
-/** List the directory contents of the volume working directory to Serial.
|
223
|
|
- *
|
224
|
|
- * \param[in] flags The inclusive OR of
|
225
|
|
- *
|
226
|
|
- * LS_DATE - %Print file modification date
|
227
|
|
- *
|
228
|
|
- * LS_SIZE - %Print file size.
|
229
|
|
- *
|
230
|
|
- * LS_R - Recursive list of subdirectories.
|
231
|
|
- */
|
232
|
|
-void SdFat::ls(uint8_t flags) {
|
233
|
|
- vwd_.ls(&Serial, flags);
|
234
|
|
-}
|
235
|
|
-//------------------------------------------------------------------------------
|
236
|
|
-/** List the directory contents of the volume working directory to Serial.
|
237
|
|
- *
|
238
|
|
- * \param[in] pr Print stream for list.
|
239
|
|
- *
|
240
|
|
- * \param[in] flags The inclusive OR of
|
241
|
|
- *
|
242
|
|
- * LS_DATE - %Print file modification date
|
243
|
|
- *
|
244
|
|
- * LS_SIZE - %Print file size.
|
245
|
|
- *
|
246
|
|
- * LS_R - Recursive list of subdirectories.
|
247
|
|
- */
|
248
|
|
-void SdFat::ls(Print* pr, uint8_t flags) {
|
249
|
|
- vwd_.ls(pr, flags);
|
250
|
|
-}
|
251
|
|
-//------------------------------------------------------------------------------
|
252
|
|
-/** Make a subdirectory in the volume working directory.
|
253
|
|
- *
|
254
|
|
- * \param[in] path A path with a valid 8.3 DOS name for the subdirectory.
|
255
|
|
- *
|
256
|
|
- * \param[in] pFlag Create missing parent directories if true.
|
257
|
|
- *
|
258
|
|
- * \return The value one, true, is returned for success and
|
259
|
|
- * the value zero, false, is returned for failure.
|
260
|
|
- */
|
261
|
|
-bool SdFat::mkdir(const char* path, bool pFlag) {
|
262
|
|
- SdBaseFile sub;
|
263
|
|
- return sub.mkdir(&vwd_, path, pFlag);
|
264
|
|
-}
|
265
|
|
-//------------------------------------------------------------------------------
|
266
|
|
-/** Remove a file from the volume working directory.
|
267
|
|
-*
|
268
|
|
-* \param[in] path A path with a valid 8.3 DOS name for the file.
|
269
|
|
-*
|
270
|
|
-* \return The value one, true, is returned for success and
|
271
|
|
-* the value zero, false, is returned for failure.
|
272
|
|
-*/
|
273
|
|
-bool SdFat::remove(const char* path) {
|
274
|
|
- return SdBaseFile::remove(&vwd_, path);
|
275
|
|
-}
|
276
|
|
-//------------------------------------------------------------------------------
|
277
|
|
-/** Rename a file or subdirectory.
|
278
|
|
- *
|
279
|
|
- * \param[in] oldPath Path name to the file or subdirectory to be renamed.
|
280
|
|
- *
|
281
|
|
- * \param[in] newPath New path name of the file or subdirectory.
|
282
|
|
- *
|
283
|
|
- * The \a newPath object must not exist before the rename call.
|
284
|
|
- *
|
285
|
|
- * The file to be renamed must not be open. The directory entry may be
|
286
|
|
- * moved and file system corruption could occur if the file is accessed by
|
287
|
|
- * a file object that was opened before the rename() call.
|
288
|
|
- *
|
289
|
|
- * \return The value one, true, is returned for success and
|
290
|
|
- * the value zero, false, is returned for failure.
|
291
|
|
- */
|
292
|
|
-bool SdFat::rename(const char *oldPath, const char *newPath) {
|
293
|
|
- SdBaseFile file;
|
294
|
|
- if (!file.open(oldPath, O_READ)) return false;
|
295
|
|
- return file.rename(&vwd_, newPath);
|
296
|
|
-}
|
297
|
|
-//------------------------------------------------------------------------------
|
298
|
|
-/** Remove a subdirectory from the volume's working directory.
|
299
|
|
- *
|
300
|
|
- * \param[in] path A path with a valid 8.3 DOS name for the subdirectory.
|
301
|
|
- *
|
302
|
|
- * The subdirectory file will be removed only if it is empty.
|
303
|
|
- *
|
304
|
|
- * \return The value one, true, is returned for success and
|
305
|
|
- * the value zero, false, is returned for failure.
|
306
|
|
- */
|
307
|
|
-bool SdFat::rmdir(const char* path) {
|
308
|
|
- SdBaseFile sub;
|
309
|
|
- if (!sub.open(path, O_READ)) return false;
|
310
|
|
- return sub.rmdir();
|
311
|
|
-}
|
312
|
|
-//------------------------------------------------------------------------------
|
313
|
|
-/** Truncate a file to a specified length. The current file position
|
314
|
|
- * will be maintained if it is less than or equal to \a length otherwise
|
315
|
|
- * it will be set to end of file.
|
316
|
|
- *
|
317
|
|
- * \param[in] path A path with a valid 8.3 DOS name for the file.
|
318
|
|
- * \param[in] length The desired length for the file.
|
319
|
|
- *
|
320
|
|
- * \return The value one, true, is returned for success and
|
321
|
|
- * the value zero, false, is returned for failure.
|
322
|
|
- * Reasons for failure include file is read only, file is a directory,
|
323
|
|
- * \a length is greater than the current file size or an I/O error occurs.
|
324
|
|
- */
|
325
|
|
-bool SdFat::truncate(const char* path, uint32_t length) {
|
326
|
|
- SdBaseFile file;
|
327
|
|
- if (!file.open(path, O_WRITE)) return false;
|
328
|
|
- return file.truncate(length);
|
329
|
|
-}
|