Add octoprint panels

This commit is contained in:
Sims
2024-12-15 00:40:11 +01:00
parent 96f8695b0e
commit 68b68af715
3 changed files with 120 additions and 6 deletions

View File

@@ -26,7 +26,7 @@ void configure_http_client(HTTPClient &client, String url_part, bool stream, int
} }
} }
bool OctoPrinter::get_request(const char* endpoint, int timeout_ms, bool stream) bool OctoPrinter::get_request(const char* endpoint, int timeout_ms)
{ {
HTTPClient client; HTTPClient client;
@@ -35,12 +35,12 @@ bool OctoPrinter::get_request(const char* endpoint, int timeout_ms, bool stream)
timeout_ms = 500; timeout_ms = 500;
} }
configure_http_client(client, endpoint, stream, timeout_ms, printer_config); configure_http_client(client, endpoint, false, timeout_ms, printer_config);
int result = client.GET(); int result = client.GET();
return result >= 200 && result < 300; return result >= 200 && result < 300;
} }
bool OctoPrinter::post_request(const char* endpoint, const char* body, int timeout_ms, bool stream) bool OctoPrinter::post_request(const char* endpoint, const char* body, int timeout_ms)
{ {
HTTPClient client; HTTPClient client;
@@ -49,7 +49,7 @@ bool OctoPrinter::post_request(const char* endpoint, const char* body, int timeo
timeout_ms = 500; timeout_ms = 500;
} }
configure_http_client(client, endpoint, stream, timeout_ms, printer_config); configure_http_client(client, endpoint, false, timeout_ms, printer_config);
if (body[0] == '{' || body[0] == '[') if (body[0] == '{' || body[0] == '[')
{ {

View File

@@ -19,8 +19,8 @@ class OctoPrinter : public BasePrinter
void parse_error(JsonDocument& in); void parse_error(JsonDocument& in);
void OctoPrinter::parse_file_list(JsonDocument &in, std::list<OctoFileSystemFile> &files, int fetch_limit); void OctoPrinter::parse_file_list(JsonDocument &in, std::list<OctoFileSystemFile> &files, int fetch_limit);
bool get_request(const char* endpoint, int timeout_ms = 1000, bool stream = true); bool get_request(const char* endpoint, int timeout_ms = 1000);
bool post_request(const char* endpoint, const char* body, int timeout_ms = 1000, bool stream = false); void init_ui_panels();
public: public:
OctoPrinter(int index) : BasePrinter(index) OctoPrinter(int index) : BasePrinter(index)
@@ -39,8 +39,11 @@ class OctoPrinter : public BasePrinter
| PrinterTemperatureDeviceNozzle1; | PrinterTemperatureDeviceNozzle1;
printer_data.error_screen_features = PrinterFeatureRetryError; printer_data.error_screen_features = PrinterFeatureRetryError;
init_ui_panels();
} }
bool post_request(const char* endpoint, const char* body, int timeout_ms = 1000);
bool move_printer(const char* axis, float amount, bool relative); bool move_printer(const char* axis, float amount, bool relative);
bool execute_feature(PrinterFeatures feature); bool execute_feature(PrinterFeatures feature);
bool connect(); bool connect();

View File

@@ -0,0 +1,111 @@
#include "octoprint_printer_integration.hpp"
#include "lvgl.h"
#include "../../ui/ui_utils.h"
#include <stdio.h>
const char* COMMAND_EXTRUDE_MULT = "{\"command\":\"flowrate\",\"factor\":%d}";
#define OCTO_TIMEOUT_POPUP_MESSAGES 4000
static void set_fan_speed_text(lv_event_t * e)
{
lv_obj_t * label = lv_event_get_target(e);
lv_label_set_text(label, "Fan");
}
static void set_speed_mult_text(lv_event_t * e)
{
lv_obj_t * label = lv_event_get_target(e);
lv_label_set_text(label, "Speed");
}
static void set_extruder_mult_text(lv_event_t * e)
{
lv_obj_t * label = lv_event_get_target(e);
lv_label_set_text(label, "Flowrate");
}
bool get_range(lv_event_t * e, int min, int max, int* out)
{
char buff[64];
lv_obj_t * ta = lv_event_get_target(e);
lv_obj_t * kb = (lv_obj_t *)lv_event_get_user_data(e);
const char * txt = lv_textarea_get_text(ta);
if (txt == NULL || *txt == '\0')
{
return false;
}
int parsed_input = atoi(txt);
if (parsed_input < min || parsed_input > max)
{
sprintf(buff, "Value out of range (%d -> %d)", min, max);
lv_create_popup_message(buff, OCTO_TIMEOUT_POPUP_MESSAGES);
return false;
}
*out = parsed_input;
return true;
}
static void set_fan_speed(lv_event_t * e)
{
int fan_speed = 0;
if (get_range(e, 0, 100, &fan_speed))
{
int actual_fan_speed = fan_speed * 255 / 100;
char buff[16];
sprintf(buff, "M106 S%d", actual_fan_speed);
((OctoPrinter*)get_current_printer())->send_gcode(buff);
}
}
static void open_fan_speed_keypad(lv_event_t * e)
{
lv_create_keyboard_text_entry(set_fan_speed, "New fan speed %", LV_KEYBOARD_MODE_NUMBER);
}
static void set_speed_mult(lv_event_t * e)
{
int speed_mult = 0;
if (get_range(e, 50, 300, &speed_mult))
{
char buff[16];
sprintf(buff, "M220 S%d", speed_mult);
((OctoPrinter*)get_current_printer())->send_gcode(buff);
}
}
static void open_speed_mult_keypad(lv_event_t * e)
{
lv_create_keyboard_text_entry(set_speed_mult, "New speed multiplier %", LV_KEYBOARD_MODE_NUMBER);
}
static void set_extrude_mult(lv_event_t * e)
{
int extrude_mult = 0;
if (get_range(e, 75, 125, &extrude_mult))
{
char buff[64];
sprintf(buff, COMMAND_EXTRUDE_MULT, extrude_mult);
((OctoPrinter*)get_current_printer())->post_request("/api/printer/tool", buff);
}
}
static void open_extrude_mult_keypad(lv_event_t * e)
{
lv_create_keyboard_text_entry(set_extrude_mult, "New extrude multiplier %", LV_KEYBOARD_MODE_NUMBER);
}
static PrinterUiPanel klipper_ui_panels[4] {
{ .set_label = (void*)set_fan_speed_text, .open_panel = (void*)open_fan_speed_keypad },
{ .set_label = (void*)set_speed_mult_text, .open_panel = (void*)open_speed_mult_keypad },
{ .set_label = (void*)set_extruder_mult_text, .open_panel = (void*)open_extrude_mult_keypad },
};
void OctoPrinter::init_ui_panels()
{
custom_menus_count = 4;
custom_menus = klipper_ui_panels;
}