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,48 +88,27 @@ 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); {
valid = true;
if (irq == 0) { x = xpt2046_cmd(CMD_X_READ);
#endif y = xpt2046_cmd(CMD_Y_READ);
#if XPT2046_TOUCH_CHECK != 0 ESP_LOGI(TAG, "P(%d,%d)", x, y);
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 /*Normalize Data back to 12-bits*/
// be enough to detect real touches on the panel x = x >> 4;
int16_t z = z1 + 4096 - z2; y = y >> 4;
ESP_LOGI(TAG, "P_norm(%d,%d)", x, y);
// seems the irq can be noisy so we only accept this as a touch if xpt2046_corr(&x, &y);
// there is some pressure (z) detected xpt2046_avg(&x, &y);
if (z >= Z_MIN) last_x = x;
{ last_y = y;
#endif
valid = true;
x = xpt2046_cmd(CMD_X_READ); ESP_LOGI(TAG, "x = %d, y = %d", x, y);
y = xpt2046_cmd(CMD_Y_READ);
ESP_LOGI(TAG, "P(%d,%d)", x, y);
/*Normalize Data back to 12-bits*/
x = x >> 4;
y = y >> 4;
ESP_LOGI(TAG, "P_norm(%d,%d)", x, y);
xpt2046_corr(&x, &y);
xpt2046_avg(&x, &y);
last_x = x;
last_y = y;
ESP_LOGI(TAG, "x = %d, y = %d", x, y);
#if XPT2046_TOUCH_CHECK != 0
}
#endif
#if XPT2046_TOUCH_ONLY == 0
} }
#endif else
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

@ -27,15 +27,16 @@ extern "C" {
*********************/ *********************/
#define XPT2046_IRQ CONFIG_LV_TOUCH_PIN_IRQ #define XPT2046_IRQ CONFIG_LV_TOUCH_PIN_IRQ
#define XPT2046_AVG 4 #define XPT2046_AVG 4
#define XPT2046_X_MIN CONFIG_LV_TOUCH_X_MIN #define XPT2046_X_MIN CONFIG_LV_TOUCH_X_MIN
#define XPT2046_Y_MIN CONFIG_LV_TOUCH_Y_MIN #define XPT2046_Y_MIN CONFIG_LV_TOUCH_Y_MIN
#define XPT2046_X_MAX CONFIG_LV_TOUCH_X_MAX #define XPT2046_X_MAX CONFIG_LV_TOUCH_X_MAX
#define XPT2046_Y_MAX CONFIG_LV_TOUCH_Y_MAX #define XPT2046_Y_MAX CONFIG_LV_TOUCH_Y_MAX
#define XPT2046_X_INV CONFIG_LV_TOUCH_INVERT_X #define XPT2046_X_INV CONFIG_LV_TOUCH_INVERT_X
#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