From 2d4ce2ac0ec183757c48b32efd26525362e7e2da Mon Sep 17 00:00:00 2001 From: Tanguy Pruvot Date: Sat, 4 Sep 2021 11:50:33 +0200 Subject: [PATCH] Move touch sleep outside low level xpt class --- Marlin/src/HAL/LPC1768/tft/xpt2046.cpp | 32 +------------- Marlin/src/HAL/LPC1768/tft/xpt2046.h | 10 ----- Marlin/src/HAL/STM32/tft/xpt2046.cpp | 31 +------------- Marlin/src/HAL/STM32/tft/xpt2046.h | 10 ----- Marlin/src/HAL/STM32F1/tft/xpt2046.cpp | 32 +------------- Marlin/src/HAL/STM32F1/tft/xpt2046.h | 10 ----- .../dogm/u8g_dev_tft_upscale_from_128x64.cpp | 8 ++-- Marlin/src/lcd/tft/touch.cpp | 42 ++++++++++++------- Marlin/src/lcd/tft/touch.h | 13 ++++-- Marlin/src/lcd/tft/ui_common.h | 2 +- Marlin/src/lcd/touch/touch_buttons.cpp | 38 ++++++++++++++--- Marlin/src/lcd/touch/touch_buttons.h | 10 ++++- 12 files changed, 85 insertions(+), 153 deletions(-) diff --git a/Marlin/src/HAL/LPC1768/tft/xpt2046.cpp b/Marlin/src/HAL/LPC1768/tft/xpt2046.cpp index 02c41f328442..9c1e158981da 100644 --- a/Marlin/src/HAL/LPC1768/tft/xpt2046.cpp +++ b/Marlin/src/HAL/LPC1768/tft/xpt2046.cpp @@ -43,8 +43,6 @@ uint16_t delta(uint16_t a, uint16_t b) { return a > b ? a - b : b - a; } } #endif -millis_t XPT2046::lastTouch; - void XPT2046::Init() { SET_INPUT(TOUCH_MISO_PIN); SET_OUTPUT(TOUCH_MOSI_PIN); @@ -58,46 +56,18 @@ void XPT2046::Init() { TERN_(TOUCH_BUTTONS_HW_SPI, touch_spi_init(SPI_SPEED_6)); - lastTouch = millis(); - // Read once to enable pendrive status pin getRawData(XPT2046_X); } -void XPT2046::doSleep() { - #if PIN_EXISTS(TFT_BACKLIGHT) - OUT_WRITE(TFT_BACKLIGHT_PIN, LOW); - #endif - lastTouch = TSLP_SLEEPING; -} - -void XPT2046::doWakeUp() { - #if PIN_EXISTS(TFT_BACKLIGHT) - WRITE(TFT_BACKLIGHT_PIN, HIGH); - #endif - lastTouch = millis(); -} - bool XPT2046::isTouched() { - bool touched = isBusy() ? false : ( + return isBusy() ? false : ( #if PIN_EXISTS(TOUCH_INT) READ(TOUCH_INT_PIN) != HIGH #else getRawData(XPT2046_Z1) >= XPT2046_Z1_THRESHOLD #endif ); - #if defined(TOUCH_IDLE_SLEEP) - if (touched) { - if (lastTouch == TSLP_SLEEPING) { - doWakeUp(); - touched = false; - } - else lastTouch = millis(); - } - else if (lastTouch != TSLP_SLEEPING && (millis() - lastTouch) > (TOUCH_IDLE_SLEEP*1000)) - doSleep(); - #endif - return touched; } bool XPT2046::getRawPoint(int16_t *x, int16_t *y) { diff --git a/Marlin/src/HAL/LPC1768/tft/xpt2046.h b/Marlin/src/HAL/LPC1768/tft/xpt2046.h index 485dbb6943d5..aba0799e445f 100644 --- a/Marlin/src/HAL/LPC1768/tft/xpt2046.h +++ b/Marlin/src/HAL/LPC1768/tft/xpt2046.h @@ -47,11 +47,6 @@ #define XPT2046_SER_MODE 0x04 #define XPT2046_CONTROL 0x80 -enum { - TSLP_PREINIT = 0, - TSLP_SLEEPING = 1 -}; - enum XPTCoordinate : uint8_t { XPT2046_X = 0x10 | XPT2046_CONTROL | XPT2046_DFR_MODE, XPT2046_Y = 0x50 | XPT2046_CONTROL | XPT2046_DFR_MODE, @@ -85,9 +80,4 @@ class XPT2046 { static void Init(); static bool getRawPoint(int16_t *x, int16_t *y); - - static millis_t lastTouch; - static bool isSleeping() { return (lastTouch == TSLP_SLEEPING); } - static void doSleep(); - static void doWakeUp(); }; diff --git a/Marlin/src/HAL/STM32/tft/xpt2046.cpp b/Marlin/src/HAL/STM32/tft/xpt2046.cpp index e9220439a0c9..912e6c2db761 100644 --- a/Marlin/src/HAL/STM32/tft/xpt2046.cpp +++ b/Marlin/src/HAL/STM32/tft/xpt2046.cpp @@ -33,7 +33,6 @@ uint16_t delta(uint16_t a, uint16_t b) { return a > b ? a - b : b - a; } SPI_HandleTypeDef XPT2046::SPIx; -millis_t XPT2046::lastTouch; void XPT2046::Init() { SPI_TypeDef *spiInstance; @@ -93,45 +92,17 @@ void XPT2046::Init() { SET_OUTPUT(TOUCH_SCK_PIN); } - lastTouch = millis(); - getRawData(XPT2046_Z1); } -void XPT2046::doSleep() { - #if PIN_EXISTS(TFT_BACKLIGHT) - OUT_WRITE(TFT_BACKLIGHT_PIN, LOW); - #endif - lastTouch = TSLP_SLEEPING; -} - -void XPT2046::doWakeUp() { - #if PIN_EXISTS(TFT_BACKLIGHT) - WRITE(TFT_BACKLIGHT_PIN, HIGH); - #endif - lastTouch = millis(); -} - bool XPT2046::isTouched() { - bool touched = isBusy() ? false : ( + return isBusy() ? false : ( #if PIN_EXISTS(TOUCH_INT) READ(TOUCH_INT_PIN) != HIGH #else getRawData(XPT2046_Z1) >= XPT2046_Z1_THRESHOLD #endif ); - #if defined(TOUCH_IDLE_SLEEP) - if (touched) { - if (lastTouch == TSLP_SLEEPING) { - doWakeUp(); - touched = false; - } - else lastTouch = millis(); - } - else if (lastTouch != TSLP_SLEEPING && (millis() - lastTouch) > (TOUCH_IDLE_SLEEP*1000)) - doSleep(); - #endif - return touched; } bool XPT2046::getRawPoint(int16_t *x, int16_t *y) { diff --git a/Marlin/src/HAL/STM32/tft/xpt2046.h b/Marlin/src/HAL/STM32/tft/xpt2046.h index c3d15a122005..2cff3e29d05b 100644 --- a/Marlin/src/HAL/STM32/tft/xpt2046.h +++ b/Marlin/src/HAL/STM32/tft/xpt2046.h @@ -49,11 +49,6 @@ #define XPT2046_SER_MODE 0x04 #define XPT2046_CONTROL 0x80 -enum { - TSLP_PREINIT = 0, - TSLP_SLEEPING = 1 -}; - enum XPTCoordinate : uint8_t { XPT2046_X = 0x10 | XPT2046_CONTROL | XPT2046_DFR_MODE, XPT2046_Y = 0x50 | XPT2046_CONTROL | XPT2046_DFR_MODE, @@ -83,9 +78,4 @@ class XPT2046 { public: static void Init(); static bool getRawPoint(int16_t *x, int16_t *y); - - static millis_t lastTouch; - static bool isSleeping() { return (lastTouch == TSLP_SLEEPING); } - static void doSleep(); - static void doWakeUp(); }; diff --git a/Marlin/src/HAL/STM32F1/tft/xpt2046.cpp b/Marlin/src/HAL/STM32F1/tft/xpt2046.cpp index 9308f24419f7..ac9ad072aa05 100644 --- a/Marlin/src/HAL/STM32F1/tft/xpt2046.cpp +++ b/Marlin/src/HAL/STM32F1/tft/xpt2046.cpp @@ -27,8 +27,6 @@ #include "xpt2046.h" #include -millis_t XPT2046::lastTouch; - uint16_t delta(uint16_t a, uint16_t b) { return a > b ? a - b : b - a; } #if ENABLED(TOUCH_BUTTONS_HW_SPI) @@ -72,46 +70,18 @@ void XPT2046::Init() { TERN_(TOUCH_BUTTONS_HW_SPI, touch_spi_init(SPI_SPEED_6)); - lastTouch = millis(); - // Read once to enable pendrive status pin getRawData(XPT2046_X); } -void XPT2046::doSleep() { - #if PIN_EXISTS(TFT_BACKLIGHT) - OUT_WRITE(TFT_BACKLIGHT_PIN, LOW); - #endif - lastTouch = TSLP_SLEEPING; -} - -void XPT2046::doWakeUp() { - #if PIN_EXISTS(TFT_BACKLIGHT) - WRITE(TFT_BACKLIGHT_PIN, HIGH); - #endif - lastTouch = millis(); -} - bool XPT2046::isTouched() { - bool touched = isBusy() ? false : ( + return isBusy() ? false : ( #if PIN_EXISTS(TOUCH_INT) READ(TOUCH_INT_PIN) != HIGH #else getRawData(XPT2046_Z1) >= XPT2046_Z1_THRESHOLD #endif ); - #if defined(TOUCH_IDLE_SLEEP) - if (touched) { - if (lastTouch == TSLP_SLEEPING) { - doWakeUp(); - touched = false; - } - else lastTouch = millis(); - } - else if (lastTouch != TSLP_SLEEPING && (millis() - lastTouch) > (TOUCH_IDLE_SLEEP*1000)) - doSleep(); - #endif - return touched; } bool XPT2046::getRawPoint(int16_t *x, int16_t *y) { diff --git a/Marlin/src/HAL/STM32F1/tft/xpt2046.h b/Marlin/src/HAL/STM32F1/tft/xpt2046.h index fd1f7817e6b3..aba0799e445f 100644 --- a/Marlin/src/HAL/STM32F1/tft/xpt2046.h +++ b/Marlin/src/HAL/STM32F1/tft/xpt2046.h @@ -54,11 +54,6 @@ enum XPTCoordinate : uint8_t { XPT2046_Z2 = 0x40 | XPT2046_CONTROL | XPT2046_DFR_MODE, }; -enum { - TSLP_PREINIT = 0, - TSLP_SLEEPING = 1 -}; - #ifndef XPT2046_Z1_THRESHOLD #define XPT2046_Z1_THRESHOLD 10 #endif @@ -85,9 +80,4 @@ class XPT2046 { static void Init(); static bool getRawPoint(int16_t *x, int16_t *y); - - static millis_t lastTouch; - static bool isSleeping() { return (lastTouch == TSLP_SLEEPING); } - static void doSleep(); - static void doWakeUp(); }; diff --git a/Marlin/src/lcd/dogm/u8g_dev_tft_upscale_from_128x64.cpp b/Marlin/src/lcd/dogm/u8g_dev_tft_upscale_from_128x64.cpp index bf38118b1d9e..e1700021224e 100644 --- a/Marlin/src/lcd/dogm/u8g_dev_tft_upscale_from_128x64.cpp +++ b/Marlin/src/lcd/dogm/u8g_dev_tft_upscale_from_128x64.cpp @@ -80,10 +80,8 @@ TFT_IO tftio; #include "../marlinui.h" #endif -#define HAS_TOUCH_SLEEP (ENABLED(TFT_TOUCH_DEVICE_XPT2046) && defined(TOUCH_IDLE_SLEEP)) +#define HAS_TOUCH_SLEEP (defined(TOUCH_IDLE_SLEEP) && TOUCH_IDLE_SLEEP > 0 && HAS_TOUCH_BUTTONS) #if HAS_TOUCH_SLEEP - #include HAL_PATH(../../HAL, tft/xpt2046.h) - extern XPT2046 touchIO; static bool sleepCleared; #endif @@ -392,7 +390,7 @@ uint8_t u8g_dev_tft_320x240_upscale_from_128x64_fn(u8g_t *u8g, u8g_dev_t *dev, u case U8G_DEV_MSG_PAGE_FIRST: page = 0; #if HAS_TOUCH_SLEEP - if (touchIO.isSleeping()) { + if (touchBt.isSleeping()) { if (!sleepCleared) { sleepCleared = true; u8g_upscale_clear_lcd(u8g, dev, buffer); @@ -408,7 +406,7 @@ uint8_t u8g_dev_tft_320x240_upscale_from_128x64_fn(u8g_t *u8g, u8g_dev_t *dev, u case U8G_DEV_MSG_PAGE_NEXT: #if HAS_TOUCH_SLEEP - if (touchIO.isSleeping()) break; + if (touchBt.isSleeping()) break; #endif if (++page > (HEIGHT / PAGE_HEIGHT)) return 1; diff --git a/Marlin/src/lcd/tft/touch.cpp b/Marlin/src/lcd/tft/touch.cpp index 5cb098727b8b..c62ff82cbbbb 100644 --- a/Marlin/src/lcd/tft/touch.cpp +++ b/Marlin/src/lcd/tft/touch.cpp @@ -48,6 +48,9 @@ millis_t Touch::last_touch_ms = 0, Touch::repeat_delay, Touch::touch_time; TouchControlType Touch::touch_control_type = NONE; +#if TOUCH_IDLE_SLEEP > 0 + millis_t Touch::last_touched_ms; +#endif #if HAS_RESUME_CONTINUE extern bool wait_for_user; #endif @@ -56,6 +59,9 @@ void Touch::init() { TERN_(TOUCH_SCREEN_CALIBRATION, touch_calibration.calibration_reset()); reset(); io.Init(); + #if TOUCH_IDLE_SLEEP > 0 + last_touched_ms = millis(); + #endif enable(); } @@ -271,23 +277,31 @@ bool Touch::get_point(int16_t *x, int16_t *y) { #elif ENABLED(TFT_TOUCH_DEVICE_GT911) bool is_touched = (TOUCH_ORIENTATION == TOUCH_PORTRAIT ? io.getPoint(y, x) : io.getPoint(x, y)); #endif - - return is_touched; -} - -bool Touch::isSleeping() { - #if ENABLED(TFT_TOUCH_DEVICE_XPT2046) - return io.isSleeping(); - #else - return false; + #if TOUCH_IDLE_SLEEP > 0 + if (is_touched) { + wakeUp(); + } else if (last_touched_ms != TSLP_SLEEPING && (millis() - last_touched_ms) > (TOUCH_IDLE_SLEEP*1000)) + sleepTimeout(); #endif + return is_touched; } -void Touch::wakeUp() { - #if ENABLED(TFT_TOUCH_DEVICE_XPT2046) - if (io.isSleeping()) io.doWakeUp(); - #endif -} +#if TOUCH_IDLE_SLEEP > 0 + void Touch::sleepTimeout() { + #if PIN_EXISTS(TFT_BACKLIGHT) + OUT_WRITE(TFT_BACKLIGHT_PIN, LOW); + #endif + last_touched_ms = TSLP_SLEEPING; + } + void Touch::wakeUp() { + if (isSleeping()) { + #if PIN_EXISTS(TFT_BACKLIGHT) + WRITE(TFT_BACKLIGHT_PIN, HIGH); + #endif + } + last_touched_ms = millis(); + } +#endif // TOUCH_IDLE_SLEEP Touch touch; diff --git a/Marlin/src/lcd/tft/touch.h b/Marlin/src/lcd/tft/touch.h index f84fbe359f26..8c353cf4aa13 100644 --- a/Marlin/src/lcd/tft/touch.h +++ b/Marlin/src/lcd/tft/touch.h @@ -90,6 +90,9 @@ typedef struct __attribute__((__packed__)) { #define UBL_REPEAT_DELAY 125 #define FREE_MOVE_RANGE 32 +#define TSLP_PREINIT 0 +#define TSLP_SLEEPING 1 + class Touch { private: static TOUCH_DRIVER_CLASS io; @@ -121,10 +124,12 @@ class Touch { } static void disable() { enabled = false; } static void enable() { enabled = true; } - - static bool isSleeping(); - static void wakeUp(); - + #if TOUCH_IDLE_SLEEP > 0 + static millis_t last_touched_ms; + static bool isSleeping() { return (last_touched_ms == TSLP_SLEEPING); } + static void sleepTimeout(); + static void wakeUp(); + #endif static void add_control(TouchControlType type, uint16_t x, uint16_t y, uint16_t width, uint16_t height, intptr_t data = 0); }; diff --git a/Marlin/src/lcd/tft/ui_common.h b/Marlin/src/lcd/tft/ui_common.h index 3a761207ac17..8ea18e4bba76 100644 --- a/Marlin/src/lcd/tft/ui_common.h +++ b/Marlin/src/lcd/tft/ui_common.h @@ -30,7 +30,7 @@ #include "tft.h" #include "tft_image.h" -#define HAS_TOUCH_SLEEP (ENABLED(TFT_TOUCH_DEVICE_XPT2046) && defined(TOUCH_IDLE_SLEEP)) +#define HAS_TOUCH_SLEEP (defined(TOUCH_IDLE_SLEEP) && TOUCH_IDLE_SLEEP > 0) #if ENABLED(TOUCH_SCREEN) #include "touch.h" diff --git a/Marlin/src/lcd/touch/touch_buttons.cpp b/Marlin/src/lcd/touch/touch_buttons.cpp index c1243930fe66..2a70ec3d8d6c 100644 --- a/Marlin/src/lcd/touch/touch_buttons.cpp +++ b/Marlin/src/lcd/touch/touch_buttons.cpp @@ -38,6 +38,10 @@ #include "../tft_io/touch_calibration.h" #endif +#if TOUCH_IDLE_SLEEP > 0 + millis_t TouchButtons::last_touch_ms; +#endif + #include "../buttons.h" // For EN_C bit mask #include "../marlinui.h" // For ui.refresh #include "../tft_io/tft_io.h" @@ -52,13 +56,24 @@ TouchButtons touchBt; -void TouchButtons::init() { touchIO.Init(); } +void TouchButtons::init() { + touchIO.Init(); + #if TOUCH_IDLE_SLEEP > 0 + last_touch_ms = millis(); + #endif +} uint8_t TouchButtons::read_buttons() { #ifdef HAS_WIRED_LCD int16_t x, y; const bool is_touched = (TERN(TOUCH_SCREEN_CALIBRATION, touch_calibration.calibration.orientation, TOUCH_ORIENTATION) == TOUCH_PORTRAIT ? touchIO.getRawPoint(&y, &x) : touchIO.getRawPoint(&x, &y)); + #if TOUCH_IDLE_SLEEP > 0 + if (is_touched) { + wakeUp(); + } else if (last_touch_ms != TSLP_SLEEPING && (millis() - last_touch_ms) > (TOUCH_IDLE_SLEEP*1000)) + sleepTimeout(); + #endif if (!is_touched) return 0; #if ENABLED(TOUCH_SCREEN_CALIBRATION) @@ -96,10 +111,21 @@ uint8_t TouchButtons::read_buttons() { return 0; } -void TouchButtons::wakeUp() { - #if ENABLED(TFT_TOUCH_DEVICE_XPT2046) - if (touchIO.isSleeping()) touchIO.doWakeUp(); - #endif -} +#if TOUCH_IDLE_SLEEP > 0 + void TouchButtons::sleepTimeout() { + #if PIN_EXISTS(TFT_BACKLIGHT) + OUT_WRITE(TFT_BACKLIGHT_PIN, LOW); + #endif + last_touch_ms = TSLP_SLEEPING; + } + void TouchButtons::wakeUp() { + if (isSleeping()) { + #if PIN_EXISTS(TFT_BACKLIGHT) + WRITE(TFT_BACKLIGHT_PIN, HIGH); + #endif + } + last_touch_ms = millis(); + } +#endif // TOUCH_IDLE_SLEEP #endif // HAS_TOUCH_BUTTONS diff --git a/Marlin/src/lcd/touch/touch_buttons.h b/Marlin/src/lcd/touch/touch_buttons.h index 91538ff55d41..e4ab78e620f1 100644 --- a/Marlin/src/lcd/touch/touch_buttons.h +++ b/Marlin/src/lcd/touch/touch_buttons.h @@ -50,11 +50,19 @@ #define BUTTON_Y_HI (TFT_HEIGHT) - BUTTON_SPACING #define BUTTON_Y_LO BUTTON_Y_HI - BUTTON_HEIGHT +#define TSLP_PREINIT 0 +#define TSLP_SLEEPING 1 + class TouchButtons { public: static void init(); static uint8_t read_buttons(); - static void wakeUp(); + #if TOUCH_IDLE_SLEEP > 0 + static millis_t last_touch_ms; + static bool isSleeping() { return (last_touch_ms == TSLP_SLEEPING); } + static void sleepTimeout(); + static void wakeUp(); + #endif }; extern TouchButtons touchBt;