refactoring for clarity on press/touch dection

This commit is contained in:
Christopher Liebman 2020-12-29 13:35:20 -08:00
parent c445eca696
commit eaf43e40fa
2 changed files with 59 additions and 48 deletions

View file

@ -22,11 +22,14 @@
#define CMD_Y_READ 0b11010000 // NOTE: XPT2046 data sheet says this is actually X #define CMD_Y_READ 0b11010000 // NOTE: XPT2046 data sheet says this is actually X
#define CMD_Z1_READ 0b10110000 #define CMD_Z1_READ 0b10110000
#define CMD_Z2_READ 0b11000000 #define CMD_Z2_READ 0b11000000
#define Z_MIN 400
/********************** /**********************
* TYPEDEFS * TYPEDEFS
**********************/ **********************/
typedef enum {
TOUCH_NOT_DETECTED = 0,
TOUCH_DETECTED = 1,
} xpt2046_touch_detect_t;
/********************** /**********************
* STATIC PROTOTYPES * STATIC PROTOTYPES
@ -34,6 +37,7 @@
static void xpt2046_corr(int16_t * x, int16_t * y); static void xpt2046_corr(int16_t * x, int16_t * y);
static void xpt2046_avg(int16_t * x, int16_t * y); static void xpt2046_avg(int16_t * x, int16_t * y);
static int16_t xpt2046_cmd(uint8_t cmd); static int16_t xpt2046_cmd(uint8_t cmd);
static xpt2046_touch_detect_t xpt2048_is_touch_detected();
/********************** /**********************
* STATIC VARIABLES * STATIC VARIABLES
@ -84,24 +88,8 @@ bool xpt2046_read(lv_indev_drv_t * drv, lv_indev_data_t * data)
int16_t x = last_x; int16_t x = last_x;
int16_t y = last_y; int16_t y = last_y;
#if XPT2046_TOUCH_ONLY == 0 if (xpt2048_is_touch_detected() == TOUCH_DETECTED)
uint8_t irq = gpio_get_level(XPT2046_IRQ);
if (irq == 0) {
#endif
#if XPT2046_TOUCH_CHECK != 0
int16_t z1 = xpt2046_cmd(CMD_Z1_READ) >> 3;
int16_t z2 = xpt2046_cmd(CMD_Z2_READ) >> 3;
// this is not what the confusing datasheet says but it seems to
// be enough to detect real touches on the panel
int16_t z = z1 + 4096 - z2;
// seems the irq can be noisy so we only accept this as a touch if
// there is some pressure (z) detected
if (z >= Z_MIN)
{ {
#endif
valid = true; valid = true;
x = xpt2046_cmd(CMD_X_READ); x = xpt2046_cmd(CMD_X_READ);
@ -119,13 +107,8 @@ bool xpt2046_read(lv_indev_drv_t * drv, lv_indev_data_t * data)
last_y = y; last_y = y;
ESP_LOGI(TAG, "x = %d, y = %d", x, y); ESP_LOGI(TAG, "x = %d, y = %d", x, y);
#if XPT2046_TOUCH_CHECK != 0
} }
#endif else
#if XPT2046_TOUCH_ONLY == 0
}
#endif
if (!valid)
{ {
avg_last = 0; avg_last = 0;
} }
@ -140,6 +123,33 @@ bool xpt2046_read(lv_indev_drv_t * drv, lv_indev_data_t * data)
/********************** /**********************
* STATIC FUNCTIONS * STATIC FUNCTIONS
**********************/ **********************/
static xpt2046_touch_detect_t xpt2048_is_touch_detected()
{
// check IRQ if we are not touch only OR are not checking touch
#if XPT2046_TOUCH_ONLY == 0 || XPT2046_TOUCH_CHECK == 0
uint8_t irq = gpio_get_level(XPT2046_IRQ);
if (irq != 0) {
return TOUCH_NOT_DETECTED;
}
#endif
#if XPT2046_TOUCH_CHECK != 0
int16_t z1 = xpt2046_cmd(CMD_Z1_READ) >> 3;
int16_t z2 = xpt2046_cmd(CMD_Z2_READ) >> 3;
// this is not what the confusing datasheet says but it seems to
// be enough to detect real touches on the panel
int16_t z = z1 + 4096 - z2;
if (z < XPT2046_TOUCH_THRESHOLD)
{
return TOUCH_NOT_DETECTED;
}
#endif
return TOUCH_DETECTED;
}
static int16_t xpt2046_cmd(uint8_t cmd) static int16_t xpt2046_cmd(uint8_t cmd)
{ {
uint8_t data[2]; uint8_t data[2];

View file

@ -36,6 +36,7 @@ extern "C" {
#define XPT2046_Y_INV CONFIG_LV_TOUCH_INVERT_Y #define XPT2046_Y_INV CONFIG_LV_TOUCH_INVERT_Y
#define XPT2046_XY_SWAP CONFIG_LV_TOUCH_XY_SWAP #define XPT2046_XY_SWAP CONFIG_LV_TOUCH_XY_SWAP
#define XPT2046_TOUCH_CHECK CONFIG_LV_TOUCH_CHECK #define XPT2046_TOUCH_CHECK CONFIG_LV_TOUCH_CHECK
#define XPT2046_TOUCH_THRESHOLD 400 // Threshold for touch detection
#if defined(CONFIG_LV_TOUCH_ONLY) #if defined(CONFIG_LV_TOUCH_ONLY)
#define XPT2046_TOUCH_ONLY CONFIG_LV_TOUCH_ONLY #define XPT2046_TOUCH_ONLY CONFIG_LV_TOUCH_ONLY
#else #else