Skip to content

Commit

Permalink
ignore first touch after calibration_start
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Mar 23, 2023
1 parent ee4c4f7 commit df8a10a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
17 changes: 7 additions & 10 deletions Marlin/src/lcd/tft_io/touch_calibration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ touch_calibration_t TouchCalibration::calibration;
calibrationState TouchCalibration::calibration_state = CALIBRATION_NONE;
touch_calibration_point_t TouchCalibration::calibration_points[4];
uint8_t TouchCalibration::failed_count;
millis_t TouchCalibration::next_button_update_ms; // = 0;

void TouchCalibration::validate_calibration() {
#define VALIDATE_PRECISION(XY, A, B) validate_precision_##XY(CALIBRATION_##A, CALIBRATION_##B)
Expand Down Expand Up @@ -89,19 +90,15 @@ void TouchCalibration::validate_calibration() {
}
}

bool TouchCalibration::handleTouch(uint16_t x, uint16_t y) {
static millis_t next_button_update_ms = 0;
const millis_t now = millis();
bool TouchCalibration::handleTouch(const uint16_t x, const uint16_t y) {
const millis_t now = millis(), next_ms = next_button_update_ms;

// Avoid registering first touch point as top-left corner when initiating touch calibration since user is touching TFT
if (next_button_update_ms == 0) {
next_button_update_ms = now + BUTTON_DELAY_MENU;
return true;
}

if (PENDING(now, next_button_update_ms)) return false;
if (next_ms && PENDING(now, next_ms)) return false;
next_button_update_ms = now + BUTTON_DELAY_MENU;

// Ignore the first touch because it was probably the one that entered the screen
if (!next_ms) return true;

if (calibration_state < CALIBRATION_SUCCESS) {
calibration_points[calibration_state].raw_x = x;
calibration_points[calibration_state].raw_y = y;
Expand Down
6 changes: 4 additions & 2 deletions Marlin/src/lcd/tft_io/touch_calibration.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,20 @@ class TouchCalibration {
public:
static calibrationState calibration_state;
static touch_calibration_point_t calibration_points[4];
static millis_t next_button_update_ms;

static bool validate_precision(int32_t a, int32_t b) { return (a > b ? (100 * b) / a : (100 * a) / b) > TOUCH_SCREEN_CALIBRATION_PRECISION; }
static bool validate_precision_x(uint8_t a, uint8_t b) { return validate_precision(calibration_points[a].raw_x, calibration_points[b].raw_x); }
static bool validate_precision_y(uint8_t a, uint8_t b) { return validate_precision(calibration_points[a].raw_y, calibration_points[b].raw_y); }
static void validate_calibration();

static touch_calibration_t calibration;
static uint8_t failed_count;
static uint8_t failed_count;
static void calibration_reset() { calibration = { TOUCH_CALIBRATION_X, TOUCH_CALIBRATION_Y, TOUCH_OFFSET_X, TOUCH_OFFSET_Y, TOUCH_ORIENTATION }; }
static bool need_calibration() { return !calibration.offset_x && !calibration.offset_y && !calibration.x && !calibration.y; }

static calibrationState calibration_start() {
next_button_update_ms = 0;
calibration = { 0, 0, 0, 0, TOUCH_ORIENTATION_NONE };
calibration_state = CALIBRATION_TOP_LEFT;
calibration_points[CALIBRATION_TOP_LEFT].x = 30;
Expand All @@ -89,7 +91,7 @@ class TouchCalibration {
return !need_calibration();
}

static bool handleTouch(uint16_t x, uint16_t y);
static bool handleTouch(const uint16_t x, const uint16_t y);
};

extern TouchCalibration touch_calibration;

0 comments on commit df8a10a

Please sign in to comment.