From 95c629a20eb47c6979289d1330201f33fb6dfb90 Mon Sep 17 00:00:00 2001 From: Victor Mateus Oliveira Date: Mon, 9 Nov 2020 22:56:00 -0300 Subject: [PATCH] touch calibration screen for LVGL --- Marlin/src/gcode/lcd/M995.cpp | 4 +- .../lib/mks_ui/draw_touch_calibration.cpp | 118 ++++++++++++++++++ .../extui/lib/mks_ui/draw_touch_calibration.h | 35 ++++++ Marlin/src/lcd/extui/lib/mks_ui/draw_ui.cpp | 9 ++ Marlin/src/lcd/extui/lib/mks_ui/draw_ui.h | 3 +- .../lib/mks_ui/tft_lvgl_configuration.cpp | 24 ++-- 6 files changed, 184 insertions(+), 9 deletions(-) create mode 100644 Marlin/src/lcd/extui/lib/mks_ui/draw_touch_calibration.cpp create mode 100644 Marlin/src/lcd/extui/lib/mks_ui/draw_touch_calibration.h diff --git a/Marlin/src/gcode/lcd/M995.cpp b/Marlin/src/gcode/lcd/M995.cpp index b35225614e..aca0401dc4 100644 --- a/Marlin/src/gcode/lcd/M995.cpp +++ b/Marlin/src/gcode/lcd/M995.cpp @@ -27,6 +27,8 @@ #include "../gcode.h" #if DISABLED(TFT_LVGL_UI) #include "../../lcd/menu/menu.h" +#else + #include "../../lcd/extui/lib/mks_ui/draw_touch_calibration.h" #endif /** @@ -36,7 +38,7 @@ void GcodeSuite::M995() { #if DISABLED(TFT_LVGL_UI) ui.goto_screen(touch_screen_calibration); #else - //TODO: show LVGL UI calibration screen + lv_draw_touch_calibration_screen(); #endif } diff --git a/Marlin/src/lcd/extui/lib/mks_ui/draw_touch_calibration.cpp b/Marlin/src/lcd/extui/lib/mks_ui/draw_touch_calibration.cpp new file mode 100644 index 0000000000..0780bcf17d --- /dev/null +++ b/Marlin/src/lcd/extui/lib/mks_ui/draw_touch_calibration.cpp @@ -0,0 +1,118 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +#include "../../../../inc/MarlinConfigPre.h" + +#if HAS_TFT_LVGL_UI && ENABLED(TOUCH_SCREEN_CALIBRATION) + +#include "draw_ui.h" +#include "draw_touch_calibration.h" +#include + +#include "../../../../inc/MarlinConfig.h" +#include "../../../tft_io/touch_calibration.h" +#include "SPI_TFT.h" + +static lv_obj_t *scr; +static lv_obj_t *status_label; + +static void event_handler(lv_obj_t *obj, lv_event_t event); + +enum { + ID_TC_RETURN = 1 +}; + +static void drawCross(uint16_t x, uint16_t y, uint16_t color) { + SPI_TFT.tftio.set_window(x - 15, y, x + 15, y); + SPI_TFT.tftio.WriteMultiple(color, 31); + SPI_TFT.tftio.set_window(x, y - 15, x, y + 15); + SPI_TFT.tftio.WriteMultiple(color, 31); +} + +void lv_update_touch_calibration_screen() { + uint16_t x, y; + + calibrationState calibration_stage = touch_calibration.get_calibration_state(); + if (calibration_stage == CALIBRATION_NONE) { + // start and clear screen + calibration_stage = touch_calibration.calibration_start(); + } + else { + // clear last cross + x = touch_calibration.calibration_points[_MIN(calibration_stage - 1, CALIBRATION_BOTTOM_RIGHT)].x; + y = touch_calibration.calibration_points[_MIN(calibration_stage - 1, CALIBRATION_BOTTOM_RIGHT)].y; + drawCross(x, y, LV_COLOR_BACKGROUND.full); + } + + const char *str = nullptr; + if (calibration_stage < CALIBRATION_SUCCESS) { + // handle current state + switch (calibration_stage) { + case CALIBRATION_TOP_LEFT: str = "Top Left"; break; + case CALIBRATION_BOTTOM_LEFT: str = "Bottom Left"; break; + case CALIBRATION_TOP_RIGHT: str = "Top Right"; break; + case CALIBRATION_BOTTOM_RIGHT: str = "Bottom Right"; break; + default: break; + } + + x = touch_calibration.calibration_points[calibration_stage].x; + y = touch_calibration.calibration_points[calibration_stage].y; + drawCross(x, y, LV_COLOR_WHITE.full); + } + else { + // end calibration + str = calibration_stage == CALIBRATION_SUCCESS ? "Calibration Completed" : "Calibration Failed, send M995 to try again"; + touch_calibration.calibration_end(); + lv_big_button_create(scr, "F:/bmp_return.bin", common_menu.text_back, BTN_X_PIXEL * 3 + INTERVAL_V * 4, BTN_Y_PIXEL + INTERVAL_H + titleHeight, event_handler, ID_TC_RETURN); + } + + // draw current message + lv_label_set_text(status_label, str); + lv_obj_align(status_label, nullptr, LV_ALIGN_CENTER, 0, 0); +} + +static void event_handler(lv_obj_t *obj, lv_event_t event) { + if (event != LV_EVENT_RELEASED) return; + switch (obj->mks_obj_id) { + case ID_TC_RETURN: + TERN_(MKS_TEST, curent_disp_ui = 1); + lv_clear_touch_calibration_screen(); + lv_draw_ready_print(); + break; + } +} + +void lv_draw_touch_calibration_screen(void) { + scr = lv_screen_create(TOUCH_CALIBRATION_UI, ""); + + status_label = lv_label_create(scr, ""); + lv_obj_align(status_label, nullptr, LV_ALIGN_CENTER, 0, 0); + + lv_refr_now(lv_refr_get_disp_refreshing()); + + lv_update_touch_calibration_screen(); +} + +void lv_clear_touch_calibration_screen() { + lv_obj_del(scr); +} + +#endif // HAS_TFT_LVGL_UI diff --git a/Marlin/src/lcd/extui/lib/mks_ui/draw_touch_calibration.h b/Marlin/src/lcd/extui/lib/mks_ui/draw_touch_calibration.h new file mode 100644 index 0000000000..8d29be3734 --- /dev/null +++ b/Marlin/src/lcd/extui/lib/mks_ui/draw_touch_calibration.h @@ -0,0 +1,35 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +#pragma once + +#ifdef __cplusplus + extern "C" { /* C-declarations for C++ */ +#endif + +extern void lv_draw_touch_calibration_screen(void); +extern void lv_clear_touch_calibration_screen(); +extern void lv_update_touch_calibration_screen(); + +//extern void disp_temp_ready_print(); +#ifdef __cplusplus + } /* C-declarations for C++ */ +#endif diff --git a/Marlin/src/lcd/extui/lib/mks_ui/draw_ui.cpp b/Marlin/src/lcd/extui/lib/mks_ui/draw_ui.cpp index c7f5f418f5..6f97bf130b 100644 --- a/Marlin/src/lcd/extui/lib/mks_ui/draw_ui.cpp +++ b/Marlin/src/lcd/extui/lib/mks_ui/draw_ui.cpp @@ -48,6 +48,10 @@ #include "../../../../feature/pause.h" #endif +#if ENABLED(TOUCH_SCREEN_CALIBRATION) + #include "draw_touch_calibration.h" +#endif + CFG_ITMES gCfgItems; UI_CFG uiCfg; DISP_STATE_STACK disp_state_stack; @@ -1360,6 +1364,11 @@ void clear_cur_ui() { lv_clear_encoder_settings(); break; #endif + #if ENABLED(TOUCH_SCREEN_CALIBRATION) + case TOUCH_CALIBRATION_UI: + lv_clear_touch_calibration_screen(); + break; + #endif default: break; } //GUI_Clear(); diff --git a/Marlin/src/lcd/extui/lib/mks_ui/draw_ui.h b/Marlin/src/lcd/extui/lib/mks_ui/draw_ui.h index eb54b3e4ea..6d162676e8 100644 --- a/Marlin/src/lcd/extui/lib/mks_ui/draw_ui.h +++ b/Marlin/src/lcd/extui/lib/mks_ui/draw_ui.h @@ -313,7 +313,8 @@ typedef enum { EEPROM_SETTINGS_UI, WIFI_SETTINGS_UI, HOMING_SENSITIVITY_UI, - ENCODER_SETTINGS_UI + ENCODER_SETTINGS_UI, + TOUCH_CALIBRATION_UI } DISP_STATE; typedef struct { diff --git a/Marlin/src/lcd/extui/lib/mks_ui/tft_lvgl_configuration.cpp b/Marlin/src/lcd/extui/lib/mks_ui/tft_lvgl_configuration.cpp index 48cadf8f4c..e35fb7521c 100644 --- a/Marlin/src/lcd/extui/lib/mks_ui/tft_lvgl_configuration.cpp +++ b/Marlin/src/lcd/extui/lib/mks_ui/tft_lvgl_configuration.cpp @@ -51,6 +51,11 @@ XPT2046 touch; #include "../../../../feature/powerloss.h" #endif +#if ENABLED(TOUCH_SCREEN_CALIBRATION) + #include "../../../tft_io/touch_calibration.h" + #include "draw_touch_calibration.h" +#endif + #include #ifndef TFT_WIDTH @@ -240,17 +245,22 @@ unsigned int getTickDiff(unsigned int curTick, unsigned int lastTick) { static bool get_point(int16_t *x, int16_t *y) { bool is_touched = touch.getRawPoint(x, y); - if (is_touched) { + if (!is_touched) return false; + + #if ENABLED(TOUCH_SCREEN_CALIBRATION) + const calibrationState state = touch_calibration.get_calibration_state(); + if (state >= CALIBRATION_TOP_LEFT && state <= CALIBRATION_BOTTOM_RIGHT) { + if (touch_calibration.handleTouch(*x, *y)) lv_update_touch_calibration_screen(); + return false; + } + *x = int16_t((int32_t(*x) * touch_calibration.calibration.x) >> 16) + touch_calibration.calibration.offset_x; + *y = int16_t((int32_t(*y) * touch_calibration.calibration.y) >> 16) + touch_calibration.calibration.offset_y; + #else *x = int16_t((int32_t(*x) * TOUCH_CALIBRATION_X) >> 16) + TOUCH_OFFSET_X; *y = int16_t((int32_t(*y) * TOUCH_CALIBRATION_Y) >> 16) + TOUCH_OFFSET_Y; - } - - #if (TFT_ROTATION & TFT_ROTATE_180) - *x = int16_t((TFT_WIDTH) - (int)(*x)); - *y = int16_t((TFT_HEIGHT) - (int)(*y)); #endif - return is_touched; + return true; } bool my_touchpad_read(lv_indev_drv_t * indev_driver, lv_indev_data_t * data) {