Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lvgl_touch/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,13 @@ menu "LVGL Touch controller"
prompt "Invert Y coordinate value."
default n

config LV_FT6X36_COORDINATES_QUEUE
bool
prompt "Send coordinates to FreeRTOS queue."
default n
help
Receive from the FreeRTOS queue using the handle 'ft6x36_touch_queue_handle'.

endmenu

menu "Touchpanel (STMPE610) Pin Assignments"
Expand Down
61 changes: 41 additions & 20 deletions lvgl_touch/ft6x36.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@
#include "lvgl_i2c/i2c_manager.h"

#define TAG "FT6X36"
#define FT6X36_TOUCH_QUEUE_ELEMENTS 1


ft6x36_status_t ft6x36_status;
uint8_t current_dev_addr; // set during init
static ft6x36_status_t ft6x36_status;
static uint8_t current_dev_addr; // set during init
static ft6x36_touch_t touch_inputs = { -1, -1, LV_INDEV_STATE_REL }; // -1 coordinates to designate it was never touched

esp_err_t ft6x06_i2c_read8(uint8_t slave_addr, uint8_t register_addr, uint8_t *data_buf) {
static esp_err_t ft6x06_i2c_read8(uint8_t slave_addr, uint8_t register_addr, uint8_t *data_buf) {
return lvgl_i2c_read(CONFIG_LV_I2C_TOUCH_PORT, slave_addr, register_addr, data_buf, 1);
}


/**
* @brief Read the FT6x36 gesture ID. Initialize first!
* @param dev_addr: I2C FT6x36 Slave address.
Expand Down Expand Up @@ -84,7 +85,16 @@ void ft6x06_init(uint16_t dev_addr) {

ft6x06_i2c_read8(dev_addr, FT6X36_RELEASECODE_REG, &data_buf);
ESP_LOGI(TAG, "\tRelease code: 0x%02x", data_buf);


#if CONFIG_LV_FT6X36_COORDINATES_QUEUE
ft6x36_touch_queue_handle = xQueueCreate( FT6X36_TOUCH_QUEUE_ELEMENTS, sizeof( ft6x36_touch_t ));
if( ft6x36_touch_queue_handle == NULL )
{
ESP_LOGE( TAG, "\tError creating touch input FreeRTOS queue" );
return;
}
xQueueSend( ft6x36_touch_queue_handle, &touch_inputs, 0 );
#endif
}

/**
Expand All @@ -99,8 +109,6 @@ bool ft6x36_read(lv_indev_drv_t *drv, lv_indev_data_t *data) {
return 0x00;
}
uint8_t data_buf[5]; // 1 byte status, 2 bytes X, 2 bytes Y
static int16_t last_x = 0; // 12bit pixel value
static int16_t last_y = 0; // 12bit pixel value

esp_err_t ret = lvgl_i2c_read(CONFIG_LV_I2C_TOUCH_PORT, current_dev_addr, FT6X36_TD_STAT_REG, &data_buf[0], 5);
if (ret != ESP_OK) {
Expand All @@ -109,29 +117,42 @@ bool ft6x36_read(lv_indev_drv_t *drv, lv_indev_data_t *data) {
uint8_t touch_pnt_cnt = data_buf[0]; // Number of detected touch points

if (ret != ESP_OK || touch_pnt_cnt != 1) { // ignore no touch & multi touch
data->point.x = last_x;
data->point.y = last_y;
data->state = LV_INDEV_STATE_REL;
if ( touch_inputs.current_state != LV_INDEV_STATE_REL)
{
touch_inputs.current_state = LV_INDEV_STATE_REL;
#if CONFIG_LV_FT6X36_COORDINATES_QUEUE
xQueueOverwrite( ft6x36_touch_queue_handle, &touch_inputs );
#endif
}
data->point.x = touch_inputs.last_x;
data->point.y = touch_inputs.last_y;
data->state = touch_inputs.current_state;
return false;
}

last_x = ((data_buf[1] & FT6X36_MSB_MASK) << 8) | (data_buf[2] & FT6X36_LSB_MASK);
last_y = ((data_buf[3] & FT6X36_MSB_MASK) << 8) | (data_buf[4] & FT6X36_LSB_MASK);
touch_inputs.current_state = LV_INDEV_STATE_PR;
touch_inputs.last_x = ((data_buf[1] & FT6X36_MSB_MASK) << 8) | (data_buf[2] & FT6X36_LSB_MASK);
touch_inputs.last_y = ((data_buf[3] & FT6X36_MSB_MASK) << 8) | (data_buf[4] & FT6X36_LSB_MASK);

#if CONFIG_LV_FT6X36_SWAPXY
int16_t swap_buf = last_x;
last_x = last_y;
last_y = swap_buf;
int16_t swap_buf = touch_inputs.last_x;
touch_inputs.last_x = touch_inputs.last_y;
touch_inputs.last_y = swap_buf;
#endif
#if CONFIG_LV_FT6X36_INVERT_X
last_x = LV_HOR_RES - last_x;
touch_inputs.last_x = LV_HOR_RES - touch_inputs.last_x;
#endif
#if CONFIG_LV_FT6X36_INVERT_Y
last_y = LV_VER_RES - last_y;
touch_inputs.last_y = LV_VER_RES - touch_inputs.last_y;
#endif
data->point.x = last_x;
data->point.y = last_y;
data->state = LV_INDEV_STATE_PR;
data->point.x = touch_inputs.last_x;
data->point.y = touch_inputs.last_y;
data->state = touch_inputs.current_state;
ESP_LOGD(TAG, "X=%u Y=%u", data->point.x, data->point.y);

#if CONFIG_LV_FT6X36_COORDINATES_QUEUE
xQueueOverwrite( ft6x36_touch_queue_handle, &touch_inputs );
#endif

return false;
}
12 changes: 12 additions & 0 deletions lvgl_touch/ft6x36.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

#include <stdint.h>
#include <stdbool.h>
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#ifdef LV_LVGL_H_INCLUDE_SIMPLE
#include "lvgl.h"
#else
Expand Down Expand Up @@ -145,6 +147,16 @@ typedef struct {
bool inited;
} ft6x36_status_t;

typedef struct
{
int16_t last_x;
int16_t last_y;
lv_indev_state_t current_state;
} ft6x36_touch_t;

#if CONFIG_LV_FT6X36_COORDINATES_QUEUE
QueueHandle_t ft6x36_touch_queue_handle;
#endif
/**
* @brief Initialize for FT6x36 communication via I2C
* @param dev_addr: Device address on communication Bus (I2C slave address of FT6X36).
Expand Down