Skip to content

Commit

Permalink
🚸 Minor touch calibration improvement (MarlinFirmware#26445)
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead authored and eoyilmaz committed Nov 21, 2023
1 parent e2b0b18 commit b8219fd
Show file tree
Hide file tree
Showing 20 changed files with 172 additions and 169 deletions.
7 changes: 3 additions & 4 deletions Marlin/src/HAL/LPC1768/tft/xpt2046.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,11 @@ bool XPT2046::isTouched() {
);
}

bool XPT2046::getRawPoint(int16_t *x, int16_t *y) {
if (isBusy()) return false;
if (!isTouched()) return false;
bool XPT2046::getRawPoint(int16_t * const x, int16_t * const y) {
if (isBusy() || !isTouched()) return false;
*x = getRawData(XPT2046_X);
*y = getRawData(XPT2046_Y);
return isTouched();
return true;
}

uint16_t XPT2046::getRawData(const XPTCoordinate coordinate) {
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/HAL/LPC1768/tft/xpt2046.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,5 @@ class XPT2046 {
#endif

static void init();
static bool getRawPoint(int16_t *x, int16_t *y);
static bool getRawPoint(int16_t * const x, int16_t * const y);
};
2 changes: 1 addition & 1 deletion Marlin/src/HAL/NATIVE_SIM/tft/xpt2046.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,5 @@ class XPT2046 {
#endif

static void init();
static bool getRawPoint(int16_t *x, int16_t *y);
static bool getRawPoint(int16_t * const x, int16_t * const y);
};
4 changes: 2 additions & 2 deletions Marlin/src/HAL/STM32/tft/gt911.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ bool GT911::getFirstTouchPoint(int16_t *x, int16_t *y) {
return false;
}

bool GT911::getPoint(int16_t *x, int16_t *y) {
static bool touched = 0;
bool GT911::getRawPoint(int16_t * const x, int16_t * const y) {
static bool touched = false;
static int16_t read_x = 0, read_y = 0;
static millis_t next_time = 0;

Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/HAL/STM32/tft/gt911.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,5 @@ class GT911 {
public:
static void init();
static bool getFirstTouchPoint(int16_t *x, int16_t *y);
static bool getPoint(int16_t *x, int16_t *y);
static bool getRawPoint(int16_t * const x, int16_t * const y);
};
7 changes: 3 additions & 4 deletions Marlin/src/HAL/STM32/tft/xpt2046.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,11 @@ bool XPT2046::isTouched() {
);
}

bool XPT2046::getRawPoint(int16_t *x, int16_t *y) {
if (isBusy()) return false;
if (!isTouched()) return false;
bool XPT2046::getRawPoint(int16_t * const x, int16_t * const y) {
if (isBusy() || !isTouched()) return false;
*x = getRawData(XPT2046_X);
*y = getRawData(XPT2046_Y);
return isTouched();
return true;
}

uint16_t XPT2046::getRawData(const XPTCoordinate coordinate) {
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/HAL/STM32/tft/xpt2046.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,5 @@ class XPT2046 {

public:
static void init();
static bool getRawPoint(int16_t *x, int16_t *y);
static bool getRawPoint(int16_t * const x, int16_t * const y);
};
7 changes: 3 additions & 4 deletions Marlin/src/HAL/STM32F1/tft/xpt2046.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,11 @@ bool XPT2046::isTouched() {
);
}

bool XPT2046::getRawPoint(int16_t *x, int16_t *y) {
if (isBusy()) return false;
if (!isTouched()) return false;
bool XPT2046::getRawPoint(int16_t * const x, int16_t * const y) {
if (isBusy() || !isTouched()) return false;
*x = getRawData(XPT2046_X);
*y = getRawData(XPT2046_Y);
return isTouched();
return true;
}

uint16_t XPT2046::getRawData(const XPTCoordinate coordinate) {
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/HAL/STM32F1/tft/xpt2046.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,5 @@ class XPT2046 {
#endif

static void init();
static bool getRawPoint(int16_t *x, int16_t *y);
static bool getRawPoint(int16_t * const x, int16_t * const y);
};
3 changes: 3 additions & 0 deletions Marlin/src/core/debug_out.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#undef DEBUG_ECHO_MSG
#undef DEBUG_ERROR_MSG
#undef DEBUG_WARN_MSG
#undef DEBUG_ECHO_TERNARY
#undef DEBUG_EOL
#undef DEBUG_FLUSH
#undef DEBUG_POS
Expand Down Expand Up @@ -67,6 +68,7 @@
#define DEBUG_ECHO_MSG SERIAL_ECHO_MSG
#define DEBUG_ERROR_MSG SERIAL_ERROR_MSG
#define DEBUG_WARN_MSG SERIAL_WARN_MSG
#define DEBUG_ECHO_TERNARY SERIAL_ECHO_TERNARY
#define DEBUG_EOL SERIAL_EOL
#define DEBUG_FLUSH SERIAL_FLUSH
#define DEBUG_POS SERIAL_POS
Expand All @@ -90,6 +92,7 @@
#define DEBUG_ECHO_MSG(...) NOOP
#define DEBUG_ERROR_MSG(...) NOOP
#define DEBUG_WARN_MSG(...) NOOP
#define DEBUG_ECHO_TERNARY(...) NOOP
#define DEBUG_EOL() NOOP
#define DEBUG_FLUSH() NOOP
#define DEBUG_POS(...) NOOP
Expand Down
10 changes: 5 additions & 5 deletions Marlin/src/lcd/e3v2/jyersui/dwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,20 +389,20 @@ class TextScroller {

// Draw value text on
if (viewer_print_value) {
int8_t offset_x, offset_y = cell_height_px / 2 - 6;
xy_int8_t offset { 0, cell_height_px / 2 - 6 };
if (isnan(bedlevel.z_values[x][y])) { // undefined
dwinDrawString(false, font6x12, COLOR_WHITE, COLOR_BG_BLUE, start_x_px + cell_width_px / 2 - 5, start_y_px + offset_y, F("X"));
dwinDrawString(false, font6x12, COLOR_WHITE, COLOR_BG_BLUE, start_x_px + cell_width_px / 2 - 5, start_y_px + offset.y, F("X"));
}
else { // has value
MString<12> msg;
if (GRID_MAX_POINTS_X < 10)
msg.set(p_float_t(abs(bedlevel.z_values[x][y]), 2));
else
msg.setf(F("%02i"), uint16_t(abs(bedlevel.z_values[x][y] - int16_t(bedlevel.z_values[x][y])) * 100));
offset_x = cell_width_px / 2 - 3 * msg.length() - 2;
offset.x = cell_width_px / 2 - 3 * msg.length() - 2;
if (GRID_MAX_POINTS_X >= 10)
dwinDrawString(false, font6x12, COLOR_WHITE, COLOR_BG_BLUE, start_x_px - 2 + offset_x, start_y_px + offset_y /*+ square / 2 - 6*/, F("."));
dwinDrawString(false, font6x12, COLOR_WHITE, COLOR_BG_BLUE, start_x_px + 1 + offset_x, start_y_px + offset_y /*+ square / 2 - 6*/, msg);
dwinDrawString(false, font6x12, COLOR_WHITE, COLOR_BG_BLUE, start_x_px - 2 + offset.x, start_y_px + offset.y /*+ square / 2 - 6*/, F("."));
dwinDrawString(false, font6x12, COLOR_WHITE, COLOR_BG_BLUE, start_x_px + 1 + offset.x, start_y_px + offset.y /*+ square / 2 - 6*/, msg);
}
safe_delay(10);
LCD_SERIAL.flushTX();
Expand Down
10 changes: 5 additions & 5 deletions Marlin/src/lcd/e3v2/proui/bedlevel_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,20 +247,20 @@ bool BedLevelTools::meshValidate() {
// Draw value text on
const uint8_t fs = DWINUI::fontWidth(meshfont);
if (viewer_print_value) {
int8_t offset_x, offset_y = cell_height_px / 2 - fs;
xy_int8_t offset { 0, cell_height_px / 2 - fs };
if (isnan(bedlevel.z_values[x][y])) { // undefined
dwinDrawString(false, meshfont, COLOR_WHITE, COLOR_BG_BLUE, start_x_px + cell_width_px / 2 - 5, start_y_px + offset_y, F("X"));
dwinDrawString(false, meshfont, COLOR_WHITE, COLOR_BG_BLUE, start_x_px + cell_width_px / 2 - 5, start_y_px + offset.y, F("X"));
}
else { // has value
MString<12> msg;
if ((GRID_MAX_POINTS_X) < TERN(TJC_DISPLAY, 8, 10))
msg.set(p_float_t(abs(bedlevel.z_values[x][y]), 2));
else
msg.setf(F("%02i"), uint16_t(abs(bedlevel.z_values[x][y] - int16_t(bedlevel.z_values[x][y])) * 100));
offset_x = cell_width_px / 2 - (fs / 2) * msg.length() - 2;
offset.x = cell_width_px / 2 - (fs / 2) * msg.length() - 2;
if ((GRID_MAX_POINTS_X) >= TERN(TJC_DISPLAY, 8, 10))
dwinDrawString(false, meshfont, COLOR_WHITE, COLOR_BG_BLUE, start_x_px - 2 + offset_x, start_y_px + offset_y, F("."));
dwinDrawString(false, meshfont, COLOR_WHITE, COLOR_BG_BLUE, start_x_px + 1 + offset_x, start_y_px + offset_y, msg);
dwinDrawString(false, meshfont, COLOR_WHITE, COLOR_BG_BLUE, start_x_px - 2 + offset.x, start_y_px + offset.y, F("."));
dwinDrawString(false, meshfont, COLOR_WHITE, COLOR_BG_BLUE, start_x_px + 1 + offset.x, start_y_px + offset.y, msg);
}
safe_delay(10);
LCD_SERIAL.flushTX();
Expand Down
38 changes: 13 additions & 25 deletions Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ XPT2046 touch;
#include "../../../module/probe.h"
#endif

#include "../../tft_io/touch_calibration.h"
#if ENABLED(TOUCH_SCREEN_CALIBRATION)
#include "../../tft_io/touch_calibration.h"
#include "draw_touch_calibration.h"
#endif

Expand Down Expand Up @@ -305,45 +305,33 @@ uint16_t getTickDiff(const uint16_t curTick, const uint16_t lastTick) {
return (TICK_CYCLE) * (lastTick <= curTick ? (curTick - lastTick) : (0xFFFFFFFF - lastTick + curTick));
}

static bool get_point(int16_t *x, int16_t *y) {
if (!touch.getRawPoint(x, y)) return false;
static bool get_point(xy_int_t &point) {
if (!touch.getRawPoint(&point.x, &point.y)) return false;

#if ENABLED(TOUCH_SCREEN_CALIBRATION)
const calibrationState state = touch_calibration.get_calibration_state();
if (WITHIN(state, CALIBRATION_TOP_LEFT, CALIBRATION_BOTTOM_LEFT)) {
if (touch_calibration.handleTouch(*x, *y)) lv_update_touch_calibration_screen();
if (touch_calibration.handleTouch(point)) 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;
#endif

point.x = int16_t((int32_t(point.x) * _TOUCH_CALIBRATION_X) >> 16) + _TOUCH_OFFSET_X;
point.y = int16_t((int32_t(point.y) * _TOUCH_CALIBRATION_Y) >> 16) + _TOUCH_OFFSET_Y;

return true;
}

bool my_touchpad_read(lv_indev_drv_t * indev_driver, lv_indev_data_t * data) {
static int16_t last_x = 0, last_y = 0;
if (get_point(&last_x, &last_y)) {
#if TFT_ROTATION == TFT_ROTATE_180
data->point.x = TFT_WIDTH - last_x;
data->point.y = TFT_HEIGHT - last_y;
#else
data->point.x = last_x;
data->point.y = last_y;
#endif
static xy_int_t last { 0, 0 };
if (get_point(last)) {
data->point.x = (TFT_ROTATION == TFT_ROTATE_180) ? TFT_WIDTH - last.x : last.x;
data->point.y = (TFT_ROTATION == TFT_ROTATE_180) ? TFT_HEIGHT - last.y : last.y;
data->state = LV_INDEV_STATE_PR;
}
else {
#if TFT_ROTATION == TFT_ROTATE_180
data->point.x = TFT_WIDTH - last_x;
data->point.y = TFT_HEIGHT - last_y;
#else
data->point.x = last_x;
data->point.y = last_y;
#endif
data->point.x = (TFT_ROTATION == TFT_ROTATE_180) ? TFT_WIDTH - last.x : last.x;
data->point.y = (TFT_ROTATION == TFT_ROTATE_180) ? TFT_HEIGHT - last.y : last.y;
data->state = LV_INDEV_STATE_REL;
}
return false; // Return `false` since no data is buffering or left to read
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/lcd/marlinui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ void MarlinUI::init() {

#if HAS_TOUCH_BUTTONS
uint8_t MarlinUI::touch_buttons;
uint8_t MarlinUI::repeat_delay;
uint16_t MarlinUI::repeat_delay;
#endif

#if ANY(AUTO_BED_LEVELING_UBL, G26_MESH_VALIDATION)
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/lcd/marlinui.h
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ class MarlinUI {

#if HAS_TOUCH_BUTTONS
static uint8_t touch_buttons;
static uint8_t repeat_delay;
static uint16_t repeat_delay;
#else
static constexpr uint8_t touch_buttons = 0;
#endif
Expand Down
66 changes: 32 additions & 34 deletions Marlin/src/lcd/tft/touch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#include "tft.h"

bool Touch::enabled = true;
int16_t Touch::x, Touch::y;
xy_int_t Touch::point;
touch_control_t Touch::controls[];
touch_control_t *Touch::current_control;
uint16_t Touch::controls_count;
Expand Down Expand Up @@ -67,26 +67,22 @@ void Touch::add_control(TouchControlType type, uint16_t x, uint16_t y, uint16_t
if (controls_count == MAX_CONTROLS) return;

controls[controls_count].type = type;
controls[controls_count].x = x;
controls[controls_count].y = y;
controls[controls_count].width = width;
controls[controls_count].height = height;
controls[controls_count].pos.set(x, y);
controls[controls_count].size.set(width, height);
controls[controls_count].data = data;
controls_count++;
}

void Touch::idle() {
uint16_t i;
int16_t _x, _y;

if (!enabled) return;

// Return if Touch::idle is called within the same millisecond
const millis_t now = millis();
if (now <= next_touch_ms) return;
next_touch_ms = now;

if (get_point(&_x, &_y)) {
xy_int_t got_point;
if (get_point(got_point)) {
#if HAS_RESUME_CONTINUE
// UI is waiting for a click anywhere?
if (wait_for_user) {
Expand All @@ -110,19 +106,24 @@ void Touch::idle() {
if (time_to_hold == 0) time_to_hold = now + MINIMUM_HOLD_TIME;
if (PENDING(now, time_to_hold)) return;

if (x != 0 && y != 0) {
if (bool(point)) {
if (current_control) {
if (WITHIN(x, current_control->x - FREE_MOVE_RANGE, current_control->x + current_control->width + FREE_MOVE_RANGE) && WITHIN(y, current_control->y - FREE_MOVE_RANGE, current_control->y + current_control->height + FREE_MOVE_RANGE)) {
LIMIT(x, current_control->x, current_control->x + current_control->width);
LIMIT(y, current_control->y, current_control->y + current_control->height);
if ( WITHIN(point.x, current_control->pos.x - FREE_MOVE_RANGE, current_control->pos.x + current_control->size.x + FREE_MOVE_RANGE)
&& WITHIN(point.y, current_control->pos.y - FREE_MOVE_RANGE, current_control->pos.y + current_control->size.y + FREE_MOVE_RANGE)
) {
LIMIT(point.x, current_control->pos.x, current_control->pos.x + current_control->size.x);
LIMIT(point.y, current_control->pos.y, current_control->pos.y + current_control->size.y);
touch(current_control);
}
else
current_control = nullptr;
}
else {
for (i = 0; i < controls_count; i++) {
if ((WITHIN(x, controls[i].x, controls[i].x + controls[i].width) && WITHIN(y, controls[i].y, controls[i].y + controls[i].height)) || (TERN(TOUCH_SCREEN_CALIBRATION, controls[i].type == CALIBRATE, false))) {
for (uint16_t i = 0; i < controls_count; i++) {
if (TERN0(TOUCH_SCREEN_CALIBRATION, controls[i].type == CALIBRATE)
|| ( WITHIN(point.x, controls[i].pos.x, controls[i].pos.x + controls[i].size.x)
&& WITHIN(point.y, controls[i].pos.y, controls[i].pos.y + controls[i].size.y))
) {
touch_control_type = controls[i].type;
touch(&controls[i]);
break;
Expand All @@ -133,11 +134,10 @@ void Touch::idle() {
if (!current_control)
touch_time = now;
}
x = _x;
y = _y;
point = got_point;
}
else {
x = y = 0;
point.reset();
current_control = nullptr;
touch_time = 0;
touch_control_type = NONE;
Expand All @@ -150,7 +150,7 @@ void Touch::touch(touch_control_t *control) {
switch (control->type) {
#if ENABLED(TOUCH_SCREEN_CALIBRATION)
case CALIBRATE:
if (touch_calibration.handleTouch(x, y)) ui.refresh();
if (touch_calibration.handleTouch(point)) ui.refresh();
break;
#endif

Expand All @@ -177,7 +177,7 @@ void Touch::touch(touch_control_t *control) {
ui.encoderPosition = ui.encoderPosition + LCD_HEIGHT < (uint32_t)screen_items ? ui.encoderPosition + LCD_HEIGHT : screen_items;
ui.refresh();
break;
case SLIDER: hold(control); ui.encoderPosition = (x - control->x) * control->data / control->width; break;
case SLIDER: hold(control); ui.encoderPosition = (point.x - control->pos.x) * control->data / control->size.x; break;
case INCREASE: hold(control, repeat_delay - 5); TERN(AUTO_BED_LEVELING_UBL, ui.external_control ? bedlevel.encoder_diff++ : ui.encoderPosition++, ui.encoderPosition++); break;
case DECREASE: hold(control, repeat_delay - 5); TERN(AUTO_BED_LEVELING_UBL, ui.external_control ? bedlevel.encoder_diff-- : ui.encoderPosition--, ui.encoderPosition--); break;
case HEATER:
Expand Down Expand Up @@ -263,28 +263,26 @@ void Touch::hold(touch_control_t *control, millis_t delay) {
ui.refresh();
}

bool Touch::get_point(int16_t *x, int16_t *y) {
#if ENABLED(TFT_TOUCH_DEVICE_XPT2046)
#if ENABLED(TOUCH_SCREEN_CALIBRATION)
const bool is_touched = (touch_calibration.calibration.orientation == TOUCH_PORTRAIT ? io.getRawPoint(y, x) : io.getRawPoint(x, y));
if (is_touched && touch_calibration.calibration.orientation != TOUCH_ORIENTATION_NONE) {
*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
const bool is_touched = (TOUCH_ORIENTATION == TOUCH_PORTRAIT ? io.getRawPoint(y, x) : io.getRawPoint(x, y));
*x = uint16_t((uint32_t(*x) * TOUCH_CALIBRATION_X) >> 16) + TOUCH_OFFSET_X;
*y = uint16_t((uint32_t(*y) * TOUCH_CALIBRATION_Y) >> 16) + TOUCH_OFFSET_Y;
bool Touch::get_point(xy_int_t &point) {
bool is_touched = false;
#if ANY(TFT_TOUCH_DEVICE_XPT2046, TFT_TOUCH_DEVICE_GT911)
is_touched = (TOUCH_ORIENTATION_NONE != _TOUCH_ORIENTATION)
&& (TOUCH_PORTRAIT == _TOUCH_ORIENTATION
? io.getRawPoint(&point.y, &point.x)
: io.getRawPoint(&point.x, &point.y));
#if ENABLED(TFT_TOUCH_DEVICE_XPT2046)
point.x = uint16_t((uint32_t(point.x) * _TOUCH_CALIBRATION_X) >> 16) + _TOUCH_OFFSET_X;
point.y = uint16_t((uint32_t(point.y) * _TOUCH_CALIBRATION_Y) >> 16) + _TOUCH_OFFSET_Y;
#endif
#elif ENABLED(TFT_TOUCH_DEVICE_GT911)
const bool is_touched = (TOUCH_ORIENTATION == TOUCH_PORTRAIT ? io.getPoint(y, x) : io.getPoint(x, y));
#endif

#if HAS_TOUCH_SLEEP
if (is_touched)
wakeUp();
else if (!isSleeping() && ELAPSED(millis(), next_sleep_ms) && ui.on_status_screen())
sleepTimeout();
#endif

return is_touched;
}

Expand Down
Loading

0 comments on commit b8219fd

Please sign in to comment.