Merge pull request #9 from liebman/use_z_test_for_press

XPT2046 optionally (Kconfig option) validate a touch with pressure, not just IRQ low
This commit is contained in:
Carlos Diaz 2020-12-29 21:44:40 -06:00 committed by GitHub
commit 6b1e219a87
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 90 additions and 35 deletions

View file

@ -99,7 +99,6 @@ menu "LVGL Touch controller"
range 0 39
default 35 if LV_PREDEFINED_PINS_38V1
default 19
help
Configure the touchpanel MISO pin here.
@ -109,7 +108,6 @@ menu "LVGL Touch controller"
range 0 39
default 32 if LV_PREDEFINED_PINS_38V1
default 23
help
Configure the touchpanel MOSI pin here.
@ -135,7 +133,7 @@ menu "LVGL Touch controller"
default 27 if LV_PREDEFINED_PINS_38V4
default 25
help
Configure the touchpanel CS pin here.
Configure the touchpanel IRQ pin here.
endmenu
menu "Touchpanel Configuration (XPT2046)"
@ -180,6 +178,19 @@ menu "LVGL Touch controller"
prompt "Invert Y coordinate value."
default y
choice
prompt "Select touch detection method."
default LV_TOUCH_DETECT_IRQ
help
Select the controller for your touch panel.
config LV_TOUCH_DETECT_IRQ
bool "IRQ pin only"
config LV_TOUCH_DETECT_IRQ_PRESSURE
bool "IRQ pin and pressure"
config LV_TOUCH_DETECT_PRESSURE
bool "Pressure only"
endchoice
endmenu
menu "Touchpanel (FT6X06) Pin Assignments"

View file

@ -18,18 +18,26 @@
*********************/
#define TAG "XPT2046"
#define CMD_X_READ 0b10010000
#define CMD_Y_READ 0b11010000
#define CMD_X_READ 0b10010000 // NOTE: XPT2046 data sheet says this is actually Y
#define CMD_Y_READ 0b11010000 // NOTE: XPT2046 data sheet says this is actually X
#define CMD_Z1_READ 0b10110000
#define CMD_Z2_READ 0b11000000
/**********************
* TYPEDEFS
**********************/
typedef enum {
TOUCH_NOT_DETECTED = 0,
TOUCH_DETECTED = 1,
} xpt2046_touch_detect_t;
/**********************
* STATIC PROTOTYPES
**********************/
static void xpt2046_corr(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 xpt2046_touch_detect_t xpt2048_is_touch_detected();
/**********************
* STATIC VARIABLES
@ -51,6 +59,9 @@ uint8_t avg_last;
*/
void xpt2046_init(void)
{
ESP_LOGI(TAG, "XPT2046 Initialization");
#if XPT2046_TOUCH_IRQ || XPT2046_TOUCH_IRQ_PRESS
gpio_config_t irq_config = {
.pin_bit_mask = BIT64(XPT2046_IRQ),
.mode = GPIO_MODE_INPUT,
@ -59,10 +70,9 @@ void xpt2046_init(void)
.intr_type = GPIO_INTR_DISABLE,
};
ESP_LOGI(TAG, "XPT2046 Initialization");
esp_err_t ret = gpio_config(&irq_config);
assert(ret == ESP_OK);
#endif
}
/**
@ -74,39 +84,33 @@ bool xpt2046_read(lv_indev_drv_t * drv, lv_indev_data_t * data)
{
static int16_t last_x = 0;
static int16_t last_y = 0;
bool valid = true;
bool valid = false;
int16_t x = 0;
int16_t y = 0;
int16_t x = last_x;
int16_t y = last_y;
if (xpt2048_is_touch_detected() == TOUCH_DETECTED)
{
valid = true;
uint8_t irq = gpio_get_level(XPT2046_IRQ);
x = xpt2046_cmd(CMD_X_READ);
y = xpt2046_cmd(CMD_Y_READ);
ESP_LOGI(TAG, "P(%d,%d)", x, y);
if (irq == 0) {
uint8_t data[2];
tp_spi_read_reg(CMD_X_READ, data, 2);
x = (data[0] << 8) | data[1];
tp_spi_read_reg(CMD_Y_READ, data, 2);
y = (data[0] << 8) | data[1];
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);
} else {
x = last_x;
y = last_y;
ESP_LOGI(TAG, "x = %d, y = %d", x, y);
}
else
{
avg_last = 0;
valid = false;
}
data->point.x = x;
@ -119,6 +123,42 @@ bool xpt2046_read(lv_indev_drv_t * drv, lv_indev_data_t * data)
/**********************
* STATIC FUNCTIONS
**********************/
static xpt2046_touch_detect_t xpt2048_is_touch_detected()
{
// check IRQ pin if we IRQ or IRQ and preessure
#if XPT2046_TOUCH_IRQ || XPT2046_TOUCH_IRQ_PRESS
uint8_t irq = gpio_get_level(XPT2046_IRQ);
if (irq != 0) {
return TOUCH_NOT_DETECTED;
}
#endif
// check pressure if we are pressure or IRQ and pressure
#if XPT2046_TOUCH_PRESS || XPT2046_TOUCH_IRQ_PRESS
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)
{
uint8_t data[2];
tp_spi_read_reg(cmd, data, 2);
int16_t val = (data[0] << 8) | data[1];
return val;
}
static void xpt2046_corr(int16_t * x, int16_t * y)
{
#if XPT2046_XY_SWAP != 0

View file

@ -27,14 +27,18 @@ extern "C" {
*********************/
#define XPT2046_IRQ CONFIG_LV_TOUCH_PIN_IRQ
#define XPT2046_AVG 4
#define XPT2046_X_MIN CONFIG_LV_TOUCH_X_MIN
#define XPT2046_Y_MIN CONFIG_LV_TOUCH_Y_MIN
#define XPT2046_X_MAX CONFIG_LV_TOUCH_X_MAX
#define XPT2046_Y_MAX CONFIG_LV_TOUCH_Y_MAX
#define XPT2046_X_INV CONFIG_LV_TOUCH_INVERT_X
#define XPT2046_Y_INV CONFIG_LV_TOUCH_INVERT_Y
#define XPT2046_XY_SWAP CONFIG_LV_TOUCH_XY_SWAP
#define XPT2046_AVG 4
#define XPT2046_X_MIN CONFIG_LV_TOUCH_X_MIN
#define XPT2046_Y_MIN CONFIG_LV_TOUCH_Y_MIN
#define XPT2046_X_MAX CONFIG_LV_TOUCH_X_MAX
#define XPT2046_Y_MAX CONFIG_LV_TOUCH_Y_MAX
#define XPT2046_X_INV CONFIG_LV_TOUCH_INVERT_X
#define XPT2046_Y_INV CONFIG_LV_TOUCH_INVERT_Y
#define XPT2046_XY_SWAP CONFIG_LV_TOUCH_XY_SWAP
#define XPT2046_TOUCH_THRESHOLD 400 // Threshold for touch detection
#define XPT2046_TOUCH_IRQ CONFIG_LV_TOUCH_DETECT_IRQ
#define XPT2046_TOUCH_IRQ_PRESS CONFIG_LV_TOUCH_DETECT_IRQ_PRESSURE
#define XPT2046_TOUCH_PRESS CONFIG_LV_TOUCH_DETECT_PRESSURE
/**********************
* TYPEDEFS