123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- #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) ){
- gfReset(0,0);
-
- if( ! gfComputeNextFileBlock() )
- return false;
- return true;
- } else {
- return false;
- }
- }
- bool SdFile::seekSetFilteredGcode(uint32_t pos){
- bool rv = seekSet(pos);
- gfComputeNextFileBlock();
- return rv;
- }
- void SdFile::gfReset(uint32_t blk, uint16_t ofs){
-
- gfBlock = blk;
- gfOffset = ofs;
- gfCachePBegin = vol_->cache()->data;
-
- gfCacheP = gfCachePBegin;
- }
- 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(){
- gfEnsureBlock();
-
-
- const uint8_t *start = gfCacheP;
- uint8_t consecutiveCommentLines = 0;
- while( *gfCacheP == ';' ){
- for(;;){
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- find_endl(gfCacheP, gfCacheP);
-
- if( gfCacheP - gfCachePBegin >= 512 ){
-
- gfUpdateCurrentPosition( gfCacheP - start );
- if( ! gfComputeNextFileBlock() )goto fail;
- gfEnsureBlock();
- gfCacheP = start = gfCachePBegin;
- } else {
- if(++consecutiveCommentLines == 255){
-
- goto forceExit;
- }
-
- if( *(gfCacheP+1) == ';' ){
-
- ++gfCacheP;
- ++consecutiveCommentLines;
- }
- break;
- }
- }
- }
- forceExit:
- {
- gfUpdateCurrentPosition( gfCacheP - start + 1 );
- int16_t rv = *gfCacheP++;
-
-
- if( gfCacheP - gfCachePBegin >= 512 ){
- if( ! gfComputeNextFileBlock() )goto fail;
-
- gfCacheP = gfCachePBegin;
- }
- return rv;
- }
- fail:
- return -1;
- }
- bool SdFile::gfEnsureBlock(){
- if ( vol_->cacheRawBlock(gfBlock, SdVolume::CACHE_FOR_READ)){
-
- const uint16_t terminateOfs = (fileSize_ - gfOffset) < 512 ? (fileSize_ - gfOffset) : 512U;
- vol_->cache()->data[ terminateOfs ] = '\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
|