SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
touch_drv.cpp
Go to the documentation of this file.
1
30#include "sdkconfig.h"
31#include "esp_log.h"
32#include "esp_err.h"
33#include "i2c_driver.h"
34#include "freertos/FreeRTOS.h"
35#include <TouchDrv.hpp>
36
37static const char *TAG = "TOUCH";
38
39#define SENSOR_INPUT_PIN (gpio_num_t)CONFIG_SENSOR_IRQ
40#define SENSOR_INPUT_PIN_SEL (1ULL<<SENSOR_INPUT_PIN)
41
43static QueueHandle_t xQueue;
44
45static void touch_home_button_callback(void *user_data)
46{
47 ESP_LOGI(TAG, "Touch Home button pressed!");
48}
49
50static void IRAM_ATTR sensor_irq_handler(void *arg)
51{
52 uint32_t gpio_num = (uint32_t) arg;
53 xQueueSendFromISR(xQueue, &gpio_num, NULL);
54}
55
56static void irq_init()
57{
58 gpio_config_t io_conf;
59 io_conf.intr_type = GPIO_INTR_DISABLE;
60 io_conf.mode = GPIO_MODE_INPUT;
61 io_conf.pin_bit_mask = SENSOR_INPUT_PIN_SEL;
62 io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
63 io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
64 gpio_config(&io_conf);
65 // gpio_set_intr_type(SENSOR_INPUT_PIN, GPIO_INTR_NEGEDGE);
66 // //install gpio isr service
67 // gpio_install_isr_service(0);
68 // //hook isr handler for specific gpio pin
69 // gpio_isr_handler_add(SENSOR_INPUT_PIN, sensor_irq_handler, (void *) SENSOR_INPUT_PIN);
70
71}
72
73esp_err_t touch_drv_init()
74{
75 uint8_t address = 0xFF;
76
77 ESP_LOGI(TAG, "----SensorLib TouchDrv Examples----");
78
79 /*
80 * CST816, CST328 Some chips have automatic sleep function.
81 * If you want to scan the device address,
82 * must reset the chip first. If the reset pin is not connected,
83 * you may not be able to obtain the device address by scanning the bus.
84 */
85 gpio_num_t gpio_num = (gpio_num_t)CONFIG_SENSOR_RST;
86 ESP_ERROR_CHECK(gpio_set_direction(gpio_num, GPIO_MODE_OUTPUT));
87
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));
92
93 // Run bus scan
95
96 // Get known address
97 if (i2c_drv_probe(CST816_SLAVE_ADDRESS)) {
98 address = CST816_SLAVE_ADDRESS;
99 ESP_LOGI(TAG, "Find touch address 0x%X", address);
100 } else if (i2c_drv_probe(CST226SE_SLAVE_ADDRESS)) {
101 address = CST226SE_SLAVE_ADDRESS;
102 ESP_LOGI(TAG, "Find touch address 0x%X", address);
103 } else if (i2c_drv_probe(CST328_SLAVE_ADDRESS)) {
104 address = CST328_SLAVE_ADDRESS;
105 ESP_LOGI(TAG, "Find touch address 0x%X", address);
106 }
107
108 if (address == 0xFF) {
109 ESP_LOGE(TAG, "Could't find touch chip!");
110 return ESP_FAIL;
111 }
112
113 // Set reset and interrupt pin
114 touch.setPins(CONFIG_SENSOR_RST, CONFIG_SENSOR_IRQ);
115
116 //* Implemented using read and write callback methods, applicable to other platforms
117#if CONFIG_I2C_COMMUNICATION_METHOD_CALLBACK_RW
118
119 // * Provide the device address to the callback function
120 i2c_drv_device_init(address);
121
122 ESP_LOGI(TAG, "Implemented using read and write callback methods");
124 ESP_LOGI(TAG, "Initializing the capacitive touch screen successfully");
125 } else {
126 ESP_LOGE(TAG, "Failed to initialize the capacitive touch screen");
127 return ESP_FAIL;
128 }
129#endif
130
131 //* Use the built-in esp-idf communication method
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)
134
135 ESP_LOGI(TAG, "Implemented using built-in read and write methods (Use higher version >= 5.0 API)");
136
137 // * Using the new API of esp-idf 5.x, you need to pass the I2C BUS handle,
138 // * which is useful when the bus shares multiple devices.
139 extern i2c_master_bus_handle_t bus_handle;
140
141 if (touch.begin(bus_handle, address)) {
142 ESP_LOGI(TAG, "Initializing the capacitive touch screen successfully");
143 } else {
144 ESP_LOGE(TAG, "Failed to initialize the capacitive touch screen");
145 return ESP_FAIL;
146 }
147#else
148
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");
152 } else {
153 ESP_LOGE(TAG, "Failed to initialize the capacitive touch screen");
154 return ESP_FAIL;
155 }
156#endif //ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)
157#endif //CONFIG_I2C_COMMUNICATION_METHOD_BUILTIN_RW
158
159 const char *model = touch.getModelName();
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");
163 // Some CST816 will automatically enter sleep,
164 // can use the touch.disableAutoSleep() to turn off
166
172 // Example touch coordinates using T-Display-S3 touch
174
175 touch.setHomeButtonCallback(touch_home_button_callback);
176 }
177
178 // Set the maximum valid coordinates
179 // touch.setMaxCoordinates(320, 170);
180
181 // Swap XY coordinates
182 // touch.setSwapXY(true);
183
184 // Mirror XY coordinates
185 // touch.setMirrorXY(false, true);
186
187
188 // Create a queue to handle gpio event from isr
189 xQueue = xQueueCreate(5, sizeof(uint32_t));
190
191 // Register Sensor interrupt pins
192 irq_init();
193
194 return ESP_OK;
195}
196
197
199{
200 TouchPoints touch_points = touch.getTouchPoints();
201 if (touch_points.hasPoints()) {
202 for (int i = 0; i < touch_points.getPointCount(); ++i) {
203 const TouchPoint &point = touch_points.getPoint(i);
204 printf("Point %u: X=%u, Y=%u\n", i, point.x, point.y);
205 }
206 }
207}
208
210{
211 uint32_t io_num;
212 // if (xQueueReceive(xQueue, &io_num, pdMS_TO_TICKS(10))) {
213 // if (gpio_get_level((gpio_num_t)CONFIG_SENSOR_IRQ) == 0) {
215 // }
216 // }
217}
@license MIT License
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)
@license MIT License
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)
uint8_t i
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()
TouchDrvCSTXXX touch
Definition touch_drv.cpp:42
#define SENSOR_INPUT_PIN_SEL
Definition touch_drv.cpp:40
esp_err_t touch_drv_init()
Definition touch_drv.cpp:73
void touch_loop()