Skip to content

Commit

Permalink
Move touch sleep outside low level xpt class
Browse files Browse the repository at this point in the history
  • Loading branch information
tpruvot committed Sep 7, 2021
1 parent 729e7ce commit 2d4ce2a
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 153 deletions.
32 changes: 1 addition & 31 deletions Marlin/src/HAL/LPC1768/tft/xpt2046.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
10 changes: 0 additions & 10 deletions Marlin/src/HAL/LPC1768/tft/xpt2046.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
};
31 changes: 1 addition & 30 deletions Marlin/src/HAL/STM32/tft/xpt2046.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 0 additions & 10 deletions Marlin/src/HAL/STM32/tft/xpt2046.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
};
32 changes: 1 addition & 31 deletions Marlin/src/HAL/STM32F1/tft/xpt2046.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
#include "xpt2046.h"
#include <SPI.h>

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)
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 0 additions & 10 deletions Marlin/src/HAL/STM32F1/tft/xpt2046.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
};
8 changes: 3 additions & 5 deletions Marlin/src/lcd/dogm/u8g_dev_tft_upscale_from_128x64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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);
Expand All @@ -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;

Expand Down
42 changes: 28 additions & 14 deletions Marlin/src/lcd/tft/touch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}

Expand Down Expand Up @@ -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;

Expand Down
13 changes: 9 additions & 4 deletions Marlin/src/lcd/tft/touch.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
};

Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/lcd/tft/ui_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading

0 comments on commit 2d4ce2a

Please sign in to comment.