upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/main/touch.c
diff options
context:
space:
mode:
Diffstat (limited to 'main/touch.c')
-rw-r--r--main/touch.c131
1 files changed, 131 insertions, 0 deletions
diff --git a/main/touch.c b/main/touch.c
new file mode 100644
index 0000000..5a1eec9
--- /dev/null
+++ b/main/touch.c
@@ -0,0 +1,131 @@
1#include "touch.h"
2#include "esp_log.h"
3#include "driver/i2c_master.h"
4#include "driver/gpio.h"
5#include "freertos/FreeRTOS.h"
6#include "freertos/task.h"
7#include <string.h>
8
9static const char *TAG = "touch";
10
11static i2c_master_bus_handle_t s_bus = NULL;
12static i2c_master_dev_handle_t s_dev = NULL;
13static bool s_initialized = false;
14
15static const uint8_t s_read_cmd[11] = {
16 0xb5, 0xab, 0xa5, 0x5a, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00
17};
18
19void touch_parse_raw(const uint8_t *data, touch_point_t *pt) {
20 memset(pt, 0, sizeof(*pt));
21
22 if (!data || data[0] != 0 || data[1] == 0 || data[1] > 1) {
23 pt->touched = false;
24 return;
25 }
26
27 uint16_t raw_x = ((data[2] & 0x0F) << 8) | data[3];
28 uint16_t raw_y = ((data[4] & 0x0F) << 8) | data[5];
29
30 if (raw_x > TOUCH_MAX_X) raw_x = TOUCH_MAX_X;
31 if (raw_y > TOUCH_MAX_Y) raw_y = TOUCH_MAX_Y;
32
33 pt->x = raw_x;
34 pt->y = raw_y;
35 pt->touched = true;
36}
37
38esp_err_t touch_init(void) {
39 if (s_initialized) return ESP_OK;
40
41 gpio_config_t rst_conf = {
42 .pin_bit_mask = (1ULL << TOUCH_RST_PIN),
43 .mode = GPIO_MODE_OUTPUT,
44 .pull_up_en = GPIO_PULLUP_DISABLE,
45 .pull_down_en = GPIO_PULLDOWN_DISABLE,
46 .intr_type = GPIO_INTR_DISABLE,
47 };
48 gpio_config(&rst_conf);
49
50 gpio_set_level(TOUCH_RST_PIN, 0);
51 vTaskDelay(pdMS_TO_TICKS(200));
52 gpio_set_level(TOUCH_RST_PIN, 1);
53 vTaskDelay(pdMS_TO_TICKS(200));
54
55 i2c_master_bus_config_t bus_cfg = {
56 .i2c_port = I2C_NUM_0,
57 .sda_io_num = TOUCH_SDA_PIN,
58 .scl_io_num = TOUCH_SCL_PIN,
59 .clk_source = I2C_CLK_SRC_DEFAULT,
60 .glitch_ignore_cnt = 7,
61 .intr_priority = 0,
62 .trans_queue_depth = 0,
63 .flags = {
64 .enable_internal_pullup = 1,
65 .allow_pd = 0,
66 },
67 };
68
69 esp_err_t ret = i2c_new_master_bus(&bus_cfg, &s_bus);
70 if (ret != ESP_OK) {
71 ESP_LOGE(TAG, "Failed to create I2C bus: %s", esp_err_to_name(ret));
72 return ret;
73 }
74
75 i2c_device_config_t dev_cfg = {
76 .dev_addr_length = I2C_ADDR_BIT_LEN_7,
77 .device_address = TOUCH_I2C_ADDR,
78 .scl_speed_hz = 400000,
79 .scl_wait_us = 0,
80 .flags = {
81 .disable_ack_check = 0,
82 },
83 };
84
85 ret = i2c_master_bus_add_device(s_bus, &dev_cfg, &s_dev);
86 if (ret != ESP_OK) {
87 ESP_LOGE(TAG, "Failed to add I2C device: %s", esp_err_to_name(ret));
88 i2c_del_master_bus(s_bus);
89 s_bus = NULL;
90 return ret;
91 }
92
93 s_initialized = true;
94 ESP_LOGI(TAG, "Touch initialized (I2C addr 0x%02X)", TOUCH_I2C_ADDR);
95 return ESP_OK;
96}
97
98bool touch_read(touch_point_t *pt) {
99 if (!s_initialized || !s_dev || !pt) {
100 if (pt) pt->touched = false;
101 return false;
102 }
103
104 esp_err_t ret = i2c_master_transmit(s_dev, s_read_cmd, sizeof(s_read_cmd), 100);
105 if (ret != ESP_OK) {
106 pt->touched = false;
107 return false;
108 }
109
110 uint8_t data[8] = {0};
111 ret = i2c_master_receive(s_dev, data, sizeof(data), 100);
112 if (ret != ESP_OK) {
113 pt->touched = false;
114 return false;
115 }
116
117 touch_parse_raw(data, pt);
118 return pt->touched;
119}
120
121void touch_deinit(void) {
122 if (s_dev) {
123 i2c_master_bus_rm_device(s_dev);
124 s_dev = NULL;
125 }
126 if (s_bus) {
127 i2c_del_master_bus(s_bus);
128 s_bus = NULL;
129 }
130 s_initialized = false;
131}