* Demonstrate opening and reading a file from the host. */
| 143 | * Demonstrate opening and reading a file from the host. |
| 144 | */ |
| 145 | void readFile(const char* filename, bool display) |
| 146 | { |
| 147 | int file = gdbfs.open(filename, File::ReadOnly); |
| 148 | Serial << _F("gdbfs.open(\"") << filename << "\") = " << file << endl; |
| 149 | if(file >= 0) { |
| 150 | OneShotFastMs timer; |
| 151 | char buf[256]; |
| 152 | size_t total{0}; |
| 153 | int len; |
| 154 | do { |
| 155 | len = gdbfs.read(file, buf, sizeof(buf)); |
| 156 | if(len > 0) { |
| 157 | total += size_t(len); |
| 158 | if(display) { |
| 159 | Serial.write(buf, len); |
| 160 | } |
| 161 | } |
| 162 | } while(len == sizeof(buf)); |
| 163 | auto elapsed = timer.elapsedTime(); |
| 164 | Serial.println(); |
| 165 | Serial << _F("gdbfs.read() = ") << len << _F(", total = ") << total << _F(", elapsed = ") << elapsed.toString() |
| 166 | << _F(", av. ") << (total == 0 ? 0 : 1000U * total / elapsed) << _F(" bytes/sec") << endl; |
| 167 | |
| 168 | gdbfs.close(file); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | * A more advanced way to use host File I/O using asynchronous syscalls. |
no test coverage detected