Skip to content

Commit

Permalink
Fix Klipper params being non-blocking
Browse files Browse the repository at this point in the history
  • Loading branch information
suchmememanyskill committed Jan 11, 2025
1 parent f8ed3af commit 0992f46
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 46 deletions.
2 changes: 1 addition & 1 deletion CYD-Klipper/src/core/current_printer.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "data_setup.h"
#include "current_printer.h"
#include "semaphore.h"

bool current_printer_move_printer(const char* axis, float amount, bool relative)
{
Expand Down
25 changes: 1 addition & 24 deletions CYD-Klipper/src/core/data_setup.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

#include "data_setup.h"
#include "semaphore.h"
#include <esp_task_wdt.h>
#include <UrlEncode.h>
#include "printer_integration.hpp"
Expand All @@ -8,32 +9,8 @@
#include "bambu/bambu_printer_integration.hpp"
#include "octoprint/octoprint_printer_integration.hpp"

SemaphoreHandle_t freezeRenderThreadSemaphore, freezeRequestThreadSemaphore;
const long data_update_interval = 780;

void semaphore_init(){
freezeRenderThreadSemaphore = xSemaphoreCreateMutex();
freezeRequestThreadSemaphore = xSemaphoreCreateMutex();
xSemaphoreGive(freezeRenderThreadSemaphore);
xSemaphoreGive(freezeRequestThreadSemaphore);
}

void freeze_request_thread(){
xSemaphoreTake(freezeRequestThreadSemaphore, portMAX_DELAY);
}

void unfreeze_request_thread(){
xSemaphoreGive(freezeRequestThreadSemaphore);
}

void freeze_render_thread(){
xSemaphoreTake(freezeRenderThreadSemaphore, portMAX_DELAY);
}

void unfreeze_render_thread(){
xSemaphoreGive(freezeRenderThreadSemaphore);
}

void fetch_printer_data()
{
freeze_request_thread();
Expand Down
5 changes: 1 addition & 4 deletions CYD-Klipper/src/core/data_setup.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#pragma once

void data_loop();
void data_setup();

void freeze_request_thread();
void unfreeze_request_thread();
void data_setup();
48 changes: 31 additions & 17 deletions CYD-Klipper/src/core/klipper/klipper_printer_panels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@
#include "../../ui/ui_utils.h"
#include "../common/constants.h"
#include <stdio.h>
#include "../semaphore.h"

bool send_gcode_blocking(const char *gcode, bool wait = true)
{
freeze_request_thread();
KlipperPrinter* printer = (KlipperPrinter*)get_current_printer(); // TODO: pass by ref
bool result = printer->send_gcode(gcode);
unfreeze_request_thread();

return result;
}

bool move_printer_blocking(const char* axis, float amount, bool relative)
{
freeze_request_thread();
KlipperPrinter* printer = (KlipperPrinter*)get_current_printer(); // TODO: pass by ref
bool result = printer->move_printer(axis, amount, relative);
unfreeze_request_thread();

return result;
}

static void set_fan_speed_text(lv_event_t * e) {
lv_obj_t * label = lv_event_get_target(e);
Expand All @@ -14,10 +35,9 @@ static void set_fan_speed_text(lv_event_t * e) {
static void set_fan_speed(lv_event_t * e){
int speed = (int)lv_event_get_user_data(e);
int actual_speed = fan_percent_to_byte(speed);
KlipperPrinter* printer = (KlipperPrinter*)get_current_printer(); // TODO: pass by ref
char gcode[16];
sprintf(gcode, "M106 S%d", actual_speed);
printer->send_gcode(gcode);
send_gcode_blocking(gcode);
}

FAN_SPEED_COLUMN(set_fan_speed, klipper_fan_speed_columns)
Expand All @@ -40,24 +60,21 @@ static void set_zoffset_text_ex(lv_event_t * e) {

static void set_zoffset(lv_event_t * e){
char* offset = (char*)lv_event_get_user_data(e);
KlipperPrinter* printer = (KlipperPrinter*)get_current_printer(); // TODO: pass by ref

char gcode[48];
sprintf(gcode, "SET_GCODE_OFFSET Z_ADJUST=%s MOVE=1", offset);
printer->send_gcode(gcode);
send_gcode_blocking(gcode);
}

static void set_z(lv_event_t * e){
void* ptr = lv_event_get_user_data(e);
float value = *(float *)(&ptr);
KlipperPrinter* printer = (KlipperPrinter*)get_current_printer(); // TODO: pass by ref

if (value < 0) {
printer->send_gcode("SET_GCODE_OFFSET Z=0 MOVE=1");
send_gcode_blocking("SET_GCODE_OFFSET Z=0 MOVE=1");
return;
}

printer->move_printer("Z", value, false);
move_printer_blocking("Z", value, false);
}

const char* zoffsets[] = { "-0.01", "-0.025", "-0.05", "-0.2" };
Expand All @@ -80,20 +97,18 @@ static void set_speed_mult_text(lv_event_t * e){

static void set_speed_mult(lv_event_t * e){
int speed = (int)lv_event_get_user_data(e);
KlipperPrinter* printer = (KlipperPrinter*)get_current_printer(); // TODO: pass by ref
char gcode[16];
sprintf(gcode, "M220 S%d", speed);
printer->send_gcode(gcode);
send_gcode_blocking(gcode);
}

static void set_speed_mult_offset(lv_event_t * e){
int speed = (int)lv_event_get_user_data(e);
KlipperPrinter* printer = (KlipperPrinter*)get_current_printer(); // TODO: pass by ref
float result = get_current_printer_data()->speed_mult * 100 + speed;
get_current_printer_data()->speed_mult = result / 100;
char gcode[16];
sprintf(gcode, "M220 S%.0f", result);
printer->send_gcode(gcode);
send_gcode_blocking(gcode);
}

const char* speed_presets[] = { "50%", "100%", "150%", "200%" };
Expand All @@ -118,20 +133,19 @@ static void set_extrude_mult_text(lv_event_t * e){

static void set_extrude_mult(lv_event_t * e){
int speed = (int)lv_event_get_user_data(e);
KlipperPrinter* printer = (KlipperPrinter*)get_current_printer(); // TODO: pass by ref
char gcode[16];
sprintf(gcode, "M221 S%d", speed);
printer->send_gcode(gcode);
send_gcode_blocking(gcode);
}

static void set_extrude_mult_offset(lv_event_t * e){
int speed = (int)lv_event_get_user_data(e);
KlipperPrinter* printer = (KlipperPrinter*)get_current_printer(); // TODO: pass by ref
float result = get_current_printer_data()->extrude_mult * 100 + speed;
get_current_printer_data()->extrude_mult = result / 100;
char gcode[16];
sprintf(gcode, "M221 S%.0f", result);
printer->send_gcode(gcode);

send_gcode_blocking(gcode);
}

const char* extrude_presets[] = { "95%", "100%", "105%", "110%" };
Expand All @@ -145,7 +159,7 @@ lv_button_column_t extrude_mult_columns[] = {
};

static void open_fan_speed_panel(lv_event_t * e){
lv_create_fullscreen_button_matrix_popup(lv_scr_act(), set_fan_speed_text, klipper_fan_speed_columns, 2);
lv_create_fullscreen_button_matrix_popup(lv_scr_act(), set_fan_speed_text, klipper_fan_speed_columns, 3);
}

static void open_zoffset_panel(lv_event_t * e){
Expand Down
28 changes: 28 additions & 0 deletions CYD-Klipper/src/core/semaphore.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "semaphore.h"
#include <UrlEncode.h>
#include <esp_task_wdt.h>

SemaphoreHandle_t freezeRenderThreadSemaphore, freezeRequestThreadSemaphore;

void semaphore_init(){
freezeRenderThreadSemaphore = xSemaphoreCreateMutex();
freezeRequestThreadSemaphore = xSemaphoreCreateMutex();
xSemaphoreGive(freezeRenderThreadSemaphore);
xSemaphoreGive(freezeRequestThreadSemaphore);
}

void freeze_request_thread(){
xSemaphoreTake(freezeRequestThreadSemaphore, portMAX_DELAY);
}

void unfreeze_request_thread(){
xSemaphoreGive(freezeRequestThreadSemaphore);
}

void freeze_render_thread(){
xSemaphoreTake(freezeRenderThreadSemaphore, portMAX_DELAY);
}

void unfreeze_render_thread(){
xSemaphoreGive(freezeRenderThreadSemaphore);
}
11 changes: 11 additions & 0 deletions CYD-Klipper/src/core/semaphore.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

void semaphore_init();

void freeze_request_thread();
void unfreeze_request_thread();

// Don't use unless you want trouble
void freeze_render_thread();
// Don't use unless you want trouble
void unfreeze_render_thread();
1 change: 1 addition & 0 deletions CYD-Klipper/src/ui/macros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <Esp.h>
#include "../core/current_printer.h"
#include "../core/data_setup.h"
#include "../core/semaphore.h"

typedef struct {
const char* power_device_name;
Expand Down
1 change: 1 addition & 0 deletions CYD-Klipper/src/ui/ota_setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "../core/data_setup.h"
#include "../conf/global_config.h"
#include "ota_setup.h"
#include "../core/semaphore.h"

//const char *ota_url = "https://gist.githubusercontent.com/suchmememanyskill/ece418fe199e155340de6c224a0badf2/raw/0d6762d68bc807cbecc71e40d55b76692397a7b3/update.json"; // Test url
const char *ota_url = "https://suchmememanyskill.github.io/CYD-Klipper/OTA.json"; // Prod url
Expand Down
1 change: 1 addition & 0 deletions CYD-Klipper/src/ui/wifi_setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "../core/lv_setup.h"
#include "serial/serial_console.h"
#include "panels/panel.h"
#include "../core/semaphore.h"

void wifi_init_inner();
void wifi_pass_entry(const char* ssid);
Expand Down

0 comments on commit 0992f46

Please sign in to comment.