Progress
This commit is contained in:
parent
1d31a8b63f
commit
9d3a97e53b
50
marlin2.cpp
50
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<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() {
|
||||
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
|
27
marlin2.h
27
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<uint8_t> rx_message_;
|
||||
std::vector<std::pair<int, sensor::Sensor *>> sensors_;
|
||||
};
|
||||
|
||||
} // namespace serial
|
||||
} // namespace esphome
|
22
sensor.py
22
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))
|
||||
|
||||
if CONF_BED_TEMPERATURE in config:
|
||||
sens = await sensor.new_sensor(config[CONF_BED_TEMPERATURE])
|
||||
cg.add(var.set_temperature_sensor(sens))
|
Loading…
Reference in New Issue
Block a user