diff --git a/marlin2.cpp b/marlin2.cpp index e69de29..3f5ba9f 100644 --- a/marlin2.cpp +++ b/marlin2.cpp @@ -0,0 +1,50 @@ +#include "marlin2.h" +#include "esphome/core/log.h" + +namespace esphome { +namespace serial { + +static const char *TAG = "serial.marlin2"; + +void Marlin2::loop() { + while (this->available()) { + uint8_t c; + this->read_byte(&c); + if (c == '\r') + continue; + if (c == '\n') + this->parse_values_(); + else + this->rx_message_.push_back(c); + } +} + +void Marlin2::parse_values_() { + std::string s(this->rx_message_.begin(), this->rx_message_.end()); + int spos = 0; + int epos = 0; + std::vector values; + while (epos != std::string::npos) { + epos = s.find(',', spos); + int len = (epos == std::string::npos ? s.size() - spos : epos - spos); + values.push_back(parse_number(s.substr(spos, len)).value_or(NAN)); + if (epos != std::string::npos) + spos = epos + 1; + } + this->rx_message_.clear(); + for (auto sens : this->sensors_) { + if (sens.first < values.size()) + sens.second->publish_state(values[sens.first]); + } +} + +void Marlin2::dump_config() { + ESP_LOGCONFIG("", "Serial CSV Reader"); + for (auto sens : this->sensors_) { + ESP_LOGCONFIG(TAG, "Index %d", sens.first); + LOG_SENSOR(TAG, "", sens.second); + } +} + +} // namespace serial +} // namespace esphome \ No newline at end of file diff --git a/marlin2.h b/marlin2.h index e69de29..b607ddc 100644 --- a/marlin2.h +++ b/marlin2.h @@ -0,0 +1,27 @@ +#pragma once + +#include "esphome/core/component.h" +#include "esphome/components/sensor/sensor.h" +#include "esphome/components/uart/uart.h" + +namespace esphome { +namespace serial { + +class Marlin2 : public Component, public uart::UARTDevice { + public: + float get_setup_priority() const override { return setup_priority::DATA; } + void loop() override; + void dump_config() override; + + void add_sensor(int index, sensor::Sensor *sens) { + this->sensors_.push_back(std::make_pair(index, sens)); + } + + protected: + void parse_values_(); + std::vector rx_message_; + std::vector> sensors_; +}; + +} // namespace serial +} // namespace esphome \ No newline at end of file diff --git a/sensor.py b/sensor.py index ca940d0..c25a496 100644 --- a/sensor.py +++ b/sensor.py @@ -9,6 +9,8 @@ CODEOWNERS = ["@jonatanrek"] DEPENDENCIES = ['uart'] +CONF_BED_TEMPERATURE = "bed_temperature" + serial_ns = cg.esphome_ns.namespace('serial') Marlin2 = serial_ns.class_('Marlin2', cg.Component, sensor.Sensor, uart.UARTDevice) @@ -16,13 +18,11 @@ Marlin2 = serial_ns.class_('Marlin2', cg.Component, sensor.Sensor, uart.UARTDevi CONFIG_SCHEMA = uart.UART_DEVICE_SCHEMA.extend( { cv.GenerateID(): cv.declare_id(Marlin2), - cv.Required(CONF_SENSORS): cv.ensure_list( - cv.Optional("bed_current"): sensor.sensor_schema( - unit_of_measurement=UNIT_CELSIUS, - accuracy_decimals=1, - device_class=DEVICE_CLASS_TEMPERATURE, - state_class=STATE_CLASS_MEASUREMENT, - ), + cv.Optional(CONF_BED_TEMPERATURE): sensor.sensor_schema( + unit_of_measurement=UNIT_CELSIUS, + accuracy_decimals=1, + device_class=DEVICE_CLASS_TEMPERATURE, + state_class=STATE_CLASS_MEASUREMENT, ), } ) @@ -31,7 +31,7 @@ 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) - for conf in config[CONF_SENSORS]: - sens = await sensor.new_sensor(conf) - index = conf[CONF_INDEX] - cg.add(var.add_sensor(index, sens)) \ No newline at end of file + + if CONF_BED_TEMPERATURE in config: + sens = await sensor.new_sensor(config[CONF_BED_TEMPERATURE]) + cg.add(var.set_temperature_sensor(sens)) \ No newline at end of file