From ebef6ce8864ee822429a6fc12fad19118a45b699 Mon Sep 17 00:00:00 2001 From: JonatanRek Date: Wed, 5 Mar 2025 19:03:06 +0100 Subject: [PATCH] Tweaks And Fixes --- __init__.py | 35 ++++++++++++++++++++++------------- automation.h | 16 +++++++--------- marlin2.cpp | 10 +++++----- marlin2.h | 3 +-- 4 files changed, 35 insertions(+), 29 deletions(-) diff --git a/__init__.py b/__init__.py index ae24eb9..a389cef 100644 --- a/__init__.py +++ b/__init__.py @@ -16,8 +16,10 @@ from esphome.const import ( DEVICE_CLASS_TEMPERATURE, DEVICE_CLASS_DURATION, CONF_DATA, + CONF_VALUE ) from esphome import pins, automation +from esphome.core import CORE, coroutine_with_priority CODEOWNERS = ["@jonatanrek"] DEPENDENCIES = ['uart'] @@ -43,19 +45,26 @@ def validate_raw_data(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") -@automation.register_action( - "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, - ), -) - +@coroutine_with_priority(100.0) 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) \ No newline at end of file + 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), +}) + +@automation.register_action( + "marlin2.write", + Marlin2WriteAction, + 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 \ No newline at end of file diff --git a/automation.h b/automation.h index d456348..8ceb2ee 100644 --- a/automation.h +++ b/automation.h @@ -1,22 +1,20 @@ #pragma once -#include "marlin2.h" #include "esphome/core/automation.h" - -#include +#include "esphome/core/component.h" +#include "marlin2.h" namespace esphome { -template class Marlin2WriteGCodeAction : public Action { +template class Marlin2WriteAction : public Action { public: - explicit Marlin2WriteGCodeAction(Marlin2 *marlin2) : marlin2_(marlin2) {} - TEMPLATABLE_VALUE(std::string, gcode) + explicit Marlin2WriteAction(Marlin2 *marlin2) : marlin2_(marlin2) {} + TEMPLATABLE_VALUE(std::string, value) void play(Ts... x) override { - this->marlin2_->write_str("\r\n\r\nM117 Action Called!\r\n");; + this->marlin2_->write("M117 Action Called!"); } - - + protected: Marlin2 *marlin2_; }; diff --git a/marlin2.cpp b/marlin2.cpp index a226556..d37b2fb 100644 --- a/marlin2.cpp +++ b/marlin2.cpp @@ -50,12 +50,12 @@ namespace esphome { 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(gcode.c_str()); + flush(); + } -} -void Marlin2::set_extruder_setpoint() { - -} void Marlin2::update() { while (available()) { char c = read(); diff --git a/marlin2.h b/marlin2.h index 1ac55b7..770d29a 100644 --- a/marlin2.h +++ b/marlin2.h @@ -24,8 +24,7 @@ class Marlin2 : public PollingComponent, public uart::UARTDevice { void add_text_sensor(const std::string& sName, text_sensor::TextSensor *tSens); text_sensor::TextSensor* find_text_sensor(std::string key); #endif - void set_bed_setpoint(); - void set_extruder_setpoint(); + void write(std::string status); float get_setup_priority() const override { return setup_priority::LATE; } void setup() override;