Read another chunk of G-codes from the file and return true if more data is available
| 339 | |
| 340 | // Read another chunk of G-codes from the file and return true if more data is available |
| 341 | GCodeInputReadResult FileGCodeInput::ReadFromFile(FileData &file) noexcept |
| 342 | { |
| 343 | size_t bytesCached = RegularGCodeInput::BytesCached(); |
| 344 | |
| 345 | // Keep track of the last file we read from |
| 346 | if (lastFileRead != file) |
| 347 | { |
| 348 | if (lastFileRead.IsLive() && bytesCached > 0) |
| 349 | { |
| 350 | // Rewind back to the right position so we can resume at the right position later. |
| 351 | // This may be necessary when nested macros are executed. |
| 352 | lastFileRead.Seek(lastFileRead.GetPosition() - bytesCached); |
| 353 | } |
| 354 | |
| 355 | RegularGCodeInput::Reset(); |
| 356 | bytesCached = 0; |
| 357 | |
| 358 | lastFileRead.CopyFrom(file); |
| 359 | } |
| 360 | |
| 361 | // Read more from the file |
| 362 | if (bytesCached < GCodeInputFileReadThreshold) |
| 363 | { |
| 364 | // Reset the read+write pointers for better performance if possible |
| 365 | if (readingPointer == writingPointer) |
| 366 | { |
| 367 | readingPointer = writingPointer = 0; |
| 368 | } |
| 369 | |
| 370 | // The code here used to read into a local buffer in blocks that are multiples of 4 bytes. |
| 371 | // However, unless we can use a buffer of at least 512 bytes then that is redundant, |
| 372 | // because the data will be copied via the sector buffer in FatFS anyway. So we don't do that any more. |
| 373 | const int bytesRead = file.Read(buffer + writingPointer, min<size_t>(BufferSpaceLeft(), GCodeInputBufferSize - writingPointer)); |
| 374 | if (bytesRead < 0) |
| 375 | { |
| 376 | return GCodeInputReadResult::error; |
| 377 | } |
| 378 | if (bytesRead > 0) |
| 379 | { |
| 380 | writingPointer = (writingPointer + (size_t)bytesRead) % GCodeInputBufferSize; |
| 381 | return GCodeInputReadResult::haveData; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | return (bytesCached > 0) ? GCodeInputReadResult::haveData : GCodeInputReadResult::noData; |
| 386 | } |
| 387 | |
| 388 | #endif |
| 389 |
no test coverage detected