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";
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<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::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

View File

@ -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<uint8_t> rx_message_;
std::vector<std::pair<int, sensor::Sensor *>> sensors_;
// protected:
// void parse_values_();
// std::vector<uint8_t> rx_message_;
// std::vector<std::pair<int, sensor::Sensor *>> sensors_;
};
} // namespace serial

View File

@ -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(
{