From f2c4b15c72a2fa89caed5d8a11a4ea9832c48be6 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 18 May 2026 22:44:37 +0530 Subject: feat: add AXS15231B touch driver with coordinate parsing tests --- tests/unit/Makefile | 5 +- tests/unit/stubs/driver/gpio.h | 44 +++++++++++++++++ tests/unit/stubs/driver/i2c_master.h | 39 +++++++++++++++ tests/unit/stubs/driver/i2c_types.h | 45 +++++++++++++++++ tests/unit/test_touch.c | 93 ++++++++++++++++++++++++++++++++++++ 5 files changed, 225 insertions(+), 1 deletion(-) create mode 100644 tests/unit/stubs/driver/gpio.h create mode 100644 tests/unit/stubs/driver/i2c_master.h create mode 100644 tests/unit/stubs/driver/i2c_types.h create mode 100644 tests/unit/test_touch.c (limited to 'tests/unit') 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 SECP256K1_OBJ := secp256k1.o precomputed_ecmult.o precomputed_ecmult_gen.o -TESTS := 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 +TESTS := 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 .PHONY: all test clean $(TESTS) @@ -81,5 +81,8 @@ test_nip04: test_nip04.c $(REPO_ROOT)/main/nip04.c $(SECP256K1_OBJ) test_cvm_server: test_cvm_server.c $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) +test_touch: test_touch.c $(REPO_ROOT)/main/touch.c + $(CC) $(CFLAGS) $< $(REPO_ROOT)/main/touch.c -o $@ $(LDFLAGS) + clean: 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 @@ +#ifndef STUBS_DRIVER_GPIO_H +#define STUBS_DRIVER_GPIO_H + +#include + +typedef enum { + GPIO_MODE_DISABLE = 0, + GPIO_MODE_INPUT, + GPIO_MODE_OUTPUT, + GPIO_MODE_OUTPUT_OD, + GPIO_MODE_INPUT_OUTPUT_OD, + GPIO_MODE_INPUT_OUTPUT, +} gpio_mode_t; + +typedef enum { + GPIO_INTR_DISABLE = 0, +} gpio_int_type_t; + +typedef enum { + GPIO_PULLUP_DISABLE = 0, + GPIO_PULLUP_ENABLE, +} gpio_pullup_t; + +typedef enum { + GPIO_PULLDOWN_DISABLE = 0, + GPIO_PULLDOWN_ENABLE, +} gpio_pulldown_t; + +typedef struct { + uint64_t pin_bit_mask; + gpio_mode_t mode; + gpio_pullup_t pull_up_en; + gpio_pulldown_t pull_down_en; + gpio_int_type_t intr_type; +} gpio_config_t; + +static inline int gpio_config(const gpio_config_t *cfg) { (void)cfg; return 0; } +static inline int gpio_set_level(uint32_t gpio_num, uint32_t level) { (void)gpio_num; (void)level; return 0; } + +#define GPIO_INTR_DISABLE 0 +#define GPIO_PULLUP_DISABLE 0 +#define GPIO_PULLDOWN_DISABLE 0 + +#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 @@ +#ifndef STUBS_DRIVER_I2C_MASTER_H +#define STUBS_DRIVER_I2C_MASTER_H + +#include "driver/i2c_types.h" +#include "esp_err.h" +#include +#include + +static inline esp_err_t i2c_new_master_bus(const i2c_master_bus_config_t *cfg, i2c_master_bus_handle_t *ret) { + (void)cfg; (void)ret; + return ESP_OK; +} + +static 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) { + (void)bus; (void)cfg; (void)ret; + return ESP_OK; +} + +static inline esp_err_t i2c_master_transmit(i2c_master_dev_handle_t dev, const uint8_t *buf, size_t len, int timeout_ms) { + (void)dev; (void)buf; (void)len; (void)timeout_ms; + return ESP_OK; +} + +static inline esp_err_t i2c_master_receive(i2c_master_dev_handle_t dev, uint8_t *buf, size_t len, int timeout_ms) { + (void)dev; (void)buf; (void)len; (void)timeout_ms; + return ESP_OK; +} + +static inline esp_err_t i2c_master_bus_rm_device(i2c_master_dev_handle_t dev) { + (void)dev; + return ESP_OK; +} + +static inline esp_err_t i2c_del_master_bus(i2c_master_bus_handle_t bus) { + (void)bus; + return ESP_OK; +} + +#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 @@ +#ifndef STUBS_DRIVER_I2C_TYPES_H +#define STUBS_DRIVER_I2C_TYPES_H + +#include +#include + +typedef int i2c_port_num_t; +#define I2C_NUM_0 0 + +typedef enum { + I2C_ADDR_BIT_LEN_7 = 0, +} i2c_addr_bit_len_t; + +typedef enum { + I2C_CLK_SRC_DEFAULT = 0, +} i2c_clock_source_t; + +typedef struct i2c_master_bus_t *i2c_master_bus_handle_t; +typedef struct i2c_master_dev_t *i2c_master_dev_handle_t; + +typedef struct { + i2c_port_num_t i2c_port; + int sda_io_num; + int scl_io_num; + i2c_clock_source_t clk_source; + uint8_t glitch_ignore_cnt; + int intr_priority; + size_t trans_queue_depth; + struct { + uint32_t enable_internal_pullup : 1; + uint32_t allow_pd : 1; + } flags; +} i2c_master_bus_config_t; + +typedef struct { + i2c_addr_bit_len_t dev_addr_length; + uint16_t device_address; + uint32_t scl_speed_hz; + uint32_t scl_wait_us; + struct { + uint32_t disable_ack_check : 1; + } flags; +} i2c_device_config_t; + +#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 @@ +#include "test_framework.h" +#include "../../main/touch.h" +#include + +int main(void) +{ + touch_point_t pt; + uint8_t data[8]; + + printf("=== test_touch ===\n"); + + memset(data, 0, sizeof(data)); + touch_parse_raw(data, &pt); + ASSERT(!pt.touched, "All-zero data = no touch"); + + data[0] = 1; + data[1] = 1; + data[2] = 0; + data[3] = 0; + touch_parse_raw(data, &pt); + ASSERT(!pt.touched, "data[0]=1 = no touch (gesture byte nonzero)"); + + data[0] = 0; + data[1] = 0; + touch_parse_raw(data, &pt); + ASSERT(!pt.touched, "data[1]=0 = no touch (touch count zero)"); + + data[0] = 0; + data[1] = 1; + data[2] = 0x00; + data[3] = 0x64; + data[4] = 0x00; + data[5] = 0xC8; + data[6] = 0; + data[7] = 0; + touch_parse_raw(data, &pt); + ASSERT(pt.touched, "Valid touch: touched=true"); + ASSERT_EQ_INT(100, (int)pt.x, "Valid touch: x=100"); + ASSERT_EQ_INT(200, (int)pt.y, "Valid touch: y=200"); + + data[0] = 0; + data[1] = 1; + data[2] = 0x0F; + data[3] = 0xFF; + data[4] = 0x0F; + data[5] = 0xFF; + touch_parse_raw(data, &pt); + ASSERT(pt.touched, "Max raw coords: touched=true"); + ASSERT_EQ_INT(TOUCH_MAX_X, (int)pt.x, "Max raw coords clamped to 319"); + ASSERT_EQ_INT(TOUCH_MAX_Y, (int)pt.y, "Max raw coords clamped to 479"); + + data[0] = 0; + data[1] = 1; + data[2] = 0x05; + data[3] = 0x00; + data[4] = 0x08; + data[5] = 0x00; + touch_parse_raw(data, &pt); + ASSERT(pt.touched, "12-bit coords: touched=true"); + ASSERT_EQ_INT(TOUCH_MAX_X, (int)pt.x, "12-bit x: (0x05 << 8) | 0x00 = 1280, clamped to 319"); + + data[0] = 0; + data[1] = 2; + touch_parse_raw(data, &pt); + ASSERT(!pt.touched, "data[1]=2 = too many touches, reject"); + + touch_parse_raw(NULL, &pt); + ASSERT(!pt.touched, "NULL data = no touch"); + + data[0] = 0; + data[1] = 1; + data[2] = 0x00; + data[3] = 0x00; + data[4] = 0x00; + data[5] = 0x00; + touch_parse_raw(data, &pt); + ASSERT(pt.touched, "Origin (0,0): touched=true"); + ASSERT_EQ_INT(0, (int)pt.x, "Origin: x=0"); + ASSERT_EQ_INT(0, (int)pt.y, "Origin: y=0"); + + data[0] = 0; + data[1] = 1; + data[2] = 0x01; + data[3] = 0x3F; + data[4] = 0x01; + data[5] = 0xDF; + touch_parse_raw(data, &pt); + ASSERT(pt.touched, "Mid-screen: touched=true"); + ASSERT_EQ_INT(319, (int)pt.x, "Mid-screen: x=0x13F=319"); + ASSERT_EQ_INT(479, (int)pt.y, "Mid-screen: y=0x1DF=479"); + + TEST_SUMMARY(); +} -- cgit v1.2.3