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.
This commit is contained in:
111
marlin2.h
111
marlin2.h
@@ -10,56 +10,81 @@
|
||||
#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 {
|
||||
namespace esphome::marlin2 {
|
||||
|
||||
class Marlin2 : public PollingComponent, public uart::UARTDevice {
|
||||
public:
|
||||
Marlin2() = default;
|
||||
public:
|
||||
Marlin2() = default;
|
||||
|
||||
#ifdef USE_SENSOR
|
||||
void add_sensor(const std::string& sName, sensor::Sensor *sens);
|
||||
sensor::Sensor* find_sensor(std::string key);
|
||||
#endif
|
||||
#ifdef USE_TEXT_SENSOR
|
||||
void add_text_sensor(const std::string& sName, text_sensor::TextSensor *tSens);
|
||||
text_sensor::TextSensor* find_text_sensor(std::string key);
|
||||
#endif
|
||||
void write(std::string status);
|
||||
std::string to_dos_name(std::string filename);
|
||||
std::string from_dos_name(std::string dos_filename);
|
||||
std::string to_lower(std::string s);
|
||||
|
||||
float get_setup_priority() const override { return setup_priority::LATE; }
|
||||
void setup() override;
|
||||
void update() override;
|
||||
#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
|
||||
|
||||
protected:
|
||||
std::string MarlinOutput;
|
||||
std::string MarlinResponseOutput;
|
||||
std::string MarlinTime;
|
||||
std::string PrinterState;
|
||||
void set_max_sd_files(uint8_t n) { max_sd_files_ = n; }
|
||||
|
||||
float print_progress = 0;
|
||||
double print_time_offset = 0;
|
||||
|
||||
#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
|
||||
void write(std::string gcode);
|
||||
std::string to_dos_name(std::string filename);
|
||||
std::string from_dos_name(std::string dos_filename);
|
||||
|
||||
void process_line();
|
||||
void set_printer_state(std::string status);
|
||||
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);
|
||||
float get_setup_priority() const override { return setup_priority::LATE; }
|
||||
void setup() override;
|
||||
void update() override;
|
||||
void dump_config() override;
|
||||
|
||||
private:
|
||||
unsigned long millisProgress=0;
|
||||
std::unordered_map<std::string, std::string> file_table_;
|
||||
bool listingFile = false;
|
||||
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
|
||||
} // namespace esphome::marlin2
|
||||
|
||||
Reference in New Issue
Block a user