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