Victor Oliveira
4 years ago
committed by
GitHub
45 changed files with 848 additions and 907 deletions
@ -0,0 +1,120 @@ |
|||
/**
|
|||
* 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 <https://www.gnu.org/licenses/>.
|
|||
* |
|||
*/ |
|||
#include "../../../../inc/MarlinConfigPre.h" |
|||
|
|||
#if BOTH(HAS_TFT_LVGL_UI, TOUCH_SCREEN_CALIBRATION) |
|||
|
|||
#include "draw_ui.h" |
|||
#include "draw_touch_calibration.h" |
|||
#include <lv_conf.h> |
|||
|
|||
#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 = GET_TEXT(MSG_TOP_LEFT); break; |
|||
case CALIBRATION_BOTTOM_LEFT: str = GET_TEXT(MSG_BOTTOM_LEFT); break; |
|||
case CALIBRATION_TOP_RIGHT: str = GET_TEXT(MSG_TOP_RIGHT); break; |
|||
case CALIBRATION_BOTTOM_RIGHT: str = GET_TEXT(MSG_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 ? GET_TEXT(MSG_CALIBRATION_COMPLETED) : GET_TEXT(MSG_CALIBRATION_FAILED); |
|||
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() { |
|||
disp_state_stack._disp_index = 0; |
|||
ZERO(disp_state_stack._disp_state); |
|||
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 && TOUCH_SCREEN_CALIBRATION
|
@ -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 <https://www.gnu.org/licenses/>.
|
|||
* |
|||
*/ |
|||
#pragma once |
|||
|
|||
#ifdef __cplusplus |
|||
extern "C" { /* C-declarations for C++ */ |
|||
#endif |
|||
|
|||
extern void lv_draw_touch_calibration_screen(); |
|||
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 |
@ -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
|
@ -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; |
Loading…
Reference in new issue