Skip to content

Commit

Permalink
Add Touch Calibration screen (#20049)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhapsodyv authored Nov 15, 2020
1 parent cab83ba commit ea37161
Show file tree
Hide file tree
Showing 45 changed files with 848 additions and 907 deletions.
8 changes: 4 additions & 4 deletions Marlin/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -2358,10 +2358,10 @@

#define TOUCH_SCREEN_CALIBRATION
//#define XPT2046_X_CALIBRATION 12316
//#define XPT2046_Y_CALIBRATION -8981
//#define XPT2046_X_OFFSET -43
//#define XPT2046_Y_OFFSET 257
//#define TOUCH_CALIBRATION_X 12316
//#define TOUCH_CALIBRATION_Y -8981
//#define TOUCH_OFFSET_X -43
//#define TOUCH_OFFSET_Y 257

#if ENABLED(TFT_COLOR_UI)
//#define SINGLE_TOUCH_NAVIGATION
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/HAL/LPC1768/tft/xpt2046.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#include "../../../inc/MarlinConfig.h"

#if HAS_TFT_XPT2046 || HAS_TOUCH_XPT2046
#if HAS_TFT_XPT2046 || HAS_TOUCH_BUTTONS

#include "xpt2046.h"
#include <SPI.h>
Expand Down
8 changes: 4 additions & 4 deletions Marlin/src/HAL/STM32F1/SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,15 @@ SPIClass::SPIClass(uint32_t spi_num) {
_currentSetting->state = SPI_STATE_IDLE;
}

SPIClass::SPIClass(int8_t mosi, int8_t miso, int8_t sclk, int8_t ssel) {
SPIClass::SPIClass(int8_t mosi, int8_t miso, int8_t sclk, int8_t ssel) : SPIClass(1) {
#if BOARD_NR_SPI >= 1
if (mosi == BOARD_SPI1_MOSI_PIN) SPIClass(1);
if (mosi == BOARD_SPI1_MOSI_PIN) setModule(1);
#endif
#if BOARD_NR_SPI >= 2
if (mosi == BOARD_SPI2_MOSI_PIN) SPIClass(2);
if (mosi == BOARD_SPI2_MOSI_PIN) setModule(2);
#endif
#if BOARD_NR_SPI >= 3
if (mosi == BOARD_SPI3_MOSI_PIN) SPIClass(3);
if (mosi == BOARD_SPI3_MOSI_PIN) setModule(3);
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/HAL/STM32F1/tft/xpt2046.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#include "../../../inc/MarlinConfig.h"

#if HAS_TFT_XPT2046 || HAS_TOUCH_XPT2046
#if HAS_TFT_XPT2046 || HAS_TOUCH_BUTTONS

#include "xpt2046.h"
#include <SPI.h>
Expand Down
8 changes: 6 additions & 2 deletions Marlin/src/MarlinCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
#include "sd/cardreader.h"

#include "lcd/marlinui.h"
#if HAS_TOUCH_XPT2046
#if HAS_TOUCH_BUTTONS
#include "lcd/touch/touch_buttons.h"
#endif

Expand Down Expand Up @@ -1109,7 +1109,7 @@ void setup() {
SETUP_RUN(ethernet.init());
#endif

#if HAS_TOUCH_XPT2046
#if HAS_TOUCH_BUTTONS
SETUP_RUN(touch.init());
#endif

Expand Down Expand Up @@ -1307,6 +1307,10 @@ void setup() {
SETUP_RUN(password.lock_machine()); // Will not proceed until correct password provided
#endif

#if BOTH(HAS_LCD_MENU, TOUCH_SCREEN_CALIBRATION) && EITHER(TFT_CLASSIC_UI, TFT_COLOR_UI)
ui.check_touch_calibration();
#endif

marlin_state = MF_RUNNING;

SETUP_LOG("setup() completed.");
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/gcode/bedlevel/G26.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ inline bool turn_on_heaters() {
inline bool prime_nozzle() {

const feedRate_t fr_slow_e = planner.settings.max_feedrate_mm_s[E_AXIS] / 15.0f;
#if HAS_LCD_MENU && !HAS_TOUCH_XPT2046 // ui.button_pressed issue with touchscreen
#if HAS_LCD_MENU && !HAS_TOUCH_BUTTONS // ui.button_pressed issue with touchscreen
#if ENABLED(PREVENT_LENGTHY_EXTRUDE)
float Total_Prime = 0.0;
#endif
Expand Down
13 changes: 11 additions & 2 deletions Marlin/src/gcode/lcd/M995.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,23 @@
#if ENABLED(TOUCH_SCREEN_CALIBRATION)

#include "../gcode.h"
#include "../../lcd/menu/menu.h"

#if ENABLED(TFT_LVGL_UI)
#include "../../lcd/extui/lib/mks_ui/draw_touch_calibration.h"
#else
#include "../../lcd/menu/menu.h"
#endif

/**
* M995: Touch screen calibration for TFT display
*/
void GcodeSuite::M995() {

ui.goto_screen(touch_screen_calibration);
#if ENABLED(TFT_LVGL_UI)
lv_draw_touch_calibration_screen();
#else
ui.goto_screen(touch_screen_calibration);
#endif

}

Expand Down
20 changes: 18 additions & 2 deletions Marlin/src/inc/Conditionals_LCD.h
Original file line number Diff line number Diff line change
Expand Up @@ -1073,8 +1073,24 @@
// This emulated DOGM has 'touch/xpt2046', not 'tft/xpt2046'
#if ENABLED(TOUCH_SCREEN) && !HAS_GRAPHICAL_TFT
#undef TOUCH_SCREEN
#undef TOUCH_SCREEN_CALIBRATION
#if !HAS_TFT_LVGL_UI
#define HAS_TOUCH_XPT2046 1
#define HAS_TOUCH_BUTTONS 1
#endif
#endif

// XPT2046_** Compatibility
#if !(defined(TOUCH_CALIBRATION_X) || defined(TOUCH_CALIBRATION_Y) || defined(TOUCH_OFFSET_X) || defined(TOUCH_OFFSET_Y) || defined(TOUCH_ORIENTATION))
#if defined(XPT2046_X_CALIBRATION) && defined(XPT2046_Y_CALIBRATION) && defined(XPT2046_X_OFFSET) && defined(XPT2046_Y_OFFSET)
#define TOUCH_CALIBRATION_X XPT2046_X_CALIBRATION
#define TOUCH_CALIBRATION_Y XPT2046_Y_CALIBRATION
#define TOUCH_OFFSET_X XPT2046_X_OFFSET
#define TOUCH_OFFSET_Y XPT2046_Y_OFFSET
#define TOUCH_ORIENTATION TOUCH_LANDSCAPE
#else
#define TOUCH_CALIBRATION_X 0
#define TOUCH_CALIBRATION_Y 0
#define TOUCH_OFFSET_X 0
#define TOUCH_OFFSET_Y 0
#define TOUCH_ORIENTATION TOUCH_ORIENTATION_NONE
#endif
#endif
2 changes: 1 addition & 1 deletion Marlin/src/inc/Conditionals_adv.h
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@

// Touch Screen or "Touch Buttons" need XPT2046 pins
// but they use different components
#if EITHER(HAS_TFT_XPT2046, HAS_TOUCH_XPT2046)
#if EITHER(HAS_TFT_XPT2046, HAS_TOUCH_BUTTONS)
#define NEED_TOUCH_PINS 1
#endif

Expand Down
16 changes: 8 additions & 8 deletions Marlin/src/inc/SanityCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -3172,17 +3172,17 @@ static_assert( _ARR_TEST(3,0) && _ARR_TEST(3,1) && _ARR_TEST(3,2)
* Touch Buttons
*/
#if ENABLED(TOUCH_SCREEN)
#ifndef XPT2046_X_CALIBRATION
#error "XPT2046_X_CALIBRATION must be defined with TOUCH_SCREEN."
#ifndef TOUCH_CALIBRATION_X
#error "TOUCH_CALIBRATION_X must be defined with TOUCH_SCREEN."
#endif
#ifndef XPT2046_Y_CALIBRATION
#error "XPT2046_Y_CALIBRATION must be defined with TOUCH_SCREEN."
#ifndef TOUCH_CALIBRATION_Y
#error "TOUCH_CALIBRATION_Y must be defined with TOUCH_SCREEN."
#endif
#ifndef XPT2046_X_OFFSET
#error "XPT2046_X_OFFSET must be defined with TOUCH_SCREEN."
#ifndef TOUCH_OFFSET_X
#error "TOUCH_OFFSET_X must be defined with TOUCH_SCREEN."
#endif
#ifndef XPT2046_Y_OFFSET
#error "XPT2046_Y_OFFSET must be defined with TOUCH_SCREEN."
#ifndef TOUCH_OFFSET_Y
#error "TOUCH_OFFSET_Y must be defined with TOUCH_SCREEN."
#endif
#endif

Expand Down
109 changes: 91 additions & 18 deletions Marlin/src/lcd/dogm/u8g_dev_tft_upscale_from_128x64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ TFT_IO tftio;

#include "../touch/touch_buttons.h"

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

#define X_HI (UPSCALE(TFT_PIXEL_OFFSET_X, WIDTH) - 1)
#define Y_HI (UPSCALE(TFT_PIXEL_OFFSET_Y, HEIGHT) - 1)

Expand Down Expand Up @@ -129,7 +134,7 @@ static void setWindow(u8g_t *u8g, u8g_dev_t *dev, uint16_t Xmin, uint16_t Ymin,
tftio.set_window(Xmin, Ymin, Xmax, Ymax);
}

#if HAS_TOUCH_XPT2046
#if HAS_TOUCH_BUTTONS

static const uint8_t buttonD[] = {
B01111111,B11111111,B11111111,B11111110,
Expand Down Expand Up @@ -302,7 +307,7 @@ static void setWindow(u8g_t *u8g, u8g_dev_t *dev, uint16_t Xmin, uint16_t Ymin,
}
}

#endif // HAS_TOUCH_XPT2046
#endif // HAS_TOUCH_BUTTONS

// Used to fill RGB565 (16bits) background
inline void memset2(const void *ptr, uint16_t fill, size_t cnt) {
Expand All @@ -313,6 +318,27 @@ inline void memset2(const void *ptr, uint16_t fill, size_t cnt) {
static bool preinit = true;
static uint8_t page;

#if HAS_TOUCH_BUTTONS
static bool redrawTouchButtons = true;
static void drawTouchButtons(u8g_t *u8g, u8g_dev_t *dev) {
if (!redrawTouchButtons) return;
redrawTouchButtons = false;

// Bottom buttons
setWindow(u8g, dev, BUTTOND_X_LO, BUTTON_Y_LO, BUTTOND_X_HI, BUTTON_Y_HI);
drawImage(buttonD, u8g, dev, BUTTON_DRAW_WIDTH, BUTTON_DRAW_HEIGHT, TFT_BTCANCEL_COLOR);

setWindow(u8g, dev, BUTTONA_X_LO, BUTTON_Y_LO, BUTTONA_X_HI, BUTTON_Y_HI);
drawImage(buttonA, u8g, dev, BUTTON_DRAW_WIDTH, BUTTON_DRAW_HEIGHT, TFT_BTARROWS_COLOR);

setWindow(u8g, dev, BUTTONB_X_LO, BUTTON_Y_LO, BUTTONB_X_HI, BUTTON_Y_HI);
drawImage(buttonB, u8g, dev, BUTTON_DRAW_WIDTH, BUTTON_DRAW_HEIGHT, TFT_BTARROWS_COLOR);

setWindow(u8g, dev, BUTTONC_X_LO, BUTTON_Y_LO, BUTTONC_X_HI, BUTTON_Y_HI);
drawImage(buttonC, u8g, dev, BUTTON_DRAW_WIDTH, BUTTON_DRAW_HEIGHT, TFT_BTOKMENU_COLOR);
}
#endif // HAS_TOUCH_BUTTONS

uint8_t u8g_dev_tft_320x240_upscale_from_128x64_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg) {
u8g_pb_t *pb = (u8g_pb_t *)(dev->dev_mem);

Expand All @@ -328,6 +354,7 @@ uint8_t u8g_dev_tft_320x240_upscale_from_128x64_fn(u8g_t *u8g, u8g_dev_t *dev, u
dev->com_fn(u8g, U8G_COM_MSG_INIT, U8G_SPI_CLK_CYCLE_NONE, nullptr);
tftio.Init();
tftio.InitTFT();
TERN_(TOUCH_SCREEN_CALIBRATION, touch_calibration.calibration_reset());

if (preinit) {
preinit = false;
Expand All @@ -343,28 +370,13 @@ uint8_t u8g_dev_tft_320x240_upscale_from_128x64_fn(u8g_t *u8g, u8g_dev_t *dev, u
for (uint16_t i = 0; i < (TFT_HEIGHT) * sq(GRAPHICAL_TFT_UPSCALE); i++)
u8g_WriteSequence(u8g, dev, (TFT_WIDTH) / 2, (uint8_t *)buffer);
#endif

// Bottom buttons
#if HAS_TOUCH_XPT2046
setWindow(u8g, dev, BUTTOND_X_LO, BUTTON_Y_LO, BUTTOND_X_HI, BUTTON_Y_HI);
drawImage(buttonD, u8g, dev, BUTTON_DRAW_WIDTH, BUTTON_DRAW_HEIGHT, TFT_BTCANCEL_COLOR);

setWindow(u8g, dev, BUTTONA_X_LO, BUTTON_Y_LO, BUTTONA_X_HI, BUTTON_Y_HI);
drawImage(buttonA, u8g, dev, BUTTON_DRAW_WIDTH, BUTTON_DRAW_HEIGHT, TFT_BTARROWS_COLOR);

setWindow(u8g, dev, BUTTONB_X_LO, BUTTON_Y_LO, BUTTONB_X_HI, BUTTON_Y_HI);
drawImage(buttonB, u8g, dev, BUTTON_DRAW_WIDTH, BUTTON_DRAW_HEIGHT, TFT_BTARROWS_COLOR);

setWindow(u8g, dev, BUTTONC_X_LO, BUTTON_Y_LO, BUTTONC_X_HI, BUTTON_Y_HI);
drawImage(buttonC, u8g, dev, BUTTON_DRAW_WIDTH, BUTTON_DRAW_HEIGHT, TFT_BTOKMENU_COLOR);
#endif // HAS_TOUCH_XPT2046

return 0;

case U8G_DEV_MSG_STOP: preinit = true; break;

case U8G_DEV_MSG_PAGE_FIRST:
page = 0;
TERN_(HAS_TOUCH_BUTTONS, drawTouchButtons(u8g, dev));
setWindow(u8g, dev, TFT_PIXEL_OFFSET_X, TFT_PIXEL_OFFSET_Y, X_HI, Y_HI);
break;

Expand Down Expand Up @@ -456,4 +468,65 @@ uint8_t u8g_com_hal_tft_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_p

U8G_PB_DEV(u8g_dev_tft_320x240_upscale_from_128x64, WIDTH, HEIGHT, PAGE_HEIGHT, u8g_dev_tft_320x240_upscale_from_128x64_fn, U8G_COM_HAL_TFT_FN);

#if ENABLED(TOUCH_SCREEN_CALIBRATION)

static void drawCross(uint16_t x, uint16_t y, uint16_t color) {
tftio.set_window(x - 15, y, x + 15, y);
tftio.WriteMultiple(color, 31);
tftio.set_window(x, y - 15, x, y + 15);
tftio.WriteMultiple(color, 31);
}

void MarlinUI::touch_calibration_screen() {
uint16_t x, y;
calibrationState calibration_stage = touch_calibration.get_calibration_state();

if (calibration_stage == CALIBRATION_NONE) {
// start and clear screen
defer_status_screen(true);
calibration_stage = touch_calibration.calibration_start();
tftio.set_window(0, 0, (TFT_WIDTH) - 1, (TFT_HEIGHT) - 1);
tftio.WriteMultiple(TFT_MARLINBG_COLOR, uint32_t(TFT_WIDTH) * (TFT_HEIGHT));
}
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, TFT_MARLINBG_COLOR);
}

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, TFT_MARLINUI_COLOR);
}
else {
// end calibration
str = calibration_stage == CALIBRATION_SUCCESS ? GET_TEXT(MSG_CALIBRATION_COMPLETED) : GET_TEXT(MSG_CALIBRATION_FAILED);
defer_status_screen(false);
touch_calibration.calibration_end();
TERN_(HAS_TOUCH_BUTTONS, redrawTouchButtons = true);
}

// draw current message
tftio.set_window(TFT_PIXEL_OFFSET_X, TFT_PIXEL_OFFSET_Y, X_HI, Y_HI);
do {
set_font(FONT_MENU);
lcd_put_u8str(0, LCD_PIXEL_HEIGHT / 2, str);
} while (u8g.nextPage());
drawing_screen = false;
}

#endif // TOUCH_SCREEN_CALIBRATION

#endif // HAS_MARLINUI_U8GLIB && (FSMC_CS_PIN || HAS_SPI_GRAPHICAL_TFT)
Loading

0 comments on commit ea37161

Please sign in to comment.