mirror of
https://github.com/suchmememanyskill/CYD-Klipper.git
synced 2026-03-21 05:33:24 +00:00
Add bambu file loading
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
#include "bambu_printer_integration.hpp"
|
#include "bambu_printer_integration.hpp"
|
||||||
#include <PubSubClient.h>
|
#include <PubSubClient.h>
|
||||||
#include "WifiClientSecure.h"
|
|
||||||
|
|
||||||
WiFiClientSecure wifi_client;
|
WiFiClientSecure wifi_client;
|
||||||
PubSubClient client(wifi_client);
|
PubSubClient client(wifi_client);
|
||||||
@@ -125,7 +124,7 @@ bool BambuPrinter::connect()
|
|||||||
sprintf(buff, "%d", printer_config->klipper_port);
|
sprintf(buff, "%d", printer_config->klipper_port);
|
||||||
if (!client.connect("id", "bblp", buff))
|
if (!client.connect("id", "bblp", buff))
|
||||||
{
|
{
|
||||||
LOG_LN(("Bambu: Wrong IP or LAN code."));
|
LOG_LN("Bambu: Wrong IP or LAN code.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,7 +133,7 @@ bool BambuPrinter::connect()
|
|||||||
|
|
||||||
if (!client.subscribe(auth))
|
if (!client.subscribe(auth))
|
||||||
{
|
{
|
||||||
LOG_LN(("Bambu: Wrong serial number."));
|
LOG_LN("Bambu: Wrong serial number.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -143,7 +142,7 @@ bool BambuPrinter::connect()
|
|||||||
|
|
||||||
if (!client.connected())
|
if (!client.connected())
|
||||||
{
|
{
|
||||||
LOG_LN(("Bambu: Connection lost. Likely wrong serial number."));
|
LOG_LN("Bambu: Connection lost. Likely wrong serial number.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -291,7 +290,11 @@ bool BambuPrinter::set_power_device_state(const char* device_name, bool state)
|
|||||||
|
|
||||||
Files BambuPrinter::get_files()
|
Files BambuPrinter::get_files()
|
||||||
{
|
{
|
||||||
Files files = {0};
|
PrinterState state = printer_data.state;
|
||||||
|
disconnect();
|
||||||
|
Files files = parse_files(wifi_client, 20);
|
||||||
|
connect();
|
||||||
|
printer_data.state = state;
|
||||||
return files;
|
return files;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -343,7 +346,7 @@ BambuConnectionStatus connection_test_bambu(PrinterConfiguration* config)
|
|||||||
sprintf(buff, "%d", config->klipper_port);
|
sprintf(buff, "%d", config->klipper_port);
|
||||||
if (!connection_test_client.connect("id", "bblp", buff))
|
if (!connection_test_client.connect("id", "bblp", buff))
|
||||||
{
|
{
|
||||||
LOG_LN(("Bambu: Wrong IP or LAN code."));
|
LOG_LN("Bambu: Wrong IP or LAN code.");
|
||||||
return BambuConnectionStatus::BambuConnectFail;
|
return BambuConnectionStatus::BambuConnectFail;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -352,7 +355,7 @@ BambuConnectionStatus connection_test_bambu(PrinterConfiguration* config)
|
|||||||
|
|
||||||
if (!connection_test_client.subscribe(auth))
|
if (!connection_test_client.subscribe(auth))
|
||||||
{
|
{
|
||||||
LOG_LN(("Bambu: Wrong serial number."));
|
LOG_LN("Bambu: Wrong serial number.");
|
||||||
return BambuConnectionStatus::BambuConnectSNFail;
|
return BambuConnectionStatus::BambuConnectSNFail;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -361,7 +364,7 @@ BambuConnectionStatus connection_test_bambu(PrinterConfiguration* config)
|
|||||||
|
|
||||||
if (!connection_test_client.connected())
|
if (!connection_test_client.connected())
|
||||||
{
|
{
|
||||||
LOG_LN(("Bambu: Connection lost. Likely wrong serial number."));
|
LOG_LN("Bambu: Connection lost. Likely wrong serial number.");
|
||||||
return BambuConnectionStatus::BambuConnectSNFail;
|
return BambuConnectionStatus::BambuConnectSNFail;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include "../printer_integration.hpp"
|
#include "../printer_integration.hpp"
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
|
#include <WifiClientSecure.h>
|
||||||
|
|
||||||
class BambuPrinter : public BasePrinter
|
class BambuPrinter : public BasePrinter
|
||||||
{
|
{
|
||||||
@@ -24,6 +25,7 @@ class BambuPrinter : public BasePrinter
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
void parse_state(JsonDocument& in);
|
void parse_state(JsonDocument& in);
|
||||||
|
Files parse_files(WiFiClientSecure& client, int max_files);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
BambuPrinter(int index) : BasePrinter(index)
|
BambuPrinter(int index) : BasePrinter(index)
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#include "bambu_printer_integration.hpp"
|
#include "bambu_printer_integration.hpp"
|
||||||
#include <HTTPClient.h>
|
#include <HTTPClient.h>
|
||||||
|
#include <list>
|
||||||
|
|
||||||
#define BIT_X_AXIS_HOMED BIT(0)
|
#define BIT_X_AXIS_HOMED BIT(0)
|
||||||
#define BIT_Y_AXIS_HOMED BIT(1)
|
#define BIT_Y_AXIS_HOMED BIT(1)
|
||||||
@@ -201,3 +202,158 @@ void BambuPrinter::parse_state(JsonDocument& in)
|
|||||||
printer_data.extrude_mult = 1;
|
printer_data.extrude_mult = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Derived from https://github.com/ldab/ESP32_FTPClient/blob/master/src/ESP32_FTPClient.cpp
|
||||||
|
bool wifi_client_response_pass(WiFiClientSecure& client)
|
||||||
|
{
|
||||||
|
unsigned long _m = millis();
|
||||||
|
bool first_char = true;
|
||||||
|
while (!client.available() && millis() < _m + 500) delay(1);
|
||||||
|
|
||||||
|
if(!client.available())
|
||||||
|
{
|
||||||
|
LOG_LN("FTPS: No response from server");
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG_LN("[FTPS response]");
|
||||||
|
bool response = true;
|
||||||
|
while (client.available())
|
||||||
|
{
|
||||||
|
char byte = client.read();
|
||||||
|
|
||||||
|
if (first_char && (byte == '4' || byte == '5'))
|
||||||
|
{
|
||||||
|
LOG_LN("FTPS: Server returned an error");
|
||||||
|
response = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
first_char = false;
|
||||||
|
|
||||||
|
LOG_F(("%c", byte));
|
||||||
|
}
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool wifi_client_response_parse(WiFiClientSecure& client, std::list<char*> &files, int max_files)
|
||||||
|
{
|
||||||
|
unsigned long _m = millis();
|
||||||
|
while (!client.available() && millis() < _m + 500) delay(1);
|
||||||
|
|
||||||
|
if(!client.available())
|
||||||
|
{
|
||||||
|
LOG_LN("FTPS: No response from server");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG_LN("[FTPS response]");
|
||||||
|
char buff[128] = {0};
|
||||||
|
int index = 0;
|
||||||
|
while (client.available()) {
|
||||||
|
int byte = client.read();
|
||||||
|
LOG_F(("%c", byte));
|
||||||
|
buff[index] = byte;
|
||||||
|
|
||||||
|
if (byte == '\n' || byte == '\r' || byte <= 0)
|
||||||
|
{
|
||||||
|
buff[index] = 0;
|
||||||
|
if (index > 10)
|
||||||
|
{
|
||||||
|
char* file = (char*)malloc(index + 1);
|
||||||
|
|
||||||
|
if (file != NULL)
|
||||||
|
{
|
||||||
|
strcpy(file, buff);
|
||||||
|
files.push_front(file);
|
||||||
|
|
||||||
|
if (files.size() > max_files)
|
||||||
|
{
|
||||||
|
auto last_entry = files.back();
|
||||||
|
|
||||||
|
if (last_entry != NULL)
|
||||||
|
free(last_entry);
|
||||||
|
|
||||||
|
files.pop_back();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
LOG_LN("Failed to allocate memory");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
index = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool send_command_without_response(WiFiClientSecure& client, const char* command)
|
||||||
|
{
|
||||||
|
client.println(command);
|
||||||
|
LOG_F(("[FTPS Command] %s\n", command));
|
||||||
|
return wifi_client_response_pass(client);
|
||||||
|
}
|
||||||
|
|
||||||
|
Files BambuPrinter::parse_files(WiFiClientSecure& wifi_client, int max_files)
|
||||||
|
{
|
||||||
|
LOG_F(("Heap space pre-file-parse: %d bytes\n", esp_get_free_heap_size()));
|
||||||
|
|
||||||
|
unsigned long timer_request = millis();
|
||||||
|
Files result = {0};
|
||||||
|
|
||||||
|
if (!wifi_client.connect(printer_config->klipper_host, 990))
|
||||||
|
{
|
||||||
|
LOG_LN("Failed to fetch files: connection failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
wifi_client_response_pass(wifi_client);
|
||||||
|
|
||||||
|
char auth_code_buff[16] = {0};
|
||||||
|
sprintf(auth_code_buff, "PASS %d", printer_config->klipper_port);
|
||||||
|
send_command_without_response(wifi_client, "USER bblp");
|
||||||
|
wifi_client_response_pass(wifi_client);
|
||||||
|
send_command_without_response(wifi_client, auth_code_buff);
|
||||||
|
send_command_without_response(wifi_client, "PASV");
|
||||||
|
send_command_without_response(wifi_client, "NLST");
|
||||||
|
wifi_client.stop();
|
||||||
|
|
||||||
|
if (wifi_client.connect(printer_config->klipper_host, 2024))
|
||||||
|
{
|
||||||
|
unsigned long timer_parse = millis();
|
||||||
|
std::list<char*> files;
|
||||||
|
wifi_client_response_parse(wifi_client, files, max_files);
|
||||||
|
result.available_files = (char**)malloc(sizeof(char*) * files.size());
|
||||||
|
if (result.available_files == NULL)
|
||||||
|
{
|
||||||
|
LOG_LN("Failed to allocate memory");
|
||||||
|
|
||||||
|
for (auto file : files){
|
||||||
|
free(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto file : files){
|
||||||
|
result.available_files[result.count++] = file;
|
||||||
|
}
|
||||||
|
|
||||||
|
result.success = true;
|
||||||
|
LOG_F(("Heap space post-file-parse: %d bytes\n", esp_get_free_heap_size()))
|
||||||
|
LOG_F(("Got %d files. Request took %dms, parsing took %dms\n", files.size(), timer_parse - timer_request, millis() - timer_parse))
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
LOG_LN("Failed to fetch files: data connection failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
wifi_client.stop();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
@@ -125,9 +125,10 @@ void data_setup()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
initialize_printers(available_printers, count);
|
initialize_printers(available_printers, count);
|
||||||
set_current_printer(true_current_printer_index);
|
set_current_printer(true_current_printer_index);
|
||||||
|
LOG_F(("Free heap after printer creation: %d bytes\n", esp_get_free_heap_size()));
|
||||||
semaphore_init();
|
semaphore_init();
|
||||||
fetch_printer_data();
|
fetch_printer_data();
|
||||||
freeze_render_thread();
|
freeze_render_thread();
|
||||||
|
|||||||
@@ -329,7 +329,7 @@ Files KlipperPrinter::get_files()
|
|||||||
{
|
{
|
||||||
Files files_result = {0};
|
Files files_result = {0};
|
||||||
HTTPClient client;
|
HTTPClient client;
|
||||||
LOG_F(("Heap space pre-file-parse: %d bytes\n", esp_get_free_heap_size()))
|
LOG_F(("Heap space pre-file-parse: %d bytes\n", esp_get_free_heap_size()));
|
||||||
std::list<FileSystemFile> files;
|
std::list<FileSystemFile> files;
|
||||||
|
|
||||||
auto timer_request = millis();
|
auto timer_request = millis();
|
||||||
|
|||||||
@@ -18,11 +18,16 @@ void setup() {
|
|||||||
screen_setup();
|
screen_setup();
|
||||||
lv_setup();
|
lv_setup();
|
||||||
LOG_LN("Screen init done");
|
LOG_LN("Screen init done");
|
||||||
|
LOG_F(("Free heap: %d bytes\n", esp_get_free_heap_size()));
|
||||||
|
|
||||||
wifi_init();
|
wifi_init();
|
||||||
|
LOG_F(("Free heap after wifi setup: %d bytes\n", esp_get_free_heap_size()));
|
||||||
ota_init();
|
ota_init();
|
||||||
|
LOG_F(("Free heap after ota setup: %d bytes\n", esp_get_free_heap_size()));
|
||||||
ip_init();
|
ip_init();
|
||||||
|
LOG_F(("Free heap after ip setup: %d bytes\n", esp_get_free_heap_size()));
|
||||||
data_setup();
|
data_setup();
|
||||||
|
LOG_F(("Free heap after data setup: %d bytes\n", esp_get_free_heap_size()));
|
||||||
|
|
||||||
nav_style_setup();
|
nav_style_setup();
|
||||||
main_ui_setup();
|
main_ui_setup();
|
||||||
|
|||||||
Reference in New Issue
Block a user