This commit is contained in:
Václav Španinger 2024-12-27 12:02:11 +01:00
parent 9d3a97e53b
commit b2f68dcb7b
3 changed files with 47 additions and 48 deletions

View File

@ -7,44 +7,32 @@ namespace serial {
static const char *TAG = "serial.marlin2"; static const char *TAG = "serial.marlin2";
void Marlin2::loop() { void Marlin2::loop() {
while (this->available()) { // while (this->available()) {
uint8_t c; // uint8_t c;
this->read_byte(&c); // this->read_byte(&c);
if (c == '\r')
continue; // if (c == '\r')
if (c == '\n') // continue;
this->parse_values_();
else // if (c == '\n')
this->rx_message_.push_back(c); // this->parse_values_();
} // else
// this->rx_message_.push_back(c);
// }
} }
void Marlin2::parse_values_() { // void Marlin2::parse_values_() {
std::string s(this->rx_message_.begin(), this->rx_message_.end()); // std::string s(this->rx_message_.begin(), this->rx_message_.end());
int spos = 0; // ESP_LOGV(TAG, s);
int epos = 0; // }
std::vector<float> 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<float>(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() { // void Marlin2::dump_config() {
ESP_LOGCONFIG("", "Serial CSV Reader"); // ESP_LOGCONFIG("", "Serial CSV Reader");
for (auto sens : this->sensors_) { // for (auto sens : this->sensors_) {
ESP_LOGCONFIG(TAG, "Index %d", sens.first); // ESP_LOGCONFIG(TAG, "Index %d", sens.first);
LOG_SENSOR(TAG, "", sens.second); // LOG_SENSOR(TAG, "", sens.second);
} // }
} // }
} // namespace serial } // namespace serial
} // namespace esphome } // namespace esphome

View File

@ -9,18 +9,18 @@ namespace serial {
class Marlin2 : public Component, public uart::UARTDevice { class Marlin2 : public Component, public uart::UARTDevice {
public: 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 loop() override;
void dump_config() override; // void dump_config() override;
void add_sensor(int index, sensor::Sensor *sens) { // void add_sensor(int index, sensor::Sensor *sens) {
this->sensors_.push_back(std::make_pair(index, sens)); // this->sensors_.push_back(std::make_pair(index, sens));
} // }
protected: // protected:
void parse_values_(); // void parse_values_();
std::vector<uint8_t> rx_message_; // std::vector<uint8_t> rx_message_;
std::vector<std::pair<int, sensor::Sensor *>> sensors_; // std::vector<std::pair<int, sensor::Sensor *>> sensors_;
}; };
} // namespace serial } // namespace serial

View File

@ -3,7 +3,19 @@ import esphome.config_validation as cv
from esphome import automation from esphome import automation
from esphome.components import uart from esphome.components import uart
from esphome.components import sensor 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"] CODEOWNERS = ["@jonatanrek"]
@ -11,9 +23,8 @@ DEPENDENCIES = ['uart']
CONF_BED_TEMPERATURE = "bed_temperature" CONF_BED_TEMPERATURE = "bed_temperature"
serial_ns = cg.esphome_ns.namespace('serial') marlin_ns = cg.esphome_ns.namespace('marlin2')
Marlin2 = marlin_ns.class_('Marlin2', cg.Component, sensor.Sensor, uart.UARTDevice)
Marlin2 = serial_ns.class_('Marlin2', cg.Component, sensor.Sensor, uart.UARTDevice)
CONFIG_SCHEMA = uart.UART_DEVICE_SCHEMA.extend( CONFIG_SCHEMA = uart.UART_DEVICE_SCHEMA.extend(
{ {