Fixes
This commit is contained in:
259
marlin2.cpp
259
marlin2.cpp
@@ -1,22 +1,20 @@
|
|||||||
#include "marlin2.h"
|
#include "marlin2.h"
|
||||||
#include "esphome/core/log.h"
|
#include "esphome/core/log.h"
|
||||||
#include <string>
|
|
||||||
|
|
||||||
namespace esphome::marlin2 {
|
namespace esphome::marlin2 {
|
||||||
static const char *TAG = "marlin2";
|
static const char *TAG = "marlin2";
|
||||||
|
|
||||||
|
// ── Entity registration ───────────────────────────────────────────────────
|
||||||
|
|
||||||
#ifdef USE_SENSOR
|
#ifdef USE_SENSOR
|
||||||
void Marlin2::add_sensor(const std::string &name, sensor::Sensor *sens) {
|
void Marlin2::add_sensor(const std::string &name, sensor::Sensor *sens) {
|
||||||
sensors_.push_back({name, sens});
|
sensors_.push_back({name, sens});
|
||||||
}
|
}
|
||||||
|
|
||||||
sensor::Sensor *Marlin2::find_sensor(const std::string &key) {
|
void Marlin2::publish_sensor(const std::string &key, float value) {
|
||||||
for (const auto &pair : sensors_) {
|
auto *s = find_in_(sensors_, key);
|
||||||
if (key == pair.first) {
|
if (s != nullptr && s->get_state() != value)
|
||||||
return pair.second;
|
s->publish_state(value);
|
||||||
}
|
|
||||||
}
|
|
||||||
return nullptr;
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -25,13 +23,10 @@ namespace esphome::marlin2 {
|
|||||||
text_sensors_.push_back({name, sens});
|
text_sensors_.push_back({name, sens});
|
||||||
}
|
}
|
||||||
|
|
||||||
text_sensor::TextSensor *Marlin2::find_text_sensor(const std::string &key) {
|
void Marlin2::publish_text_sensor(const std::string &key, const std::string &value) {
|
||||||
for (const auto &pair : text_sensors_) {
|
auto *s = find_in_(text_sensors_, key);
|
||||||
if (key == pair.first) {
|
if (s != nullptr && s->get_state() != value)
|
||||||
return pair.second;
|
s->publish_state(value);
|
||||||
}
|
|
||||||
}
|
|
||||||
return nullptr;
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -39,15 +34,6 @@ namespace esphome::marlin2 {
|
|||||||
void Marlin2::add_select(const std::string &name, select::Select *sel) {
|
void Marlin2::add_select(const std::string &name, select::Select *sel) {
|
||||||
selects_.push_back({name, sel});
|
selects_.push_back({name, sel});
|
||||||
}
|
}
|
||||||
|
|
||||||
select::Select *Marlin2::find_select(const std::string &key) {
|
|
||||||
for (const auto &pair : selects_) {
|
|
||||||
if (key == pair.first) {
|
|
||||||
return pair.second;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef USE_BINARY_SENSOR
|
#ifdef USE_BINARY_SENSOR
|
||||||
@@ -55,42 +41,32 @@ namespace esphome::marlin2 {
|
|||||||
binary_sensors_.push_back({name, bs});
|
binary_sensors_.push_back({name, bs});
|
||||||
}
|
}
|
||||||
|
|
||||||
binary_sensor::BinarySensor *Marlin2::find_binary_sensor(const std::string &key) {
|
void Marlin2::publish_binary_sensor(const std::string &key, bool value) {
|
||||||
for (const auto &pair : binary_sensors_) {
|
auto *s = find_in_(binary_sensors_, key);
|
||||||
if (key == pair.first) {
|
if (s != nullptr && s->state != value)
|
||||||
return pair.second;
|
s->publish_state(value);
|
||||||
}
|
|
||||||
}
|
|
||||||
return nullptr;
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// ── Lifecycle ─────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
void Marlin2::setup() {
|
void Marlin2::setup() {
|
||||||
marlin_output_.reserve(256);
|
marlin_output_.reserve(256);
|
||||||
marlin_response_output_.reserve(256);
|
|
||||||
marlin_time_.reserve(32);
|
marlin_time_.reserve(32);
|
||||||
printer_state_.reserve(32);
|
|
||||||
|
|
||||||
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");
|
||||||
flush();
|
flush();
|
||||||
|
|
||||||
set_printer_state("IDLE");
|
set_printer_state("IDLE");
|
||||||
|
|
||||||
|
set_interval(15000, [this]() {
|
||||||
|
if (print_progress_ != 100)
|
||||||
|
write_str("M27\r\nM31\r\n");
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void Marlin2::dump_config() {
|
void Marlin2::loop() {
|
||||||
ESP_LOGCONFIG(TAG, "Marlin2:");
|
|
||||||
this->check_uart_settings(115200);
|
|
||||||
LOG_UPDATE_INTERVAL(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Marlin2::write(std::string gcode) {
|
|
||||||
ESP_LOGD(TAG, "->GCODE: %s", gcode.c_str());
|
|
||||||
write_str((std::string("\r\n\r\n") + gcode + std::string("\r\n")).c_str());
|
|
||||||
flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Marlin2::update() {
|
|
||||||
while (available()) {
|
while (available()) {
|
||||||
char c = read();
|
char c = read();
|
||||||
if (c == '\n' || c == '\r') {
|
if (c == '\n' || c == '\r') {
|
||||||
@@ -99,13 +75,23 @@ namespace esphome::marlin2 {
|
|||||||
marlin_output_ += c;
|
marlin_output_ += c;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (millis() - millis_progress_ > 15000 && print_progress_ != 100) {
|
|
||||||
millis_progress_ = millis();
|
|
||||||
write_str("M27\r\nM31\r\n");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Marlin2::dump_config() {
|
||||||
|
ESP_LOGCONFIG(TAG, "Marlin2:");
|
||||||
|
this->check_uart_settings(115200);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── G-code output ─────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
void Marlin2::write(const std::string &gcode) {
|
||||||
|
ESP_LOGD(TAG, "->GCODE: %s", gcode.c_str());
|
||||||
|
write_str(("\r\n\r\n" + gcode + "\r\n").c_str());
|
||||||
|
flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Line parsing ──────────────────────────────────────────────────────────
|
||||||
|
|
||||||
void Marlin2::process_line() {
|
void Marlin2::process_line() {
|
||||||
if (marlin_output_.size() < 3) {
|
if (marlin_output_.size() < 3) {
|
||||||
marlin_output_ = "";
|
marlin_output_ = "";
|
||||||
@@ -131,27 +117,19 @@ namespace esphome::marlin2 {
|
|||||||
if (listing_file_ && marlin_output_.compare("End file list") == 0) {
|
if (listing_file_ && marlin_output_.compare("End file list") == 0) {
|
||||||
ESP_LOGD(TAG, "Listing of files stopped!");
|
ESP_LOGD(TAG, "Listing of files stopped!");
|
||||||
listing_file_ = false;
|
listing_file_ = false;
|
||||||
|
|
||||||
#ifdef USE_BINARY_SENSOR
|
|
||||||
if (find_binary_sensor("sd_card_present") != nullptr) {
|
|
||||||
find_binary_sensor("sd_card_present")->publish_state(true);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
publish_sd_files();
|
publish_sd_files();
|
||||||
|
#ifdef USE_BINARY_SENSOR
|
||||||
#ifdef USE_SENSOR
|
publish_binary_sensor("sd_card_present", true);
|
||||||
if (find_sensor("sd_card_file_count") != nullptr) {
|
#endif
|
||||||
find_sensor("sd_card_file_count")->publish_state(static_cast<float>(file_table_.size()));
|
#ifdef USE_SENSOR
|
||||||
}
|
publish_sensor("sd_card_file_count", static_cast<float>(file_table_.size()));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
marlin_output_ = "";
|
marlin_output_ = "";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (listing_file_ && marlin_output_.find(".GCO ") > 1) {
|
if (listing_file_ && marlin_output_.find(".GCO ") > 1) {
|
||||||
size_t first_space = marlin_output_.find(' ');
|
size_t first_space = marlin_output_.find(' ');
|
||||||
size_t second_space = marlin_output_.find(' ', first_space + 1);
|
size_t second_space = marlin_output_.find(' ', first_space + 1);
|
||||||
|
|
||||||
if (first_space != std::string::npos) {
|
if (first_space != std::string::npos) {
|
||||||
@@ -163,11 +141,9 @@ namespace esphome::marlin2 {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string long_name = short_name;
|
std::string long_name = (second_space != std::string::npos && second_space > first_space)
|
||||||
|
? marlin_output_.substr(second_space + 1)
|
||||||
if (second_space != std::string::npos && second_space > first_space) {
|
: short_name;
|
||||||
long_name = marlin_output_.substr(second_space + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
file_table_[long_name] = short_name;
|
file_table_[long_name] = short_name;
|
||||||
}
|
}
|
||||||
@@ -176,73 +152,56 @@ namespace esphome::marlin2 {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse periodic temperature read out message
|
// Temperature report: T:xx/xx B:xx/xx
|
||||||
if (
|
if (
|
||||||
marlin_output_.find(" T:") == 0 ||
|
marlin_output_.find(" T:") == 0 ||
|
||||||
marlin_output_.find("T:") == 0 ||
|
marlin_output_.find("T:") == 0 ||
|
||||||
marlin_output_.find("ok T:") == 0 ||
|
marlin_output_.find("ok T:") == 0 ||
|
||||||
marlin_output_.find(" ok T:") == 0
|
marlin_output_.find(" ok T:") == 0
|
||||||
) {
|
) {
|
||||||
float ext_temperature, ext_set_temperature, bed_temperature, bed_set_temperature;
|
float ext_temp, ext_set, bed_temp, bed_set;
|
||||||
if (process_temp_msg(&ext_temperature, &ext_set_temperature, &bed_temperature, &bed_set_temperature) != 0) {
|
if (process_temp_msg(&ext_temp, &ext_set, &bed_temp, &bed_set) != 0) {
|
||||||
#ifdef USE_SENSOR
|
#ifdef USE_SENSOR
|
||||||
if (find_sensor("bed_temperature") != nullptr)
|
publish_sensor("bed_temperature", bed_temp);
|
||||||
find_sensor("bed_temperature")->publish_state(bed_temperature);
|
publish_sensor("bed_set_temperature", bed_set);
|
||||||
|
publish_sensor("ext_temperature", ext_temp);
|
||||||
if (find_sensor("bed_set_temperature") != nullptr)
|
publish_sensor("ext_set_temperature", ext_set);
|
||||||
find_sensor("bed_set_temperature")->publish_state(bed_set_temperature);
|
|
||||||
|
|
||||||
if (find_sensor("ext_temperature") != nullptr)
|
|
||||||
find_sensor("ext_temperature")->publish_state(ext_temperature);
|
|
||||||
|
|
||||||
if (find_sensor("ext_set_temperature") != nullptr)
|
|
||||||
find_sensor("ext_set_temperature")->publish_state(ext_set_temperature);
|
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_TEXT_SENSOR
|
#ifdef USE_TEXT_SENSOR
|
||||||
if (bed_set_temperature == 0.0 && ext_set_temperature == 0.0) {
|
if (bed_set == 0.0f && ext_set == 0.0f) {
|
||||||
if (ext_temperature < 32.0 && bed_temperature < 32.0) {
|
if (ext_temp < 32.0f && bed_temp < 32.0f)
|
||||||
set_printer_state("IDLE");
|
set_printer_state("IDLE");
|
||||||
} else if (ext_temperature < 150.0 && bed_temperature < 55.0) {
|
else if (ext_temp < 150.0f && bed_temp < 55.0f)
|
||||||
set_printer_state("COOLING");
|
set_printer_state("COOLING");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (print_progress_ == 0.0 && (bed_set_temperature != 0.0 || ext_set_temperature != 0.0)) {
|
if (print_progress_ == 0.0f && (bed_set != 0.0f || ext_set != 0.0f))
|
||||||
set_printer_state("PREHEATING");
|
set_printer_state("PREHEATING");
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
marlin_output_ = "";
|
marlin_output_ = "";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse progress of the print
|
// Print progress: SD printing byte X/Y
|
||||||
if (marlin_output_.find("SD printing byte") == 0) {
|
if (marlin_output_.find("SD printing byte") == 0) {
|
||||||
print_progress_ = process_progress_msg();
|
print_progress_ = process_progress_msg();
|
||||||
#ifdef USE_SENSOR
|
#ifdef USE_SENSOR
|
||||||
if (find_sensor("print_progress") != nullptr)
|
publish_sensor("print_progress", print_progress_);
|
||||||
find_sensor("print_progress")->publish_state(print_progress_);
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
set_printer_state("PRINTING");
|
set_printer_state("PRINTING");
|
||||||
marlin_output_ = "";
|
marlin_output_ = "";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse print time
|
// Print time: echo:Print time: Xh Xm Xs
|
||||||
if (marlin_output_.find("echo:Print time: ") == 0) {
|
if (marlin_output_.find("echo:Print time: ") == 0) {
|
||||||
double current = 0;
|
double current = 0, remaining = 0;
|
||||||
double remaining = 0;
|
|
||||||
if (process_print_time_msg(¤t, &remaining, print_progress_) != 0) {
|
if (process_print_time_msg(¤t, &remaining, print_progress_) != 0) {
|
||||||
#ifdef USE_SENSOR
|
#ifdef USE_SENSOR
|
||||||
if (find_sensor("print_time") != nullptr)
|
publish_sensor("print_time", static_cast<float>(current));
|
||||||
find_sensor("print_time")->publish_state(current);
|
publish_sensor("print_time_remaining", static_cast<float>(remaining));
|
||||||
|
|
||||||
if (find_sensor("print_time_remaining") != nullptr)
|
|
||||||
find_sensor("print_time_remaining")->publish_state(remaining);
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
marlin_output_ = "";
|
marlin_output_ = "";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -251,48 +210,34 @@ namespace esphome::marlin2 {
|
|||||||
if (marlin_output_.find("File opened: ") == 0) {
|
if (marlin_output_.find("File opened: ") == 0) {
|
||||||
size_t start_pos = std::strlen("File opened: ");
|
size_t start_pos = std::strlen("File opened: ");
|
||||||
size_t size_pos = marlin_output_.find(" Size: ");
|
size_t size_pos = marlin_output_.find(" Size: ");
|
||||||
|
|
||||||
if (size_pos != std::string::npos) {
|
if (size_pos != std::string::npos) {
|
||||||
std::string filename = from_dos_name(marlin_output_.substr(start_pos, size_pos - start_pos));
|
std::string filename = from_dos_name(marlin_output_.substr(start_pos, size_pos - start_pos));
|
||||||
ESP_LOGD(TAG, "Soubor: %s", filename.c_str());
|
ESP_LOGD(TAG, "File: %s", filename.c_str());
|
||||||
|
|
||||||
#ifdef USE_TEXT_SENSOR
|
#ifdef USE_TEXT_SENSOR
|
||||||
if (find_text_sensor("sd_card_file_selected") != nullptr) {
|
publish_text_sensor("sd_card_file_selected", filename);
|
||||||
find_text_sensor("sd_card_file_selected")->publish_state(filename.c_str());
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
marlin_output_ = "";
|
marlin_output_ = "";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print from SD card started
|
|
||||||
if (marlin_output_.compare("File selected") == 0) {
|
if (marlin_output_.compare("File selected") == 0) {
|
||||||
set_printer_state("PRINTING");
|
set_printer_state("PRINTING");
|
||||||
marlin_output_ = "";
|
marlin_output_ = "";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print finished
|
|
||||||
if (marlin_output_.compare("Done printing") == 0) {
|
if (marlin_output_.compare("Done printing") == 0) {
|
||||||
print_progress_ = 100;
|
print_progress_ = 100;
|
||||||
#ifdef USE_SENSOR
|
#ifdef USE_SENSOR
|
||||||
if (find_sensor("print_progress") != nullptr)
|
publish_sensor("print_progress", 100.0f);
|
||||||
find_sensor("print_progress")->publish_state(print_progress_);
|
publish_sensor("print_time_remaining", 0.0f);
|
||||||
|
|
||||||
if (find_sensor("print_time_remaining") != nullptr)
|
|
||||||
find_sensor("print_time_remaining")->publish_state(0);
|
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_TEXT_SENSOR
|
|
||||||
set_printer_state("FINISHED");
|
set_printer_state("FINISHED");
|
||||||
#endif
|
|
||||||
|
|
||||||
marlin_output_ = "";
|
marlin_output_ = "";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print aborted
|
|
||||||
if (marlin_output_.compare("Print Aborted") == 0) {
|
if (marlin_output_.compare("Print Aborted") == 0) {
|
||||||
set_printer_state("STOPPED");
|
set_printer_state("STOPPED");
|
||||||
marlin_output_ = "";
|
marlin_output_ = "";
|
||||||
@@ -303,63 +248,55 @@ namespace esphome::marlin2 {
|
|||||||
marlin_output_ = "";
|
marlin_output_ = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ── SD card file list ─────────────────────────────────────────────────────
|
||||||
|
|
||||||
void Marlin2::publish_sd_files() {
|
void Marlin2::publish_sd_files() {
|
||||||
#if defined(USE_TEXT_SENSOR) || defined(USE_SELECT)
|
|
||||||
std::string chunk;
|
std::string chunk;
|
||||||
uint8_t count = 0;
|
uint8_t count = 0;
|
||||||
|
|
||||||
#ifdef USE_SELECT
|
#ifdef USE_SELECT
|
||||||
std::vector<std::string> file_names;
|
std::vector<std::string> file_names;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
for (const auto &[key, value] : file_table_) {
|
for (const auto &[key, value] : file_table_) {
|
||||||
if (count >= max_sd_files_) {
|
if (count >= max_sd_files_) {
|
||||||
ESP_LOGW(TAG, "SD file list truncated at %u files (max_sd_files=%u, total=%u)",
|
ESP_LOGW(TAG, "SD file list truncated to %u files (total: %u)",
|
||||||
max_sd_files_, max_sd_files_, (unsigned) file_table_.size());
|
max_sd_files_, (unsigned) file_table_.size());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if (!chunk.empty()) chunk += "|";
|
||||||
|
chunk += key;
|
||||||
#ifdef USE_SELECT
|
#ifdef USE_SELECT
|
||||||
file_names.push_back(key);
|
file_names.push_back(key);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (!chunk.empty()) chunk += "|";
|
|
||||||
chunk += key;
|
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef USE_TEXT_SENSOR
|
#ifdef USE_TEXT_SENSOR
|
||||||
if (find_text_sensor("sd_card_files") != nullptr) {
|
publish_text_sensor("sd_card_files", chunk);
|
||||||
find_text_sensor("sd_card_files")->publish_state(chunk);
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef USE_SELECT
|
#ifdef USE_SELECT
|
||||||
if (find_select("sd_card_file_select") != nullptr) {
|
auto *sel = find_in_(selects_, std::string("sd_card_file_select"));
|
||||||
find_select("sd_card_file_select")->traits.set_options(file_names);
|
if (sel != nullptr)
|
||||||
}
|
sel->traits.set_options(file_names);
|
||||||
#endif
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ── Message parsers ───────────────────────────────────────────────────────
|
||||||
|
|
||||||
int Marlin2::process_temp_msg(float *ext_temperature, float *ext_set_temperature, float *bed_temperature, float *bed_set_temperature) {
|
int Marlin2::process_temp_msg(float *ext_temperature, float *ext_set_temperature, float *bed_temperature, float *bed_set_temperature) {
|
||||||
float dc;
|
float dc;
|
||||||
|
|
||||||
while (marlin_output_.find(" ") != std::string::npos)
|
while (marlin_output_.find(" ") != std::string::npos)
|
||||||
marlin_output_.erase(marlin_output_.find(' '), 1);
|
marlin_output_.erase(marlin_output_.find(' '), 1);
|
||||||
|
|
||||||
while (marlin_output_.find("ok") != std::string::npos)
|
while (marlin_output_.find("ok") != std::string::npos)
|
||||||
marlin_output_.erase(marlin_output_.find("ok"), 2);
|
marlin_output_.erase(marlin_output_.find("ok"), 2);
|
||||||
|
|
||||||
if (sscanf(marlin_output_.c_str(), "T:%f/%fB:%f/%f", ext_temperature, ext_set_temperature, bed_temperature, bed_set_temperature) == 4)
|
if (sscanf(marlin_output_.c_str(), "T:%f/%fB:%f/%f", ext_temperature, ext_set_temperature, bed_temperature, bed_set_temperature) == 4)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
if (sscanf(marlin_output_.c_str(), "T:%f/%f(%f)B:%f/%f(%f)", ext_temperature, ext_set_temperature, &dc, bed_temperature, bed_set_temperature, &dc) == 6)
|
if (sscanf(marlin_output_.c_str(), "T:%f/%f(%f)B:%f/%f(%f)", ext_temperature, ext_set_temperature, &dc, bed_temperature, bed_set_temperature, &dc) == 6)
|
||||||
return 2;
|
return 2;
|
||||||
|
|
||||||
if (sscanf(marlin_output_.c_str(), "T:%f/%fT0:%f/%fT1:%f/%fB:%f/%f", ext_temperature, ext_set_temperature, &dc, &dc, &dc, &dc, bed_temperature, bed_set_temperature) == 8)
|
if (sscanf(marlin_output_.c_str(), "T:%f/%fT0:%f/%fT1:%f/%fB:%f/%f", ext_temperature, ext_set_temperature, &dc, &dc, &dc, &dc, bed_temperature, bed_set_temperature) == 8)
|
||||||
return 3;
|
return 3;
|
||||||
|
|
||||||
if (sscanf(marlin_output_.c_str(), "T0:%f/%fT1:%f/%fB:%f/%f", ext_temperature, ext_set_temperature, &dc, &dc, bed_temperature, bed_set_temperature) == 6)
|
if (sscanf(marlin_output_.c_str(), "T0:%f/%fT1:%f/%fB:%f/%f", ext_temperature, ext_set_temperature, &dc, &dc, bed_temperature, bed_set_temperature) == 6)
|
||||||
return 4;
|
return 4;
|
||||||
|
|
||||||
@@ -387,47 +324,39 @@ namespace esphome::marlin2 {
|
|||||||
if (sscanf(marlin_time_.c_str(), "%fd %fh %fm %fs", &d, &h, &m, &s) != 4) {
|
if (sscanf(marlin_time_.c_str(), "%fd %fh %fm %fs", &d, &h, &m, &s) != 4) {
|
||||||
d = 0;
|
d = 0;
|
||||||
if (sscanf(marlin_time_.c_str(), "%fh %fm %fs", &h, &m, &s) != 3) {
|
if (sscanf(marlin_time_.c_str(), "%fh %fm %fs", &h, &m, &s) != 3) {
|
||||||
d = 0; h = 0;
|
h = 0;
|
||||||
if (sscanf(marlin_time_.c_str(), "%fm %fs", &m, &s) != 2) {
|
if (sscanf(marlin_time_.c_str(), "%fm %fs", &m, &s) != 2) {
|
||||||
d = 0; h = 0; m = 0;
|
m = 0;
|
||||||
if (sscanf(marlin_time_.c_str(), "%fs", &s) != 1) {
|
if (sscanf(marlin_time_.c_str(), "%fs", &s) != 1)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
*current = round(((d) * 24 * 60 * 60) + ((h) * 60 * 60) + ((m) * 60) + (s));
|
*current = round((d * 24 * 60 * 60) + (h * 60 * 60) + (m * 60) + s);
|
||||||
|
|
||||||
if (progress != 0.0 && progress != 100.0) {
|
if (progress != 0.0f && progress != 100.0f)
|
||||||
*remaining = (((100 * *current) / round(progress)) - *current);
|
*remaining = ((100.0 * *current) / round(progress)) - *current;
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Marlin2::set_printer_state(std::string status) {
|
void Marlin2::set_printer_state(const std::string &status) {
|
||||||
#ifdef USE_TEXT_SENSOR
|
#ifdef USE_TEXT_SENSOR
|
||||||
if (find_text_sensor("printer_state") != nullptr) {
|
publish_text_sensor("printer_state", status);
|
||||||
find_text_sensor("printer_state")->publish_state(status);
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Marlin2::to_dos_name(std::string filename) {
|
// ── Filename mapping ──────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
std::string Marlin2::to_dos_name(const std::string &filename) {
|
||||||
auto it = file_table_.find(filename);
|
auto it = file_table_.find(filename);
|
||||||
if (it != file_table_.end()) {
|
return (it != file_table_.end()) ? it->second : filename;
|
||||||
return it->second;
|
|
||||||
}
|
|
||||||
return filename;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Marlin2::from_dos_name(std::string dos_filename) {
|
std::string Marlin2::from_dos_name(const std::string &dos_filename) {
|
||||||
for (const auto &[key, value] : file_table_) {
|
for (const auto &[key, value] : file_table_)
|
||||||
if (value == dos_filename) {
|
if (value == dos_filename) return key;
|
||||||
return key;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return dos_filename;
|
return dos_filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
30
marlin2.h
30
marlin2.h
@@ -19,46 +19,42 @@
|
|||||||
|
|
||||||
namespace esphome::marlin2 {
|
namespace esphome::marlin2 {
|
||||||
|
|
||||||
class Marlin2 : public PollingComponent, public uart::UARTDevice {
|
class Marlin2 : public Component, public uart::UARTDevice {
|
||||||
public:
|
public:
|
||||||
Marlin2() = default;
|
Marlin2() = default;
|
||||||
|
|
||||||
#ifdef USE_SENSOR
|
#ifdef USE_SENSOR
|
||||||
void add_sensor(const std::string &name, sensor::Sensor *sens);
|
void add_sensor(const std::string &name, sensor::Sensor *sens);
|
||||||
sensor::Sensor *find_sensor(const std::string &key);
|
void publish_sensor(const std::string &key, float value);
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_TEXT_SENSOR
|
#ifdef USE_TEXT_SENSOR
|
||||||
void add_text_sensor(const std::string &name, text_sensor::TextSensor *tSens);
|
void add_text_sensor(const std::string &name, text_sensor::TextSensor *tSens);
|
||||||
text_sensor::TextSensor *find_text_sensor(const std::string &key);
|
void publish_text_sensor(const std::string &key, const std::string &value);
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_SELECT
|
#ifdef USE_SELECT
|
||||||
void add_select(const std::string &name, select::Select *sel);
|
void add_select(const std::string &name, select::Select *sel);
|
||||||
select::Select *find_select(const std::string &key);
|
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_BINARY_SENSOR
|
#ifdef USE_BINARY_SENSOR
|
||||||
void add_binary_sensor(const std::string &name, binary_sensor::BinarySensor *bs);
|
void add_binary_sensor(const std::string &name, binary_sensor::BinarySensor *bs);
|
||||||
binary_sensor::BinarySensor *find_binary_sensor(const std::string &key);
|
void publish_binary_sensor(const std::string &key, bool value);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void set_max_sd_files(uint8_t n) { max_sd_files_ = n; }
|
void set_max_sd_files(uint8_t n) { max_sd_files_ = n; }
|
||||||
|
|
||||||
void write(std::string gcode);
|
void write(const std::string &gcode);
|
||||||
std::string to_dos_name(std::string filename);
|
std::string to_dos_name(const std::string &filename);
|
||||||
std::string from_dos_name(std::string dos_filename);
|
std::string from_dos_name(const std::string &dos_filename);
|
||||||
|
|
||||||
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 loop() override;
|
||||||
void dump_config() override;
|
void dump_config() override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::string marlin_output_;
|
std::string marlin_output_;
|
||||||
std::string marlin_response_output_;
|
|
||||||
std::string marlin_time_;
|
std::string marlin_time_;
|
||||||
std::string printer_state_;
|
|
||||||
|
|
||||||
float print_progress_ = 0;
|
float print_progress_ = 0;
|
||||||
double print_time_offset_ = 0;
|
|
||||||
uint8_t max_sd_files_ = 20;
|
uint8_t max_sd_files_ = 20;
|
||||||
|
|
||||||
#ifdef USE_SENSOR
|
#ifdef USE_SENSOR
|
||||||
@@ -75,14 +71,20 @@ class Marlin2 : public PollingComponent, public uart::UARTDevice {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
void process_line();
|
void process_line();
|
||||||
void set_printer_state(std::string status);
|
void set_printer_state(const std::string &status);
|
||||||
void publish_sd_files();
|
void publish_sd_files();
|
||||||
int process_temp_msg(float *ext_temperature, float *ext_set_temperature, float *bed_temperature, float *bed_set_temperature);
|
int process_temp_msg(float *ext_temperature, float *ext_set_temperature, float *bed_temperature, float *bed_set_temperature);
|
||||||
float process_progress_msg();
|
float process_progress_msg();
|
||||||
int process_print_time_msg(double *current, double *remaining, float progress);
|
int process_print_time_msg(double *current, double *remaining, float progress);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
unsigned long millis_progress_ = 0;
|
template<typename T>
|
||||||
|
static T *find_in_(std::vector<std::pair<std::string, T *>> &vec, const std::string &key) {
|
||||||
|
for (auto &pair : vec)
|
||||||
|
if (pair.first == key) return pair.second;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
std::unordered_map<std::string, std::string> file_table_;
|
std::unordered_map<std::string, std::string> file_table_;
|
||||||
bool listing_file_ = false;
|
bool listing_file_ = false;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user