ESP32 -fs.open作为文件名增量较慢
使用 VScode platformio 的 ESP32。 Adafruit SD 卡模块使用 spi 和 ESP32 的标准 SD 库连接。 我在 SD 卡上存储了文件,它们的名称如下:
1, 2, 3, ... 253, 254。
所有文件大小均为 3kb,包含 ascii 字符,备用文件是精确副本。
我注意到,随着代码继续打开和关闭文件进行读取(从 1 开始,递增 1,最后到 254),RGB 显示器的输出变得越来越慢。
我通过测量执行文件 1 和文件 254 的某些行所花费的时间来查明确切原因。此行导致了问题:
for (byte x=1; x<=254; x++) { // Iterate for all images on sd card
fname = "/" + String(x); // Form image file name
unsigned long t1 = millis();
image = fs.open(fname, FILE_READ); //LINE CAUSING DELAY
unsigned long t2 = millis();
unsigned long time = (t2-t1);
Serial.println("Time = " + (String) time);
其中 fname 是范围从 1 - 254 的数字。似乎随着数字的增加,打开时间会更长文件:
文件 254 的时间 = 74ms 文件1的时间 = 1ms
随着文件变到1,时间均匀减少。这不是文件名长度引起的(即254是3位,1是1位),文件101的打开时间成比例增加与文件 200 相比,它们不一样。
任何人都知道为什么会发生这种情况?这是我的代码中的一个关键问题,因为输出需要保持恒定速度,并且不会随着文件的增加而减少。
ESP32 using VScode platformio.
Adafruit sd card module connected using spi and standard SD library for ESP32.
I have files stored on SD card and they are named as follows:
1, 2, 3, ... 253, 254.
All files are 3kb in size and contain ascii characters, and alternate files are exact copies.
I noticed that as the code continued to open and close files for reading (Starting from 1, incrementing by 1, finishing at 254), the output to my RGB display was becoming slower and slower.
I pinpointed the exact cause by measuring the time taken to execute some lines for file 1 and file 254. This line was causing the issue:
for (byte x=1; x<=254; x++) { // Iterate for all images on sd card
fname = "/" + String(x); // Form image file name
unsigned long t1 = millis();
image = fs.open(fname, FILE_READ); //LINE CAUSING DELAY
unsigned long t2 = millis();
unsigned long time = (t2-t1);
Serial.println("Time = " + (String) time);
Where fname is a number ranging from 1 - 254. It seems as the number increases it takes longer to open the file:
Time for file 254 = 74ms
Time for file 1 = 1ms
The time decreases in a uniform maner as file goes to 1. It is not caused by the filename length (ie. 254 is 3 digit, 1 is 1 digit), file 101 had a proportionate increase in open time than file 200, they were not the same.
Anyone have any idea why this is happening? It is a crucial issue in my code as the output needs to be at a constant speed and not decrease as the file increments.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论