progress
This commit is contained in:
parent
68ab694d8a
commit
91f2dd54be
169
marlin2.cpp
169
marlin2.cpp
@ -5,32 +5,32 @@ namespace esphome {
|
||||
static const char *TAG = "marlin2";
|
||||
|
||||
#ifdef USE_SENSOR
|
||||
void Marlin2::add_sensor(const std::string& sName, sensor::Sensor *sens) {
|
||||
sensors.push_back({sName, sens});
|
||||
}
|
||||
void Marlin2::add_sensor(const std::string& sName, sensor::Sensor *sens) {
|
||||
sensors.push_back({sName, sens});
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
return nullptr; // Return nullptr if no match is found
|
||||
}
|
||||
return nullptr; // Return nullptr if no match is found
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_TEXT_SENSOR
|
||||
void Marlin2::add_text_sensor(const std::string& sName, text_sensor::TextSensor *sens) {
|
||||
text_sensors.push_back({sName, sens});
|
||||
}
|
||||
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;
|
||||
}
|
||||
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
|
||||
}
|
||||
return nullptr; // Return nullptr if no match is found
|
||||
}
|
||||
#endif
|
||||
|
||||
void Marlin2::setup() {
|
||||
@ -86,10 +86,43 @@ namespace esphome {
|
||||
}
|
||||
|
||||
if(!MarlinOutput.compare("ok") || !MarlinOutput.compare(" ok")) {
|
||||
listingFile = false;
|
||||
MarlinOutput="";
|
||||
return;
|
||||
}
|
||||
|
||||
if(!listingFile && MarlinOutput.compare("Begin file list") == 0) {
|
||||
ESP_LOGD(TAG, "Listing of files started!");
|
||||
listingFile = true;
|
||||
//reset string for next line
|
||||
MarlinOutput="";
|
||||
return;
|
||||
}
|
||||
|
||||
if(listingFile && MarlinOutput.compare(".GCO ")) {
|
||||
int first_space = MarlinOutput.find(' ');
|
||||
int second_space = MarlinOutput.find(' ', first_space + 1);
|
||||
|
||||
if(first_space > 0) {
|
||||
std::string short_name = MarlinOutput.substr(0, first_space);
|
||||
std::string long_name = short_name;
|
||||
|
||||
if (second_space > first_space){
|
||||
long_name = MarlinOutput.substr(second_space + 1);
|
||||
}
|
||||
|
||||
file_table_[long_name] = short_name;
|
||||
ESP_LOGD(TAG, "Uloženo do tabulky: %s => %s", long_name.c_str(), short_name.c_str());
|
||||
|
||||
}
|
||||
|
||||
//reset string for next line
|
||||
MarlinOutput="";
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//Parse periodic Temperature read out message
|
||||
if(
|
||||
MarlinOutput.find(" T:") == 0 ||
|
||||
@ -100,31 +133,31 @@ namespace esphome {
|
||||
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) {
|
||||
#ifdef USE_SENSOR
|
||||
if (find_sensor("bed_temperature") != nullptr)
|
||||
find_sensor("bed_temperature")->publish_state(bed_temperature);
|
||||
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("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_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);
|
||||
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) {
|
||||
if(ext_temperature < 32.0 && bed_temperature < 32.0){ //TODO define constants for these
|
||||
set_printer_state("IDLE");
|
||||
}
|
||||
else if(ext_temperature < 150.0 && bed_temperature < 55.0){
|
||||
set_printer_state("COOLING");
|
||||
}
|
||||
if(bed_set_temperature==0.0 && ext_set_temperature==0.0) {
|
||||
if(ext_temperature < 32.0 && bed_temperature < 32.0){ //TODO define constants for these
|
||||
set_printer_state("IDLE");
|
||||
}
|
||||
if(print_progress == 0.0 && (bed_set_temperature!=0.0 || ext_set_temperature!=0.0)) {
|
||||
//print_time_offset = print_time save print time ofset to deduct from total value send to hass
|
||||
set_printer_state("PREHEATING");
|
||||
else if(ext_temperature < 150.0 && bed_temperature < 55.0){
|
||||
set_printer_state("COOLING");
|
||||
}
|
||||
}
|
||||
if(print_progress == 0.0 && (bed_set_temperature!=0.0 || ext_set_temperature!=0.0)) {
|
||||
//print_time_offset = print_time save print time ofset to deduct from total value send to hass
|
||||
set_printer_state("PREHEATING");
|
||||
}
|
||||
#endif
|
||||
|
||||
ESP_LOGD(TAG, "Bed Temperature=%.1f°C Ext Temperature=%.1f°C ", bed_temperature, ext_temperature);
|
||||
@ -139,10 +172,10 @@ namespace esphome {
|
||||
if(MarlinOutput.find("SD printing byte") == 0 ) {
|
||||
print_progress = process_progress_msg();
|
||||
#ifdef USE_SENSOR
|
||||
if (find_sensor("print_progress") != nullptr)
|
||||
find_sensor("print_progress")->publish_state(print_progress);
|
||||
if (find_sensor("print_progress") != nullptr)
|
||||
find_sensor("print_progress")->publish_state(print_progress);
|
||||
|
||||
ESP_LOGD(TAG, "progress=%.1f", print_progress);
|
||||
ESP_LOGD(TAG, "progress=%.1f", print_progress);
|
||||
#endif
|
||||
|
||||
set_printer_state("PRINTING");
|
||||
@ -157,11 +190,11 @@ namespace esphome {
|
||||
double remaining=0;
|
||||
if (process_print_time_msg(¤t, &remaining, print_progress) != 0) {
|
||||
#ifdef USE_SENSOR
|
||||
if (find_sensor("print_time") != nullptr)
|
||||
find_sensor("print_time")->publish_state(current);
|
||||
if (find_sensor("print_time") != nullptr)
|
||||
find_sensor("print_time")->publish_state(current);
|
||||
|
||||
if (find_sensor("print_time_remaining") != nullptr)
|
||||
find_sensor("print_time_remaining")->publish_state(remaining);
|
||||
if (find_sensor("print_time_remaining") != nullptr)
|
||||
find_sensor("print_time_remaining")->publish_state(remaining);
|
||||
#endif
|
||||
|
||||
ESP_LOGD(TAG, "time=%f remaining=%f", current, remaining);
|
||||
@ -172,7 +205,7 @@ namespace esphome {
|
||||
return;
|
||||
}
|
||||
|
||||
//Print From SD Card Started
|
||||
//Print From SD Card Started
|
||||
if(MarlinOutput.find("File selected") != std::string::npos) {
|
||||
set_printer_state("PRINTING");
|
||||
|
||||
@ -185,14 +218,14 @@ namespace esphome {
|
||||
if(MarlinOutput.find("Done printing") != std::string::npos) {
|
||||
print_progress = 100;
|
||||
#ifdef USE_SENSOR
|
||||
if (find_sensor("print_progress") != nullptr)
|
||||
find_sensor("print_progress")->publish_state(print_progress);
|
||||
if (find_sensor("print_progress") != nullptr)
|
||||
find_sensor("print_progress")->publish_state(print_progress);
|
||||
|
||||
if (find_sensor("print_time_remaining") != nullptr)
|
||||
find_sensor("print_time_remaining")->publish_state(0);
|
||||
if (find_sensor("print_time_remaining") != nullptr)
|
||||
find_sensor("print_time_remaining")->publish_state(0);
|
||||
#endif
|
||||
#ifdef USE_TEXT_SENSOR
|
||||
set_printer_state("FINISHED");
|
||||
set_printer_state("FINISHED");
|
||||
#endif
|
||||
|
||||
//reset string for next line
|
||||
@ -218,17 +251,11 @@ namespace esphome {
|
||||
return;
|
||||
}
|
||||
|
||||
size_t first_space = MarlinOutput.find(' ');
|
||||
size_t second_space = MarlinOutput.find(' ', first_space + 1);
|
||||
|
||||
if(second_space == std::string::npos && first_space == std::string::npos) {
|
||||
std::string short_name = MarlinOutput.substr(0, first_space);
|
||||
std::string long_name = MarlinOutput.substr(second_space + 1);
|
||||
|
||||
file_table_[long_name] = short_name;
|
||||
|
||||
ESP_LOGD(TAG, "Uloženo do tabulky: %s => %s", long_name.c_str(), short_name.c_str());
|
||||
|
||||
if(MarlinOutput.find("End file list") != std::string::npos) {
|
||||
ESP_LOGD(TAG, "Listing of files stopped!");
|
||||
listingFile = false;
|
||||
//reset string for next line
|
||||
MarlinOutput="";
|
||||
return;
|
||||
@ -244,22 +271,22 @@ namespace esphome {
|
||||
float dc;
|
||||
|
||||
while(MarlinOutput.find(" ") != std::string::npos)
|
||||
MarlinOutput.erase(MarlinOutput.find(' '), 1);
|
||||
MarlinOutput.erase(MarlinOutput.find(' '), 1);
|
||||
|
||||
while(MarlinOutput.find("ok") != std::string::npos)
|
||||
MarlinOutput.erase(MarlinOutput.find("ok"), 2);
|
||||
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;
|
||||
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;
|
||||
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;
|
||||
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 4;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -305,15 +332,15 @@ namespace esphome {
|
||||
|
||||
void Marlin2::set_printer_state(std::string status){
|
||||
#ifdef USE_TEXT_SENSOR
|
||||
// if (!PrinterState.compare(status))
|
||||
// return;
|
||||
// if (!PrinterState.compare(status))
|
||||
// return;
|
||||
|
||||
if (find_text_sensor("printer_state") != nullptr){
|
||||
find_text_sensor("printer_state")->publish_state(status);
|
||||
}
|
||||
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;
|
||||
ESP_LOGD(TAG, "Printer Status %s", status.c_str());
|
||||
// PrinterState = status;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user