From b2f68dcb7b0836c6864d74e764d3868ffeb07dfe Mon Sep 17 00:00:00 2001 From: JonatanRek Date: Fri, 27 Dec 2024 12:02:11 +0100 Subject: [PATCH] Tests --- marlin2.cpp | 58 +++++++++++++++++++++-------------------------------- marlin2.h | 18 ++++++++--------- sensor.py | 19 ++++++++++++++---- 3 files changed, 47 insertions(+), 48 deletions(-) diff --git a/marlin2.cpp b/marlin2.cpp index 3f5ba9f..1753cde 100644 --- a/marlin2.cpp +++ b/marlin2.cpp @@ -7,44 +7,32 @@ 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); - } + // 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::parse_values_() { +// std::string s(this->rx_message_.begin(), this->rx_message_.end()); +// ESP_LOGV(TAG, s); +// } -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); - } -} +// 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 b607ddc..548e84f 100644 --- a/marlin2.h +++ b/marlin2.h @@ -9,18 +9,18 @@ namespace serial { class Marlin2 : public Component, public uart::UARTDevice { public: - float get_setup_priority() const override { return setup_priority::DATA; } + // float get_setup_priority() const override { return setup_priority::DATA; } void loop() override; - void dump_config() override; + // void dump_config() override; - void add_sensor(int index, sensor::Sensor *sens) { - this->sensors_.push_back(std::make_pair(index, sens)); - } + // 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_; + // protected: + // void parse_values_(); + // std::vector rx_message_; + // std::vector> sensors_; }; } // namespace serial diff --git a/sensor.py b/sensor.py index c25a496..0174d9a 100644 --- a/sensor.py +++ b/sensor.py @@ -3,7 +3,19 @@ import esphome.config_validation as cv from esphome import automation from esphome.components import uart from esphome.components import sensor -from esphome.const import CONF_ID, CONF_INDEX, CONF_SENSORS, CONF_HUMIDITY, CONF_MODEL, CONF_PIN, CONF_TEMPERATURE, STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, UNIT_PERCENT, DEVICE_CLASS_TEMPERATURE +from esphome.const import ( + CONF_ID, + CONF_INDEX, + CONF_SENSORS, + CONF_HUMIDITY, + CONF_MODEL, + CONF_PIN, + CONF_TEMPERATURE, + STATE_CLASS_MEASUREMENT, + UNIT_CELSIUS, + UNIT_PERCENT, + DEVICE_CLASS_TEMPERATURE +) CODEOWNERS = ["@jonatanrek"] @@ -11,9 +23,8 @@ 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) +marlin_ns = cg.esphome_ns.namespace('marlin2') +Marlin2 = marlin_ns.class_('Marlin2', cg.Component, sensor.Sensor, uart.UARTDevice) CONFIG_SCHEMA = uart.UART_DEVICE_SCHEMA.extend( {