Files
ESPHomeMarlin2/marlin2.h
JonatanRek 3159d4c875 Fixes
2026-04-07 09:35:29 +02:00

93 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 Component, public uart::UARTDevice {
public:
Marlin2() = default;
#ifdef USE_SENSOR
void add_sensor(const std::string &name, sensor::Sensor *sens);
void publish_sensor(const std::string &key, float value);
#endif
#ifdef USE_TEXT_SENSOR
void add_text_sensor(const std::string &name, text_sensor::TextSensor *tSens);
void publish_text_sensor(const std::string &key, const std::string &value);
#endif
#ifdef USE_SELECT
void add_select(const std::string &name, select::Select *sel);
#endif
#ifdef USE_BINARY_SENSOR
void add_binary_sensor(const std::string &name, binary_sensor::BinarySensor *bs);
void publish_binary_sensor(const std::string &key, bool value);
#endif
void set_max_sd_files(uint8_t n) { max_sd_files_ = n; }
void write(const std::string &gcode);
std::string to_dos_name(const std::string &filename);
std::string from_dos_name(const std::string &dos_filename);
float get_setup_priority() const override { return setup_priority::LATE; }
void setup() override;
void loop() override;
void dump_config() override;
protected:
std::string marlin_output_;
std::string marlin_time_;
float print_progress_ = 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(const 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:
template<typename T>
static T *find_in_(std::vector<std::pair<std::string, T *>> &vec, const std::string &key) {
for (auto &pair : vec)
if (pair.first == key) return pair.second;
return nullptr;
}
std::unordered_map<std::string, std::string> file_table_;
bool listing_file_ = false;
};
} // namespace esphome::marlin2