Additional Fixes Steady Read Stream of Utar Data
This commit is contained in:
parent
dd7540b8bb
commit
e8cd4a8f66
@ -9,11 +9,10 @@ Example:
|
||||
sensor:
|
||||
- platform: serial_csv
|
||||
uart_id: my_uart # optional
|
||||
sensors:
|
||||
- index: 0
|
||||
name: First value
|
||||
- index: 3
|
||||
name: Fourth value
|
||||
bed_temperature:
|
||||
name: Current Bed Temp
|
||||
ext_temperature:
|
||||
name: Current Ext Temp
|
||||
```
|
||||
|
||||
|
||||
|
148
marlin2.cpp
148
marlin2.cpp
@ -5,33 +5,135 @@ namespace esphome {
|
||||
|
||||
static const char *TAG = "marlin2";
|
||||
|
||||
void Marlin2::setup() {
|
||||
this->write_str("\r\n\r\nM155 S10\r\n");
|
||||
ESP_LOGV(TAG, "M155 S10");
|
||||
}
|
||||
void Marlin2::setup() {
|
||||
MarlinOutput.reserve(256);
|
||||
MarlinOutput = "";
|
||||
|
||||
void Marlin2::loop() {
|
||||
while (this->available()) {
|
||||
uint8_t c;
|
||||
this->read_byte(&c);
|
||||
ESP_LOGD(TAG, "M155 S10");
|
||||
|
||||
if (c == '\r')
|
||||
continue;
|
||||
|
||||
if (c == '\n')
|
||||
this->parse_values_();
|
||||
else
|
||||
this->rx_message_.push_back(c);
|
||||
write_str("\r\n\r\nM155 S10\r\n");
|
||||
write_str("\r\n\r\nM117 Hello World!\r\n");
|
||||
flush();
|
||||
}
|
||||
}
|
||||
|
||||
void Marlin2::parse_values_() {
|
||||
std::string s(this->rx_message_.begin(), this->rx_message_.end());
|
||||
ESP_LOGV(TAG, s);
|
||||
}
|
||||
void Marlin2::update() {
|
||||
while (available()) {
|
||||
char c = read();
|
||||
if( c == '\n' || c == '\r' ) {
|
||||
process_line();
|
||||
} else {
|
||||
MarlinOutput += c;
|
||||
}
|
||||
}
|
||||
|
||||
void Marlin2::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "Serial CSV Reader");
|
||||
}
|
||||
if(millis() - millisProgress > 15000 ) {
|
||||
millisProgress = millis();
|
||||
|
||||
ESP_LOGD(TAG, "M27");
|
||||
ESP_LOGD(TAG, "M31");
|
||||
|
||||
write_str("M27\r\nM31\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
void Marlin2::process_line() {
|
||||
if(MarlinOutput.size() < 3) {
|
||||
MarlinOutput="";
|
||||
return;
|
||||
}
|
||||
|
||||
if(!MarlinOutput.compare("ok") || !MarlinOutput.compare(" ok")) {
|
||||
MarlinOutput="";
|
||||
return;
|
||||
}
|
||||
|
||||
//Parse periodic Temperature read out message
|
||||
if(
|
||||
MarlinOutput.find(" T:") == 0 ||
|
||||
MarlinOutput.find("T:") == 0 ||
|
||||
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);
|
||||
ESP_LOGD(TAG, "Bed Temperature=%.1f°C Ext Temperature=%.1f°C ", bed_temperature, ext_temperature);
|
||||
}
|
||||
|
||||
//reset string for next line
|
||||
MarlinOutput="";
|
||||
return;
|
||||
}
|
||||
|
||||
//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);
|
||||
|
||||
//reset string for next line
|
||||
MarlinOutput="";
|
||||
return;
|
||||
}
|
||||
|
||||
//Parse Printitme
|
||||
if(MarlinOutput.find("echo:Print time: ") == 0) {
|
||||
int d=0, h=0, m=0, s=0;
|
||||
unsigned long current=0, remaining=0;
|
||||
|
||||
if (process_print_time_msg(&d, &h, &m, ¤t, &remaining) != 0) {
|
||||
}
|
||||
|
||||
//reset string for next line
|
||||
MarlinOutput="";
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGD(TAG, "#%s#",MarlinOutput.c_str());
|
||||
MarlinOutput="";
|
||||
return;
|
||||
}
|
||||
|
||||
int Marlin2::process_temp_msg(float* ext_temperature, float* ext_set_temperature, float* bed_temperature, float* bed_set_temperature) {
|
||||
float dc;
|
||||
|
||||
while(MarlinOutput.find(' ') != std::string::npos)
|
||||
MarlinOutput.erase(MarlinOutput.find(' '), 1);
|
||||
|
||||
while(MarlinOutput.find("ok") != std::string::npos)
|
||||
MarlinOutput.erase(MarlinOutput.find("ok"), 2);
|
||||
|
||||
if(sscanf(MarlinOutput.c_str() ,"T:%f/%fB:%f/%f", ext_temperature, ext_set_temperature, bed_temperature, bed_set_temperature) == 4 )
|
||||
return 1;
|
||||
|
||||
if(sscanf(MarlinOutput.c_str() ,"T:%f/%f(%f)B:%f/%f(%f)", ext_temperature, ext_set_temperature, &dc, bed_temperature, bed_set_temperature, &dc) == 6 )
|
||||
return 2;
|
||||
|
||||
if(sscanf(MarlinOutput.c_str() ,"T:%f/%fT0:%f/%fT1:%f/%fB:%f/%f", ext_temperature, ext_set_temperature, &dc, &dc, &dc, &dc, bed_temperature, bed_set_temperature) == 8 )
|
||||
return 3;
|
||||
|
||||
if(sscanf(MarlinOutput.c_str() ,"T0:%f/%fT1:%f/%fB:%f/%f", ext_temperature, ext_set_temperature, &dc, &dc, bed_temperature, bed_set_temperature) == 6 )
|
||||
return 4;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
float Marlin2::process_progress_msg(){
|
||||
float current = std::stoi(MarlinOutput.substr(17));
|
||||
float total = std::stoi(MarlinOutput.substr(MarlinOutput.find('/')+1));
|
||||
|
||||
if (total==0) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
return ((float) current / (float) total) * 100.0;
|
||||
}
|
||||
|
||||
int Marlin2::process_print_time_msg(int* d, int* h, int* m, unsigned long* current, unsigned long* remaining){
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace esphome
|
27
marlin2.h
27
marlin2.h
@ -6,21 +6,26 @@
|
||||
|
||||
namespace esphome {
|
||||
|
||||
class Marlin2 : public Component, public uart::UARTDevice {
|
||||
class Marlin2 : public PollingComponent , public uart::UARTDevice {
|
||||
public:
|
||||
void setup() override;
|
||||
float get_setup_priority() const override { return setup_priority::LATE; }
|
||||
void loop() override;
|
||||
void dump_config() override;
|
||||
|
||||
void add_sensor(int index, sensor::Sensor *sens) {
|
||||
this->sensors_.push_back(std::make_pair(index, sens));
|
||||
}
|
||||
|
||||
void update() override;
|
||||
|
||||
protected:
|
||||
void parse_values_();
|
||||
std::vector<uint8_t> rx_message_;
|
||||
std::vector<std::pair<int, sensor::Sensor *>> sensors_;
|
||||
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)
|
||||
std::string MarlinOutput;
|
||||
|
||||
sensor::Sensor *bed_temperature_sensor;
|
||||
sensor::Sensor *ext_temperature_sensor;
|
||||
sensor::Sensor *print_progress_sensor;
|
||||
|
||||
|
||||
private:
|
||||
unsigned long millisProgress=0;
|
||||
};
|
||||
|
||||
} // namespace esphome
|
21
sensor.py
21
sensor.py
@ -22,6 +22,9 @@ CODEOWNERS = ["@jonatanrek"]
|
||||
DEPENDENCIES = ['uart']
|
||||
|
||||
CONF_BED_TEMPERATURE = "bed_temperature"
|
||||
CONF_EXT_TEMPERATURE = "ext_temperature"
|
||||
CONF_PROGRESS = "progress"
|
||||
|
||||
|
||||
Marlin2 = cg.esphome_ns.class_('Marlin2', cg.Component, sensor.Sensor, uart.UARTDevice)
|
||||
|
||||
@ -34,6 +37,18 @@ CONFIG_SCHEMA = uart.UART_DEVICE_SCHEMA.extend(
|
||||
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(
|
||||
unit_of_measurement=UNIT_CELSIUS,
|
||||
accuracy_decimals=1,
|
||||
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
@ -44,3 +59,9 @@ async def to_code(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])
|
||||
|
Loading…
Reference in New Issue
Block a user