upleb.uk

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

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYour Name <you@example.com>2026-05-18 22:44:37 +0530
committerYour Name <you@example.com>2026-05-18 22:44:37 +0530
commitf2c4b15c72a2fa89caed5d8a11a4ea9832c48be6 (patch)
tree4cfaa400b1bddcd6cff2a5e53f3789256c40700d
parent7820837d79ebc8e00221b5206bdd8e3ca0ae4c15 (diff)
feat: add AXS15231B touch driver with coordinate parsing tests
-rw-r--r--TOUCH_WIFI_SETUP_PLAN.md92
-rw-r--r--main/touch.c131
-rw-r--r--main/touch.h28
-rw-r--r--tests/unit/Makefile5
-rw-r--r--tests/unit/stubs/driver/gpio.h44
-rw-r--r--tests/unit/stubs/driver/i2c_master.h39
-rw-r--r--tests/unit/stubs/driver/i2c_types.h45
-rw-r--r--tests/unit/test_touch.c93
8 files changed, 476 insertions, 1 deletions
diff --git a/TOUCH_WIFI_SETUP_PLAN.md b/TOUCH_WIFI_SETUP_PLAN.md
new file mode 100644
index 0000000..afbc0b3
--- /dev/null
+++ b/TOUCH_WIFI_SETUP_PLAN.md
@@ -0,0 +1,92 @@
1# Touchscreen WiFi Setup Plan
2
3## Overview
4Add touchscreen WiFi configuration to the TollGate display so users can select a gateway network and enter a password directly on the device.
5
6## Hardware
7
8### Touch Controller
9- **IC:** AXS15231B built-in touch (same chip as display, separate I2C interface)
10- **Bus:** I2C, address `0x3B`
11- **Pins:** SDA=GPIO4, SCL=GPIO8, RST=GPIO12, INT=GPIO11
12- **Protocol:** Write 11 bytes `[0xb5,0xab,0xa5,0x5a,0x00,0x00,0x00,0x08,0x00,0x00,0x00]`, read 8 bytes
13- **Coordinates:** X/Y from data bytes [2..5], 12-bit
14- **RST sequence:** LOW 200ms -> HIGH 200ms
15
16### No Pin Conflicts
17| Pin | Display | Touch | Conflict? |
18|-----|---------|-------|-----------|
19| 4 | -- | SDA | No |
20| 8 | -- | SCL | No |
21| 11 | -- | INT | No |
22| 12 | -- | RST | No |
23| 1,21,39,40,45,47,48 | Display QSPI | -- | No |
24
25## Trigger Points (all three)
26
27| Trigger | How | When |
28|---------|-----|------|
29| A) Tap on ERROR screen | "Setup WiFi" button | Any time upstream is down |
30| B) Auto-show on first boot | Check wifi_networks empty | Fresh device with no credentials |
31| C) Setup button on READY/ERROR | Small gear icon in corner | Always accessible |
32
33## UI Screens
34
35### 1. Scanning
36Title + "Scanning..." spinner
37
38### 2. Network List
39Top 8 by RSSI, sorted strongest first, scrollable. Lock icon for secured networks.
40
41### 3. Password Entry
42SSID name, masked password field with eye reveal toggle, QWERTY keyboard.
43
44### 4. Connecting
45"Connecting to SSID..." spinner
46
47### 5. Result
48Green "Connected!" with IP, or red "Failed" with retry options.
49
50## New Files
51
52| File | Purpose | Testable Logic |
53|------|---------|----------------|
54| `main/touch.h/c` | AXS15231B I2C touch driver | Coordinate parsing |
55| `main/keyboard.h/c` | On-screen keyboard rendering + hit detection | Layout, key lookup |
56| `main/wifi_setup.h/c` | WiFi scan/select/connect flow | State machine |
57| `tests/unit/test_touch.c` | Touch coordinate decode | Pure math |
58| `tests/unit/test_keyboard.c` | Key layout + hit detection | Pure logic |
59| `tests/unit/test_wifi_setup.c` | Setup state machine | State transitions |
60
61## Config Extension
62Add `tollgate_config_add_wifi(const char *ssid, const char *password)` to `config.c` - rewrites `/spiffs/config.json`.
63
64## Display Extension
65Add `DISPLAY_WIFI_SETUP` to `display_state_t`.
66
67## Implementation Checklist
68
69### Phase 1: Touch Driver
70- [ ] Create `main/touch.h` with API
71- [ ] Create `main/touch.c` with ESP-IDF I2C v5 implementation
72- [ ] Create `tests/unit/test_touch.c` with coordinate parsing tests
73- [ ] Run `make test-unit`, verify all pass
74
75### Phase 2: On-Screen Keyboard
76- [ ] Create `main/keyboard.h` with API
77- [ ] Create `main/keyboard.c` with QWERTY layout + hit detection
78- [ ] Create `tests/unit/test_keyboard.c` with layout + key lookup tests
79- [ ] Run `make test-unit`, verify all pass
80
81### Phase 3: WiFi Setup Flow
82- [ ] Create `main/wifi_setup.h` with API
83- [ ] Create `main/wifi_setup.c` with scan/list/connect state machine
84- [ ] Add `tollgate_config_add_wifi()` to config.c + config.h
85- [ ] Create `tests/unit/test_wifi_setup.c` with state machine tests
86- [ ] Run `make test-unit`, verify all pass
87
88### Phase 4: Integration
89- [ ] Add `DISPLAY_WIFI_SETUP` to display.h
90- [ ] Update display.c with WiFi setup rendering + touch input
91- [ ] Update `main/CMakeLists.txt` with new source files
92- [ ] Build, flash to Board C, verify full flow
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}
diff --git a/main/touch.h b/main/touch.h
new file mode 100644
index 0000000..a4a5aa4
--- /dev/null
+++ b/main/touch.h
@@ -0,0 +1,28 @@
1#ifndef TOUCH_H
2#define TOUCH_H
3
4#include "esp_err.h"
5#include <stdint.h>
6#include <stdbool.h>
7
8#define TOUCH_SDA_PIN 4
9#define TOUCH_SCL_PIN 8
10#define TOUCH_RST_PIN 12
11#define TOUCH_INT_PIN 11
12#define TOUCH_I2C_ADDR 0x3B
13#define TOUCH_MAX_X 319
14#define TOUCH_MAX_Y 479
15
16typedef struct {
17 uint16_t x;
18 uint16_t y;
19 bool touched;
20} touch_point_t;
21
22esp_err_t touch_init(void);
23bool touch_read(touch_point_t *pt);
24void touch_deinit(void);
25
26void touch_parse_raw(const uint8_t *data, touch_point_t *pt);
27
28#endif
diff --git a/tests/unit/Makefile b/tests/unit/Makefile
index 7ebc3b2..5bc5eb1 100644
--- a/tests/unit/Makefile
+++ b/tests/unit/Makefile
@@ -22,7 +22,7 @@ LDFLAGS := -lmbedcrypto -lcjson -lm
22 22
23SECP256K1_OBJ := secp256k1.o precomputed_ecmult.o precomputed_ecmult_gen.o 23SECP256K1_OBJ := secp256k1.o precomputed_ecmult.o precomputed_ecmult_gen.o
24 24
25TESTS := test_geohash test_identity test_nostr_event test_cashu test_session test_tollgate_client test_lnurl_pay test_lightning_payout test_mcp_handler test_nip04 test_cvm_server 25TESTS := test_geohash test_identity test_nostr_event test_cashu test_session test_tollgate_client test_lnurl_pay test_lightning_payout test_mcp_handler test_nip04 test_cvm_server test_touch
26 26
27.PHONY: all test clean $(TESTS) 27.PHONY: all test clean $(TESTS)
28 28
@@ -81,5 +81,8 @@ test_nip04: test_nip04.c $(REPO_ROOT)/main/nip04.c $(SECP256K1_OBJ)
81test_cvm_server: test_cvm_server.c 81test_cvm_server: test_cvm_server.c
82 $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) 82 $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
83 83
84test_touch: test_touch.c $(REPO_ROOT)/main/touch.c
85 $(CC) $(CFLAGS) $< $(REPO_ROOT)/main/touch.c -o $@ $(LDFLAGS)
86
84clean: 87clean:
85 rm -f $(TESTS) $(SECP256K1_OBJ) 88 rm -f $(TESTS) $(SECP256K1_OBJ)
diff --git a/tests/unit/stubs/driver/gpio.h b/tests/unit/stubs/driver/gpio.h
new file mode 100644
index 0000000..d8dda0a
--- /dev/null
+++ b/tests/unit/stubs/driver/gpio.h
@@ -0,0 +1,44 @@
1#ifndef STUBS_DRIVER_GPIO_H
2#define STUBS_DRIVER_GPIO_H
3
4#include <stdint.h>
5
6typedef enum {
7 GPIO_MODE_DISABLE = 0,
8 GPIO_MODE_INPUT,
9 GPIO_MODE_OUTPUT,
10 GPIO_MODE_OUTPUT_OD,
11 GPIO_MODE_INPUT_OUTPUT_OD,
12 GPIO_MODE_INPUT_OUTPUT,
13} gpio_mode_t;
14
15typedef enum {
16 GPIO_INTR_DISABLE = 0,
17} gpio_int_type_t;
18
19typedef enum {
20 GPIO_PULLUP_DISABLE = 0,
21 GPIO_PULLUP_ENABLE,
22} gpio_pullup_t;
23
24typedef enum {
25 GPIO_PULLDOWN_DISABLE = 0,
26 GPIO_PULLDOWN_ENABLE,
27} gpio_pulldown_t;
28
29typedef struct {
30 uint64_t pin_bit_mask;
31 gpio_mode_t mode;
32 gpio_pullup_t pull_up_en;
33 gpio_pulldown_t pull_down_en;
34 gpio_int_type_t intr_type;
35} gpio_config_t;
36
37static inline int gpio_config(const gpio_config_t *cfg) { (void)cfg; return 0; }
38static inline int gpio_set_level(uint32_t gpio_num, uint32_t level) { (void)gpio_num; (void)level; return 0; }
39
40#define GPIO_INTR_DISABLE 0
41#define GPIO_PULLUP_DISABLE 0
42#define GPIO_PULLDOWN_DISABLE 0
43
44#endif
diff --git a/tests/unit/stubs/driver/i2c_master.h b/tests/unit/stubs/driver/i2c_master.h
new file mode 100644
index 0000000..f49eaad
--- /dev/null
+++ b/tests/unit/stubs/driver/i2c_master.h
@@ -0,0 +1,39 @@
1#ifndef STUBS_DRIVER_I2C_MASTER_H
2#define STUBS_DRIVER_I2C_MASTER_H
3
4#include "driver/i2c_types.h"
5#include "esp_err.h"
6#include <stdint.h>
7#include <stddef.h>
8
9static inline esp_err_t i2c_new_master_bus(const i2c_master_bus_config_t *cfg, i2c_master_bus_handle_t *ret) {
10 (void)cfg; (void)ret;
11 return ESP_OK;
12}
13
14static inline esp_err_t i2c_master_bus_add_device(i2c_master_bus_handle_t bus, const i2c_device_config_t *cfg, i2c_master_dev_handle_t *ret) {
15 (void)bus; (void)cfg; (void)ret;
16 return ESP_OK;
17}
18
19static inline esp_err_t i2c_master_transmit(i2c_master_dev_handle_t dev, const uint8_t *buf, size_t len, int timeout_ms) {
20 (void)dev; (void)buf; (void)len; (void)timeout_ms;
21 return ESP_OK;
22}
23
24static inline esp_err_t i2c_master_receive(i2c_master_dev_handle_t dev, uint8_t *buf, size_t len, int timeout_ms) {
25 (void)dev; (void)buf; (void)len; (void)timeout_ms;
26 return ESP_OK;
27}
28
29static inline esp_err_t i2c_master_bus_rm_device(i2c_master_dev_handle_t dev) {
30 (void)dev;
31 return ESP_OK;
32}
33
34static inline esp_err_t i2c_del_master_bus(i2c_master_bus_handle_t bus) {
35 (void)bus;
36 return ESP_OK;
37}
38
39#endif
diff --git a/tests/unit/stubs/driver/i2c_types.h b/tests/unit/stubs/driver/i2c_types.h
new file mode 100644
index 0000000..3590a8b
--- /dev/null
+++ b/tests/unit/stubs/driver/i2c_types.h
@@ -0,0 +1,45 @@
1#ifndef STUBS_DRIVER_I2C_TYPES_H
2#define STUBS_DRIVER_I2C_TYPES_H
3
4#include <stdint.h>
5#include <stddef.h>
6
7typedef int i2c_port_num_t;
8#define I2C_NUM_0 0
9
10typedef enum {
11 I2C_ADDR_BIT_LEN_7 = 0,
12} i2c_addr_bit_len_t;
13
14typedef enum {
15 I2C_CLK_SRC_DEFAULT = 0,
16} i2c_clock_source_t;
17
18typedef struct i2c_master_bus_t *i2c_master_bus_handle_t;
19typedef struct i2c_master_dev_t *i2c_master_dev_handle_t;
20
21typedef struct {
22 i2c_port_num_t i2c_port;
23 int sda_io_num;
24 int scl_io_num;
25 i2c_clock_source_t clk_source;
26 uint8_t glitch_ignore_cnt;
27 int intr_priority;
28 size_t trans_queue_depth;
29 struct {
30 uint32_t enable_internal_pullup : 1;
31 uint32_t allow_pd : 1;
32 } flags;
33} i2c_master_bus_config_t;
34
35typedef struct {
36 i2c_addr_bit_len_t dev_addr_length;
37 uint16_t device_address;
38 uint32_t scl_speed_hz;
39 uint32_t scl_wait_us;
40 struct {
41 uint32_t disable_ack_check : 1;
42 } flags;
43} i2c_device_config_t;
44
45#endif
diff --git a/tests/unit/test_touch.c b/tests/unit/test_touch.c
new file mode 100644
index 0000000..13f04b5
--- /dev/null
+++ b/tests/unit/test_touch.c
@@ -0,0 +1,93 @@
1#include "test_framework.h"
2#include "../../main/touch.h"
3#include <string.h>
4
5int main(void)
6{
7 touch_point_t pt;
8 uint8_t data[8];
9
10 printf("=== test_touch ===\n");
11
12 memset(data, 0, sizeof(data));
13 touch_parse_raw(data, &pt);
14 ASSERT(!pt.touched, "All-zero data = no touch");
15
16 data[0] = 1;
17 data[1] = 1;
18 data[2] = 0;
19 data[3] = 0;
20 touch_parse_raw(data, &pt);
21 ASSERT(!pt.touched, "data[0]=1 = no touch (gesture byte nonzero)");
22
23 data[0] = 0;
24 data[1] = 0;
25 touch_parse_raw(data, &pt);
26 ASSERT(!pt.touched, "data[1]=0 = no touch (touch count zero)");
27
28 data[0] = 0;
29 data[1] = 1;
30 data[2] = 0x00;
31 data[3] = 0x64;
32 data[4] = 0x00;
33 data[5] = 0xC8;
34 data[6] = 0;
35 data[7] = 0;
36 touch_parse_raw(data, &pt);
37 ASSERT(pt.touched, "Valid touch: touched=true");
38 ASSERT_EQ_INT(100, (int)pt.x, "Valid touch: x=100");
39 ASSERT_EQ_INT(200, (int)pt.y, "Valid touch: y=200");
40
41 data[0] = 0;
42 data[1] = 1;
43 data[2] = 0x0F;
44 data[3] = 0xFF;
45 data[4] = 0x0F;
46 data[5] = 0xFF;
47 touch_parse_raw(data, &pt);
48 ASSERT(pt.touched, "Max raw coords: touched=true");
49 ASSERT_EQ_INT(TOUCH_MAX_X, (int)pt.x, "Max raw coords clamped to 319");
50 ASSERT_EQ_INT(TOUCH_MAX_Y, (int)pt.y, "Max raw coords clamped to 479");
51
52 data[0] = 0;
53 data[1] = 1;
54 data[2] = 0x05;
55 data[3] = 0x00;
56 data[4] = 0x08;
57 data[5] = 0x00;
58 touch_parse_raw(data, &pt);
59 ASSERT(pt.touched, "12-bit coords: touched=true");
60 ASSERT_EQ_INT(TOUCH_MAX_X, (int)pt.x, "12-bit x: (0x05 << 8) | 0x00 = 1280, clamped to 319");
61
62 data[0] = 0;
63 data[1] = 2;
64 touch_parse_raw(data, &pt);
65 ASSERT(!pt.touched, "data[1]=2 = too many touches, reject");
66
67 touch_parse_raw(NULL, &pt);
68 ASSERT(!pt.touched, "NULL data = no touch");
69
70 data[0] = 0;
71 data[1] = 1;
72 data[2] = 0x00;
73 data[3] = 0x00;
74 data[4] = 0x00;
75 data[5] = 0x00;
76 touch_parse_raw(data, &pt);
77 ASSERT(pt.touched, "Origin (0,0): touched=true");
78 ASSERT_EQ_INT(0, (int)pt.x, "Origin: x=0");
79 ASSERT_EQ_INT(0, (int)pt.y, "Origin: y=0");
80
81 data[0] = 0;
82 data[1] = 1;
83 data[2] = 0x01;
84 data[3] = 0x3F;
85 data[4] = 0x01;
86 data[5] = 0xDF;
87 touch_parse_raw(data, &pt);
88 ASSERT(pt.touched, "Mid-screen: touched=true");
89 ASSERT_EQ_INT(319, (int)pt.x, "Mid-screen: x=0x13F=319");
90 ASSERT_EQ_INT(479, (int)pt.y, "Mid-screen: y=0x1DF=479");
91
92 TEST_SUMMARY();
93}