mirror of
https://github.com/suchmememanyskill/CYD-Klipper.git
synced 2026-03-21 13:43:25 +00:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ffdc8ae87e | ||
|
|
7c786d1e6b | ||
|
|
34c6a5e031 | ||
|
|
7a430f81c5 | ||
|
|
230884c2cc | ||
|
|
f2d232d9eb | ||
|
|
1e3f0ab637 | ||
|
|
e15c7e37ff |
@@ -12,6 +12,34 @@ const char *printer_state_messages[] = {
|
||||
"Printing"};
|
||||
|
||||
Printer printer = {0};
|
||||
int klipper_request_consecutive_fail_count = 0;
|
||||
char filename_buff[512] = {0};
|
||||
SemaphoreHandle_t freezeRenderThreadSemaphore, freezeRequestThreadSemaphore;
|
||||
long last_data_update = 0;
|
||||
const long data_update_interval = 800;
|
||||
|
||||
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 send_gcode(bool wait, const char *gcode)
|
||||
{
|
||||
@@ -35,10 +63,10 @@ void send_gcode(bool wait, const char *gcode)
|
||||
}
|
||||
}
|
||||
|
||||
char filename_buff[512] = {0};
|
||||
|
||||
void fetch_printer_data()
|
||||
{
|
||||
bool frozen = true;
|
||||
freeze_request_thread();
|
||||
char buff[256] = {};
|
||||
sprintf(buff, "http://%s:%d/printer/objects/query?extruder&heater_bed&toolhead&gcode_move&virtual_sdcard&print_stats&webhooks", global_config.klipperHost, global_config.klipperPort);
|
||||
HTTPClient client;
|
||||
@@ -46,6 +74,7 @@ void fetch_printer_data()
|
||||
int httpCode = client.GET();
|
||||
if (httpCode == 200)
|
||||
{
|
||||
klipper_request_consecutive_fail_count = 0;
|
||||
String payload = client.getString();
|
||||
DynamicJsonDocument doc(4096);
|
||||
deserializeJson(doc, payload);
|
||||
@@ -53,6 +82,10 @@ void fetch_printer_data()
|
||||
bool emit_state_update = false;
|
||||
int printer_state = printer.state;
|
||||
|
||||
unfreeze_request_thread();
|
||||
frozen = false;
|
||||
freeze_render_thread();
|
||||
|
||||
if (status.containsKey("webhooks"))
|
||||
{
|
||||
const char *state = status["webhooks"]["state"];
|
||||
@@ -156,29 +189,46 @@ void fetch_printer_data()
|
||||
printer.state = printer_state;
|
||||
lv_msg_send(DATA_PRINTER_STATE, &printer);
|
||||
}
|
||||
|
||||
unfreeze_render_thread();
|
||||
}
|
||||
else
|
||||
{
|
||||
klipper_request_consecutive_fail_count++;
|
||||
Serial.printf("Failed to fetch printer data: %d\n", httpCode);
|
||||
}
|
||||
}
|
||||
|
||||
long last_data_update = 0;
|
||||
const long data_update_interval = 1500;
|
||||
if (frozen)
|
||||
unfreeze_request_thread();
|
||||
}
|
||||
|
||||
void data_loop()
|
||||
{
|
||||
if (millis() - last_data_update < data_update_interval)
|
||||
return;
|
||||
|
||||
last_data_update = millis();
|
||||
|
||||
fetch_printer_data();
|
||||
// Causes other threads that are trying to lock the thread to actually lock it
|
||||
unfreeze_render_thread();
|
||||
delay(1);
|
||||
freeze_render_thread();
|
||||
}
|
||||
|
||||
void data_loop_background(void * param){
|
||||
while (true){
|
||||
delay(100);
|
||||
if (millis() - last_data_update < data_update_interval)
|
||||
continue;
|
||||
|
||||
fetch_printer_data();
|
||||
last_data_update = millis();
|
||||
}
|
||||
}
|
||||
|
||||
TaskHandle_t background_loop;
|
||||
|
||||
void data_setup()
|
||||
{
|
||||
semaphore_init();
|
||||
printer.print_filename = filename_buff;
|
||||
fetch_printer_data();
|
||||
macros_query_setup();
|
||||
}
|
||||
freeze_render_thread();
|
||||
xTaskCreatePinnedToCore(data_loop_background, "data_loop_background", 5000, NULL, 1, &background_loop, 0);
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ typedef struct _Printer {
|
||||
} Printer;
|
||||
|
||||
extern Printer printer;
|
||||
extern int klipper_request_consecutive_fail_count;
|
||||
|
||||
#define DATA_PRINTER_STATE 1
|
||||
#define DATA_PRINTER_DATA 2
|
||||
@@ -35,4 +36,7 @@ extern Printer printer;
|
||||
|
||||
void data_loop();
|
||||
void data_setup();
|
||||
void send_gcode(bool wait, const char* gcode);
|
||||
void send_gcode(bool wait, const char* gcode);
|
||||
|
||||
void freeze_request_thread();
|
||||
void unfreeze_request_thread();
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <list>
|
||||
#include "files_query.h"
|
||||
#include "../conf/global_config.h"
|
||||
#include "data_setup.h"
|
||||
#include <HTTPClient.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <HardwareSerial.h>
|
||||
@@ -9,6 +10,8 @@
|
||||
FILESYSTEM_FILE* last_query = NULL;
|
||||
|
||||
FILESYSTEM_FILE* get_files(){
|
||||
freeze_request_thread();
|
||||
|
||||
if (last_query != NULL){
|
||||
FILESYSTEM_FILE* current = last_query;
|
||||
|
||||
@@ -59,5 +62,6 @@ FILESYSTEM_FILE* get_files(){
|
||||
result += 1;
|
||||
}
|
||||
|
||||
unfreeze_request_thread();
|
||||
return last_query;
|
||||
}
|
||||
@@ -100,12 +100,20 @@ void screen_timer_wake()
|
||||
lv_timer_reset(screenSleepTimer);
|
||||
isScreenInSleep = false;
|
||||
set_screen_brightness();
|
||||
|
||||
// Reset cpu freq
|
||||
setCpuFrequencyMhz(CPU_FREQ_HIGH);
|
||||
Serial.printf("CPU Speed: %d MHz\n", ESP.getCpuFreqMHz());
|
||||
}
|
||||
|
||||
void screen_timer_sleep(lv_timer_t *timer)
|
||||
{
|
||||
screen_setBrightness(0);
|
||||
isScreenInSleep = true;
|
||||
|
||||
// Screen is off, no need to make the cpu run fast, the user won't notice ;)
|
||||
setCpuFrequencyMhz(CPU_FREQ_LOW);
|
||||
Serial.printf("CPU Speed: %d MHz\n", ESP.getCpuFreqMHz());
|
||||
}
|
||||
|
||||
void screen_timer_setup()
|
||||
|
||||
@@ -4,6 +4,9 @@
|
||||
#ifndef _SCREEN_DRIVER_INIT
|
||||
#define _SCREEN_DRIVER_INIT
|
||||
|
||||
#define CPU_FREQ_HIGH 240
|
||||
#define CPU_FREQ_LOW 80
|
||||
|
||||
#include <XPT2046_Touchscreen.h>
|
||||
#include <TFT_eSPI.h>
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ void setup() {
|
||||
Serial.println("Screen init done");
|
||||
|
||||
wifi_init();
|
||||
ip_setup();
|
||||
ip_init();
|
||||
data_setup();
|
||||
|
||||
nav_style_setup();
|
||||
@@ -34,6 +34,7 @@ void setup() {
|
||||
|
||||
void loop(){
|
||||
wifi_ok();
|
||||
ip_ok();
|
||||
data_loop();
|
||||
lv_timer_handler();
|
||||
lv_task_handler();
|
||||
|
||||
@@ -3,18 +3,22 @@
|
||||
#include "lvgl.h"
|
||||
#include <TFT_eSPI.h>
|
||||
#include <HTTPClient.h>
|
||||
#include "core/data_setup.h"
|
||||
|
||||
bool connect_ok = false;
|
||||
lv_obj_t * ipEntry;
|
||||
lv_obj_t * portEntry;
|
||||
lv_obj_t * label = NULL;
|
||||
|
||||
void ip_init_inner();
|
||||
|
||||
bool verify_ip(){
|
||||
HTTPClient client;
|
||||
String url = "http://" + String(global_config.klipperHost) + ":" + String(global_config.klipperPort) + "/printer/info";
|
||||
int httpCode;
|
||||
try {
|
||||
Serial.println(url);
|
||||
client.setTimeout(500);
|
||||
client.begin(url.c_str());
|
||||
httpCode = client.GET();
|
||||
return httpCode == 200;
|
||||
@@ -25,17 +29,6 @@ bool verify_ip(){
|
||||
}
|
||||
}
|
||||
|
||||
bool retry_ip_verify(){
|
||||
for (int i = 0; i < 5; i++){
|
||||
if (verify_ip()){
|
||||
return true;
|
||||
}
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static void ta_event_cb(lv_event_t * e) {
|
||||
lv_event_code_t code = lv_event_get_code(e);
|
||||
lv_obj_t * ta = lv_event_get_target(e);
|
||||
@@ -53,8 +46,8 @@ static void ta_event_cb(lv_event_t * e) {
|
||||
{
|
||||
strcpy(global_config.klipperHost, lv_textarea_get_text(ipEntry));
|
||||
global_config.klipperPort = atoi(lv_textarea_get_text(portEntry));
|
||||
bool result = verify_ip();
|
||||
if (result)
|
||||
|
||||
if (verify_ip())
|
||||
{
|
||||
global_config.ipConfigured = true;
|
||||
WriteGlobalConfig();
|
||||
@@ -67,9 +60,33 @@ static void ta_event_cb(lv_event_t * e) {
|
||||
}
|
||||
}
|
||||
|
||||
void ip_setup_inner(){
|
||||
static void reset_btn_event_handler(lv_event_t * e){
|
||||
lv_event_code_t code = lv_event_get_code(e);
|
||||
|
||||
if(code == LV_EVENT_CLICKED) {
|
||||
global_config.ipConfigured = false;
|
||||
ip_init_inner();
|
||||
}
|
||||
}
|
||||
|
||||
void ip_init_inner(){
|
||||
lv_obj_clean(lv_scr_act());
|
||||
|
||||
if (global_config.ipConfigured) {
|
||||
label = lv_label_create(lv_scr_act());
|
||||
lv_label_set_text(label, "Connecting to Klipper");
|
||||
lv_obj_align(label, LV_ALIGN_CENTER, 0, 0);
|
||||
|
||||
lv_obj_t * resetBtn = lv_btn_create(lv_scr_act());
|
||||
lv_obj_add_event_cb(resetBtn, reset_btn_event_handler, LV_EVENT_ALL, NULL);
|
||||
lv_obj_align(resetBtn, LV_ALIGN_CENTER, 0, 40);
|
||||
|
||||
lv_obj_t * btnLabel = lv_label_create(resetBtn);
|
||||
lv_label_set_text(btnLabel, "Reset");
|
||||
lv_obj_center(btnLabel);
|
||||
return;
|
||||
}
|
||||
|
||||
lv_obj_t * keyboard = lv_keyboard_create(lv_scr_act());
|
||||
label = lv_label_create(lv_scr_act());
|
||||
lv_label_set_text(label, "Enter Klipper IP and Port");
|
||||
@@ -95,18 +112,37 @@ void ip_setup_inner(){
|
||||
lv_keyboard_set_textarea(keyboard, ipEntry);
|
||||
}
|
||||
|
||||
void ip_setup(){
|
||||
long last_data_update_ip = -10000;
|
||||
const long data_update_interval_ip = 10000;
|
||||
int retry_count = 0;
|
||||
|
||||
void ip_init(){
|
||||
connect_ok = false;
|
||||
retry_count = 0;
|
||||
|
||||
if (global_config.ipConfigured && retry_ip_verify()){
|
||||
return;
|
||||
}
|
||||
|
||||
ip_setup_inner();
|
||||
ip_init_inner();
|
||||
|
||||
while (!connect_ok)
|
||||
{
|
||||
lv_timer_handler();
|
||||
lv_task_handler();
|
||||
|
||||
if (!connect_ok && global_config.ipConfigured && (millis() - last_data_update_ip) > data_update_interval_ip){
|
||||
connect_ok = verify_ip();
|
||||
last_data_update_ip = millis();
|
||||
retry_count++;
|
||||
String retry_count_text = "Connecting to Klipper (Try " + String(retry_count + 1) + ")";
|
||||
lv_label_set_text(label, retry_count_text.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ip_ok(){
|
||||
if (klipper_request_consecutive_fail_count > 5){
|
||||
freeze_request_thread();
|
||||
ip_init();
|
||||
unfreeze_request_thread();
|
||||
klipper_request_consecutive_fail_count = 0;
|
||||
lv_msg_send(DATA_PRINTER_STATE, &printer);
|
||||
}
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
void ip_setup();
|
||||
void ip_init();
|
||||
void ip_ok();
|
||||
@@ -77,12 +77,11 @@ static void wifi_btn_event_handler(lv_event_t * e){
|
||||
|
||||
void wifi_init_inner(){
|
||||
WiFi.disconnect();
|
||||
lv_obj_clean(lv_scr_act());
|
||||
|
||||
if (global_config.wifiConfigured){
|
||||
WiFi.begin(global_config.wifiSSID, global_config.wifiPassword);
|
||||
|
||||
lv_obj_clean(lv_scr_act());
|
||||
|
||||
lv_obj_t * label = lv_label_create(lv_scr_act());
|
||||
lv_label_set_text(label, "Connecting to WiFi");
|
||||
lv_obj_align(label, LV_ALIGN_CENTER, 0, 0);
|
||||
@@ -98,8 +97,6 @@ void wifi_init_inner(){
|
||||
return;
|
||||
}
|
||||
|
||||
lv_obj_clean(lv_scr_act());
|
||||
|
||||
lv_obj_t * label = lv_label_create(lv_scr_act());
|
||||
lv_label_set_text(label, "Scanning for networks...");
|
||||
lv_obj_align(label, LV_ALIGN_CENTER, 0, 0);
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
[](https://ko-fi.com/suchmememanyskill)
|
||||
|
||||
# CYD-Klipper
|
||||
An implementation of a Klipper status display on an ESP32 + screen. Uses Moonraker to fetch data.
|
||||
An implementation of a wireless Klipper status display on an ESP32 + screen. Uses Moonraker to fetch data.
|
||||
|
||||
A simple and cheap solution to use a dedicated screen with Klipper, a 3d printing Firmware.
|
||||
|
||||
@@ -19,6 +19,7 @@ A ESP32-2432S028R is required to run this project. You can find out where to buy
|
||||
- Move the printer
|
||||
- Manage temperature
|
||||
- Extrude/Retract filament
|
||||
- Execute predefined gcode macros
|
||||
|
||||
### Install
|
||||
|
||||
|
||||
Reference in New Issue
Block a user