34#include "freertos/FreeRTOS.h"
37static const char *TAG =
"TOUCH";
39#define SENSOR_INPUT_PIN (gpio_num_t)CONFIG_SENSOR_IRQ
40#define SENSOR_INPUT_PIN_SEL (1ULL<<SENSOR_INPUT_PIN)
43static QueueHandle_t xQueue;
45static void touch_home_button_callback(
void *user_data)
47 ESP_LOGI(TAG,
"Touch Home button pressed!");
50static void IRAM_ATTR sensor_irq_handler(
void *arg)
52 uint32_t gpio_num = (uint32_t) arg;
53 xQueueSendFromISR(xQueue, &gpio_num, NULL);
58 gpio_config_t io_conf;
59 io_conf.intr_type = GPIO_INTR_DISABLE;
60 io_conf.mode = GPIO_MODE_INPUT;
62 io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
63 io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
64 gpio_config(&io_conf);
75 uint8_t address = 0xFF;
77 ESP_LOGI(TAG,
"----SensorLib TouchDrv Examples----");
85 gpio_num_t gpio_num = (gpio_num_t)CONFIG_SENSOR_RST;
86 ESP_ERROR_CHECK(gpio_set_direction(gpio_num, GPIO_MODE_OUTPUT));
88 gpio_set_level(gpio_num, 0);
89 vTaskDelay(pdMS_TO_TICKS(15));
90 gpio_set_level(gpio_num, 1);
91 vTaskDelay(pdMS_TO_TICKS(80));
98 address = CST816_SLAVE_ADDRESS;
99 ESP_LOGI(TAG,
"Find touch address 0x%X", address);
101 address = CST226SE_SLAVE_ADDRESS;
102 ESP_LOGI(TAG,
"Find touch address 0x%X", address);
104 address = CST328_SLAVE_ADDRESS;
105 ESP_LOGI(TAG,
"Find touch address 0x%X", address);
108 if (address == 0xFF) {
109 ESP_LOGE(TAG,
"Could't find touch chip!");
117#if CONFIG_I2C_COMMUNICATION_METHOD_CALLBACK_RW
120 i2c_drv_device_init(address);
122 ESP_LOGI(TAG,
"Implemented using read and write callback methods");
124 ESP_LOGI(TAG,
"Initializing the capacitive touch screen successfully");
126 ESP_LOGE(TAG,
"Failed to initialize the capacitive touch screen");
132#if CONFIG_I2C_COMMUNICATION_METHOD_BUILTIN_RW
133#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
135 ESP_LOGI(TAG,
"Implemented using built-in read and write methods (Use higher version >= 5.0 API)");
139 extern i2c_master_bus_handle_t bus_handle;
142 ESP_LOGI(TAG,
"Initializing the capacitive touch screen successfully");
144 ESP_LOGE(TAG,
"Failed to initialize the capacitive touch screen");
149 ESP_LOGI(TAG,
"Implemented using built-in read and write methods (Use lower version < 5.0 API)");
150 if (
touch.
begin((i2c_port_t)CONFIG_I2C_MASTER_PORT_NUM, address, CONFIG_SENSOR_SDA, CONFIG_SENSOR_SCL)) {
151 ESP_LOGI(TAG,
"Initializing the capacitive touch screen successfully");
153 ESP_LOGE(TAG,
"Failed to initialize the capacitive touch screen");
160 ESP_LOGI(TAG,
"Using %s model", model);
161 if (strncmp(model,
"CST8", 4) == 0) {
162 ESP_LOGI(TAG,
"Some CST816 will automatically enter sleep,can use the touch.disableAutoSleep() to turn off");
189 xQueue = xQueueCreate(5,
sizeof(uint32_t));
204 printf(
"Point %u: X=%u, Y=%u\n",
i, point.
x, point.
y);
void setHomeButtonCallback(TouchDrvInterface::HomeButtonCallback callback, void *user_data=NULL) override
Set the home button callback.
bool begin(SensorCommCustom::CustomCallback callback, SensorCommCustomHal::CustomHalCallback hal_callback, uint8_t addr) override
Initialize the touch driver for custom communication.
const char * getModelName() override
Get the model name.
const TouchPoints & getTouchPoints() override
Get the touch points.
void disableAutoSleep()
Disable automatic sleep.
void setCenterButtonCoordinate(int16_t x, int16_t y) override
Set the center button coordinate.
virtual void setPins(int rst, int irq)
Set the hardware reset and interrupt pin numbers.
Container for touch point data and optional gesture information.
uint8_t getPointCount() const
Returns the number of stored touch points.
TouchPoint & getPoint(uint8_t index)
Gets a reference to a touch point at the specified index.
bool hasPoints() const
Checks whether any touch points are stored.
bool i2c_drv_probe(uint8_t devAddr)
int i2c_write_callback(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint8_t len)
int i2c_read_callback(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint8_t len)
Represents a single touch point with its properties.
uint16_t x
X coordinate of the touch point.
uint16_t y
Y coordinate of the touch point.
void touch_drv_isr_handler()
#define SENSOR_INPUT_PIN_SEL
esp_err_t touch_drv_init()