Add Touch Calibration screen (#20049)
This commit is contained in:
81
Marlin/src/lcd/tft_io/touch_calibration.cpp
Normal file
81
Marlin/src/lcd/tft_io/touch_calibration.cpp
Normal file
@ -0,0 +1,81 @@
|
||||
/**
|
||||
* Marlin 3D Printer Firmware
|
||||
* Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "../../inc/MarlinConfig.h"
|
||||
|
||||
#if ENABLED(TOUCH_SCREEN_CALIBRATION)
|
||||
|
||||
#include "touch_calibration.h"
|
||||
|
||||
TouchCalibration touch_calibration;
|
||||
|
||||
touch_calibration_t TouchCalibration::calibration;
|
||||
calibrationState TouchCalibration::calibration_state = CALIBRATION_NONE;
|
||||
touch_calibration_point_t TouchCalibration::calibration_points[4];
|
||||
|
||||
void TouchCalibration::validate_calibration() {
|
||||
const bool landscape = validate_precision_x(0, 1) && validate_precision_x(2, 3) && validate_precision_y(0, 2) && validate_precision_y(1, 3);
|
||||
const bool portrait = validate_precision_y(0, 1) && validate_precision_y(2, 3) && validate_precision_x(0, 2) && validate_precision_x(1, 3);
|
||||
|
||||
if (landscape || portrait) {
|
||||
calibration_state = CALIBRATION_SUCCESS;
|
||||
calibration.x = ((calibration_points[2].x - calibration_points[0].x) << 17) / (calibration_points[3].raw_x + calibration_points[2].raw_x - calibration_points[1].raw_x - calibration_points[0].raw_x);
|
||||
calibration.y = ((calibration_points[1].y - calibration_points[0].y) << 17) / (calibration_points[3].raw_y - calibration_points[2].raw_y + calibration_points[1].raw_y - calibration_points[0].raw_y);
|
||||
calibration.offset_x = calibration_points[0].x - int16_t(((calibration_points[0].raw_x + calibration_points[1].raw_x) * calibration.x) >> 17);
|
||||
calibration.offset_y = calibration_points[0].y - int16_t(((calibration_points[0].raw_y + calibration_points[2].raw_y) * calibration.y) >> 17);
|
||||
calibration.orientation = landscape ? TOUCH_LANDSCAPE : TOUCH_PORTRAIT;
|
||||
}
|
||||
else {
|
||||
calibration_state = CALIBRATION_FAIL;
|
||||
calibration_reset();
|
||||
}
|
||||
|
||||
if (calibration_state == CALIBRATION_SUCCESS) {
|
||||
SERIAL_ECHOLNPGM("Touch screen calibration completed");
|
||||
SERIAL_ECHOLNPAIR("TOUCH_CALIBRATION_X ", calibration.x);
|
||||
SERIAL_ECHOLNPAIR("TOUCH_CALIBRATION_Y ", calibration.y);
|
||||
SERIAL_ECHOLNPAIR("TOUCH_OFFSET_X ", calibration.offset_x);
|
||||
SERIAL_ECHOLNPAIR("TOUCH_OFFSET_Y ", calibration.offset_y);
|
||||
SERIAL_ECHOPGM("TOUCH_ORIENTATION "); if (calibration.orientation == TOUCH_LANDSCAPE) SERIAL_ECHOLNPGM("TOUCH_LANDSCAPE"); else SERIAL_ECHOLNPGM("TOUCH_PORTRAIT");
|
||||
}
|
||||
}
|
||||
|
||||
bool TouchCalibration::handleTouch(uint16_t x, uint16_t y) {
|
||||
static millis_t next_button_update_ms = 0;
|
||||
const millis_t now = millis();
|
||||
if (PENDING(now, next_button_update_ms)) return false;
|
||||
next_button_update_ms = now + BUTTON_DELAY_MENU;
|
||||
|
||||
if (calibration_state < CALIBRATION_SUCCESS) {
|
||||
calibration_points[calibration_state].raw_x = x;
|
||||
calibration_points[calibration_state].raw_y = y;
|
||||
}
|
||||
|
||||
switch (calibration_state) {
|
||||
case CALIBRATION_TOP_LEFT: calibration_state = CALIBRATION_BOTTOM_LEFT; break;
|
||||
case CALIBRATION_BOTTOM_LEFT: calibration_state = CALIBRATION_TOP_RIGHT; break;
|
||||
case CALIBRATION_TOP_RIGHT: calibration_state = CALIBRATION_BOTTOM_RIGHT; break;
|
||||
case CALIBRATION_BOTTOM_RIGHT: validate_calibration(); break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif // TOUCH_SCREEN_CALIBRATION
|
93
Marlin/src/lcd/tft_io/touch_calibration.h
Normal file
93
Marlin/src/lcd/tft_io/touch_calibration.h
Normal file
@ -0,0 +1,93 @@
|
||||
/**
|
||||
* Marlin 3D Printer Firmware
|
||||
* Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "../../inc/MarlinConfigPre.h"
|
||||
|
||||
#ifndef TOUCH_SCREEN_CALIBRATION_PRECISION
|
||||
#define TOUCH_SCREEN_CALIBRATION_PRECISION 80
|
||||
#endif
|
||||
|
||||
#ifndef TOUCH_SCREEN_HOLD_TO_CALIBRATE_MS
|
||||
#define TOUCH_SCREEN_HOLD_TO_CALIBRATE_MS 2500
|
||||
#endif
|
||||
|
||||
#define TOUCH_ORIENTATION_NONE 0
|
||||
#define TOUCH_LANDSCAPE 1
|
||||
#define TOUCH_PORTRAIT 2
|
||||
|
||||
#ifndef TOUCH_ORIENTATION
|
||||
#define TOUCH_ORIENTATION TOUCH_LANDSCAPE
|
||||
#endif
|
||||
|
||||
typedef struct __attribute__((__packed__)) {
|
||||
int32_t x, y;
|
||||
int16_t offset_x, offset_y;
|
||||
uint8_t orientation;
|
||||
} touch_calibration_t;
|
||||
|
||||
typedef struct __attribute__((__packed__)) {
|
||||
uint16_t x, y;
|
||||
int16_t raw_x, raw_y;
|
||||
} touch_calibration_point_t;
|
||||
|
||||
enum calibrationState : uint8_t {
|
||||
CALIBRATION_TOP_LEFT = 0x00,
|
||||
CALIBRATION_BOTTOM_LEFT,
|
||||
CALIBRATION_TOP_RIGHT,
|
||||
CALIBRATION_BOTTOM_RIGHT,
|
||||
CALIBRATION_SUCCESS,
|
||||
CALIBRATION_FAIL,
|
||||
CALIBRATION_NONE,
|
||||
};
|
||||
|
||||
class TouchCalibration {
|
||||
public:
|
||||
static calibrationState calibration_state;
|
||||
static touch_calibration_point_t calibration_points[4];
|
||||
|
||||
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 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() {
|
||||
calibration = { 0, 0, 0, 0, TOUCH_ORIENTATION_NONE };
|
||||
calibration_state = CALIBRATION_TOP_LEFT;
|
||||
calibration_points[CALIBRATION_TOP_LEFT].x = 30;
|
||||
calibration_points[CALIBRATION_TOP_LEFT].y = 30;
|
||||
calibration_points[CALIBRATION_BOTTOM_LEFT].x = 30;
|
||||
calibration_points[CALIBRATION_BOTTOM_LEFT].y = TFT_HEIGHT - 31;
|
||||
calibration_points[CALIBRATION_TOP_RIGHT].x = TFT_WIDTH - 31;
|
||||
calibration_points[CALIBRATION_TOP_RIGHT].y = 30;
|
||||
calibration_points[CALIBRATION_BOTTOM_RIGHT].x = TFT_WIDTH - 31;
|
||||
calibration_points[CALIBRATION_BOTTOM_RIGHT].y = TFT_HEIGHT - 31;
|
||||
return calibration_state;
|
||||
}
|
||||
static void calibration_end() { calibration_state = CALIBRATION_NONE; }
|
||||
static calibrationState get_calibration_state() { return calibration_state; }
|
||||
|
||||
static bool handleTouch(uint16_t x, uint16_t y);
|
||||
};
|
||||
|
||||
extern TouchCalibration touch_calibration;
|
Reference in New Issue
Block a user