Fixes
This commit is contained in:
160
marlin2.cpp
160
marlin2.cpp
@@ -1,5 +1,6 @@
|
|||||||
#include "marlin2.h"
|
#include "marlin2.h"
|
||||||
#include "esphome/core/log.h"
|
#include "esphome/core/log.h"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
static const char *TAG = "marlin2";
|
static const char *TAG = "marlin2";
|
||||||
@@ -18,6 +19,7 @@ namespace esphome {
|
|||||||
return nullptr; // Return nullptr if no match is found
|
return nullptr; // Return nullptr if no match is found
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef USE_TEXT_SENSOR
|
#ifdef USE_TEXT_SENSOR
|
||||||
void Marlin2::add_text_sensor(const std::string& sName, text_sensor::TextSensor *sens) {
|
void Marlin2::add_text_sensor(const std::string& sName, text_sensor::TextSensor *sens) {
|
||||||
text_sensors.push_back({sName, sens});
|
text_sensors.push_back({sName, sens});
|
||||||
@@ -43,7 +45,7 @@ namespace esphome {
|
|||||||
MarlinTime.reserve(32);
|
MarlinTime.reserve(32);
|
||||||
PrinterState.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\nM155 S10\r\n");
|
||||||
write_str("\r\n\r\nM117 ESP Home Connected!\r\n");
|
write_str("\r\n\r\nM117 ESP Home Connected!\r\n");
|
||||||
@@ -62,7 +64,7 @@ namespace esphome {
|
|||||||
while (available()) {
|
while (available()) {
|
||||||
char c = read();
|
char c = read();
|
||||||
if( c == '\n' || c == '\r' ) {
|
if( c == '\n' || c == '\r' ) {
|
||||||
ESP_LOGD(TAG, "#%s#",MarlinOutput.c_str());
|
//ESP_LOGD(TAG, "#%s#",MarlinOutput.c_str());
|
||||||
process_line();
|
process_line();
|
||||||
} else {
|
} else {
|
||||||
MarlinOutput += c;
|
MarlinOutput += c;
|
||||||
@@ -72,8 +74,8 @@ namespace esphome {
|
|||||||
if(millis() - millisProgress > 15000 && print_progress != 100) {
|
if(millis() - millisProgress > 15000 && print_progress != 100) {
|
||||||
millisProgress = millis();
|
millisProgress = millis();
|
||||||
|
|
||||||
ESP_LOGD(TAG, "M27");
|
//ESP_LOGD(TAG, "M27");
|
||||||
ESP_LOGD(TAG, "M31");
|
//ESP_LOGD(TAG, "M31");
|
||||||
|
|
||||||
write_str("M27\r\nM31\r\n");
|
write_str("M27\r\nM31\r\n");
|
||||||
}
|
}
|
||||||
@@ -85,12 +87,15 @@ namespace esphome {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!MarlinOutput.compare("ok") || !MarlinOutput.compare(" ok")) {
|
if(MarlinOutput.compare("ok") == 0 || MarlinOutput.compare(" ok") == 0) {
|
||||||
listingFile = false;
|
listingFile = false;
|
||||||
MarlinOutput="";
|
MarlinOutput="";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
ESP_LOGD(TAG, "DEBUG>#%s#",MarlinOutput.c_str());
|
||||||
|
|
||||||
if(!listingFile && MarlinOutput.compare("Begin file list") == 0) {
|
if(!listingFile && MarlinOutput.compare("Begin file list") == 0) {
|
||||||
ESP_LOGD(TAG, "Listing of files started!");
|
ESP_LOGD(TAG, "Listing of files started!");
|
||||||
listingFile = true;
|
listingFile = true;
|
||||||
@@ -99,21 +104,49 @@ namespace esphome {
|
|||||||
return;
|
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 first_space = MarlinOutput.find(' ');
|
||||||
int second_space = MarlinOutput.find(' ', first_space + 1);
|
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);
|
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;
|
std::string long_name = short_name;
|
||||||
|
|
||||||
if (second_space > first_space){
|
if (second_space > first_space){
|
||||||
long_name = MarlinOutput.substr(second_space + 1);
|
long_name = MarlinOutput.substr(second_space + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
file_table_[long_name] = short_name;
|
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
|
//reset string for next line
|
||||||
@@ -121,8 +154,6 @@ namespace esphome {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//Parse periodic Temperature read out message
|
//Parse periodic Temperature read out message
|
||||||
if(
|
if(
|
||||||
MarlinOutput.find(" T:") == 0 ||
|
MarlinOutput.find(" T:") == 0 ||
|
||||||
@@ -160,7 +191,7 @@ namespace esphome {
|
|||||||
}
|
}
|
||||||
#endif
|
#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
|
//reset string for next line
|
||||||
@@ -175,7 +206,7 @@ namespace esphome {
|
|||||||
if (find_sensor("print_progress") != nullptr)
|
if (find_sensor("print_progress") != nullptr)
|
||||||
find_sensor("print_progress")->publish_state(print_progress);
|
find_sensor("print_progress")->publish_state(print_progress);
|
||||||
|
|
||||||
ESP_LOGD(TAG, "progress=%.1f", print_progress);
|
//ESP_LOGD(TAG, "progress=%.1f", print_progress);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
set_printer_state("PRINTING");
|
set_printer_state("PRINTING");
|
||||||
@@ -197,7 +228,31 @@ namespace esphome {
|
|||||||
find_sensor("print_time_remaining")->publish_state(remaining);
|
find_sensor("print_time_remaining")->publish_state(remaining);
|
||||||
#endif
|
#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
|
//reset string for next line
|
||||||
@@ -206,7 +261,7 @@ namespace esphome {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Print From SD Card Started
|
//Print From SD Card Started
|
||||||
if(MarlinOutput.find("File selected") != std::string::npos) {
|
if(MarlinOutput.compare("File selected") == 0) {
|
||||||
set_printer_state("PRINTING");
|
set_printer_state("PRINTING");
|
||||||
|
|
||||||
//reset string for next line
|
//reset string for next line
|
||||||
@@ -215,7 +270,7 @@ namespace esphome {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Print Finished
|
//Print Finished
|
||||||
if(MarlinOutput.find("Done printing") != std::string::npos) {
|
if(MarlinOutput.compare("Done printing") == 0) {
|
||||||
print_progress = 100;
|
print_progress = 100;
|
||||||
#ifdef USE_SENSOR
|
#ifdef USE_SENSOR
|
||||||
if (find_sensor("print_progress") != nullptr)
|
if (find_sensor("print_progress") != nullptr)
|
||||||
@@ -234,7 +289,7 @@ namespace esphome {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// //Print Paused
|
// //Print Paused
|
||||||
// if(MarlinOutput.find("Printer halted") != std::string::npos) {
|
// if(MarlinOutput.compare("Printer halted") == 0) {
|
||||||
// set_printer_state("PAUSED");
|
// set_printer_state("PAUSED");
|
||||||
|
|
||||||
// //reset string for next line
|
// //reset string for next line
|
||||||
@@ -243,26 +298,15 @@ namespace esphome {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
// //Print Stoped
|
// //Print Stoped
|
||||||
if(MarlinOutput.find("Print Aborted") != std::string::npos) {
|
if(MarlinOutput.compare("Print Aborted") == 0) {
|
||||||
set_printer_state("STOPPED");
|
set_printer_state("STOPPED");
|
||||||
|
|
||||||
//reset string for next line
|
//reset string for next line
|
||||||
MarlinOutput="";
|
MarlinOutput="";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ESP_LOGD(TAG, ">#%s#",MarlinOutput.c_str());
|
||||||
|
|
||||||
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());
|
|
||||||
MarlinOutput="";
|
MarlinOutput="";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -299,14 +343,14 @@ namespace esphome {
|
|||||||
return 0.0;
|
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){
|
int Marlin2::process_print_time_msg(double* current, double* remaining, float progress){
|
||||||
MarlinTime = MarlinOutput.substr(16);
|
MarlinTime = MarlinOutput.substr(16);
|
||||||
float d = 0, h = 0, m = 0, s = 0;
|
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) {
|
if (sscanf(MarlinTime.c_str() ,"%fd %fh %fm %fs", &d, &h, &m, &s)!=4) {
|
||||||
d=0;
|
d=0;
|
||||||
@@ -339,39 +383,35 @@ namespace esphome {
|
|||||||
find_text_sensor("printer_state")->publish_state(status);
|
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;
|
// PrinterState = status;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Marlin2::to_dos_name(std::string filename) {
|
std::string Marlin2::to_dos_name(std::string filename) {
|
||||||
std::string shortName;
|
auto it = file_table_.find(filename);
|
||||||
size_t dotPos = filename.find_last_of('.');
|
if (it != file_table_.end()) {
|
||||||
|
return file_table_[filename];
|
||||||
// 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";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Truncate extension to 3 characters
|
return filename;
|
||||||
if (extPart.length() > 3) {
|
}
|
||||||
extPart = extPart.substr(0, 3);
|
|
||||||
|
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
|
return dos_filename;
|
||||||
shortName = namePart;
|
}
|
||||||
if (!extPart.empty()) {
|
|
||||||
shortName += "." + extPart;
|
std::string to_lower(std::string s) {
|
||||||
}
|
return s;
|
||||||
|
// String arduinoStr(s.c_str()); // převod na Arduino String
|
||||||
// Convert to uppercase (DOS filenames are case-insensitive, usually stored in uppercase)
|
// arduinoStr.toLowerCase(); // změní obsah přímo
|
||||||
std::transform(shortName.begin(), shortName.end(), shortName.begin(), ::toupper);
|
// return std::string(arduinoStr.c_str()); // zpět na std::string
|
||||||
|
|
||||||
return shortName;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace esphome
|
} // namespace esphome
|
@@ -26,8 +26,10 @@ class Marlin2 : public PollingComponent, public uart::UARTDevice {
|
|||||||
text_sensor::TextSensor* find_text_sensor(std::string key);
|
text_sensor::TextSensor* find_text_sensor(std::string key);
|
||||||
#endif
|
#endif
|
||||||
void write(std::string status);
|
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; }
|
float get_setup_priority() const override { return setup_priority::LATE; }
|
||||||
void setup() override;
|
void setup() override;
|
||||||
void update() override;
|
void update() override;
|
||||||
|
@@ -13,13 +13,19 @@ CONFIG_SCHEMA = cv.Schema(
|
|||||||
cv.Optional("printer_state"): text_sensor.text_sensor_schema(
|
cv.Optional("printer_state"): text_sensor.text_sensor_schema(
|
||||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
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"))
|
).extend(cv.polling_component_schema("15s"))
|
||||||
|
|
||||||
async def to_code(config):
|
async def to_code(config):
|
||||||
server = await cg.get_variable(config[CONF_MARLIN])
|
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:
|
if sName in config:
|
||||||
sens = await text_sensor.new_text_sensor(config[sName])
|
sens = await text_sensor.new_text_sensor(config[sName])
|
||||||
cg.add(server.add_text_sensor(sName,sens))
|
cg.add(server.add_text_sensor(sName,sens))
|
Reference in New Issue
Block a user