Files
ESPHomeMarlin2/marlin2.h
JonatanRek 270a92ba06 Refactor Marlin2 component and add new features
- Updated __init__.py to include max_sd_files configuration option.
- Refactored Marlin2 class to use new namespaces and improved structure.
- Added support for binary sensors and select components in binary_sensor.py and select.py.
- Enhanced sensor.py and text_sensor.py to include new configuration options for SD card file count and selected file.
- Improved code readability and organization across multiple files.
2026-04-07 08:19:25 +02:00

91 lines
2.9 KiB
C++

#pragma once
#include <string>
#include <unordered_map>
#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<std::pair<std::string, sensor::Sensor *>> sensors_;
#endif
#ifdef USE_TEXT_SENSOR
std::vector<std::pair<std::string, text_sensor::TextSensor *>> text_sensors_;
#endif
#ifdef USE_SELECT
std::vector<std::pair<std::string, select::Select *>> selects_;
#endif
#ifdef USE_BINARY_SENSOR
std::vector<std::pair<std::string, binary_sensor::BinarySensor *>> 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<std::string, std::string> file_table_;
bool listing_file_ = false;
};
} // namespace esphome::marlin2