diff options
| author | Your Name <you@example.com> | 2026-05-18 22:44:37 +0530 |
|---|---|---|
| committer | Your Name <you@example.com> | 2026-05-18 22:44:37 +0530 |
| commit | f2c4b15c72a2fa89caed5d8a11a4ea9832c48be6 (patch) | |
| tree | 4cfaa400b1bddcd6cff2a5e53f3789256c40700d /tests/unit | |
| parent | 7820837d79ebc8e00221b5206bdd8e3ca0ae4c15 (diff) | |
feat: add AXS15231B touch driver with coordinate parsing tests
Diffstat (limited to 'tests/unit')
| -rw-r--r-- | tests/unit/Makefile | 5 | ||||
| -rw-r--r-- | tests/unit/stubs/driver/gpio.h | 44 | ||||
| -rw-r--r-- | tests/unit/stubs/driver/i2c_master.h | 39 | ||||
| -rw-r--r-- | tests/unit/stubs/driver/i2c_types.h | 45 | ||||
| -rw-r--r-- | tests/unit/test_touch.c | 93 |
5 files changed, 225 insertions, 1 deletions
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 | ||
| 23 | SECP256K1_OBJ := secp256k1.o precomputed_ecmult.o precomputed_ecmult_gen.o | 23 | SECP256K1_OBJ := secp256k1.o precomputed_ecmult.o precomputed_ecmult_gen.o |
| 24 | 24 | ||
| 25 | 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 | 25 | 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 |
| 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) | |||
| 81 | test_cvm_server: test_cvm_server.c | 81 | test_cvm_server: test_cvm_server.c |
| 82 | $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) | 82 | $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) |
| 83 | 83 | ||
| 84 | test_touch: test_touch.c $(REPO_ROOT)/main/touch.c | ||
| 85 | $(CC) $(CFLAGS) $< $(REPO_ROOT)/main/touch.c -o $@ $(LDFLAGS) | ||
| 86 | |||
| 84 | clean: | 87 | clean: |
| 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 | |||
| 6 | typedef 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 | |||
| 15 | typedef enum { | ||
| 16 | GPIO_INTR_DISABLE = 0, | ||
| 17 | } gpio_int_type_t; | ||
| 18 | |||
| 19 | typedef enum { | ||
| 20 | GPIO_PULLUP_DISABLE = 0, | ||
| 21 | GPIO_PULLUP_ENABLE, | ||
| 22 | } gpio_pullup_t; | ||
| 23 | |||
| 24 | typedef enum { | ||
| 25 | GPIO_PULLDOWN_DISABLE = 0, | ||
| 26 | GPIO_PULLDOWN_ENABLE, | ||
| 27 | } gpio_pulldown_t; | ||
| 28 | |||
| 29 | typedef 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 | |||
| 37 | static inline int gpio_config(const gpio_config_t *cfg) { (void)cfg; return 0; } | ||
| 38 | static 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 | |||
| 9 | static 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 | |||
| 14 | 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) { | ||
| 15 | (void)bus; (void)cfg; (void)ret; | ||
| 16 | return ESP_OK; | ||
| 17 | } | ||
| 18 | |||
| 19 | static 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 | |||
| 24 | static 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 | |||
| 29 | static inline esp_err_t i2c_master_bus_rm_device(i2c_master_dev_handle_t dev) { | ||
| 30 | (void)dev; | ||
| 31 | return ESP_OK; | ||
| 32 | } | ||
| 33 | |||
| 34 | static 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 | |||
| 7 | typedef int i2c_port_num_t; | ||
| 8 | #define I2C_NUM_0 0 | ||
| 9 | |||
| 10 | typedef enum { | ||
| 11 | I2C_ADDR_BIT_LEN_7 = 0, | ||
| 12 | } i2c_addr_bit_len_t; | ||
| 13 | |||
| 14 | typedef enum { | ||
| 15 | I2C_CLK_SRC_DEFAULT = 0, | ||
| 16 | } i2c_clock_source_t; | ||
| 17 | |||
| 18 | typedef struct i2c_master_bus_t *i2c_master_bus_handle_t; | ||
| 19 | typedef struct i2c_master_dev_t *i2c_master_dev_handle_t; | ||
| 20 | |||
| 21 | typedef 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 | |||
| 35 | typedef 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 | |||
| 5 | int 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 | } | ||