123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- #include "Marlin.h"
- #ifdef SDSUPPORT
- #include "SdFile.h"
- SdFile::SdFile(const char* path, uint8_t oflag) : SdBaseFile(path, oflag) {
- }
- bool SdFile::openFilteredGcode(SdBaseFile* dirFile, const char* path){
- if( open(dirFile, path, O_READ) ){
-
- if( ! gfComputeNextFileBlock() )
- return false;
- gfReset();
- return true;
- } else {
- return false;
- }
- }
- bool SdFile::seekSetFilteredGcode(uint32_t pos){
- if(! seekSet(pos) )return false;
- if(! gfComputeNextFileBlock() )return false;
- gfReset();
- return true;
- }
- const uint8_t *SdFile::gfBlockBuffBegin() const {
- return vol_->cache()->data;
- }
- void SdFile::gfReset(){
-
- gfReadPtr = gfBlockBuffBegin() + gfOffset;
- }
- void __attribute__((noinline)) SdFile::gfUpdateCurrentPosition(uint16_t inc){
- curPosition_ += inc;
- }
- #define find_endl(resultP, startP) \
- __asm__ __volatile__ ( \
- "cycle: \n" \
- "ld r22, Z+ \n" \
- "cpi r22, 0x0A \n" \
- "brne cycle \n" \
- : "=z" (resultP) \
- : "z" (startP) \
- : "r22" \
- )
- int16_t SdFile::readFilteredGcode(){
- if( ! gfEnsureBlock() ){
- goto eof_or_fail;
- }
-
- {
- const uint8_t *start = gfReadPtr;
-
-
-
-
-
-
-
- const uint8_t *rdPtr = gfReadPtr;
-
- const uint8_t *blockBuffBegin = gfBlockBuffBegin();
-
- uint8_t consecutiveCommentLines = 0;
- while( *rdPtr == ';' ){
- for(;;){
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- find_endl(rdPtr, rdPtr);
-
- if( rdPtr - blockBuffBegin > 512 ){
-
- gfUpdateCurrentPosition( rdPtr - start - 1 );
- if( ! gfComputeNextFileBlock() )goto eof_or_fail;
- if( ! gfEnsureBlock() )goto eof_or_fail;
- rdPtr = start = blockBuffBegin;
- } else {
- if(consecutiveCommentLines >= 250){
- --rdPtr;
- goto emit_char;
- }
-
- if( *rdPtr == ';' ){
-
- ++consecutiveCommentLines;
- } else {
- --rdPtr;
- goto emit_char;
- }
- break;
- }
- }
- }
- emit_char:
- {
- gfUpdateCurrentPosition( rdPtr - start + 1 );
- int16_t rv = *rdPtr++;
-
- if( curPosition_ >= fileSize_ ){
-
- goto eof_or_fail;
- } else if( rdPtr - blockBuffBegin >= 512 ){
-
- if( ! gfComputeNextFileBlock() )goto eof_or_fail;
-
- rdPtr = blockBuffBegin;
- }
-
- gfReadPtr = rdPtr;
- return rv;
- }
- }
- eof_or_fail:
-
- gfReadPtr = gfBlockBuffBegin() + 512;
- return -1;
- }
- bool SdFile::gfEnsureBlock(){
- if ( vol_->cacheRawBlock(gfBlock, SdVolume::CACHE_FOR_READ)){
-
- const uint16_t terminateOfs = fileSize_ - gfOffset;
- vol_->cache()->data[ terminateOfs < 512 ? terminateOfs : 512 ] = '\n';
- return true;
- } else {
- return false;
- }
- }
- bool SdFile::gfComputeNextFileBlock() {
-
- if (!isOpen() || !(flags_ & O_READ)) return false;
- gfOffset = curPosition_ & 0X1FF;
- if (type_ == FAT_FILE_TYPE_ROOT_FIXED) {
-
-
-
- gfBlock = vol_->rootDirStart() + (curPosition_ >> 9);
- } else {
- uint8_t blockOfCluster = vol_->blockOfCluster(curPosition_);
- if (gfOffset == 0 && blockOfCluster == 0) {
-
- if (curPosition_ == 0) {
-
- curCluster_ = firstCluster_;
- } else {
-
- if (!vol_->fatGet(curCluster_, &curCluster_)) return false;
- }
- }
- gfBlock = vol_->clusterStartBlock(curCluster_) + blockOfCluster;
- }
- return true;
- }
- int16_t SdFile::write(const void* buf, uint16_t nbyte) {
- return SdBaseFile::write(buf, nbyte);
- }
- #if ARDUINO >= 100
- size_t SdFile::write(uint8_t b)
- {
- return SdBaseFile::write(&b, 1);
- }
- #else
- void SdFile::write(uint8_t b)
- {
- SdBaseFile::write(&b, 1);
- }
- #endif
- void SdFile::write(const char* str) {
- SdBaseFile::write(str, strlen(str));
- }
- void SdFile::write_P(PGM_P str) {
- for (uint8_t c; (c = pgm_read_byte(str)); str++) write(c);
- }
- void SdFile::writeln_P(PGM_P str) {
- write_P(str);
- write_P(PSTR("\r\n"));
- }
- #endif
|