From bf892d685533683a1cc03219ed7a0e6b735ef083 Mon Sep 17 00:00:00 2001 From: suchmememanyskill <38142618+suchmememanyskill@users.noreply.github.com> Date: Sun, 12 Nov 2023 00:18:54 +0100 Subject: [PATCH] Connect to Klipper (although websockets are broken :( ) --- CYD-Klipper-Display/platformio.ini | 51 +++++---- CYD-Klipper-Display/src/conf/global_config.h | 6 +- .../src/core/websocket_setup.cpp | 41 +++++++ .../src/core/websocket_setup.h | 2 + CYD-Klipper-Display/src/main.cpp | 12 +++ CYD-Klipper-Display/src/ui/ip_setup.cpp | 101 ++++++++++++++++++ CYD-Klipper-Display/src/ui/ip_setup.h | 1 + CYD-Klipper-Display/src/ui/wifi_setup.cpp | 7 ++ CYD-Klipper-Display/src/ui/wifi_setup.h | 3 +- 9 files changed, 195 insertions(+), 29 deletions(-) create mode 100644 CYD-Klipper-Display/src/core/websocket_setup.cpp create mode 100644 CYD-Klipper-Display/src/core/websocket_setup.h create mode 100644 CYD-Klipper-Display/src/ui/ip_setup.cpp create mode 100644 CYD-Klipper-Display/src/ui/ip_setup.h diff --git a/CYD-Klipper-Display/platformio.ini b/CYD-Klipper-Display/platformio.ini index f3170ae..87562af 100644 --- a/CYD-Klipper-Display/platformio.ini +++ b/CYD-Klipper-Display/platformio.ini @@ -14,34 +14,31 @@ board = esp32dev framework = arduino monitor_speed = 115200 lib_deps = - SPI - lvgl/lvgl@^8.3.9 - https://github.com/Bodmer/TFT_eSPI.git - https://github.com/PaulStoffregen/XPT2046_Touchscreen.git + SPI + lvgl/lvgl@^8.3.9 + https://github.com/Bodmer/TFT_eSPI.git + https://github.com/PaulStoffregen/XPT2046_Touchscreen.git + bblanchon/ArduinoJson@^6.21.3 + links2004/WebSockets@^2.4.1 build_flags = - -DLV_CONF_PATH="../../../../src/conf/lv_conf.h" - -DUSER_SETUP_LOADED=1 - #-DILI9341_DRIVER=1 - -DILI9341_2_DRIVER=1 - -DTFT_INVERSION_ON=1 - -DTFT_WIDTH=240 - -DTFT_HEIGHT=320 - -DTFT_BL=21 - -DTFT_BACKLIGHT_ON=HIGH - -DTFT_MISO=12 - -DTFT_MOSI=13 - -DTFT_SCLK=14 - -DTFT_CS=15 - -DTFT_DC=2 - -DTFT_RST=-1 + -DLV_CONF_PATH="../../../../src/conf/lv_conf.h" + -DUSER_SETUP_LOADED=1 + -DILI9341_2_DRIVER=1 + -DTFT_INVERSION_ON=1 + -DTFT_WIDTH=240 + -DTFT_HEIGHT=320 + -DTFT_BL=21 + -DTFT_BACKLIGHT_ON=HIGH + -DTFT_MISO=12 + -DTFT_MOSI=13 + -DTFT_SCLK=14 + -DTFT_CS=15 + -DTFT_DC=2 + -DTFT_RST=-1 -DLOAD_GCLD=1 -DLOAD_FONT2=1 - #-DLOAD_FONT4=1 - #-DLOAD_FONT6=1 - #-DLOAD_FONT7=1 - #-DLOAD_FONT8=1 -DLOAD_GFXFF=1 - -DSMOOTH_FONT=1 - -DSPI_FREQUENCY=55000000 - -DSPI_READ_FREQUENCY=20000000 - -DSPI_TOUCH_FREQUENCY=2500000 \ No newline at end of file + -DSMOOTH_FONT=1 + -DSPI_FREQUENCY=55000000 + -DSPI_READ_FREQUENCY=20000000 + -DSPI_TOUCH_FREQUENCY=2500000 diff --git a/CYD-Klipper-Display/src/conf/global_config.h b/CYD-Klipper-Display/src/conf/global_config.h index ed6b7ba..eeb1985 100644 --- a/CYD-Klipper-Display/src/conf/global_config.h +++ b/CYD-Klipper-Display/src/conf/global_config.h @@ -1,7 +1,7 @@ #ifndef _GLOBAL_CONFIG_INIT #define _GLOBAL_CONFIG_INIT -#define CONFIG_VERSION 1 +#define CONFIG_VERSION 80 typedef struct _GLOBAL_CONFIG { unsigned char version; @@ -10,6 +10,7 @@ typedef struct _GLOBAL_CONFIG { struct { bool screenCalibrated : 1; bool wifiConfigured : 1; + bool ipConfigured : 1; }; }; float screenCalXOffset; @@ -19,6 +20,9 @@ typedef struct _GLOBAL_CONFIG { char wifiSSID[32]; char wifiPassword[64]; + + char klipperHost[64]; + unsigned short klipperPort; } GLOBAL_CONFIG; extern GLOBAL_CONFIG global_config; diff --git a/CYD-Klipper-Display/src/core/websocket_setup.cpp b/CYD-Klipper-Display/src/core/websocket_setup.cpp new file mode 100644 index 0000000..184451b --- /dev/null +++ b/CYD-Klipper-Display/src/core/websocket_setup.cpp @@ -0,0 +1,41 @@ +#include +#include "../conf/global_config.h" + +WebSocketsClient webSocket; + +void websocket_event(WStype_t type, uint8_t * payload, size_t length) { + switch(type) { + case WStype_DISCONNECTED: + Serial.printf("[WSc] Disconnected!\n"); + break; + case WStype_CONNECTED: + Serial.printf("[WSc] Connected to url: %s\n", payload); + + // send message to server when Connected + webSocket.sendTXT("Connected"); + break; + case WStype_TEXT: + Serial.printf("[WSc] get text: %s\n", payload); + + // send message to server + // webSocket.sendTXT("message here"); + break; + case WStype_BIN: + case WStype_ERROR: + case WStype_FRAGMENT_TEXT_START: + case WStype_FRAGMENT_BIN_START: + case WStype_FRAGMENT: + case WStype_FRAGMENT_FIN: + break; + } +} + +void websocket_process(){ + webSocket.loop(); +} + +void websocket_setup(){ + webSocket.begin("192.168.0.122", global_config.klipperPort, "/websocket"); + webSocket.onEvent(websocket_event); + webSocket.setReconnectInterval(5000); +} \ No newline at end of file diff --git a/CYD-Klipper-Display/src/core/websocket_setup.h b/CYD-Klipper-Display/src/core/websocket_setup.h new file mode 100644 index 0000000..eee63c9 --- /dev/null +++ b/CYD-Klipper-Display/src/core/websocket_setup.h @@ -0,0 +1,2 @@ +void websocket_process(); +void websocket_setup(); \ No newline at end of file diff --git a/CYD-Klipper-Display/src/main.cpp b/CYD-Klipper-Display/src/main.cpp index c8edb60..9c0eb00 100644 --- a/CYD-Klipper-Display/src/main.cpp +++ b/CYD-Klipper-Display/src/main.cpp @@ -1,6 +1,8 @@ #include "conf/global_config.h" #include "conf/screen_driver.h" #include "ui/wifi_setup.h" +#include "ui/ip_setup.h" +#include "core/websocket_setup.h" #include "lvgl.h" static void event_handler(lv_event_t * e){ @@ -22,6 +24,8 @@ void setup() { Serial.println("Screen init done"); wifi_init(); + ip_setup(); + websocket_setup(); lv_obj_clean(lv_scr_act()); @@ -34,9 +38,17 @@ void setup() { label = lv_label_create(btn1); lv_label_set_text(label, "Reset Configuration"); lv_obj_center(label); + + lv_obj_t * slider = lv_slider_create(lv_scr_act()); + lv_obj_set_width(slider, 200); + lv_obj_align(slider, LV_ALIGN_CENTER, 0, 40); + lv_slider_set_range(slider, 0, 100); + lv_slider_set_value(slider, 50, LV_ANIM_OFF); } void loop(){ + wifi_ok(); + websocket_process(); lv_timer_handler(); lv_task_handler(); } \ No newline at end of file diff --git a/CYD-Klipper-Display/src/ui/ip_setup.cpp b/CYD-Klipper-Display/src/ui/ip_setup.cpp new file mode 100644 index 0000000..e5c60dd --- /dev/null +++ b/CYD-Klipper-Display/src/ui/ip_setup.cpp @@ -0,0 +1,101 @@ +#include "ip_setup.h" +#include "../conf/global_config.h" +#include "lvgl.h" +#include +#include + +bool connect_ok = false; +lv_obj_t * ipEntry; +lv_obj_t * portEntry; +lv_obj_t * label = NULL; + +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.begin(url.c_str()); + httpCode = client.GET(); + return httpCode == 200; + } + catch (...) { + Serial.println("Failed to connect"); + 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); + lv_obj_t * kb = (lv_obj_t *)lv_event_get_user_data(e); + + if(code == LV_EVENT_FOCUSED) { + lv_keyboard_set_textarea(kb, ta); + lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN); + } + else if(code == LV_EVENT_DEFOCUSED) { + lv_keyboard_set_textarea(kb, NULL); + lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN); + } + else if (code == LV_EVENT_READY) + { + strcpy(global_config.klipperHost, lv_textarea_get_text(ipEntry)); + global_config.klipperPort = atoi(lv_textarea_get_text(portEntry)); + bool result = verify_ip(); + if (result) + { + global_config.ipConfigured = true; + WriteGlobalConfig(); + connect_ok = true; + } + else + { + lv_label_set_text(label, "Failed to connect"); + } + } +} + +void ip_setup_inner(){ + lv_obj_clean(lv_scr_act()); + + 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"); + lv_obj_align(label, LV_ALIGN_TOP_LEFT, 10, 10 + 2); + + ipEntry = lv_textarea_create(lv_scr_act()); + lv_textarea_set_one_line(ipEntry, true); + lv_textarea_set_max_length(ipEntry, 57); + lv_textarea_set_text(ipEntry, ""); + lv_obj_align(ipEntry, LV_ALIGN_TOP_LEFT, 10, 40); + lv_obj_add_event_cb(ipEntry, ta_event_cb, LV_EVENT_ALL, keyboard); + lv_obj_set_size(ipEntry, TFT_HEIGHT - 20 - 100, 60); + + portEntry = lv_textarea_create(lv_scr_act()); + lv_textarea_set_one_line(portEntry, true); + lv_textarea_set_max_length(portEntry, 5); + lv_textarea_set_text(portEntry, "80"); + lv_obj_align(portEntry, LV_ALIGN_TOP_LEFT, TFT_HEIGHT - 20 - 80, 40); + lv_obj_add_event_cb(portEntry, ta_event_cb, LV_EVENT_ALL, keyboard); + lv_obj_set_size(portEntry, 90, 60); + + lv_keyboard_set_mode(keyboard, LV_KEYBOARD_MODE_NUMBER); + lv_keyboard_set_textarea(keyboard, ipEntry); +} + +void ip_setup(){ + connect_ok = false; + + if (global_config.ipConfigured && verify_ip()){ + return; + } + + ip_setup_inner(); + + while (!connect_ok) + { + lv_timer_handler(); + lv_task_handler(); + } +} \ No newline at end of file diff --git a/CYD-Klipper-Display/src/ui/ip_setup.h b/CYD-Klipper-Display/src/ui/ip_setup.h new file mode 100644 index 0000000..25ce25a --- /dev/null +++ b/CYD-Klipper-Display/src/ui/ip_setup.h @@ -0,0 +1 @@ +void ip_setup(); \ No newline at end of file diff --git a/CYD-Klipper-Display/src/ui/wifi_setup.cpp b/CYD-Klipper-Display/src/ui/wifi_setup.cpp index 0742104..ddaea2f 100644 --- a/CYD-Klipper-Display/src/ui/wifi_setup.cpp +++ b/CYD-Klipper-Display/src/ui/wifi_setup.cpp @@ -1,4 +1,5 @@ #include "lvgl.h" +#include "wifi_setup.h" #include "../conf/global_config.h" #include "WiFi.h" @@ -149,4 +150,10 @@ void wifi_init(){ lv_timer_handler(); lv_task_handler(); } +} + +void wifi_ok(){ + if (WiFi.status() != WL_CONNECTED){ + wifi_init(); + } } \ No newline at end of file diff --git a/CYD-Klipper-Display/src/ui/wifi_setup.h b/CYD-Klipper-Display/src/ui/wifi_setup.h index 8eda998..6b42223 100644 --- a/CYD-Klipper-Display/src/ui/wifi_setup.h +++ b/CYD-Klipper-Display/src/ui/wifi_setup.h @@ -1 +1,2 @@ -void wifi_init(); \ No newline at end of file +void wifi_init(); +void wifi_ok(); \ No newline at end of file