Clean up and consolidate SD-related code (#10831)

This commit is contained in:
Scott Lahteine 2018-05-24 01:18:56 -05:00 committed by GitHub
parent 9644d56b42
commit 8b44745bc7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 93 additions and 267 deletions

View file

@ -1055,7 +1055,7 @@ int8_t SdBaseFile::readDir(dir_t* dir, char* longFilename) {
if (!isDir() || (0x1F & curPosition_)) return -1;
// If we have a longFilename buffer, mark it as invalid.
// If long filename is found it will be filled automatically.
// If a long filename is found it will be filled automatically.
if (longFilename) longFilename[0] = '\0';
while (1) {
@ -1725,8 +1725,4 @@ int16_t SdBaseFile::write(const void* buf, uint16_t nbyte) {
return -1;
}
#if ALLOW_DEPRECATED_FUNCTIONS
void (*SdBaseFile::oldDateTime_)(uint16_t &date, uint16_t &time) = 0;
#endif
#endif // SDSUPPORT

View file

@ -386,119 +386,6 @@ class SdBaseFile {
bool open(SdBaseFile* dirFile, const uint8_t dname[11], uint8_t oflag);
bool openCachedEntry(uint8_t cacheIndex, uint8_t oflags);
dir_t* readDirCache();
// Deprecated functions
#if ALLOW_DEPRECATED_FUNCTIONS
public:
/**
* \deprecated Use:
* bool contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock);
* \param[out] bgnBlock the first block address for the file.
* \param[out] endBlock the last block address for the file.
* \return true for success or false for failure.
*/
bool contiguousRange(uint32_t& bgnBlock, uint32_t& endBlock) {
return contiguousRange(&bgnBlock, &endBlock);
}
/**
* \deprecated Use:
* bool createContiguous(SdBaseFile* dirFile, const char* path, uint32_t size)
* \param[in] dirFile The directory where the file will be created.
* \param[in] path A path with a valid DOS 8.3 file name.
* \param[in] size The desired file size.
* \return true for success or false for failure.
*/
bool createContiguous(SdBaseFile& dirFile, const char* path, uint32_t size) {
return createContiguous(&dirFile, path, size);
}
/**
* \deprecated Use:
* static void dateTimeCallback(
* void (*dateTime)(uint16_t* date, uint16_t* time));
* \param[in] dateTime The user's call back function.
*/
static void dateTimeCallback(
void (*dateTime)(uint16_t &date, uint16_t &time)) {
oldDateTime_ = dateTime;
dateTime_ = dateTime ? oldToNew : 0;
}
/**
* \deprecated Use:
* bool open(SdBaseFile* dirFile, const char* path, uint8_t oflag);
* \param[in] dirFile An open SdFat instance for the directory containing the
* file to be opened.
* \param[in] path A path with a valid 8.3 DOS name for the file.
* \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive
* OR of flags O_READ, O_WRITE, O_TRUNC, and O_SYNC.
* \return true for success or false for failure.
*/
bool open(SdBaseFile& dirFile, const char* path, uint8_t oflag) {
return open(&dirFile, path, oflag);
}
/**
* \deprecated Do not use in new apps
* \param[in] dirFile An open SdFat instance for the directory containing the
* file to be opened.
* \param[in] path A path with a valid 8.3 DOS name for a file to be opened.
* \return true for success or false for failure.
*/
bool open(SdBaseFile& dirFile, const char* path) {
return open(dirFile, path, O_RDWR);
}
/**
* \deprecated Use:
* bool open(SdBaseFile* dirFile, uint16_t index, uint8_t oflag);
* \param[in] dirFile An open SdFat instance for the directory.
* \param[in] index The \a index of the directory entry for the file to be
* opened. The value for \a index is (directory file position)/32.
* \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive
* OR of flags O_READ, O_WRITE, O_TRUNC, and O_SYNC.
* \return true for success or false for failure.
*/
bool open(SdBaseFile& dirFile, uint16_t index, uint8_t oflag) {
return open(&dirFile, index, oflag);
}
/**
* \deprecated Use: bool openRoot(SdVolume* vol);
* \param[in] vol The FAT volume containing the root directory to be opened.
* \return true for success or false for failure.
*/
bool openRoot(SdVolume& vol) { return openRoot(&vol); }
/**
* \deprecated Use: int8_t readDir(dir_t* dir);
* \param[out] dir The dir_t struct that will receive the data.
* \return bytes read for success zero for eof or -1 for failure.
*/
int8_t readDir(dir_t& dir, char* longFilename) {
return readDir(&dir, longFilename);
}
/**
* \deprecated Use:
* static uint8_t remove(SdBaseFile* dirFile, const char* path);
* \param[in] dirFile The directory that contains the file.
* \param[in] path The name of the file to be removed.
* \return true for success or false for failure.
*/
static bool remove(SdBaseFile& dirFile, const char* path) { return remove(&dirFile, path); }
private:
static void (*oldDateTime_)(uint16_t &date, uint16_t &time);
static void oldToNew(uint16_t * const date, uint16_t * const time) {
uint16_t d, t;
oldDateTime_(d, t);
*date = d;
*time = t;
}
#endif // ALLOW_DEPRECATED_FUNCTIONS
};
#endif // _SDBASEFILE_H_

View file

@ -62,11 +62,6 @@
*/
#define ENDL_CALLS_FLUSH 0
/**
* Allow use of deprecated functions if ALLOW_DEPRECATED_FUNCTIONS is nonzero
*/
#define ALLOW_DEPRECATED_FUNCTIONS 1
/**
* Allow FAT12 volumes if FAT12_SUPPORT is nonzero.
* FAT12 has not been well tested.

View file

@ -190,24 +190,6 @@ class SdVolume {
}
bool readBlock(uint32_t block, uint8_t* dst) { return sdCard_->readBlock(block, dst); }
bool writeBlock(uint32_t block, const uint8_t* dst) { return sdCard_->writeBlock(block, dst); }
// Deprecated functions
#if ALLOW_DEPRECATED_FUNCTIONS
public:
/**
* \deprecated Use: bool SdVolume::init(Sd2Card* dev);
* \param[in] dev The SD card where the volume is located.
* \return true for success or false for failure.
*/
bool init(Sd2Card& dev) { return init(&dev); }
/**
* \deprecated Use: bool SdVolume::init(Sd2Card* dev, uint8_t vol);
* \param[in] dev The SD card where the volume is located.
* \param[in] part The partition to be used.
* \return true for success or false for failure.
*/
bool init(Sd2Card& dev, uint8_t part) { return init(&dev, part); }
#endif // ALLOW_DEPRECATED_FUNCTIONS
};
#endif // _SDVOLUME_H_

View file

@ -100,25 +100,25 @@ void CardReader::lsDive(const char *prepend, SdFile parent, const char * const m
uint8_t cnt = 0;
// Read the next entry from a directory
while (parent.readDir(p, longFilename) > 0) {
while (parent.readDir(&p, longFilename) > 0) {
// If the entry is a directory and the action is LS_SerialPrint
if (DIR_IS_SUBDIR(&p) && lsAction != LS_Count && lsAction != LS_GetFilename) {
// Get the short name for the item, which we know is a folder
char lfilename[FILENAME_LENGTH];
createFilename(lfilename, p);
char dosFilename[FILENAME_LENGTH];
createFilename(dosFilename, p);
// Allocate enough stack space for the full path to a folder, trailing slash, and nul
bool prepend_is_empty = (prepend[0] == '\0');
int len = (prepend_is_empty ? 1 : strlen(prepend)) + strlen(lfilename) + 1 + 1;
int len = (prepend_is_empty ? 1 : strlen(prepend)) + strlen(dosFilename) + 1 + 1;
char path[len];
// Append the FOLDERNAME12/ to the passed string.
// It contains the full path to the "parent" argument.
// We now have the full path to the item in this folder.
strcpy(path, prepend_is_empty ? "/" : prepend); // root slash if prepend is empty
strcat(path, lfilename); // FILENAME_LENGTH-1 characters maximum
strcat(path, dosFilename); // FILENAME_LENGTH-1 characters maximum
strcat(path, "/"); // 1 character
// Serial.print(path);
@ -126,11 +126,11 @@ void CardReader::lsDive(const char *prepend, SdFile parent, const char * const m
// Get a new directory object using the full path
// and dive recursively into it.
SdFile dir;
if (!dir.open(parent, lfilename, O_READ)) {
if (!dir.open(&parent, dosFilename, O_READ)) {
if (lsAction == LS_SerialPrint) {
SERIAL_ECHO_START_P(port);
SERIAL_ECHOPGM_P(port, MSG_SD_CANT_OPEN_SUBDIR);
SERIAL_ECHOLN_P(port, lfilename);
SERIAL_ECHOLN_P(port, dosFilename);
}
}
lsDive(path, dir
@ -246,7 +246,7 @@ void CardReader::ls(
// Open the sub-item as the new dive parent
SdFile dir;
if (!dir.open(diveDir, segment, O_READ)) {
if (!dir.open(&diveDir, segment, O_READ)) {
SERIAL_EOL_P(port);
SERIAL_ECHO_START_P(port);
SERIAL_ECHOPGM_P(port, MSG_SD_CANT_OPEN_SUBDIR);
@ -273,11 +273,11 @@ void CardReader::printFilename(
#endif
) {
if (file.isOpen()) {
char lfilename[FILENAME_LENGTH];
file.getFilename(lfilename);
SERIAL_ECHO_P(port, lfilename);
char dosFilename[FILENAME_LENGTH];
file.getFilename(dosFilename);
SERIAL_ECHO_P(port, dosFilename);
#if ENABLED(LONG_FILENAME_HOST_SUPPORT)
getfilename(0, lfilename);
getfilename(0, dosFilename);
if (longFilename[0]) {
SERIAL_ECHO_P(port, ' ');
SERIAL_ECHO_P(port, longFilename);
@ -298,16 +298,16 @@ void CardReader::initsd() {
#define SPI_SPEED SPI_FULL_SPEED
#endif
if (!card.init(SPI_SPEED, SDSS)
if (!sd2card.init(SPI_SPEED, SDSS)
#if defined(LCD_SDSS) && (LCD_SDSS != SDSS)
&& !card.init(SPI_SPEED, LCD_SDSS)
&& !sd2card.init(SPI_SPEED, LCD_SDSS)
#endif
) {
//if (!card.init(SPI_HALF_SPEED,SDSS))
//if (!sd2card.init(SPI_HALF_SPEED,SDSS))
SERIAL_ECHO_START();
SERIAL_ECHOLNPGM(MSG_SD_INIT_FAIL);
}
else if (!volume.init(&card)) {
else if (!volume.init(&sd2card)) {
SERIAL_ERROR_START();
SERIAL_ERRORLNPGM(MSG_SD_VOL_INIT_FAIL);
}
@ -323,17 +323,6 @@ void CardReader::initsd() {
setroot();
}
void CardReader::setroot() {
/*if (!workDir.openRoot(&volume)) {
SERIAL_ECHOLNPGM(MSG_SD_WORKDIR_FAIL);
}*/
workDir = root;
curDir = &workDir;
#if ENABLED(SDCARD_SORT_ALPHA)
presort();
#endif
}
void CardReader::release() {
sdprinting = false;
cardOK = false;
@ -371,9 +360,9 @@ void CardReader::stopSDPrint(
#endif
}
void CardReader::openLogFile(char* name) {
void CardReader::openLogFile(char * const path) {
logging = true;
openFile(name, false);
openFile(path, false);
}
void appendAtom(SdFile &file, char *& dst, uint8_t &cnt) {
@ -396,7 +385,7 @@ void CardReader::getAbsFilename(char *t) {
*t = '\0';
}
void CardReader::openFile(char* name, const bool read, const bool subcall/*=false*/) {
void CardReader::openFile(char * const path, const bool read, const bool subcall/*=false*/) {
if (!cardOK) return;
@ -416,7 +405,7 @@ void CardReader::openFile(char* name, const bool read, const bool subcall/*=fals
filespos[file_subcall_ctr] = sdpos;
SERIAL_ECHO_START();
SERIAL_ECHOPAIR("SUBROUTINE CALL target:\"", name);
SERIAL_ECHOPAIR("SUBROUTINE CALL target:\"", path);
SERIAL_ECHOPAIR("\" parent:\"", proc_filenames[file_subcall_ctr]);
SERIAL_ECHOLNPAIR("\" pos", sdpos);
file_subcall_ctr++;
@ -437,49 +426,14 @@ void CardReader::openFile(char* name, const bool read, const bool subcall/*=fals
SERIAL_ECHO_START();
SERIAL_ECHOPGM("Now ");
serialprintPGM(doing == 1 ? PSTR("doing") : PSTR("fresh"));
SERIAL_ECHOLNPAIR(" file: ", name);
SERIAL_ECHOLNPAIR(" file: ", path);
}
stopSDPrint();
SdFile myDir;
curDir = &root;
char *fname = name;
char *dirname_start, *dirname_end;
if (name[0] == '/') {
dirname_start = &name[1];
while (dirname_start != NULL) {
dirname_end = strchr(dirname_start, '/');
//SERIAL_ECHOPGM("start:");SERIAL_ECHOLN((int)(dirname_start - name));
//SERIAL_ECHOPGM("end :");SERIAL_ECHOLN((int)(dirname_end - name));
if (dirname_end != NULL && dirname_end > dirname_start) {
char subdirname[FILENAME_LENGTH];
strncpy(subdirname, dirname_start, dirname_end - dirname_start);
subdirname[dirname_end - dirname_start] = '\0';
if (!myDir.open(curDir, subdirname, O_READ)) {
SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL);
SERIAL_PROTOCOL(subdirname);
SERIAL_PROTOCOLCHAR('.');
return;
}
else {
//SERIAL_ECHOLNPGM("dive ok");
}
curDir = &myDir;
dirname_start = dirname_end + 1;
}
else { // the remainder after all /fsa/fdsa/ is the filename
fname = dirname_start;
//SERIAL_ECHOLNPGM("remainder");
//SERIAL_ECHOLN(fname);
break;
}
}
}
else
curDir = &workDir; // Relative paths start in current directory
SdFile *curDir;
const char * const fname = diveToFile(curDir, path, false);
if (!fname) return;
if (read) {
if (file.open(curDir, fname, O_READ)) {
@ -509,7 +463,7 @@ void CardReader::openFile(char* name, const bool read, const bool subcall/*=fals
}
else {
saving = true;
SERIAL_PROTOCOLLNPAIR(MSG_SD_WRITE_TO_FILE, name);
SERIAL_PROTOCOLLNPAIR(MSG_SD_WRITE_TO_FILE, path);
lcd_setstatus(fname);
}
}
@ -520,40 +474,9 @@ void CardReader::removeFile(const char * const name) {
stopSDPrint();
SdFile myDir;
curDir = &root;
const char *fname = name;
char *dirname_start, *dirname_end;
if (name[0] == '/') {
dirname_start = strchr(name, '/') + 1;
while (dirname_start != NULL) {
dirname_end = strchr(dirname_start, '/');
//SERIAL_ECHOPGM("start:");SERIAL_ECHOLN((int)(dirname_start - name));
//SERIAL_ECHOPGM("end :");SERIAL_ECHOLN((int)(dirname_end - name));
if (dirname_end != NULL && dirname_end > dirname_start) {
char subdirname[FILENAME_LENGTH];
strncpy(subdirname, dirname_start, dirname_end - dirname_start);
subdirname[dirname_end - dirname_start] = 0;
SERIAL_ECHOLN(subdirname);
if (!myDir.open(curDir, subdirname, O_READ)) {
SERIAL_PROTOCOLPAIR(MSG_SD_OPEN_FILE_FAIL, subdirname);
SERIAL_PROTOCOLCHAR('.');
SERIAL_EOL();
return;
}
curDir = &myDir;
dirname_start = dirname_end + 1;
}
else {
fname = dirname_start;
break;
}
}
}
else // Relative paths are rooted in the current directory
curDir = &workDir;
SdFile *curDir;
const char * const fname = diveToFile(curDir, name, false);
if (!fname) return;
if (file.remove(curDir, fname)) {
SERIAL_PROTOCOLPGM("File deleted:");
@ -623,7 +546,7 @@ void CardReader::checkautostart() {
sprintf_P(autoname, PSTR("auto%i.g"), autostart_index);
dir_t p;
root.rewind();
while (root.readDir(p, NULL) > 0) {
while (root.readDir(&p, NULL) > 0) {
for (int8_t i = (int8_t)strlen((char*)p.name); i--;) p.name[i] = tolower(p.name[i]);
if (p.name[9] != '~' && strncmp((char*)p.name, autoname, 5) == 0) {
openAndPrintFile(autoname);
@ -653,6 +576,7 @@ void CardReader::closefile(const bool store_location) {
/**
* Get the name of a file in the current directory by index
* with optional name to match.
*/
void CardReader::getfilename(uint16_t nr, const char * const match/*=NULL*/) {
#if ENABLED(SDSORT_CACHE_NAMES)
@ -669,35 +593,60 @@ void CardReader::getfilename(uint16_t nr, const char * const match/*=NULL*/) {
return;
}
#endif // SDSORT_CACHE_NAMES
curDir = &workDir;
lsAction = LS_GetFilename;
nrFile_index = nr;
curDir->rewind();
lsDive(NULL, *curDir, match);
workDir.rewind();
lsDive(NULL, workDir, match);
}
uint16_t CardReader::getnrfilenames() {
curDir = &workDir;
lsAction = LS_Count;
nrFiles = 0;
curDir->rewind();
lsDive(NULL, *curDir);
workDir.rewind();
lsDive(NULL, workDir);
//SERIAL_ECHOLN(nrFiles);
return nrFiles;
}
/**
* Dive to the given file path, with optional echo.
* On exit set curDir and return the name part of the path.
* A NULL result indicates an unrecoverable error.
*/
const char* CardReader::diveToFile(SdFile*& curDir, const char * const path, const bool echo) {
SdFile myDir;
if (path[0] != '/') { curDir = &workDir; return path; }
curDir = &root;
const char *dirname_start = &path[1];
while (dirname_start) {
char * const dirname_end = strchr(dirname_start, '/');
if (dirname_end <= dirname_start) break;
char dosSubdirname[FILENAME_LENGTH];
const uint8_t len = dirname_end - dirname_start;
strncpy(dosSubdirname, dirname_start, len);
dosSubdirname[len] = 0;
if (echo) SERIAL_ECHOLN(dosSubdirname);
if (!myDir.open(curDir, dosSubdirname, O_READ)) {
SERIAL_PROTOCOLPAIR(MSG_SD_OPEN_FILE_FAIL, dosSubdirname);
SERIAL_PROTOCOLCHAR('.');
SERIAL_EOL();
return NULL;
}
curDir = &myDir;
dirname_start = dirname_end + 1;
}
return dirname_start;
}
void CardReader::chdir(const char * relpath) {
SdFile newDir;
SdFile *parent = &root;
SdFile *parent = workDir.isOpen() ? &workDir : &root;
if (workDir.isOpen()) parent = &workDir;
if (!newDir.open(*parent, relpath, O_READ)) {
SERIAL_ECHO_START();
SERIAL_ECHOPGM(MSG_SD_CANT_ENTER_SUBDIR);
SERIAL_ECHOLN(relpath);
}
else {
if (newDir.open(parent, relpath, O_READ)) {
workDir = newDir;
if (workDirDepth < MAX_DIR_DEPTH)
workDirParents[workDirDepth++] = workDir;
@ -705,6 +654,11 @@ void CardReader::chdir(const char * relpath) {
presort();
#endif
}
else {
SERIAL_ECHO_START();
SERIAL_ECHOPGM(MSG_SD_CANT_ENTER_SUBDIR);
SERIAL_ECHOLN(relpath);
}
}
int8_t CardReader::updir() {
@ -717,6 +671,16 @@ int8_t CardReader::updir() {
return workDirDepth;
}
void CardReader::setroot() {
/*if (!workDir.openRoot(&volume)) {
SERIAL_ECHOLNPGM(MSG_SD_WORKDIR_FAIL);
}*/
workDir = root;
#if ENABLED(SDCARD_SORT_ALPHA)
presort();
#endif
}
#if ENABLED(SDCARD_SORT_ALPHA)
/**

View file

@ -43,8 +43,8 @@ public:
void beginautostart();
void checkautostart();
void openFile(char* name, const bool read, const bool subcall=false);
void openLogFile(char* name);
void openFile(char * const path, const bool read, const bool subcall=false);
void openLogFile(char * const path);
void removeFile(const char * const name);
void closefile(const bool store_location=false);
void release();
@ -89,6 +89,8 @@ public:
int8_t updir();
void setroot();
const char* diveToFile(SdFile*& curDir, const char * const path, const bool echo);
uint16_t get_num_Files();
#if ENABLED(SDCARD_SORT_ALPHA)
@ -119,7 +121,7 @@ public:
FORCE_INLINE uint8_t percentDone() { return (isFileOpen() && filesize) ? sdpos / ((filesize + 99) / 100) : 0; }
FORCE_INLINE char* getWorkDirName() { workDir.getFilename(filename); return filename; }
Sd2Card& getSd2Card() { return card; }
Sd2Card& getSd2Card() { return sd2card; }
#if ENABLED(AUTO_REPORT_SD_STATUS)
void auto_report_sd_status(void);
@ -142,7 +144,7 @@ public:
char filename[FILENAME_LENGTH], longFilename[LONG_FILENAME_LENGTH];
int autostart_index;
private:
SdFile root, *curDir, workDir, workDirParents[MAX_DIR_DEPTH];
SdFile root, workDir, workDirParents[MAX_DIR_DEPTH];
uint8_t workDirDepth;
// Sort files and folders alphabetically.
@ -195,7 +197,7 @@ private:
#endif // SDCARD_SORT_ALPHA
Sd2Card card;
Sd2Card sd2card;
SdVolume volume;
SdFile file;