From 9b0a5b89fef62ee4aa91c47ec23922b47ba18c63 Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Mon, 10 Jan 2022 13:48:40 +0100 Subject: [PATCH] LVGL prepare for Touch Screen calibration --- .../Adafruit_LvGL_Glue.cpp | 26 +++++++------------ .../lv_berry/generate/be_lvgl_module.c | 4 +++ lib/libesp32_lvgl/lv_berry/mapping/lv_funcs.h | 1 + .../lv_berry/src/be_lvgl_ctypes_definitions.c | 15 +++++++++++ .../lv_berry/src/embedded/lvgl_ctypes.be | 12 +++++++++ lib/libesp32_lvgl/lv_berry/src/lv_berry.c | 13 ++++++++++ lib/libesp32_lvgl/lv_berry/src/lv_berry.h | 11 ++++++++ lib/libesp32_lvgl/lv_berry/tools/convert.py | 4 +++ .../lv_berry/tools/preprocessor.py | 1 + tasmota/xdrv_55_touch.ino | 18 +++++++++---- 10 files changed, 84 insertions(+), 21 deletions(-) diff --git a/lib/libesp32_lvgl/Adafruit_LvGL_Glue-shadinger/Adafruit_LvGL_Glue.cpp b/lib/libesp32_lvgl/Adafruit_LvGL_Glue-shadinger/Adafruit_LvGL_Glue.cpp index cc64444a0..75f6baa0a 100755 --- a/lib/libesp32_lvgl/Adafruit_LvGL_Glue-shadinger/Adafruit_LvGL_Glue.cpp +++ b/lib/libesp32_lvgl/Adafruit_LvGL_Glue-shadinger/Adafruit_LvGL_Glue.cpp @@ -1,5 +1,6 @@ #include "Adafruit_LvGL_Glue.h" #include +#include "lv_berry.h" // ARCHITECTURE-SPECIFIC TIMER STUFF --------------------------------------- @@ -12,29 +13,22 @@ static void lv_tick_handler(void) { lv_tick_inc(lv_tick_interval_ms); } // TOUCHSCREEN STUFF ------------------------------------------------------- -// STMPE610 calibration for raw touch data -#define TS_MINX 100 -#define TS_MAXX 3800 -#define TS_MINY 100 -#define TS_MAXY 3750 -// Same, for ADC touchscreen -#define ADC_XMIN 325 -#define ADC_XMAX 750 -#define ADC_YMIN 240 -#define ADC_YMAX 840 - - - -uint32_t Touch_Status(uint32_t sel); +uint32_t Touch_Status(int32_t sel); static void touchscreen_read(lv_indev_drv_t *indev_drv, lv_indev_data_t *data) { - //lv_coord_t last_x = 0, last_y = 0; - //static uint8_t release_count = 0; data->point.x = Touch_Status(1); // Last-pressed coordinates data->point.y = Touch_Status(2); data->state = Touch_Status(0) ? LV_INDEV_STATE_PRESSED : LV_INDEV_STATE_RELEASED; data->continue_reading = false; /*No buffering now so no more data read*/ + // keep data for TS calibration + lv_ts_calibration.state = data->state; + if (data->state == LV_INDEV_STATE_PRESSED) { // if not pressed, the data may be invalid + lv_ts_calibration.x = data->point.x; + lv_ts_calibration.y = data->point.y; + lv_ts_calibration.raw_x = Touch_Status(-1); + lv_ts_calibration.raw_y = Touch_Status(-2); + } } // OTHER LITTLEVGL VITALS -------------------------------------------------- diff --git a/lib/libesp32_lvgl/lv_berry/generate/be_lvgl_module.c b/lib/libesp32_lvgl/lv_berry/generate/be_lvgl_module.c index dcebc0415..b907f6ac9 100644 --- a/lib/libesp32_lvgl/lv_berry/generate/be_lvgl_module.c +++ b/lib/libesp32_lvgl/lv_berry/generate/be_lvgl_module.c @@ -8,11 +8,14 @@ #include "lvgl.h" #include "be_mapping.h" +#include "lv_berry.h" #include "lv_theme_openhasp.h" extern int lv0_member(bvm *vm); // resolve virtual members extern int lv0_load_font(bvm *vm); +extern lv_ts_calibration_t * lv_get_ts_calibration(void); + static int lv_get_hor_res(void) { return lv_disp_get_hor_res(lv_disp_get_default()); @@ -62,6 +65,7 @@ const be_ntv_func_def_t lv_func[] = { { "event_set_ext_draw_size", (void*) &lv_event_set_ext_draw_size, "", "(lv.lv_event)i" }, { "flex_init", (void*) &lv_flex_init, "", "" }, { "get_hor_res", (void*) &lv_get_hor_res, "i", "" }, + { "get_ts_calibration", (void*) &lv_get_ts_calibration, "lv.lv_ts_calibration", "" }, { "get_ver_res", (void*) &lv_get_ver_res, "i", "" }, { "grid_fr", (void*) &lv_grid_fr, "i", "i" }, { "grid_init", (void*) &lv_grid_init, "", "" }, diff --git a/lib/libesp32_lvgl/lv_berry/mapping/lv_funcs.h b/lib/libesp32_lvgl/lv_berry/mapping/lv_funcs.h index 95ec2d37a..85309fbf8 100644 --- a/lib/libesp32_lvgl/lv_berry/mapping/lv_funcs.h +++ b/lib/libesp32_lvgl/lv_berry/mapping/lv_funcs.h @@ -4,6 +4,7 @@ // Custom Tasmota functions void lv_img_set_tasmota_logo(lv_obj_t * img); +lv_ts_calibration_t * lv_get_ts_calibration(void); // ====================================================================== // Artificial diff --git a/lib/libesp32_lvgl/lv_berry/src/be_lvgl_ctypes_definitions.c b/lib/libesp32_lvgl/lv_berry/src/be_lvgl_ctypes_definitions.c index 1063a33eb..3e21208bd 100644 --- a/lib/libesp32_lvgl/lv_berry/src/be_lvgl_ctypes_definitions.c +++ b/lib/libesp32_lvgl/lv_berry/src/be_lvgl_ctypes_definitions.c @@ -463,6 +463,18 @@ const be_ctypes_structure_t be_lv_event = { { "user_data", 12, 0, 0, ctypes_ptr32, 0 }, }}; +const be_ctypes_structure_t be_lv_ts_calibration = { + 12, /* size in bytes */ + 5, /* number of elements */ + be_ctypes_instance_mappings, + (const be_ctypes_structure_item_t[5]) { + { "raw_x", 0, 0, 0, ctypes_i16, 0 }, + { "raw_y", 2, 0, 0, ctypes_i16, 0 }, + { "state", 8, 0, 0, ctypes_i32, 0 }, + { "x", 4, 0, 0, ctypes_i16, 0 }, + { "y", 6, 0, 0, ctypes_i16, 0 }, +}}; + static const char * be_ctypes_instance_mappings[] = { "lv_color", NULL @@ -496,6 +508,7 @@ static be_define_ctypes_class(lv_meter_scale, &be_lv_meter_scale, &be_class_ctyp static be_define_ctypes_class(lv_obj_class, &be_lv_obj_class, &be_class_ctypes, "lv_obj_class"); static be_define_ctypes_class(lv_point, &be_lv_point, &be_class_ctypes, "lv_point"); static be_define_ctypes_class(lv_sqrt_res, &be_lv_sqrt_res, &be_class_ctypes, "lv_sqrt_res"); +static be_define_ctypes_class(lv_ts_calibration, &be_lv_ts_calibration, &be_class_ctypes, "lv_ts_calibration"); void be_load_ctypes_lvgl_definitions_lib(bvm *vm) { ctypes_register_class(vm, &be_class_lv_area, &be_lv_area); @@ -526,6 +539,7 @@ void be_load_ctypes_lvgl_definitions_lib(bvm *vm) { ctypes_register_class(vm, &be_class_lv_obj_class, &be_lv_obj_class); ctypes_register_class(vm, &be_class_lv_point, &be_lv_point); ctypes_register_class(vm, &be_class_lv_sqrt_res, &be_lv_sqrt_res); + ctypes_register_class(vm, &be_class_lv_ts_calibration, &be_lv_ts_calibration); } be_ctypes_class_by_name_t be_ctypes_lvgl_classes[] = { @@ -557,6 +571,7 @@ be_ctypes_class_by_name_t be_ctypes_lvgl_classes[] = { { "lv_obj_class", &be_class_lv_obj_class }, { "lv_point", &be_class_lv_point }, { "lv_sqrt_res", &be_class_lv_sqrt_res }, + { "lv_ts_calibration", &be_class_lv_ts_calibration }, }; const size_t be_ctypes_lvgl_classes_size = sizeof(be_ctypes_lvgl_classes)/sizeof(be_ctypes_lvgl_classes[0]); diff --git a/lib/libesp32_lvgl/lv_berry/src/embedded/lvgl_ctypes.be b/lib/libesp32_lvgl/lv_berry/src/embedded/lvgl_ctypes.be index ff9e75310..6198158e4 100644 --- a/lib/libesp32_lvgl/lv_berry/src/embedded/lvgl_ctypes.be +++ b/lib/libesp32_lvgl/lv_berry/src/embedded/lvgl_ctypes.be @@ -393,6 +393,18 @@ lv_event = [ # valid LVGL8 ] lv_event = ctypes.structure(lv_event, "lv_event") +####################################################################### +# Special structure used to calibrate resistive touchscreens +####################################################################### +lv_ts_calibration = [ # valid LVGL8 + [lv_coord, "raw_x"], + [lv_coord, "raw_y"], + [lv_coord, "x"], + [lv_coord, "y"], + [int32_t, "state"], +] +lv_ts_calibration = ctypes.structure(lv_ts_calibration, "lv_ts_calibration") + # ctypes.print_classes("lvgl") diff --git a/lib/libesp32_lvgl/lv_berry/src/lv_berry.c b/lib/libesp32_lvgl/lv_berry/src/lv_berry.c index b276f2868..28e473004 100644 --- a/lib/libesp32_lvgl/lv_berry/src/lv_berry.c +++ b/lib/libesp32_lvgl/lv_berry/src/lv_berry.c @@ -228,6 +228,19 @@ int lv0_load_font(bvm *vm) { be_raise(vm, "type_error", NULL); } +/*********************************************************************************************\ + * Get Touch Screen calibration information +\*********************************************************************************************/ +lv_ts_calibration_t lv_ts_calibration = { + 0, 0, + 0, 0, + LV_INDEV_STATE_RELEASED +}; + +lv_ts_calibration_t * lv_get_ts_calibration(void) { + return &lv_ts_calibration; +} + /*********************************************************************************************\ * LVGL top level virtual members * diff --git a/lib/libesp32_lvgl/lv_berry/src/lv_berry.h b/lib/libesp32_lvgl/lv_berry/src/lv_berry.h index a3f9e296d..e494e1187 100644 --- a/lib/libesp32_lvgl/lv_berry/src/lv_berry.h +++ b/lib/libesp32_lvgl/lv_berry/src/lv_berry.h @@ -27,6 +27,17 @@ extern void be_load_lvgl_classes(bvm *vm); // TODO temporary fix extern void lv_img_set_tasmota_logo(lv_obj_t * img); +// used for ToushScreen calibration +typedef struct lv_ts_calibration_t { + lv_coord_t raw_x; + lv_coord_t raw_y; + lv_coord_t x; + lv_coord_t y; + lv_indev_state_t state; +} lv_ts_calibration_t; +extern lv_ts_calibration_t lv_ts_calibration; + + #ifdef __cplusplus } #endif diff --git a/lib/libesp32_lvgl/lv_berry/tools/convert.py b/lib/libesp32_lvgl/lv_berry/tools/convert.py index 7f8c70793..6c5c1b300 100644 --- a/lib/libesp32_lvgl/lv_berry/tools/convert.py +++ b/lib/libesp32_lvgl/lv_berry/tools/convert.py @@ -134,6 +134,7 @@ return_types = { "lv_theme_t *": "lv_theme", "lv_disp_t *": "lv_disp", "lv_indev_t *": "lv_indev", + "lv_ts_calibration_t *": "lv_ts_calibration", #"lv_disp_t*": "lv_disp", #"lv_style_list_t*": "", @@ -671,11 +672,14 @@ print("""/******************************************************************** #include "lvgl.h" #include "be_mapping.h" +#include "lv_berry.h" #include "lv_theme_openhasp.h" extern int lv0_member(bvm *vm); // resolve virtual members extern int lv0_load_font(bvm *vm); +extern lv_ts_calibration_t * lv_get_ts_calibration(void); + static int lv_get_hor_res(void) { return lv_disp_get_hor_res(lv_disp_get_default()); diff --git a/lib/libesp32_lvgl/lv_berry/tools/preprocessor.py b/lib/libesp32_lvgl/lv_berry/tools/preprocessor.py index 3906116b2..78f5d1301 100644 --- a/lib/libesp32_lvgl/lv_berry/tools/preprocessor.py +++ b/lib/libesp32_lvgl/lv_berry/tools/preprocessor.py @@ -86,6 +86,7 @@ print(""" // Custom Tasmota functions void lv_img_set_tasmota_logo(lv_obj_t * img); +lv_ts_calibration_t * lv_get_ts_calibration(void); // ====================================================================== // Artificial diff --git a/tasmota/xdrv_55_touch.ino b/tasmota/xdrv_55_touch.ino index 1016cc467..ed0b3c6f7 100644 --- a/tasmota/xdrv_55_touch.ino +++ b/tasmota/xdrv_55_touch.ino @@ -31,6 +31,8 @@ bool FT5206_found = false; bool XPT2046_found = false; +int16_t raw_touch_xp; +int16_t raw_touch_yp; int16_t touch_xp; int16_t touch_yp; bool touched; @@ -43,7 +45,7 @@ bool touched; VButton *buttons[MAX_TOUCH_BUTTONS]; #endif -uint32_t Touch_Status(uint32_t sel) { +uint32_t Touch_Status(int32_t sel) { if (FT5206_found || XPT2046_found) { switch (sel) { case 0: @@ -52,6 +54,10 @@ uint32_t Touch_Status(uint32_t sel) { return touch_xp; case 2: return touch_yp; + case -1: // before calibration + return raw_touch_xp; + case -2: + return raw_touch_yp; } return 0; } else { @@ -141,8 +147,9 @@ void Touch_Check(void(*rotconvert)(int16_t *x, int16_t *y)) { } #endif // USE_XPT2046 + raw_touch_xp = touch_xp; + raw_touch_yp = touch_yp; if (touched) { - AddLog(LOG_LEVEL_DEBUG_MORE, "TS : touched x=%i y=%i", touch_xp, touch_yp); #ifdef USE_TOUCH_BUTTONS #ifdef USE_M5STACK_CORE2 // handle 3 built in touch buttons @@ -165,6 +172,7 @@ void Touch_Check(void(*rotconvert)(int16_t *x, int16_t *y)) { #endif // USE_TOUCH_BUTTONS rotconvert(&touch_xp, &touch_yp); + AddLog(LOG_LEVEL_DEBUG_MORE, "TS : touched x=%i y=%i (raw x=%i y=%i)", touch_xp, touch_yp, raw_touch_xp, raw_touch_yp); #ifdef USE_TOUCH_BUTTONS CheckTouchButtons(touched, touch_xp, touch_yp); @@ -290,8 +298,8 @@ void CheckTouchButtons(bool touched, int16_t touch_x, int16_t touch_y) { } } } - touch_xp = 0; - touch_yp = 0; + raw_touch_xp = touch_xp = 0; + raw_touch_yp = touch_yp = 0; } } #endif // USE_TOUCH_BUTTONS @@ -321,7 +329,7 @@ bool Xdrv55(uint8_t function) { #else // #if defined(USE_FT5206) || defined(USE_XPT2046) || defined(USE_LILYGO47) || defined(USE_TOUCH_BUTTONS) // dummy for LVGL without a touch controller -uint32_t Touch_Status(uint32_t sel) { +uint32_t Touch_Status(int32_t sel) { return 0; }