ESPHomeMarlin2/marlin2.cpp

303 lines
11 KiB
C++
Raw Normal View History

2024-12-27 10:47:42 +00:00
#include "marlin2.h"
#include "esphome/core/log.h"
namespace esphome {
2024-12-27 20:06:46 +00:00
static const char *TAG = "marlin2";
2024-12-27 10:47:42 +00:00
2024-12-30 09:39:19 +00:00
#ifdef USE_SENSOR
void Marlin2::add_sensor(const std::string& sName, sensor::Sensor *sens) {
sensors.push_back({sName, sens});
}
2024-12-27 20:06:46 +00:00
2024-12-30 09:39:19 +00:00
sensor::Sensor* Marlin2::find_sensor(std::string key) {
for (const auto& pair : sensors) {
if (key == std::string(pair.first)) { // Convert char* to std::string for comparison
return pair.second;
}
2024-12-27 20:06:46 +00:00
}
2024-12-30 09:39:19 +00:00
return nullptr; // Return nullptr if no match is found
2024-12-27 20:06:46 +00:00
}
2024-12-30 09:39:19 +00:00
#endif
#ifdef USE_TEXT_SENSOR
void Marlin2::add_text_sensor(const std::string& sName, text_sensor::TextSensor *sens) {
text_sensors.push_back({sName, sens});
}
text_sensor::TextSensor* Marlin2::find_text_sensor(std::string key) {
for (const auto& pair : text_sensors) {
if (key == std::string(pair.first)) { // Convert char* to std::string for comparison
return pair.second;
}
}
return nullptr; // Return nullptr if no match is found
}
#endif
2024-12-27 11:21:33 +00:00
void Marlin2::setup() {
MarlinOutput.reserve(256);
MarlinOutput = "";
2024-12-27 20:06:46 +00:00
MarlinTime.reserve(32);
PrinterState.reserve(32);
2024-12-27 20:06:46 +00:00
ESP_LOGD(TAG, "M155 S10");
2024-12-30 09:39:19 +00:00
write_str("\r\n\r\nM155 S10\r\n");
2024-12-30 11:20:59 +00:00
write_str("\r\n\r\nM117 ESP Home Connected!\r\n");
2024-12-30 09:39:19 +00:00
flush();
2024-12-30 11:07:51 +00:00
set_printer_state("IDLE");
2024-12-31 07:18:35 +00:00
register_service(&Marlin2::set_bed_setpoint, "set_bed_setpoint", {"temp_degC"});
register_service(&Marlin2::set_extruder_setpoint, "set_extruder_setpoint", {"temp_degC"});
}
2024-12-31 07:18:35 +00:00
void Marlin2::set_bed_setpoint() {
}
void Marlin2::set_extruder_setpoint() {
}
2024-12-30 08:42:31 +00:00
void Marlin2::update() {
2024-12-30 09:39:19 +00:00
while (available()) {
char c = read();
if( c == '\n' || c == '\r' ) {
process_line();
} else {
MarlinOutput += c;
}
}
2024-12-28 18:16:10 +00:00
if(millis() - millisProgress > 15000 && print_progress != 100) {
millisProgress = millis();
ESP_LOGD(TAG, "M27");
ESP_LOGD(TAG, "M31");
2024-12-30 09:39:19 +00:00
write_str("M27\r\nM31\r\n");
}
2024-12-27 11:21:33 +00:00
}
2024-12-27 10:47:42 +00:00
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
) {
2024-12-27 20:06:46 +00:00
float ext_temperature, ext_set_temperature, bed_temperature, bed_set_temperature;
if (process_temp_msg(&ext_temperature, &ext_set_temperature, &bed_temperature, &bed_set_temperature) != 0) {
2024-12-30 08:42:31 +00:00
#ifdef USE_SENSOR
if (find_sensor("bed_temperature") != nullptr)
find_sensor("bed_temperature")->publish_state(bed_temperature);
if (find_sensor("bed_set_temperature") != nullptr)
find_sensor("bed_set_temperature")->publish_state(bed_set_temperature);
if (find_sensor("ext_temperature") != nullptr)
find_sensor("ext_temperature")->publish_state(ext_temperature);
if (find_sensor("ext_set_temperature") != nullptr)
find_sensor("ext_set_temperature")->publish_state(ext_set_temperature);
#endif
#ifdef USE_TEXT_SENSOR
if(bed_set_temperature==0.0 && ext_set_temperature==0.0) {
2024-12-30 09:39:19 +00:00
if(ext_temperature < 32.0 && bed_temperature < 32.0){ //TODO define constants for these
2024-12-30 11:07:51 +00:00
set_printer_state("IDLE");
2024-12-30 09:39:19 +00:00
}
else if(ext_temperature < 150.0 && bed_temperature < 55.0){
2024-12-30 11:07:51 +00:00
set_printer_state("COOLING");
2024-12-30 09:39:19 +00:00
}
2024-12-30 08:42:31 +00:00
}
2024-12-30 11:15:08 +00:00
if(print_progress == 0.0 && (bed_set_temperature!=0.0 || ext_set_temperature!=0.0)) {
2024-12-30 11:18:21 +00:00
//print_time_offset = print_time save print time ofset to deduct from total value send to hass
2024-12-30 11:07:51 +00:00
set_printer_state("PREHEATING");
2024-12-30 08:42:31 +00:00
}
#endif
2024-12-27 20:06:46 +00:00
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 ) {
2024-12-28 15:57:08 +00:00
print_progress = process_progress_msg();
2024-12-30 08:42:31 +00:00
#ifdef USE_SENSOR
if (find_sensor("print_progress") != nullptr)
find_sensor("print_progress")->publish_state(print_progress);
2024-12-27 20:06:46 +00:00
2024-12-30 08:42:31 +00:00
ESP_LOGD(TAG, "progress=%.1f", print_progress);
#endif
2024-12-30 11:09:44 +00:00
set_printer_state("PRINTING");
//reset string for next line
MarlinOutput="";
return;
}
//Parse Printitme
if(MarlinOutput.find("echo:Print time: ") == 0) {
2024-12-28 13:13:24 +00:00
double current=0;
double remaining=0;
2024-12-28 15:57:08 +00:00
if (process_print_time_msg(&current, &remaining, print_progress) != 0) {
2024-12-30 08:42:31 +00:00
#ifdef USE_SENSOR
if (find_sensor("print_time") != nullptr)
find_sensor("print_time")->publish_state(current);
2024-12-27 20:06:46 +00:00
2024-12-30 08:42:31 +00:00
if (find_sensor("print_time_remaining") != nullptr)
find_sensor("print_time_remaining")->publish_state(remaining);
#endif
2024-12-27 20:06:46 +00:00
2024-12-28 13:13:24 +00:00
ESP_LOGD(TAG, "time=%f remaining=%f", current, remaining);
}
2024-12-27 11:02:11 +00:00
//reset string for next line
MarlinOutput="";
return;
}
2024-12-30 11:07:51 +00:00
//Print From SD Card Started
if(MarlinOutput.find("File selected") != std::string::npos) {
set_printer_state("PRINTING");
//reset string for next line
MarlinOutput="";
return;
}
2024-12-28 15:57:08 +00:00
//Print Finished
2024-12-30 09:39:19 +00:00
if(MarlinOutput.find("Done printing") != std::string::npos) {
2024-12-28 15:57:08 +00:00
print_progress = 100;
2024-12-30 08:42:31 +00:00
#ifdef USE_SENSOR
if (find_sensor("print_progress") != nullptr)
find_sensor("print_progress")->publish_state(print_progress);
2024-12-30 08:42:31 +00:00
if (find_sensor("print_time_remaining") != nullptr)
find_sensor("print_time_remaining")->publish_state(0);
#endif
2024-12-30 11:07:51 +00:00
#ifdef USE_TEXT_SENSOR
set_printer_state("FINISHED");
#endif
2024-12-28 15:57:08 +00:00
//reset string for next line
MarlinOutput="";
return;
}
2024-12-30 16:48:35 +00:00
// //Print Paused
// if(MarlinOutput.find("Printer halted") != std::string::npos) {
// set_printer_state("PAUSED");
2024-12-30 16:48:35 +00:00
// //reset string for next line
// MarlinOutput="";
// return;
// }
2024-12-30 11:07:51 +00:00
// //Print Stoped
if(MarlinOutput.find("Print Aborted") != std::string::npos) {
set_printer_state("STOPPED");
2024-12-30 11:07:51 +00:00
//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;
2024-12-30 11:07:51 +00:00
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;
}
2024-12-28 15:57:08 +00:00
return (((float) current * 100.0) / (float) total);
}
2024-12-28 15:57:08 +00:00
int Marlin2::process_print_time_msg(double* current, double* remaining, float progress){
2024-12-27 20:06:46 +00:00
MarlinTime = MarlinOutput.substr(16);
2024-12-28 13:13:24 +00:00
float d = 0, h = 0, m = 0, s = 0;
2024-12-27 20:06:46 +00:00
ESP_LOGD(TAG,MarlinTime.c_str());
2024-12-28 13:13:24 +00:00
if (sscanf(MarlinTime.c_str() ,"%fd %fh %fm %fs", &d, &h, &m, &s)!=4) {
2024-12-27 20:06:46 +00:00
d=0;
2024-12-28 13:13:24 +00:00
if (sscanf(MarlinTime.c_str() ,"%fh %fm %fs", &h, &m, &s)!=3) {
2024-12-27 20:06:46 +00:00
d=0; h=0;
2024-12-28 13:13:24 +00:00
if (sscanf(MarlinTime.c_str() ,"%fm %fs", &m, &s)!=2) {
2024-12-27 20:06:46 +00:00
d=0; h=0; m=0;
2024-12-28 13:13:24 +00:00
if (sscanf(MarlinTime.c_str() ,"%fs", &s)!=1) {
2024-12-28 09:33:44 +00:00
return 0;
2024-12-27 20:06:46 +00:00
}
}
}
}
2024-12-28 13:13:24 +00:00
*current = round(((d)*24*60*60) + ((h)*60*60) + ((m)*60) + (s));
2024-12-28 15:57:08 +00:00
if(progress != 0.0 && progress != 100.0) {
2024-12-31 07:18:35 +00:00
*remaining = (((100 * *current) / round(progress)) - *current);
2024-12-28 15:57:08 +00:00
}
2024-12-28 09:41:08 +00:00
return 1;
}
2024-12-27 11:02:11 +00:00
2024-12-30 11:07:51 +00:00
void Marlin2::set_printer_state(std::string status){
#ifdef USE_TEXT_SENSOR
// if (!PrinterState.compare(status))
// return;
if (find_text_sensor("printer_state") != nullptr){
find_text_sensor("printer_state")->publish_state(status);
}
ESP_LOGD(TAG, "Printer Status %s", status.c_str());
// PrinterState = status;
#endif
}
2024-12-27 10:47:42 +00:00
} // namespace esphome