#pragma once #include #include #include "esphome/core/component.h" #include "esphome/components/uart/uart.h" #ifdef USE_SENSOR #include "esphome/components/sensor/sensor.h" #endif #ifdef USE_TEXT_SENSOR #include "esphome/components/text_sensor/text_sensor.h" #endif #ifdef USE_SELECT #include "esphome/components/select/select.h" #endif #ifdef USE_BINARY_SENSOR #include "esphome/components/binary_sensor/binary_sensor.h" #endif namespace esphome::marlin2 { class Marlin2 : public PollingComponent, public uart::UARTDevice { public: Marlin2() = default; #ifdef USE_SENSOR void add_sensor(const std::string &name, sensor::Sensor *sens); sensor::Sensor *find_sensor(const std::string &key); #endif #ifdef USE_TEXT_SENSOR void add_text_sensor(const std::string &name, text_sensor::TextSensor *tSens); text_sensor::TextSensor *find_text_sensor(const std::string &key); #endif #ifdef USE_SELECT void add_select(const std::string &name, select::Select *sel); select::Select *find_select(const std::string &key); #endif #ifdef USE_BINARY_SENSOR void add_binary_sensor(const std::string &name, binary_sensor::BinarySensor *bs); binary_sensor::BinarySensor *find_binary_sensor(const std::string &key); #endif void set_max_sd_files(uint8_t n) { max_sd_files_ = n; } void write(std::string gcode); std::string to_dos_name(std::string filename); std::string from_dos_name(std::string dos_filename); float get_setup_priority() const override { return setup_priority::LATE; } void setup() override; void update() override; void dump_config() override; protected: std::string marlin_output_; std::string marlin_response_output_; std::string marlin_time_; std::string printer_state_; float print_progress_ = 0; double print_time_offset_ = 0; uint8_t max_sd_files_ = 20; #ifdef USE_SENSOR std::vector> sensors_; #endif #ifdef USE_TEXT_SENSOR std::vector> text_sensors_; #endif #ifdef USE_SELECT std::vector> selects_; #endif #ifdef USE_BINARY_SENSOR std::vector> binary_sensors_; #endif void process_line(); void set_printer_state(std::string status); void publish_sd_files(); 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(double *current, double *remaining, float progress); private: unsigned long millis_progress_ = 0; std::unordered_map file_table_; bool listing_file_ = false; }; } // namespace esphome::marlin2