touch driver: Add LVGL v8 compatibility
This commit is contained in:
parent
f726311525
commit
0d32432f4c
168
lvgl_touch/gt911.c
Normal file
168
lvgl_touch/gt911.c
Normal file
|
@ -0,0 +1,168 @@
|
|||
/*
|
||||
* Copyright © 2021 Sturnus Inc.
|
||||
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
||||
* software and associated documentation files (the “Software”), to deal in the Software
|
||||
* without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||
* publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||
* to whom the Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all copies or
|
||||
* substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||
* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <esp_log.h>
|
||||
#include <driver/i2c.h>
|
||||
#ifdef LV_LVGL_H_INCLUDE_SIMPLE
|
||||
#include <lvgl.h>
|
||||
#else
|
||||
#include <lvgl/lvgl.h>
|
||||
#endif
|
||||
#include "gt911.h"
|
||||
#include "tp_i2c.h"
|
||||
#include "../lvgl_i2c_conf.h"
|
||||
|
||||
#define TAG "GT911"
|
||||
|
||||
gt911_status_t gt911_status;
|
||||
|
||||
//TODO: handle multibyte read and refactor to just one read transaction
|
||||
esp_err_t gt911_i2c_read(uint8_t slave_addr, uint16_t register_addr, uint8_t *data_buf, uint8_t len) {
|
||||
i2c_cmd_handle_t i2c_cmd = i2c_cmd_link_create();
|
||||
|
||||
i2c_master_start(i2c_cmd);
|
||||
i2c_master_write_byte(i2c_cmd, (slave_addr << 1) | I2C_MASTER_WRITE, true);
|
||||
i2c_master_write_byte(i2c_cmd, (register_addr >> 8), I2C_MASTER_ACK);
|
||||
i2c_master_write_byte(i2c_cmd, (register_addr & 0xFF), I2C_MASTER_ACK);
|
||||
|
||||
i2c_master_start(i2c_cmd);
|
||||
i2c_master_write_byte(i2c_cmd, (slave_addr << 1) | I2C_MASTER_READ, true);
|
||||
|
||||
i2c_master_read_byte(i2c_cmd, data_buf, I2C_MASTER_NACK);
|
||||
i2c_master_stop(i2c_cmd);
|
||||
esp_err_t ret = i2c_master_cmd_begin(TOUCH_I2C_PORT, i2c_cmd, 1000 / portTICK_RATE_MS);
|
||||
i2c_cmd_link_delete(i2c_cmd);
|
||||
return ret;
|
||||
}
|
||||
|
||||
esp_err_t gt911_i2c_write8(uint8_t slave_addr, uint16_t register_addr, uint8_t data) {
|
||||
i2c_cmd_handle_t i2c_cmd = i2c_cmd_link_create();
|
||||
|
||||
i2c_master_start(i2c_cmd);
|
||||
i2c_master_write_byte(i2c_cmd, (slave_addr << 1) | I2C_MASTER_WRITE, true);
|
||||
i2c_master_write_byte(i2c_cmd, (register_addr >> 8), I2C_MASTER_ACK);
|
||||
i2c_master_write_byte(i2c_cmd, (register_addr & 0xFF), I2C_MASTER_ACK);
|
||||
i2c_master_write_byte(i2c_cmd, data, I2C_MASTER_ACK);
|
||||
i2c_master_stop(i2c_cmd);
|
||||
esp_err_t ret = i2c_master_cmd_begin(TOUCH_I2C_PORT, i2c_cmd, 1000 / portTICK_RATE_MS);
|
||||
i2c_cmd_link_delete(i2c_cmd);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Initialize for GT911 communication via I2C
|
||||
* @param dev_addr: Device address on communication Bus (I2C slave address of GT911).
|
||||
* @retval None
|
||||
*/
|
||||
void gt911_init(uint8_t dev_addr) {
|
||||
if (!gt911_status.inited) {
|
||||
gt911_status.i2c_dev_addr = dev_addr;
|
||||
uint8_t data_buf;
|
||||
esp_err_t ret;
|
||||
|
||||
ESP_LOGI(TAG, "Checking for GT911 Touch Controller");
|
||||
if ((ret = gt911_i2c_read(dev_addr, GT911_PRODUCT_ID1, &data_buf, 1) != ESP_OK)) {
|
||||
ESP_LOGE(TAG, "Error reading from device: %s",
|
||||
esp_err_to_name(ret)); // Only show error the first time
|
||||
return;
|
||||
}
|
||||
|
||||
// Read 4 bytes for Product ID in ASCII
|
||||
for (int i = 0; i < GT911_PRODUCT_ID_LEN; i++) {
|
||||
gt911_i2c_read(dev_addr, (GT911_PRODUCT_ID1 + i), (uint8_t *)&(gt911_status.product_id[i]), 1);
|
||||
}
|
||||
ESP_LOGI(TAG, "\tProduct ID: %s", gt911_status.product_id);
|
||||
|
||||
gt911_i2c_read(dev_addr, GT911_VENDOR_ID, &data_buf, 1);
|
||||
ESP_LOGI(TAG, "\tVendor ID: 0x%02x", data_buf);
|
||||
|
||||
gt911_i2c_read(dev_addr, GT911_X_COORD_RES_L, &data_buf, 1);
|
||||
gt911_status.max_x_coord = data_buf;
|
||||
gt911_i2c_read(dev_addr, GT911_X_COORD_RES_H, &data_buf, 1);
|
||||
gt911_status.max_x_coord |= ((uint16_t)data_buf << 8);
|
||||
ESP_LOGI(TAG, "\tX Resolution: %d", gt911_status.max_x_coord);
|
||||
|
||||
gt911_i2c_read(dev_addr, GT911_Y_COORD_RES_L, &data_buf, 1);
|
||||
gt911_status.max_y_coord = data_buf;
|
||||
gt911_i2c_read(dev_addr, GT911_Y_COORD_RES_H, &data_buf, 1);
|
||||
gt911_status.max_y_coord |= ((uint16_t)data_buf << 8);
|
||||
ESP_LOGI(TAG, "\tY Resolution: %d", gt911_status.max_y_coord);
|
||||
gt911_status.inited = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get the touch screen X and Y positions values. Ignores multi touch
|
||||
* @param drv:
|
||||
* @param data: Store data here
|
||||
* @retval Always false
|
||||
*/
|
||||
bool gt911_read(lv_indev_drv_t *drv, lv_indev_data_t *data) {
|
||||
uint8_t touch_pnt_cnt; // Number of detected touch points
|
||||
static int16_t last_x = 0; // 12bit pixel value
|
||||
static int16_t last_y = 0; // 12bit pixel value
|
||||
uint8_t data_buf;
|
||||
uint8_t status_reg;
|
||||
|
||||
gt911_i2c_read(gt911_status.i2c_dev_addr, GT911_STATUS_REG, &status_reg, 1);
|
||||
// ESP_LOGI(TAG, "\tstatus: 0x%02x", status_reg);
|
||||
touch_pnt_cnt = status_reg & 0x0F;
|
||||
if ((status_reg & 0x80) || (touch_pnt_cnt < 6)) {
|
||||
//Reset Status Reg Value
|
||||
gt911_i2c_write8(gt911_status.i2c_dev_addr, GT911_STATUS_REG, 0x00);
|
||||
}
|
||||
if (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;
|
||||
return false;
|
||||
}
|
||||
|
||||
// gt911_i2c_read(gt911_status.i2c_dev_addr, GT911_TRACK_ID1, &data_buf, 1);
|
||||
// ESP_LOGI(TAG, "\ttrack_id: %d", data_buf);
|
||||
|
||||
gt911_i2c_read(gt911_status.i2c_dev_addr, GT911_PT1_X_COORD_L, &data_buf, 1);
|
||||
last_x = data_buf;
|
||||
gt911_i2c_read(gt911_status.i2c_dev_addr, GT911_PT1_X_COORD_H, &data_buf, 1);
|
||||
last_x |= ((uint16_t)data_buf << 8);
|
||||
|
||||
gt911_i2c_read(gt911_status.i2c_dev_addr, GT911_PT1_Y_COORD_L, &data_buf, 1);
|
||||
last_y = data_buf;
|
||||
gt911_i2c_read(gt911_status.i2c_dev_addr, GT911_PT1_Y_COORD_H, &data_buf, 1);
|
||||
last_y |= ((uint16_t)data_buf << 8);
|
||||
|
||||
#if CONFIG_LV_GT911_INVERT_X
|
||||
last_x = gt911_status.max_x_coord - last_x;
|
||||
#endif
|
||||
#if CONFIG_LV_GT911_INVERT_Y
|
||||
last_y = gt911_status.max_y_coord - last_y;
|
||||
#endif
|
||||
#if CONFIG_LV_GT911_SWAPXY
|
||||
int16_t swap_buf = last_x;
|
||||
last_x = last_y;
|
||||
last_y = swap_buf;
|
||||
#endif
|
||||
data->point.x = last_x;
|
||||
data->point.y = last_y;
|
||||
data->state = LV_INDEV_STATE_PR;
|
||||
ESP_LOGI(TAG, "X=%u Y=%u", data->point.x, data->point.y);
|
||||
ESP_LOGV(TAG, "X=%u Y=%u", data->point.x, data->point.y);
|
||||
return false;
|
||||
}
|
94
lvgl_touch/gt911.h
Normal file
94
lvgl_touch/gt911.h
Normal file
|
@ -0,0 +1,94 @@
|
|||
#ifndef __GT911_H
|
||||
/*
|
||||
* Copyright © 2021 Sturnus Inc.
|
||||
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
||||
* software and associated documentation files (the “Software”), to deal in the Software
|
||||
* without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||
* publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||
* to whom the Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all copies or
|
||||
* substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||
* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#define __GT911_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#ifdef LV_LVGL_H_INCLUDE_SIMPLE
|
||||
#include "lvgl.h"
|
||||
#else
|
||||
#include "lvgl/lvgl.h"
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define GT911_I2C_SLAVE_ADDR 0x5D
|
||||
|
||||
#define GT911_PRODUCT_ID_LEN 4
|
||||
|
||||
/* Register Map of GT911 */
|
||||
#define GT911_PRODUCT_ID1 0x8140
|
||||
#define GT911_PRODUCT_ID2 0x8141
|
||||
#define GT911_PRODUCT_ID3 0x8142
|
||||
#define GT911_PRODUCT_ID4 0x8143
|
||||
#define GT911_FIRMWARE_VER_L 0x8144
|
||||
#define GT911_FIRMWARE_VER_H 0x8145
|
||||
#define GT911_X_COORD_RES_L 0x8146
|
||||
#define GT911_X_COORD_RES_H 0x8147
|
||||
#define GT911_Y_COORD_RES_L 0x8148
|
||||
#define GT911_Y_COORD_RES_H 0x8149
|
||||
#define GT911_VENDOR_ID 0x814A
|
||||
|
||||
#define GT911_STATUS_REG 0x814E
|
||||
#define GT911_STATUS_REG_BUF 0x80
|
||||
#define GT911_STATUS_REG_LARGE 0x40
|
||||
#define GT911_STATUS_REG_PROX_VALID 0x20
|
||||
#define GT911_STATUS_REG_HAVEKEY 0x10
|
||||
#define GT911_STATUS_REG_PT_MASK 0x0F
|
||||
|
||||
#define GT911_TRACK_ID1 0x814F
|
||||
#define GT911_PT1_X_COORD_L 0x8150
|
||||
#define GT911_PT1_X_COORD_H 0x8151
|
||||
#define GT911_PT1_Y_COORD_L 0x8152
|
||||
#define GT911_PT1_Y_COORD_H 0x8153
|
||||
#define GT911_PT1_X_SIZE_L 0x8154
|
||||
#define GT911_PT1_X_SIZE_H 0x8155
|
||||
|
||||
typedef struct {
|
||||
bool inited;
|
||||
char product_id[GT911_PRODUCT_ID_LEN];
|
||||
uint16_t max_x_coord;
|
||||
uint16_t max_y_coord;
|
||||
uint8_t i2c_dev_addr;
|
||||
} gt911_status_t;
|
||||
|
||||
/**
|
||||
* @brief Initialize for GT911 communication via I2C
|
||||
* @param dev_addr: Device address on communication Bus (I2C slave address of GT911).
|
||||
* @retval None
|
||||
*/
|
||||
void gt911_init(uint8_t dev_addr);
|
||||
|
||||
/**
|
||||
* @brief Get the touch screen X and Y positions values. Ignores multi touch
|
||||
* @param drv:
|
||||
* @param data: Store data here
|
||||
* @retval Always false
|
||||
*/
|
||||
bool gt911_read(lv_indev_drv_t *drv, lv_indev_data_t *data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif /* __GT911_H */
|
|
@ -26,7 +26,11 @@ void touch_driver_init(void)
|
|||
#endif
|
||||
}
|
||||
|
||||
#if LVGL_VERSION_MAJOR >= 8
|
||||
void touch_driver_read(lv_indev_drv_t *drv, lv_indev_data_t *data)
|
||||
#else
|
||||
bool touch_driver_read(lv_indev_drv_t *drv, lv_indev_data_t *data)
|
||||
#endif
|
||||
{
|
||||
bool res = false;
|
||||
|
||||
|
@ -46,6 +50,10 @@ bool touch_driver_read(lv_indev_drv_t *drv, lv_indev_data_t *data)
|
|||
res = gt911_read(drv, data);
|
||||
#endif
|
||||
|
||||
#if LVGL_VERSION_MAJOR >= 8
|
||||
data->continue_reading = res;
|
||||
#else
|
||||
return res;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
|
|
@ -44,7 +44,12 @@ extern "C" {
|
|||
* GLOBAL PROTOTYPES
|
||||
**********************/
|
||||
void touch_driver_init(void);
|
||||
|
||||
#if LVGL_VERSION_MAJOR >= 8
|
||||
void touch_driver_read(lv_indev_drv_t *drv, lv_indev_data_t *data);
|
||||
#else
|
||||
bool touch_driver_read(lv_indev_drv_t *drv, lv_indev_data_t *data);
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
|
|
Loading…
Reference in a new issue