/*
This file is part of Repetier-Firmware.
Repetier-Firmware is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Repetier-Firmware is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Repetier-Firmware. If not, see
init(targetFile); } if (!targetFile.isOpen()) { Com::printF(Com::tJSONErrorStart); Com::printF(Com::tNotSDPrinting); Com::printFLN(Com::tJSONErrorEnd); return; } // {"err":0,"size":457574,"height":4.00,"layerHeight":0.25,"filament":[6556.3],"generatedBy":"Slic3r 1.1.7 on 2014-11-09 at 17:11:32"} Com::printF(Com::tJSONFileInfoStart); Com::print(info->fileSize); Com::printF(Com::tJSONFileInfoHeight); Com::print(info->objectHeight); Com::printF(Com::tJSONFileInfoLayerHeight); Com::print(info->layerHeight); Com::printF(Com::tJSONFileInfoFilament); Com::print(info->filamentNeeded); Com::printF(Com::tJSONFileInfoGeneratedBy); Com::print(info->generatedBy); Com::print('"'); if (strlen(filename) == 0) { Com::printF(Com::tJSONFileInfoName); file.printName(); Com::print('"'); } Com::print('}'); Com::println(); }; #endif bool SDCard::selectFile(const char* filename, bool silent) { const char* oldP = filename; if (!sdactive) return false; sdmode = 0; file.close(); // Filename for progress view strncpy(Printer::printName, filename, 20); Printer::printName[20] = 0; if (file.open(fat.vwd(), filename, O_READ)) { if ((oldP = strrchr(filename, '/')) != NULL) oldP++; else oldP = filename; if (!silent) { Com::printF(Com::tFileOpened, oldP); Com::printFLN(Com::tSpaceSizeColon, file.fileSize()); } #if JSON_OUTPUT fileInfo.init(file); #endif sdpos = 0; filesize = file.fileSize(); Com::printFLN(Com::tFileSelected); return true; } else { if (!silent) Com::printFLN(Com::tFileOpenFailed); return false; } } void SDCard::printStatus() { if(sdactive) { Com::printF(Com::tSDPrintingByte, sdpos); Com::printFLN(Com::tSlash, filesize); } else { Com::printFLN(Com::tNotSDPrinting); } } void SDCard::startWrite(char *filename) { if(!sdactive) return; file.close(); sdmode = 0; fat.chdir(); if(!file.open(filename, O_CREAT | O_APPEND | O_WRITE | O_TRUNC)) { Com::printFLN(Com::tOpenFailedFile, filename); } else { UI_STATUS_F(Com::translatedF(UI_TEXT_UPLOADING_ID)); savetosd = true; Com::printFLN(Com::tWritingToFile, filename); } } void SDCard::finishWrite() { if(!savetosd) return; // already closed or never opened file.sync(); file.close(); savetosd = false; Com::printFLN(Com::tDoneSavingFile); UI_CLEAR_STATUS; } void SDCard::deleteFile(char *filename) { if(!sdactive) return; sdmode = 0; file.close(); if(fat.remove(filename)) { Com::printFLN(Com::tFileDeleted); } else { if(fat.rmdir(filename)) Com::printFLN(Com::tFileDeleted); else Com::printFLN(Com::tDeletionFailed); } } void SDCard::makeDirectory(char *filename) { if(!sdactive) return; sdmode = 0; file.close(); if(fat.mkdir(filename)) { Com::printFLN(Com::tDirectoryCreated); } else { Com::printFLN(Com::tCreationFailed); } } #ifdef GLENN_DEBUG void SDCard::writeToFile() { size_t nbyte; char szName[10]; strcpy(szName, "Testing\r\n"); nbyte = file.write(szName, strlen(szName)); Com::print("L="); Com::print((long)nbyte); Com::println(); } #endif #endif #if JSON_OUTPUT // --------------------------------------------------------------- // // Code that gets gcode information is adapted from RepRapFirmware // // Originally licensed under GPL // // Authors: reprappro, dc42, dcnewman, others // // Source: https://github.com/dcnewman/RepRapFirmware // // Copy date: 15 Nov 2015 // // --------------------------------------------------------------- // void GCodeFileInfo::init(SdFile &file) { this->fileSize = file.fileSize(); this->filamentNeeded = 0.0; this->objectHeight = 0.0; this->layerHeight = 0.0; if (!file.isOpen()) return; bool genByFound = false, layerHeightFound = false, filamentNeedFound = false; #if CPU_ARCH==ARCH_AVR #define GCI_BUF_SIZE 120 #else #define GCI_BUF_SIZE 1024 #endif // READ 4KB FROM THE BEGINNING char buf[GCI_BUF_SIZE]; for (int i = 0; i < 4096; i += GCI_BUF_SIZE - 50) { if(!file.seekSet(i)) break; file.read(buf, GCI_BUF_SIZE); if (!genByFound && findGeneratedBy(buf, this->generatedBy)) genByFound = true; if (!layerHeightFound && findLayerHeight(buf, this->layerHeight)) layerHeightFound = true; if (!filamentNeedFound && findFilamentNeed(buf, this->filamentNeeded)) filamentNeedFound = true; if(genByFound && layerHeightFound && filamentNeedFound) goto get_objectHeight; } // READ 4KB FROM END for (int i = 0; i < 4096; i += GCI_BUF_SIZE - 50) { if(!file.seekEnd(-4096 + i)) break; file.read(buf, GCI_BUF_SIZE); if (!genByFound && findGeneratedBy(buf, this->generatedBy)) genByFound = true; if (!layerHeightFound && findLayerHeight(buf, this->layerHeight)) layerHeightFound = true; if (!filamentNeedFound && findFilamentNeed(buf, this->filamentNeeded)) filamentNeedFound = true; if(genByFound && layerHeightFound && filamentNeedFound) goto get_objectHeight; } get_objectHeight: // MOVE FROM END UP IN 1KB BLOCKS UP TO 30KB for (int i = GCI_BUF_SIZE; i < 30000; i += GCI_BUF_SIZE - 50) { if(!file.seekEnd(-i)) break; file.read(buf, GCI_BUF_SIZE); if (findTotalHeight(buf, this->objectHeight)) break; } file.seekSet(0); } bool GCodeFileInfo::findGeneratedBy(char *buf, char *genBy) { // Slic3r & S3D const char* generatedByString = PSTR("generated by "); char* pos = strstr_P(buf, generatedByString); if (pos) { pos += strlen_P(generatedByString); size_t i = 0; while (i < GENBY_SIZE - 1 && *pos >= ' ') { char c = *pos++; if (c == '"' || c == '\\') { // Need to escape the quote-mark for JSON if (i > GENBY_SIZE - 3) break; genBy[i++] = '\\'; } genBy[i++] = c; } genBy[i] = 0; return true; } // CURA const char* slicedAtString = PSTR(";Sliced at: "); pos = strstr_P(buf, slicedAtString); if (pos) { strcpy_P(genBy, PSTR("Cura")); return true; } // UNKNOWN strcpy_P(genBy, PSTR("Unknown")); return false; } bool GCodeFileInfo::findLayerHeight(char *buf, float &layerHeight) { // SLIC3R layerHeight = 0; const char* layerHeightSlic3r = PSTR("; layer_height "); char *pos = strstr_P(buf, layerHeightSlic3r); if (pos) { pos += strlen_P(layerHeightSlic3r); while (*pos == ' ' || *pos == 't' || *pos == '=' || *pos == ':') { ++pos; } layerHeight = strtod(pos, NULL); return true; } // CURA const char* layerHeightCura = PSTR("Layer height: "); pos = strstr_P(buf, layerHeightCura); if (pos) { pos += strlen_P(layerHeightCura); while (*pos == ' ' || *pos == 't' || *pos == '=' || *pos == ':') { ++pos; } layerHeight = strtod(pos, NULL); return true; } return false; } bool GCodeFileInfo::findFilamentNeed(char *buf, float &filament) { const char* filamentUsedStr = PSTR("filament used"); const char* pos = strstr_P(buf, filamentUsedStr); filament = 0; if (pos != NULL) { pos += strlen_P(filamentUsedStr); while (*pos == ' ' || *pos == 't' || *pos == '=' || *pos == ':') { ++pos; // this allows for " = " from default slic3r comment and ": " from default Cura comment } if (isDigit(*pos)) { char *q; filament += strtod(pos, &q); if (*q == 'm' && *(q + 1) != 'm') { filament *= 1000.0; // Cura outputs filament used in metres not mm } } return true; } return false; } bool GCodeFileInfo::findTotalHeight(char *buf, float &height) { int len = 1024; bool inComment, inRelativeMode = false; unsigned int zPos; for (int i = len - 5; i > 0; i--) { if (inRelativeMode) { inRelativeMode = !(buf[i] == 'G' && buf[i + 1] == '9' && buf[i + 2] == '1' && buf[i + 3] <= ' '); } else if (buf[i] == 'G') { // Ignore G0/G1 codes if absolute mode was switched back using G90 (typical for Cura files) if (buf[i + 1] == '9' && buf[i + 2] == '0' && buf[i + 3] <= ' ') { inRelativeMode = true; } else if ((buf[i + 1] == '0' || buf[i + 1] == '1') && buf[i + 2] == ' ') { // Look for last "G0/G1 ... Z#HEIGHT#" command as generated by common slicers // Looks like we found a controlled move, however it could be in a comment, especially when using slic3r 1.1.1 inComment = false; size_t j = i; while (j != 0) { --j; char c = buf[j]; if (c == '\n' || c == '\r') break; if (c == ';') { // It is in a comment, so give up on this one inComment = true; break; } } if (inComment) continue; // Find 'Z' position and grab that value zPos = 0; for (int j = i + 3; j < len - 2; j++) { char c = buf[j]; if (c < ' ') { // Skip all white spaces... while (j < len - 2 && c <= ' ') { c = buf[++j]; } // ...to make sure ";End" doesn't follow G0 .. Z#HEIGHT# if (zPos != 0) { //debugPrintf("Found at offset %u text: %.100s\n", zPos, &buf[zPos + 1]); height = strtod(&buf[zPos + 1], NULL); return true; } break; } else if (c == ';') break; else if (c == 'Z') zPos = j; } } } } return false; } #endif // JSON_OUTPUT