Compare commits
6 Commits
9f9227cf53
...
main
Author | SHA1 | Date | |
---|---|---|---|
91f2dd54be | |||
68ab694d8a | |||
fd901fd1e1 | |||
6f7ccc267d | |||
ebef6ce886 | |||
dce9c5d67d |
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
s
|
||||||
|
__pycache__/*
|
11
README.md
11
README.md
@@ -8,7 +8,16 @@ A configured uart is required.
|
|||||||
|
|
||||||
Example:
|
Example:
|
||||||
```yaml
|
```yaml
|
||||||
|
api:
|
||||||
|
actions:
|
||||||
|
- action: 'set_bed_temperature'
|
||||||
|
variables:
|
||||||
|
temperature: int
|
||||||
|
then:
|
||||||
|
- marlin2.write_g_code
|
||||||
|
gcode: !lampda "return temperature"
|
||||||
|
...
|
||||||
|
|
||||||
marlin2:
|
marlin2:
|
||||||
uart_id: uart_bus
|
uart_id: uart_bus
|
||||||
|
|
||||||
|
55
__init__.py
55
__init__.py
@@ -16,15 +16,18 @@ from esphome.const import (
|
|||||||
DEVICE_CLASS_TEMPERATURE,
|
DEVICE_CLASS_TEMPERATURE,
|
||||||
DEVICE_CLASS_DURATION,
|
DEVICE_CLASS_DURATION,
|
||||||
CONF_DATA,
|
CONF_DATA,
|
||||||
|
CONF_VALUE
|
||||||
)
|
)
|
||||||
from esphome import pins, automation
|
from esphome import pins, automation
|
||||||
|
from esphome.core import CORE, coroutine_with_priority
|
||||||
|
|
||||||
CODEOWNERS = ["@jonatanrek"]
|
CODEOWNERS = ["@jonatanrek"]
|
||||||
DEPENDENCIES = ['uart']
|
DEPENDENCIES = ['uart']
|
||||||
CONF_MARLIN2_ID = "marlin2_id"
|
CONF_MARLIN2_ID = "marlin2_id"
|
||||||
|
|
||||||
Marlin2 = cg.esphome_ns.class_('Marlin2', cg.Component)
|
Marlin2 = cg.esphome_ns.class_('Marlin2', cg.Component)
|
||||||
Marlin2WriteAction = Marlin2.class_("Marlin2WriteAction", automation.Action)
|
WriteAction = cg.esphome_ns.class_("WriteAction", automation.Action)
|
||||||
|
PrintFileAction = cg.esphome_ns.class_("PrintFileAction", automation.Action)
|
||||||
|
|
||||||
CONFIG_SCHEMA = cv.All(
|
CONFIG_SCHEMA = cv.All(
|
||||||
cv.Schema({
|
cv.Schema({
|
||||||
@@ -43,19 +46,43 @@ def validate_raw_data(value):
|
|||||||
return cv.Schema([cv.hex_uint8_t])(value)
|
return cv.Schema([cv.hex_uint8_t])(value)
|
||||||
raise cv.Invalid("data must either be a string wrapped in quotes or a list of bytes")
|
raise cv.Invalid("data must either be a string wrapped in quotes or a list of bytes")
|
||||||
|
|
||||||
@automation.register_action(
|
@coroutine_with_priority(100.0)
|
||||||
"marlin2.write",
|
|
||||||
Marlin2WriteAction,
|
|
||||||
cv.maybe_simple_value(
|
|
||||||
{
|
|
||||||
cv.GenerateID(): cv.declare_id(Marlin2),
|
|
||||||
cv.Required(CONF_DATA): cv.templatable(validate_raw_data),
|
|
||||||
},
|
|
||||||
key=CONF_DATA,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
async def to_code(config):
|
async def to_code(config):
|
||||||
var = cg.new_Pvariable(config[CONF_ID])
|
var = cg.new_Pvariable(config[CONF_ID])
|
||||||
await cg.register_component(var, config)
|
await cg.register_component(var, config)
|
||||||
await uart.register_uart_device(var, config)
|
await uart.register_uart_device(var, config)
|
||||||
|
|
||||||
|
OPERATION_BASE_SCHEMA = cv.Schema({
|
||||||
|
cv.GenerateID(): cv.use_id(Marlin2),
|
||||||
|
cv.Required(CONF_VALUE): cv.templatable(cv.string_strict),
|
||||||
|
})
|
||||||
|
|
||||||
|
OPERATION_BASE_SCHEMA_2 = cv.Schema({
|
||||||
|
cv.GenerateID(): cv.use_id(Marlin2),
|
||||||
|
cv.Required(CONF_VALUE): cv.templatable(cv.string_strict),
|
||||||
|
})
|
||||||
|
|
||||||
|
@automation.register_action(
|
||||||
|
"marlin2.write",
|
||||||
|
WriteAction,
|
||||||
|
OPERATION_BASE_SCHEMA,
|
||||||
|
)
|
||||||
|
|
||||||
|
async def marlin2_write_to_code(config, action_id, template_arg, args):
|
||||||
|
paren = await cg.get_variable(config[CONF_ID])
|
||||||
|
var = cg.new_Pvariable(action_id, template_arg, paren)
|
||||||
|
template_ = await cg.templatable(config[CONF_VALUE], args, cg.std_string)
|
||||||
|
cg.add(var.set_value(template_))
|
||||||
|
return var
|
||||||
|
|
||||||
|
@automation.register_action(
|
||||||
|
"marlin2.print_file",
|
||||||
|
PrintFileAction,
|
||||||
|
OPERATION_BASE_SCHEMA_2,
|
||||||
|
)
|
||||||
|
async def marlin2_print_file_to_code(config, action_id, template_arg, args):
|
||||||
|
paren = await cg.get_variable(config[CONF_ID])
|
||||||
|
var = cg.new_Pvariable(action_id, template_arg, paren)
|
||||||
|
template_ = await cg.templatable(config[CONF_VALUE], args, cg.std_string)
|
||||||
|
cg.add(var.set_value(template_))
|
||||||
|
return var
|
52
automation.h
52
automation.h
@@ -1,36 +1,38 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "marlin2.h"
|
|
||||||
#include "esphome/core/automation.h"
|
#include "esphome/core/automation.h"
|
||||||
|
#include "esphome/core/component.h"
|
||||||
#include <vector>
|
#include "marlin2.h"
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
|
static const char *TAG = "marlin2";
|
||||||
|
|
||||||
template<typename... Ts> class Marlin2WriteAction : public Action<Ts...>, public Parented<Marlin2> {
|
template<typename... Ts> class WriteAction : public Action<Ts...> {
|
||||||
public:
|
public:
|
||||||
void set_data_template(std::function<std::vector<uint8_t>(Ts...)> func) {
|
explicit WriteAction(Marlin2 *marlin2) : marlin2_(marlin2) {}
|
||||||
this->data_func_ = func;
|
TEMPLATABLE_VALUE(std::string, value)
|
||||||
this->static_ = false;
|
|
||||||
}
|
|
||||||
void set_data_static(const std::vector<uint8_t> &data) {
|
|
||||||
this->data_static_ = data;
|
|
||||||
this->static_ = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void play(Ts... x) override {
|
void play(Ts... x) override {
|
||||||
if (this->static_) {
|
this->marlin2_->write(this->value_.value(x...));
|
||||||
this->parent_->write_array(this->data_static_);
|
|
||||||
} else {
|
|
||||||
auto val = this->data_func_(x...);
|
|
||||||
this->parent_->write_array(val);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool static_{false};
|
Marlin2 *marlin2_;
|
||||||
std::function<std::vector<uint8_t>(Ts...)> data_func_{};
|
};
|
||||||
std::vector<uint8_t> data_static_{};
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
template<typename... Ts> class PrintFileAction : public Action<Ts...> {
|
||||||
|
public:
|
||||||
|
explicit PrintFileAction(Marlin2 *marlin2) : marlin2_(marlin2) {}
|
||||||
|
TEMPLATABLE_VALUE(std::string, value)
|
||||||
|
|
||||||
|
void play(Ts... x) override {
|
||||||
|
std::string file_name = this->marlin2_->to_dos_name(this->value_.value(x...));
|
||||||
|
ESP_LOGD(TAG, "->FILE: %s", file_name.c_str());
|
||||||
|
this->marlin2_->write(str_sprintf("M32 P !%s#", file_name.c_str()));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
Marlin2 *marlin2_;
|
||||||
|
};
|
||||||
} // namespace esphome
|
} // namespace esphome
|
321
marlin2.cpp
321
marlin2.cpp
@@ -3,90 +3,126 @@
|
|||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
static const char *TAG = "marlin2";
|
static const char *TAG = "marlin2";
|
||||||
|
|
||||||
#ifdef USE_SENSOR
|
#ifdef USE_SENSOR
|
||||||
void Marlin2::add_sensor(const std::string& sName, sensor::Sensor *sens) {
|
void Marlin2::add_sensor(const std::string& sName, sensor::Sensor *sens) {
|
||||||
sensors.push_back({sName, sens});
|
sensors.push_back({sName, sens});
|
||||||
}
|
}
|
||||||
|
|
||||||
sensor::Sensor* Marlin2::find_sensor(std::string key) {
|
sensor::Sensor* Marlin2::find_sensor(std::string key) {
|
||||||
for (const auto& pair : sensors) {
|
for (const auto& pair : sensors) {
|
||||||
if (key == std::string(pair.first)) { // Convert char* to std::string for comparison
|
if (key == std::string(pair.first)) { // Convert char* to std::string for comparison
|
||||||
return pair.second;
|
return pair.second;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
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});
|
||||||
}
|
}
|
||||||
|
|
||||||
text_sensor::TextSensor* Marlin2::find_text_sensor(std::string key) {
|
text_sensor::TextSensor* Marlin2::find_text_sensor(std::string key) {
|
||||||
for (const auto& pair : text_sensors) {
|
for (const auto& pair : text_sensors) {
|
||||||
if (key == std::string(pair.first)) { // Convert char* to std::string for comparison
|
if (key == std::string(pair.first)) { // Convert char* to std::string for comparison
|
||||||
return pair.second;
|
return pair.second;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nullptr; // Return nullptr if no match is found
|
|
||||||
}
|
}
|
||||||
|
return nullptr; // Return nullptr if no match is found
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
void Marlin2::setup() {
|
void Marlin2::setup() {
|
||||||
MarlinOutput.reserve(256);
|
MarlinOutput.reserve(256);
|
||||||
MarlinOutput = "";
|
MarlinOutput = "";
|
||||||
|
|
||||||
|
MarlinResponseOutput.reserve(256);
|
||||||
|
MarlinResponseOutput = "";
|
||||||
|
|
||||||
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");
|
||||||
flush();
|
flush();
|
||||||
|
|
||||||
set_printer_state("IDLE");
|
set_printer_state("IDLE");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Marlin2::set_bed_setpoint() {
|
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());
|
||||||
void Marlin2::set_extruder_setpoint() {
|
flush();
|
||||||
|
}
|
||||||
}
|
|
||||||
void Marlin2::update() {
|
void Marlin2::update() {
|
||||||
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());
|
||||||
process_line();
|
process_line();
|
||||||
} else {
|
} else {
|
||||||
MarlinOutput += c;
|
MarlinOutput += c;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Marlin2::process_line() {
|
void Marlin2::process_line() {
|
||||||
if(MarlinOutput.size() < 3) {
|
if(MarlinOutput.size() < 3) {
|
||||||
MarlinOutput="";
|
MarlinOutput="";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!MarlinOutput.compare("ok") || !MarlinOutput.compare(" ok")) {
|
if(!MarlinOutput.compare("ok") || !MarlinOutput.compare(" ok")) {
|
||||||
|
listingFile = false;
|
||||||
MarlinOutput="";
|
MarlinOutput="";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!listingFile && MarlinOutput.compare("Begin file list") == 0) {
|
||||||
|
ESP_LOGD(TAG, "Listing of files started!");
|
||||||
|
listingFile = true;
|
||||||
|
//reset string for next line
|
||||||
|
MarlinOutput="";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(listingFile && MarlinOutput.compare(".GCO ")) {
|
||||||
|
int first_space = MarlinOutput.find(' ');
|
||||||
|
int second_space = MarlinOutput.find(' ', first_space + 1);
|
||||||
|
|
||||||
|
if(first_space > 0) {
|
||||||
|
std::string short_name = MarlinOutput.substr(0, first_space);
|
||||||
|
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
|
||||||
|
MarlinOutput="";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//Parse periodic Temperature read out message
|
//Parse periodic Temperature read out message
|
||||||
if(
|
if(
|
||||||
MarlinOutput.find(" T:") == 0 ||
|
MarlinOutput.find(" T:") == 0 ||
|
||||||
@@ -97,153 +133,164 @@ void Marlin2::set_extruder_setpoint() {
|
|||||||
float ext_temperature, ext_set_temperature, bed_temperature, bed_set_temperature;
|
float ext_temperature, ext_set_temperature, bed_temperature, bed_set_temperature;
|
||||||
if (process_temp_msg(&ext_temperature, &ext_set_temperature, &bed_temperature, &bed_set_temperature) != 0) {
|
if (process_temp_msg(&ext_temperature, &ext_set_temperature, &bed_temperature, &bed_set_temperature) != 0) {
|
||||||
#ifdef USE_SENSOR
|
#ifdef USE_SENSOR
|
||||||
if (find_sensor("bed_temperature") != nullptr)
|
if (find_sensor("bed_temperature") != nullptr)
|
||||||
find_sensor("bed_temperature")->publish_state(bed_temperature);
|
find_sensor("bed_temperature")->publish_state(bed_temperature);
|
||||||
|
|
||||||
if (find_sensor("bed_set_temperature") != nullptr)
|
if (find_sensor("bed_set_temperature") != nullptr)
|
||||||
find_sensor("bed_set_temperature")->publish_state(bed_set_temperature);
|
find_sensor("bed_set_temperature")->publish_state(bed_set_temperature);
|
||||||
|
|
||||||
if (find_sensor("ext_temperature") != nullptr)
|
if (find_sensor("ext_temperature") != nullptr)
|
||||||
find_sensor("ext_temperature")->publish_state(ext_temperature);
|
find_sensor("ext_temperature")->publish_state(ext_temperature);
|
||||||
|
|
||||||
if (find_sensor("ext_set_temperature") != nullptr)
|
if (find_sensor("ext_set_temperature") != nullptr)
|
||||||
find_sensor("ext_set_temperature")->publish_state(ext_set_temperature);
|
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_temperature==0.0 && ext_set_temperature==0.0) {
|
||||||
if(ext_temperature < 32.0 && bed_temperature < 32.0){ //TODO define constants for these
|
if(ext_temperature < 32.0 && bed_temperature < 32.0){ //TODO define constants for these
|
||||||
set_printer_state("IDLE");
|
set_printer_state("IDLE");
|
||||||
}
|
}
|
||||||
else if(ext_temperature < 150.0 && bed_temperature < 55.0){
|
else if(ext_temperature < 150.0 && bed_temperature < 55.0){
|
||||||
set_printer_state("COOLING");
|
set_printer_state("COOLING");
|
||||||
}
|
|
||||||
}
|
|
||||||
if(print_progress == 0.0 && (bed_set_temperature!=0.0 || ext_set_temperature!=0.0)) {
|
|
||||||
//print_time_offset = print_time save print time ofset to deduct from total value send to hass
|
|
||||||
set_printer_state("PREHEATING");
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if(print_progress == 0.0 && (bed_set_temperature!=0.0 || ext_set_temperature!=0.0)) {
|
||||||
|
//print_time_offset = print_time save print time ofset to deduct from total value send to hass
|
||||||
|
set_printer_state("PREHEATING");
|
||||||
|
}
|
||||||
#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
|
||||||
MarlinOutput="";
|
MarlinOutput="";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Parse Progress of the print
|
//Parse Progress of the print
|
||||||
if(MarlinOutput.find("SD printing byte") == 0 ) {
|
if(MarlinOutput.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)
|
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");
|
||||||
//reset string for next line
|
//reset string for next line
|
||||||
MarlinOutput="";
|
MarlinOutput="";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Parse Printitme
|
//Parse Printitme
|
||||||
if(MarlinOutput.find("echo:Print time: ") == 0) {
|
if(MarlinOutput.find("echo:Print time: ") == 0) {
|
||||||
double current=0;
|
double current=0;
|
||||||
double 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)
|
if (find_sensor("print_time") != nullptr)
|
||||||
find_sensor("print_time")->publish_state(current);
|
find_sensor("print_time")->publish_state(current);
|
||||||
|
|
||||||
if (find_sensor("print_time_remaining") != nullptr)
|
if (find_sensor("print_time_remaining") != nullptr)
|
||||||
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
|
//reset string for next line
|
||||||
MarlinOutput="";
|
MarlinOutput="";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Print From SD Card Started
|
//Print From SD Card Started
|
||||||
if(MarlinOutput.find("File selected") != std::string::npos) {
|
if(MarlinOutput.find("File selected") != std::string::npos) {
|
||||||
set_printer_state("PRINTING");
|
set_printer_state("PRINTING");
|
||||||
|
|
||||||
//reset string for next line
|
//reset string for next line
|
||||||
MarlinOutput="";
|
MarlinOutput="";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Print Finished
|
//Print Finished
|
||||||
if(MarlinOutput.find("Done printing") != std::string::npos) {
|
if(MarlinOutput.find("Done printing") != std::string::npos) {
|
||||||
print_progress = 100;
|
print_progress = 100;
|
||||||
#ifdef USE_SENSOR
|
#ifdef USE_SENSOR
|
||||||
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);
|
||||||
|
|
||||||
if (find_sensor("print_time_remaining") != nullptr)
|
if (find_sensor("print_time_remaining") != nullptr)
|
||||||
find_sensor("print_time_remaining")->publish_state(0);
|
find_sensor("print_time_remaining")->publish_state(0);
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_TEXT_SENSOR
|
#ifdef USE_TEXT_SENSOR
|
||||||
set_printer_state("FINISHED");
|
set_printer_state("FINISHED");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
//reset string for next line
|
//reset string for next line
|
||||||
MarlinOutput="";
|
MarlinOutput="";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// //Print Paused
|
// //Print Paused
|
||||||
// if(MarlinOutput.find("Printer halted") != std::string::npos) {
|
// if(MarlinOutput.find("Printer halted") != std::string::npos) {
|
||||||
// set_printer_state("PAUSED");
|
// set_printer_state("PAUSED");
|
||||||
|
|
||||||
// //reset string for next line
|
// //reset string for next line
|
||||||
// MarlinOutput="";
|
// MarlinOutput="";
|
||||||
// return;
|
// return;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// //Print Stoped
|
// //Print Stoped
|
||||||
if(MarlinOutput.find("Print Aborted") != std::string::npos) {
|
if(MarlinOutput.find("Print Aborted") != std::string::npos) {
|
||||||
set_printer_state("STOPPED");
|
set_printer_state("STOPPED");
|
||||||
|
|
||||||
//reset string for next line
|
//reset string for next line
|
||||||
MarlinOutput="";
|
MarlinOutput="";
|
||||||
return;
|
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="";
|
MarlinOutput="";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
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(MarlinOutput.find(" ") != std::string::npos)
|
while(MarlinOutput.find(" ") != std::string::npos)
|
||||||
MarlinOutput.erase(MarlinOutput.find(' '), 1);
|
MarlinOutput.erase(MarlinOutput.find(' '), 1);
|
||||||
|
|
||||||
while(MarlinOutput.find("ok") != std::string::npos)
|
while(MarlinOutput.find("ok") != std::string::npos)
|
||||||
MarlinOutput.erase(MarlinOutput.find("ok"), 2);
|
MarlinOutput.erase(MarlinOutput.find("ok"), 2);
|
||||||
|
|
||||||
if(sscanf(MarlinOutput.c_str() ,"T:%f/%fB:%f/%f", ext_temperature, ext_set_temperature, bed_temperature, bed_set_temperature) == 4 )
|
if(sscanf(MarlinOutput.c_str() ,"T:%f/%fB:%f/%f", ext_temperature, ext_set_temperature, bed_temperature, bed_set_temperature) == 4 )
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
if(sscanf(MarlinOutput.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(MarlinOutput.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(MarlinOutput.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(MarlinOutput.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(MarlinOutput.c_str() ,"T0:%f/%fT1:%f/%fB:%f/%f", ext_temperature, ext_set_temperature, &dc, &dc, bed_temperature, bed_set_temperature) == 6 )
|
if(sscanf(MarlinOutput.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;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
float Marlin2::process_progress_msg(){
|
float Marlin2::process_progress_msg(){
|
||||||
float current = std::stoi(MarlinOutput.substr(17));
|
float current = std::stoi(MarlinOutput.substr(17));
|
||||||
float total = std::stoi(MarlinOutput.substr(MarlinOutput.find('/')+1));
|
float total = std::stoi(MarlinOutput.substr(MarlinOutput.find('/')+1));
|
||||||
@@ -254,7 +301,7 @@ void Marlin2::set_extruder_setpoint() {
|
|||||||
|
|
||||||
return (((float) current * 100.0) / (float) total);
|
return (((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;
|
||||||
@@ -273,28 +320,58 @@ void Marlin2::set_extruder_setpoint() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
*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.0 && progress != 100.0) {
|
||||||
*remaining = (((100 * *current) / round(progress)) - *current);
|
*remaining = (((100 * *current) / round(progress)) - *current);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Marlin2::set_printer_state(std::string status){
|
void Marlin2::set_printer_state(std::string status){
|
||||||
#ifdef USE_TEXT_SENSOR
|
#ifdef USE_TEXT_SENSOR
|
||||||
// if (!PrinterState.compare(status))
|
// if (!PrinterState.compare(status))
|
||||||
// return;
|
// return;
|
||||||
|
|
||||||
if (find_text_sensor("printer_state") != nullptr){
|
if (find_text_sensor("printer_state") != nullptr){
|
||||||
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 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";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Truncate extension to 3 characters
|
||||||
|
if (extPart.length() > 3) {
|
||||||
|
extPart = extPart.substr(0, 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace esphome
|
} // namespace esphome
|
13
marlin2.h
13
marlin2.h
@@ -1,5 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <unordered_map>
|
||||||
#include "esphome/core/component.h"
|
#include "esphome/core/component.h"
|
||||||
#include "esphome/components/uart/uart.h"
|
#include "esphome/components/uart/uart.h"
|
||||||
#ifdef USE_SENSOR
|
#ifdef USE_SENSOR
|
||||||
@@ -15,7 +17,6 @@ class Marlin2 : public PollingComponent, public uart::UARTDevice {
|
|||||||
public:
|
public:
|
||||||
Marlin2() = default;
|
Marlin2() = default;
|
||||||
|
|
||||||
|
|
||||||
#ifdef USE_SENSOR
|
#ifdef USE_SENSOR
|
||||||
void add_sensor(const std::string& sName, sensor::Sensor *sens);
|
void add_sensor(const std::string& sName, sensor::Sensor *sens);
|
||||||
sensor::Sensor* find_sensor(std::string key);
|
sensor::Sensor* find_sensor(std::string key);
|
||||||
@@ -24,8 +25,8 @@ class Marlin2 : public PollingComponent, public uart::UARTDevice {
|
|||||||
void add_text_sensor(const std::string& sName, text_sensor::TextSensor *tSens);
|
void add_text_sensor(const std::string& sName, text_sensor::TextSensor *tSens);
|
||||||
text_sensor::TextSensor* find_text_sensor(std::string key);
|
text_sensor::TextSensor* find_text_sensor(std::string key);
|
||||||
#endif
|
#endif
|
||||||
void set_bed_setpoint();
|
void write(std::string status);
|
||||||
void set_extruder_setpoint();
|
std::string to_dos_name(std::string file_name);
|
||||||
|
|
||||||
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;
|
||||||
@@ -33,12 +34,12 @@ class Marlin2 : public PollingComponent, public uart::UARTDevice {
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::string MarlinOutput;
|
std::string MarlinOutput;
|
||||||
|
std::string MarlinResponseOutput;
|
||||||
std::string MarlinTime;
|
std::string MarlinTime;
|
||||||
std::string PrinterState;
|
std::string PrinterState;
|
||||||
|
|
||||||
float print_progress = 0;
|
float print_progress = 0;
|
||||||
double print_time_offset = 0;
|
double print_time_offset = 0;
|
||||||
|
|
||||||
|
|
||||||
#ifdef USE_SENSOR
|
#ifdef USE_SENSOR
|
||||||
std::vector<std::pair<std::string, sensor::Sensor *>> sensors;
|
std::vector<std::pair<std::string, sensor::Sensor *>> sensors;
|
||||||
@@ -52,9 +53,11 @@ class Marlin2 : public PollingComponent, public uart::UARTDevice {
|
|||||||
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 millisProgress=0;
|
unsigned long millisProgress=0;
|
||||||
|
std::unordered_map<std::string, std::string> file_table_;
|
||||||
|
bool listingFile = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace esphome
|
} // namespace esphome
|
Reference in New Issue
Block a user