From cf3039439fb12c3535a944bb19b38b70e37c8044 Mon Sep 17 00:00:00 2001 From: JonatanRek Date: Sat, 13 Sep 2025 23:06:54 +0200 Subject: [PATCH] Fixes --- marlin2.cpp | 160 ++++++++++++++++++++++++++++++------------------- marlin2.h | 6 +- text_sensor.py | 8 ++- 3 files changed, 111 insertions(+), 63 deletions(-) diff --git a/marlin2.cpp b/marlin2.cpp index bc3af88..88f097d 100644 --- a/marlin2.cpp +++ b/marlin2.cpp @@ -1,5 +1,6 @@ #include "marlin2.h" #include "esphome/core/log.h" +#include namespace esphome { static const char *TAG = "marlin2"; @@ -18,6 +19,7 @@ namespace esphome { return nullptr; // Return nullptr if no match is found } #endif + #ifdef USE_TEXT_SENSOR void Marlin2::add_text_sensor(const std::string& sName, text_sensor::TextSensor *sens) { text_sensors.push_back({sName, sens}); @@ -43,7 +45,7 @@ namespace esphome { MarlinTime.reserve(32); PrinterState.reserve(32); - ESP_LOGD(TAG, "M155 S10"); + //ESP_LOGD(TAG, "M155 S10"); write_str("\r\n\r\nM155 S10\r\n"); write_str("\r\n\r\nM117 ESP Home Connected!\r\n"); @@ -62,7 +64,7 @@ namespace esphome { while (available()) { char c = read(); if( c == '\n' || c == '\r' ) { - ESP_LOGD(TAG, "#%s#",MarlinOutput.c_str()); + //ESP_LOGD(TAG, "#%s#",MarlinOutput.c_str()); process_line(); } else { MarlinOutput += c; @@ -72,8 +74,8 @@ namespace esphome { if(millis() - millisProgress > 15000 && print_progress != 100) { millisProgress = millis(); - ESP_LOGD(TAG, "M27"); - ESP_LOGD(TAG, "M31"); + //ESP_LOGD(TAG, "M27"); + //ESP_LOGD(TAG, "M31"); write_str("M27\r\nM31\r\n"); } @@ -85,12 +87,15 @@ namespace esphome { return; } - if(!MarlinOutput.compare("ok") || !MarlinOutput.compare(" ok")) { + if(MarlinOutput.compare("ok") == 0 || MarlinOutput.compare(" ok") == 0) { listingFile = false; MarlinOutput=""; return; } + + ESP_LOGD(TAG, "DEBUG>#%s#",MarlinOutput.c_str()); + if(!listingFile && MarlinOutput.compare("Begin file list") == 0) { ESP_LOGD(TAG, "Listing of files started!"); listingFile = true; @@ -99,21 +104,49 @@ namespace esphome { return; } - if(listingFile && MarlinOutput.compare(".GCO ")) { + if(listingFile && MarlinOutput.compare("End file list") == 0) { + ESP_LOGD(TAG, "Listing of files stopped!"); + listingFile = false; + //reset string for next line + + #ifdef USE_TEXT_SENSOR + std::string sd_files; + + for (const auto& [key, value] : file_table_) { + if (!sd_files.empty()) sd_files += "|"; + sd_files += key; + } + + if (find_text_sensor("sd_card_files") != nullptr){ + ESP_LOGD(TAG, "SD Files: %s", sd_files.c_str()); + find_text_sensor("sd_card_files")->publish_state(sd_files.c_str()); + } + #endif + + MarlinOutput=""; + return; + } + + if(listingFile && MarlinOutput.find(".GCO ") > 1) { int first_space = MarlinOutput.find(' '); int second_space = MarlinOutput.find(' ', first_space + 1); - if(first_space > 0) { + if(first_space > 0) { std::string short_name = MarlinOutput.substr(0, first_space); + + if (short_name.find("/") != std::string::npos){ // Omit subdirectories + ESP_LOGD(TAG, "peskočeno kvůli vnořen %s",short_name.c_str()); + MarlinOutput=""; + return; + } + std::string long_name = short_name; - + if (second_space > first_space){ long_name = MarlinOutput.substr(second_space + 1); } file_table_[long_name] = short_name; - ESP_LOGD(TAG, "Uloženo do tabulky: %s => %s", long_name.c_str(), short_name.c_str()); - } //reset string for next line @@ -121,8 +154,6 @@ namespace esphome { return; } - - //Parse periodic Temperature read out message if( MarlinOutput.find(" T:") == 0 || @@ -160,7 +191,7 @@ namespace esphome { } #endif - ESP_LOGD(TAG, "Bed Temperature=%.1f°C Ext Temperature=%.1f°C ", bed_temperature, ext_temperature); + //ESP_LOGD(TAG, "Bed Temperature=%.1f°C Ext Temperature=%.1f°C ", bed_temperature, ext_temperature); } //reset string for next line @@ -175,7 +206,7 @@ namespace esphome { if (find_sensor("print_progress") != nullptr) find_sensor("print_progress")->publish_state(print_progress); - ESP_LOGD(TAG, "progress=%.1f", print_progress); + //ESP_LOGD(TAG, "progress=%.1f", print_progress); #endif set_printer_state("PRINTING"); @@ -197,7 +228,31 @@ namespace esphome { find_sensor("print_time_remaining")->publish_state(remaining); #endif - ESP_LOGD(TAG, "time=%f remaining=%f", current, remaining); + //ESP_LOGD(TAG, "time=%f remaining=%f", current, remaining); + } + + //reset string for next line + MarlinOutput=""; + return; + } + + //File opened: salma_~2.gco Size: 12279971 + if(MarlinOutput.find("File opened: ") == 0) { + size_t first_space = MarlinOutput.find("File opened: "); + size_t second_space = MarlinOutput.find(" Size: "); + + if (first_space != std::string::npos && second_space != std::string::npos) { + size_t start = first_space + std::strlen("File opened: "); + size_t len = second_space - start; + ESP_LOGD(TAG, "position"); + std::string filename = from_dos_name(MarlinOutput.substr(start, len)); + ESP_LOGD("Soubor: %s\n", filename.c_str()); + + #ifdef USE_TEXT_SENSOR + if (find_text_sensor("sd_card_file_selected") != nullptr){ + find_text_sensor("sd_card_file_selected")->publish_state(filename.c_str()); + } + #endif } //reset string for next line @@ -206,7 +261,7 @@ namespace esphome { } //Print From SD Card Started - if(MarlinOutput.find("File selected") != std::string::npos) { + if(MarlinOutput.compare("File selected") == 0) { set_printer_state("PRINTING"); //reset string for next line @@ -215,7 +270,7 @@ namespace esphome { } //Print Finished - if(MarlinOutput.find("Done printing") != std::string::npos) { + if(MarlinOutput.compare("Done printing") == 0) { print_progress = 100; #ifdef USE_SENSOR if (find_sensor("print_progress") != nullptr) @@ -234,7 +289,7 @@ namespace esphome { } // //Print Paused - // if(MarlinOutput.find("Printer halted") != std::string::npos) { + // if(MarlinOutput.compare("Printer halted") == 0) { // set_printer_state("PAUSED"); // //reset string for next line @@ -243,26 +298,15 @@ namespace esphome { // } // //Print Stoped - if(MarlinOutput.find("Print Aborted") != std::string::npos) { + if(MarlinOutput.compare("Print Aborted") == 0) { set_printer_state("STOPPED"); //reset string for next line MarlinOutput=""; return; - } + } - - - if(MarlinOutput.find("End file list") != std::string::npos) { - ESP_LOGD(TAG, "Listing of files stopped!"); - listingFile = false; - //reset string for next line - MarlinOutput=""; - return; - } - - - ESP_LOGD(TAG, "#%s#",MarlinOutput.c_str()); + ESP_LOGD(TAG, ">#%s#",MarlinOutput.c_str()); MarlinOutput=""; return; } @@ -299,14 +343,14 @@ namespace esphome { return 0.0; } - return (((float) current * 100.0) / (float) total); + return round(((float) current * 100.0) / (float) total); } int Marlin2::process_print_time_msg(double* current, double* remaining, float progress){ MarlinTime = MarlinOutput.substr(16); float d = 0, h = 0, m = 0, s = 0; - ESP_LOGD(TAG,MarlinTime.c_str()); + //ESP_LOGD(TAG,MarlinTime.c_str()); if (sscanf(MarlinTime.c_str() ,"%fd %fh %fm %fs", &d, &h, &m, &s)!=4) { d=0; @@ -339,39 +383,35 @@ namespace esphome { find_text_sensor("printer_state")->publish_state(status); } - ESP_LOGD(TAG, "Printer Status %s", status.c_str()); + // ESP_LOGD(TAG, "Printer Status %s", status.c_str()); // PrinterState = status; #endif } std::string Marlin2::to_dos_name(std::string filename) { - std::string shortName; - size_t dotPos = filename.find_last_of('.'); - - // Extract name and extension - std::string namePart = (dotPos != std::string::npos) ? filename.substr(0, dotPos) : filename; - std::string extPart = (dotPos != std::string::npos) ? filename.substr(dotPos + 1) : ""; - - // Truncate name to 6 characters + ~1 - if (namePart.length() > 6) { - namePart = namePart.substr(0, 6) + "~1"; + auto it = file_table_.find(filename); + if (it != file_table_.end()) { + return file_table_[filename]; } - - // Truncate extension to 3 characters - if (extPart.length() > 3) { - extPart = extPart.substr(0, 3); + + return filename; + } + + std::string Marlin2::from_dos_name(std::string dos_filename ) { + for (const auto& [key, value] : file_table_) { + if (value == dos_filename) { + return key; + } } - - // Construct 8.3 filename - shortName = namePart; - if (!extPart.empty()) { - shortName += "." + extPart; - } - - // Convert to uppercase (DOS filenames are case-insensitive, usually stored in uppercase) - std::transform(shortName.begin(), shortName.end(), shortName.begin(), ::toupper); - - return shortName; + + return dos_filename; + } + + std::string to_lower(std::string s) { + return s; + // String arduinoStr(s.c_str()); // převod na Arduino String + // arduinoStr.toLowerCase(); // změní obsah přímo + // return std::string(arduinoStr.c_str()); // zpět na std::string } } // namespace esphome \ No newline at end of file diff --git a/marlin2.h b/marlin2.h index d2e3c10..73ca6f7 100644 --- a/marlin2.h +++ b/marlin2.h @@ -26,8 +26,10 @@ class Marlin2 : public PollingComponent, public uart::UARTDevice { text_sensor::TextSensor* find_text_sensor(std::string key); #endif void write(std::string status); - std::string to_dos_name(std::string file_name); - + std::string to_dos_name(std::string filename); + std::string from_dos_name(std::string dos_filename); + std::string to_lower(std::string s); + float get_setup_priority() const override { return setup_priority::LATE; } void setup() override; void update() override; diff --git a/text_sensor.py b/text_sensor.py index 5dacbd6..cd0d22f 100644 --- a/text_sensor.py +++ b/text_sensor.py @@ -13,13 +13,19 @@ CONFIG_SCHEMA = cv.Schema( cv.Optional("printer_state"): text_sensor.text_sensor_schema( entity_category=ENTITY_CATEGORY_DIAGNOSTIC, ), + cv.Optional("sd_card_files"): text_sensor.text_sensor_schema( + entity_category=ENTITY_CATEGORY_DIAGNOSTIC, + ), + cv.Optional("sd_card_file_selected"): text_sensor.text_sensor_schema( + entity_category=ENTITY_CATEGORY_DIAGNOSTIC, + ), } ).extend(cv.polling_component_schema("15s")) async def to_code(config): server = await cg.get_variable(config[CONF_MARLIN]) - for sName in ["printer_state"]: + for sName in ["printer_state", "sd_card_files", "sd_card_file_selected"]: if sName in config: sens = await text_sensor.new_text_sensor(config[sName]) cg.add(server.add_text_sensor(sName,sens)) \ No newline at end of file