我正在使用Arduino SD胖库在SD卡上查找文件。我正在向函数发送指向char
数组和对象SdFile
的指针作为模板,我将代码基于以下示例:
const uint8_t SD_CS_PIN = 10;
SdFat sd;
SdFile file;
SdFile dirFile;
// Number of files found.
uint16_t n = 0;
// Max of ten files since files are selected with a single digit.
const uint16_t nMax = 10;
// Position of file's directory entry.
uint16_t dirIndex[nMax];
// List files in root directory.
while (file.openNext(&dirFile, O_READ))
{
// Skip directories and hidden files.
if (!file.isSubDir() && !file.isHidden())
{
// Save dirIndex of file in directory.
dirIndex[n] = file.dirIndex();
// Print the file number and name.
Serial.print(n++);
Serial.write(' ');
file.printName(&Serial);
Serial.println();
}
file.close();
}
下面是我写的代码,当loc.openNext(&dir, O_READ)
对我的逻辑进行一些解释时,它没有返回true。
功能回声用户输入
bool FindFile (SdFile dir, char *findname)
{
Serial.println("getting file");
const char *p;
p = findname;
while (*p)
{
Serial.print(*p);
p++;
}
Serial.println();
Serial.println();
bool found = false;
(这部分有效)
if(dir.exists(findname))
{
ActiveDir=dir;
found = true;
Serial.println("found");
}
如果findname
不在dir
中,请查找第一个子目录,检查并继续直到找到或检查所有子目录。该功能还会跳过任何隐藏的内容,因为我不希望用户能够选择隐藏文件。
else
{
//debug marker
Serial.println("else");
SdFile loc;
while (loc.openNext(&dir, O_READ))
{
//while loop is always returning false so this part is not running when it should
//debug marker
Serial.println("while");
if (loc.isHidden())
{
//file hidden, skip
}
else if (loc.isSubDir())
{
Serial.println("SUB");
loc.open(&dir, loc.dirIndex(), O_READ);
if(FindFile(loc, findname))
{
found = true;
break;
}
}
}
loc.close();
}
return found;
}
对于我的生活,我无法弄清file.openNext(&dirFile, O_READ)
与loc.openNext(&dir, O_READ)
的区别
编辑:更新代码以显示更多示例代码。