Skip to content

Commit de095c0

Browse files
author
Federico Fissore
committed
File: implemented File.openNextFile() and File.rewindDirectory()
1 parent 08d9e57 commit de095c0

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

hardware/arduino/avr/libraries/Bridge/FileIO.cpp

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
#include <FileIO.h>
2020

21+
22+
2123
File::File(BridgeClass &b) : mode(255), bridge(b) {
2224
// Empty
2325
}
@@ -27,6 +29,7 @@ File::File(const char *_filename, uint8_t _mode, BridgeClass &b) : mode(_mode),
2729
char modes[] = {'r','w','a'};
2830
uint8_t cmd[] = {'F', modes[mode]};
2931
uint8_t res[2];
32+
dirPosition = 1;
3033
bridge.transfer(cmd, 2, (uint8_t*)filename.c_str(), filename.length(), res, 2);
3134
if (res[0] != 0) { // res[0] contains error code
3235
mode = 255; // In case of error keep the file closed
@@ -161,9 +164,42 @@ boolean File::isDirectory() {
161164
bridge.transfer(cmd, 1, (uint8_t *)filename.c_str(), filename.length(), res, 1);
162165
return res[0];
163166
}
164-
//boolean isDirectory(void)
165-
//File openNextFile(uint8_t mode = O_RDONLY);
166-
//void rewindDirectory(void)
167+
168+
169+
File File::openNextFile(uint8_t mode){
170+
Process awk;
171+
char tmp;
172+
String command;
173+
String filepath;
174+
if (dirPosition == 0xFFFF) return File();
175+
176+
command = "ls ";
177+
command += filename;
178+
command += " | awk 'NR==";
179+
command += dirPosition;
180+
command += "'";
181+
182+
awk.runShellCommand(command);
183+
184+
while(awk.running());
185+
186+
command = "";
187+
188+
while (awk.available()){
189+
tmp = awk.read();
190+
if (tmp!='\n') command += tmp;
191+
}
192+
if (command.length() == 0)
193+
return File();
194+
dirPosition++;
195+
filepath = filename + "/" + command;
196+
return File(filepath.c_str(),mode);
197+
198+
}
199+
200+
void File::rewindDirectory(void){
201+
dirPosition = 1;
202+
}
167203

168204

169205

hardware/arduino/avr/libraries/Bridge/FileIO.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,17 @@ class File : public Stream {
5555
void doBuffer();
5656
uint8_t buffered;
5757
uint8_t readPos;
58+
uint16_t dirPosition;
5859
static const int BUFFER_SIZE = 64;
5960
uint8_t buffer[BUFFER_SIZE];
6061

62+
6163
private:
6264
BridgeClass &bridge;
6365
String filename;
6466
uint8_t mode;
6567
uint8_t handle;
68+
6669
};
6770

6871
class FileSystemClass {

0 commit comments

Comments
 (0)