|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: CC0-1.0 |
| 5 | + */ |
| 6 | +#include <memory> |
| 7 | +#include "freertos/FreeRTOS.h" |
| 8 | +#include "freertos/task.h" |
| 9 | +#include "esp_heap_caps.h" |
| 10 | +#include "esp_log.h" |
| 11 | +#include "unity.h" |
| 12 | +#include "unity_test_runner.h" |
| 13 | +#include "ESP_Panel_Library.h" |
| 14 | + |
| 15 | +using namespace std; |
| 16 | + |
| 17 | +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 18 | +//////////////////// Please update the following configuration according to your touch_device spec //////////////////////////// |
| 19 | +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 20 | +#define TEST_TOUCH_ADDRESS (0) // Typically set to 0 to use the default address. |
| 21 | +// - For touchs with only one address, set to 0 |
| 22 | +// - For touchs with multiple addresses, set to 0 or the address |
| 23 | +// Like GT911, there are two addresses: 0x5D(default) and 0x14 |
| 24 | +#define TEST_TOUCH_WIDTH (480) |
| 25 | +#define TEST_TOUCH_HEIGHT (480) |
| 26 | +#define TEST_TOUCH_I2C_FREQ_HZ (400 * 1000) |
| 27 | +#define TEST_TOUCH_READ_POINTS_NUM (5) |
| 28 | + |
| 29 | +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 30 | +//////////////////// Please update the following configuration according to your board spec //////////////////////////// |
| 31 | +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 32 | +#define TEST_TOUCH_PIN_NUM_I2C_SCL (10) |
| 33 | +#define TEST_TOUCH_PIN_NUM_I2C_SDA (9) |
| 34 | +#define TEST_TOUCH_PIN_NUM_RST (13) // Set to `-1` if not used |
| 35 | +// For GT911, the RST pin is also used to configure the I2C address |
| 36 | +#define TEST_TOUCH_PIN_NUM_INT (14) // Set to `-1` if not used |
| 37 | +// For GT911, the INT pin is also used to configure the I2C address |
| 38 | + |
| 39 | +#define TEST_READ_TOUCH_DELAY_MS (30) |
| 40 | +#define TEST_READ_TOUCH_TIME_MS (3000) |
| 41 | + |
| 42 | +static const char *TAG = "test_i2c_touch"; |
| 43 | + |
| 44 | +#define delay(x) vTaskDelay(pdMS_TO_TICKS(x)) |
| 45 | + |
| 46 | +#if TEST_TOUCH_PIN_NUM_INT >= 0 |
| 47 | +IRAM_ATTR static bool onTouchInterruptCallback(void *user_data) |
| 48 | +{ |
| 49 | + esp_rom_printf("Touch interrupt callback\n"); |
| 50 | + |
| 51 | + return false; |
| 52 | +} |
| 53 | +#endif |
| 54 | + |
| 55 | +static void run_test(shared_ptr<ESP_PanelTouch> touch_device) |
| 56 | +{ |
| 57 | + touch_device->init(); |
| 58 | + touch_device->begin(); |
| 59 | +#if TEST_TOUCH_PIN_NUM_INT >= 0 |
| 60 | + touch_device->attachInterruptCallback(onTouchInterruptCallback, NULL); |
| 61 | +#endif |
| 62 | + |
| 63 | + uint32_t t = 0; |
| 64 | + while (t++ < TEST_READ_TOUCH_TIME_MS / TEST_READ_TOUCH_DELAY_MS) { |
| 65 | + ESP_PanelTouchPoint point[TEST_TOUCH_READ_POINTS_NUM]; |
| 66 | + int read_touch_result = touch_device->readPoints(point, TEST_TOUCH_READ_POINTS_NUM, TEST_READ_TOUCH_DELAY_MS); |
| 67 | + |
| 68 | + if (read_touch_result > 0) { |
| 69 | + for (int i = 0; i < read_touch_result; i++) { |
| 70 | + ESP_LOGI(TAG, "Touch point(%d): x %d, y %d, strength %d\n", i, point[i].x, point[i].y, point[i].strength); |
| 71 | + } |
| 72 | + } else if (read_touch_result < 0) { |
| 73 | + ESP_LOGE(TAG, "Read touch_device point failed"); |
| 74 | + } |
| 75 | +#if TEST_TOUCH_PIN_NUM_INT < 0 |
| 76 | + delay(TEST_READ_TOUCH_DELAY_MS); |
| 77 | +#endif |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +#define CREATE_TOUCH_BUS(name) \ |
| 82 | + ({ \ |
| 83 | + ESP_LOGI(TAG, "Create touch bus"); \ |
| 84 | + shared_ptr<ESP_PanelBus_I2C> touch_bus = make_shared<ESP_PanelBus_I2C>( \ |
| 85 | + TEST_TOUCH_PIN_NUM_I2C_SCL, TEST_TOUCH_PIN_NUM_I2C_SDA, \ |
| 86 | + (esp_lcd_panel_io_i2c_config_t)ESP_PANEL_TOUCH_I2C_PANEL_IO_CONFIG(name) \ |
| 87 | + ); \ |
| 88 | + TEST_ASSERT_NOT_NULL_MESSAGE(touch_bus, "Create panel bus object failed"); \ |
| 89 | + touch_bus->configI2cFreqHz(TEST_TOUCH_I2C_FREQ_HZ); \ |
| 90 | + TEST_ASSERT_TRUE_MESSAGE(touch_bus->begin(), "Panel bus begin failed"); \ |
| 91 | + touch_bus; \ |
| 92 | + }) |
| 93 | +#define CREATE_TOUCH(name, touch_bus) \ |
| 94 | + ({ \ |
| 95 | + ESP_LOGI(TAG, "Create touch device: " #name); \ |
| 96 | + shared_ptr<ESP_PanelTouch> touch_device = make_shared<ESP_PanelTouch_##name>( \ |
| 97 | + touch_bus, TEST_TOUCH_WIDTH, TEST_TOUCH_HEIGHT, TEST_TOUCH_PIN_NUM_RST, TEST_TOUCH_PIN_NUM_INT \ |
| 98 | + ); \ |
| 99 | + TEST_ASSERT_NOT_NULL_MESSAGE(touch_device, "Create TOUCH object failed"); \ |
| 100 | + touch_device; \ |
| 101 | + }) |
| 102 | +#define CREATE_TEST_CASE(name) \ |
| 103 | + TEST_CASE("Test touch (" #name ") to draw color bar", "[i2c_touch][" #name "]") \ |
| 104 | + { \ |
| 105 | + shared_ptr<ESP_PanelBus_I2C> touch_bus = CREATE_TOUCH_BUS(name); \ |
| 106 | + shared_ptr<ESP_PanelTouch> touch_device = CREATE_TOUCH(name, touch_bus.get()); \ |
| 107 | + run_test(touch_device); \ |
| 108 | + } |
| 109 | + |
| 110 | +/** |
| 111 | + * Here to create test cases for different touchs |
| 112 | + * |
| 113 | + */ |
| 114 | +CREATE_TEST_CASE(CST816S) |
| 115 | +CREATE_TEST_CASE(FT5x06) |
| 116 | +CREATE_TEST_CASE(GT1151) |
| 117 | +CREATE_TEST_CASE(TT21100) |
| 118 | +CREATE_TEST_CASE(ST1633) |
| 119 | +CREATE_TEST_CASE(ST7123) |
0 commit comments