Small Improvements

This commit is contained in:
Václav Španinger 2024-12-27 21:06:46 +01:00
parent e8cd4a8f66
commit 3c229f2cb8
4 changed files with 116 additions and 33 deletions

View File

@ -7,14 +7,21 @@ Configure a list of sensors. The index is required, the rest is the standard se
Example:
```yaml
sensor:
- platform: serial_csv
uart_id: my_uart # optional
- platform: marlin2
uart_id: uart_bus #optional
bed_temperature:
name: Current Bed Temp
bed_set_temperature:
name: Set Bed Temp
ext_temperature:
name: Current Ext Temp
ext_set_temperature:
name: Set Ext Temp
print_progress:
name: Progress
```
https://github.com/ssieb/esphome_components/tree/master/components/serial_csv
https://github.com/esphome/esphome/blob/dev/esphome/components/dht/sensor.py#L34
* https://github.com/ssieb/esphome_components/tree/master/components/serial_csv
* https://github.com/esphome/esphome/blob/dev/esphome/components/dht/sensor.py#L34
* https://github.com/mulcmu/esphome-marlin-uart

View File

@ -2,13 +2,27 @@
#include "esphome/core/log.h"
namespace esphome {
static const char *TAG = "marlin2";
static const char *TAG = "marlin2";
void Marlin2::add_sensor(const std::string& sName, sensor::Sensor *sens) {
sensors.push_back({sName, sens});
}
sensor::Sensor* Marlin2::find_sensor(std::string key) {
for (const auto& pair : sensors) {
if (key == std::string(pair.first)) { // Convert char* to std::string for comparison
return pair.second;
}
}
return nullptr; // Return nullptr if no match is found
}
void Marlin2::setup() {
MarlinOutput.reserve(256);
MarlinOutput = "";
MarlinTime.reserve(32);
ESP_LOGD(TAG, "M155 S10");
write_str("\r\n\r\nM155 S10\r\n");
@ -54,12 +68,21 @@ static const char *TAG = "marlin2";
MarlinOutput.find("ok T:") == 0 ||
MarlinOutput.find(" ok T:") == 0
) {
float ext_temperature, ext_set_temperature, bed_temperature, bed_set_tempertaure;
if (process_temp_msg(&ext_temperature, &ext_set_temperature, &bed_temperature, &bed_set_tempertaure) != 0) {
//bed_temperature_sensor->publish_state(bed_temperature);
// bed_temperature->publish_state(bed_set_tempertaure);
//ext_temperature_sensor->publish_state(ext_temperature);
// ext_temperature->publish_state(ext_set_temperature);
float ext_temperature, ext_set_temperature, bed_temperature, bed_set_temperature;
if (process_temp_msg(&ext_temperature, &ext_set_temperature, &bed_temperature, &bed_set_temperature) != 0) {
if (find_sensor("bed_temperature") != nullptr)
find_sensor("bed_temperature")->publish_state(bed_temperature);
if (find_sensor("bed_set_temperature") != nullptr)
find_sensor("bed_set_temperature")->publish_state(bed_set_temperature);
if (find_sensor("ext_temperature") != nullptr)
find_sensor("ext_temperature")->publish_state(ext_temperature);
if (find_sensor("ext_set_temperature") != nullptr)
find_sensor("ext_set_temperature")->publish_state(ext_set_temperature);
ESP_LOGD(TAG, "Bed Temperature=%.1f°C Ext Temperature=%.1f°C ", bed_temperature, ext_temperature);
}
@ -70,9 +93,12 @@ static const char *TAG = "marlin2";
//Parse Progress of the print
if(MarlinOutput.find("SD printing byte") == 0 ) {
float progress = process_progress_msg();
//print_progress_sensor->publish_state(progress);
ESP_LOGD(TAG, "progress=%.1f", progress);
float print_progress = process_progress_msg();
if (find_sensor("print_progress") != nullptr)
find_sensor("print_progress")->publish_state(print_progress);
ESP_LOGD(TAG, "progress=%.1f", print_progress);
//reset string for next line
MarlinOutput="";
@ -85,6 +111,13 @@ static const char *TAG = "marlin2";
unsigned long current=0, remaining=0;
if (process_print_time_msg(&d, &h, &m, &current, &remaining) != 0) {
if (find_sensor("print_time") != nullptr)
find_sensor("print_time")->publish_state(print_time)
if (find_sensor("print_time_remaining") != nullptr)
find_sensor("print_time_remaining")->publish_state(print_time_remaining)
ESP_LOGD(TAG, "time=%.1f remaining=%.1f", current, remaining);
}
//reset string for next line
@ -133,6 +166,27 @@ static const char *TAG = "marlin2";
}
int Marlin2::process_print_time_msg(int* d, int* h, int* m, unsigned long* current, unsigned long* remaining){
MarlinTime = MarlinOutput.substr(16);
ESP_LOGD(TAG,MarlinTime.c_str());
if (sscanf(MarlinTime.c_str() ,"%dd %dh %dm %ds", &d, &h, &m, &s)!=4) {
d=0;
if (sscanf(MarlinTime.c_str() ,"%dh %dm %ds", &h, &m, &s)!=3) {
d=0; h=0;
if (sscanf(MarlinTime.c_str() ,"%dm %ds", &m, &s)!=2) {
d=0; h=0; m=0;
if (sscanf(MarlinTime.c_str() ,"%ds", &s)!=1) {
MarlinOutput="";
return;
}
}
}
}
current = d*24*60*60 + h*60*60 + m*60 + s;
return 0;
}

View File

@ -11,18 +11,17 @@ class Marlin2 : public PollingComponent , public uart::UARTDevice {
void setup() override;
float get_setup_priority() const override { return setup_priority::LATE; }
void update() override;
void add_sensor(const std::string& sName, sensor::Sensor *sens);
sensor::Sensor* find_sensor(std::string key);
protected:
void process_line();
int process_temp_msg(float* ext_temperature, float* ext_set_temperature, float* bed_temperature, float* bed_set_temperature);
float process_progress_msg();
int process_print_time_msg(int* d, int* h, int* m, unsigned long* current, unsigned long* remaining)
int process_print_time_msg(int* d, int* h, int* m, unsigned long* current, unsigned long* remaining);
std::string MarlinOutput;
sensor::Sensor *bed_temperature_sensor;
sensor::Sensor *ext_temperature_sensor;
sensor::Sensor *print_progress_sensor;
std::vector<std::pair<std::string, sensor::Sensor *>> sensors;
private:
unsigned long millisProgress=0;

View File

@ -22,9 +22,14 @@ CODEOWNERS = ["@jonatanrek"]
DEPENDENCIES = ['uart']
CONF_BED_TEMPERATURE = "bed_temperature"
CONF_EXT_TEMPERATURE = "ext_temperature"
CONF_PROGRESS = "progress"
CONF_BED_SET_TEMPERATURE = "bed_set_temperature"
CONF_EXT_TEMPERATURE = "ext_temperature"
CONF_EXT_SET_TEMPERATURE = "ext_set_temperature"
CONF_PRINT_PROGRESS = "print_progress"
CONF_PRINT_TIME = "print_time"
CONF_PRINT_TIME_REMAINING = "print_time_remaining"
Marlin2 = cg.esphome_ns.class_('Marlin2', cg.Component, sensor.Sensor, uart.UARTDevice)
@ -37,31 +42,49 @@ CONFIG_SCHEMA = uart.UART_DEVICE_SCHEMA.extend(
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_BED_SET_TEMPERATURE): sensor.sensor_schema(
unit_of_measurement=UNIT_CELSIUS,
accuracy_decimals=1,
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_EXT_TEMPERATURE): sensor.sensor_schema(
unit_of_measurement=UNIT_CELSIUS,
accuracy_decimals=1,
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_PROGRESS): sensor.sensor_schema(
cv.Optional(CONF_EXT_SET_TEMPERATURE): sensor.sensor_schema(
unit_of_measurement=UNIT_CELSIUS,
accuracy_decimals=1,
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_PRINT_PROGRESS): sensor.sensor_schema(
unit_of_measurement=UNIT_PERCENT,
accuracy_decimals=1,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_PRINT_TIME): sensor.sensor_schema(
unit_of_measurement=UNIT_PERCENT,
accuracy_decimals=1,
device_class=DEVICE_CLASS_DURATION,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_PRINT_TIME_REMAINING): sensor.sensor_schema(
unit_of_measurement=UNIT_PERCENT,
accuracy_decimals=1,
device_class=DEVICE_CLASS_DURATION,
state_class=STATE_CLASS_MEASUREMENT,
),
}
)
).extend(cv.polling_component_schema("15s"))
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)
if CONF_BED_TEMPERATURE in config:
await sensor.new_sensor(config[CONF_BED_TEMPERATURE])
if CONF_EXT_TEMPERATURE in config:
await sensor.new_sensor(config[CONF_EXT_TEMPERATURE])
if CONF_PROGRESS in config:
await sensor.new_sensor(config[CONF_PROGRESS])
for sName in [CONF_BED_TEMPERATURE, CONF_BED_SET_TEMPERATURE, CONF_EXT_TEMPERATURE, CONF_EXT_SET_TEMPERATURE, CONF_PRINT_PROGRESS, CONF_PRINT_TIME, CONF_PRINT_TIME_REMAINING]:
if sName in config:
sens = await sensor.new_sensor(config[sName])
cg.add(var.add_sensor(sName,sens))