This commit is contained in:
Václav Španinger 2024-12-30 10:39:19 +01:00
parent 5112a91bec
commit 64183c33d2
5 changed files with 68 additions and 57 deletions

View File

@ -19,11 +19,12 @@ from esphome.const import (
CODEOWNERS = ["@jonatanrek"]
DEPENDENCIES = ['uart']
CONF_MARLIN2_ID = "marlin2_id"
Marlin2 = cg.esphome_ns.class_('Marlin2', cg.Component)
CONFIG_SCHEMA = cv.All(
cv.Schema( {
cv.Schema({
cv.GenerateID(): cv.declare_id(Marlin2),
})
.extend(cv.COMPONENT_SCHEMA)

View File

@ -4,18 +4,35 @@
namespace esphome {
static const char *TAG = "marlin2";
void Marlin2::add_sensor(const std::string& sName, sensor::Sensor *sens) {
sensors.push_back({sName, sens});
}
sensor::Sensor* Marlin2::find_sensor(std::string key) {
for (const auto& pair : sensors) {
if (key == std::string(pair.first)) { // Convert char* to std::string for comparison
return pair.second;
}
#ifdef USE_SENSOR
void Marlin2::add_sensor(const std::string& sName, sensor::Sensor *sens) {
sensors.push_back({sName, sens});
}
return nullptr; // Return nullptr if no match is found
}
sensor::Sensor* Marlin2::find_sensor(std::string key) {
for (const auto& pair : sensors) {
if (key == std::string(pair.first)) { // Convert char* to std::string for comparison
return pair.second;
}
}
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});
}
text_sensor::TextSensor* Marlin2::find_text_sensor(std::string key) {
for (const auto& pair : text_sensors) {
if (key == std::string(pair.first)) { // Convert char* to std::string for comparison
return pair.second;
}
}
return nullptr; // Return nullptr if no match is found
}
#endif
void Marlin2::setup() {
MarlinOutput.reserve(256);
@ -26,14 +43,14 @@ namespace esphome {
ESP_LOGD(TAG, "M155 S10");
this->uart_device_->write_str("\r\n\r\nM155 S10\r\n");
this->uart_device_->write_str("\r\n\r\nM117 Hello World!\r\n");
this->uart_device_->flush();
write_str("\r\n\r\nM155 S10\r\n");
write_str("\r\n\r\nM117 Hello World!\r\n");
flush();
}
void Marlin2::update() {
while (this->uart_device_->available()) {
char c = this->uart_device_->read();
while (available()) {
char c = read();
if( c == '\n' || c == '\r' ) {
process_line();
} else {
@ -47,7 +64,7 @@ namespace esphome {
ESP_LOGD(TAG, "M27");
ESP_LOGD(TAG, "M31");
this->uart_device_->write_str("M27\r\nM31\r\n");
write_str("M27\r\nM31\r\n");
}
}
@ -86,16 +103,18 @@ namespace esphome {
#endif
#ifdef USE_TEXT_SENSOR
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 (find_text_sensor("printer_status") != nullptr)
//find_text_sensor("printer_status")->publish_state("IDLE");
else if(ext_temperature < 150.0 && bed_temperature < 55.0)
//if (find_text_sensor("printer_status") != nullptr)
//find_text_sensor("printer_status")->publish_state("COOLING");
if(ext_temperature < 32.0 && bed_temperature < 32.0){ //TODO define constants for these
if (find_text_sensor("printer_status") != nullptr)
find_text_sensor("printer_status")->publish_state("IDLE");
}
else if(ext_temperature < 150.0 && bed_temperature < 55.0){
if (find_text_sensor("printer_status") != nullptr)
find_text_sensor("printer_status")->publish_state("COOLING");
}
}
if(bed_set_temperature!=0.0 || ext_set_temperature!=0.0) {
//if (find_text_sensor("printer_status") != nullptr)
//find_text_sensor("printer_status")->publish_state("PREHEATING");
if (find_text_sensor("printer_status") != nullptr)
find_text_sensor("printer_status")->publish_state("PREHEATING");
}
#endif
@ -144,9 +163,11 @@ namespace esphome {
}
//Print Finished
if(MarlinOutput.find("Done printing") != std::string::npos) {
// //if (find_text_sensor("printer_status") != nullptr)
// //find_text_sensor("printer_status")->publish_state("FINISHED");
if(MarlinOutput.find("Done printing") != std::string::npos) {
#ifdef USE_TEXT_SENSOR
if (find_text_sensor("printer_status") != nullptr)
find_text_sensor("printer_status")->publish_state("FINISHED");
#endif
ESP_LOGD(TAG, "Print Finished");
print_progress = 100;

View File

@ -11,11 +11,10 @@
namespace esphome {
class Marlin2 : public esphome::Component, public uart::UARTDevice {
class Marlin2 : public PollingComponent, public uart::UARTDevice {
public:
Marlin2() = default;
explicit Marlin2(uart::UARTDevice *uart_device) : uart_device_{uart_device} {}
void set_uart_parent(uart::UARTDevice *parent) { this->uart_device_ = parent; }
#ifdef USE_SENSOR
void add_sensor(const std::string& sName, sensor::Sensor *sens);
@ -31,8 +30,6 @@ class Marlin2 : public esphome::Component, public uart::UARTDevice {
void update() override;
protected:
esphome::uart::UARTDevice *uart_device_{nullptr};
std::string MarlinOutput;
std::string MarlinTime;
std::string PrinterState;

View File

@ -19,6 +19,7 @@ from esphome.const import (
DEVICE_CLASS_TEMPERATURE,
DEVICE_CLASS_DURATION,
)
from . import Marlin2
CONF_BED_TEMPERATURE = "bed_temperature"
CONF_BED_SET_TEMPERATURE = "bed_set_temperature"
@ -30,13 +31,10 @@ CONF_PRINT_PROGRESS = "print_progress"
CONF_PRINT_TIME = "print_time"
CONF_PRINT_TIME_REMAINING = "print_time_remaining"
# CONF_PRINTER_STATUS = "printer_status"
Marlin2 = cg.esphome_ns.class_('Marlin2', cg.Component, sensor.Sensor, uart.UARTDevice)
CONFIG_SCHEMA = uart.UART_DEVICE_SCHEMA.extend(
CONF_MARLIN = "marlin2"
CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.declare_id(Marlin2),
cv.GenerateID(CONF_MARLIN): cv.use_id(Marlin2),
cv.Optional(CONF_BED_TEMPERATURE): sensor.sensor_schema(
unit_of_measurement=UNIT_CELSIUS,
accuracy_decimals=1,
@ -78,20 +76,13 @@ CONFIG_SCHEMA = uart.UART_DEVICE_SCHEMA.extend(
device_class=DEVICE_CLASS_DURATION,
state_class=STATE_CLASS_MEASUREMENT,
),
# cv.Optional(CONF_PRINTER_STATUS): text_sensor.text_sensor_schema(),
}
)
).extend(cv.polling_component_schema("15s"))
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await uart.register_uart_device(var, config)
server = await cg.get_variable(config[CONF_MARLIN])
for sName in [CONF_BED_TEMPERATURE, CONF_BED_SET_TEMPERATURE, CONF_EXT_TEMPERATURE, CONF_EXT_SET_TEMPERATURE, CONF_PRINT_PROGRESS, CONF_PRINT_TIME, CONF_PRINT_TIME_REMAINING]:
if sName in config:
sens = await sensor.new_sensor(config[sName])
cg.add(var.add_sensor(sName,sens))
# for sName in [CONF_PRINTER_STATUS]:
# if sName in config:
# sens = await sensor.new_text_sensor(config[sName])
# cg.add(var.add_text_sensor(sName,sens))
cg.add(server.add_sensor(sName,sens))

View File

@ -4,21 +4,22 @@ from esphome.components import text_sensor
from esphome.const import (
ENTITY_CATEGORY_DIAGNOSTIC,
)
from . import ns, Marlin2
from . import Marlin2
CONF_MARLIN = "marlin2"
CONFIG_SCHEMA = cv.Schema(
CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(CONF_MARLIN): cv.use_id(Marlin2),
cv.Optional(CONF_CONNECTED): text_sensor.text_sensor(
cv.Optional("printer_state"): 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"]:
if sName in config:
sens = await sensor.new_text_sensor(config[sName])
cg.add(var.add_text_sensor(sName,sens))
sens = await text_sensor.new_text_sensor(config[sName])
cg.add(server.add_text_sensor(sName,sens))