test_touch_matrix.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <time.h>
  10. #include "freertos/FreeRTOS.h"
  11. #include "freertos/task.h"
  12. #include "freertos/queue.h"
  13. #include "freertos/semphr.h"
  14. #include "unity.h"
  15. #include "touch_element/touch_element_private.h"
  16. #include "touch_element/touch_matrix.h"
  17. static const touch_pad_t x_axis_channel[3] = {
  18. TOUCH_PAD_NUM5,
  19. TOUCH_PAD_NUM7,
  20. TOUCH_PAD_NUM9,
  21. };
  22. static const touch_pad_t y_axis_channel[3] = {
  23. TOUCH_PAD_NUM11,
  24. TOUCH_PAD_NUM12,
  25. TOUCH_PAD_NUM14,
  26. };
  27. static const float x_axis_channel_sens[3] = {
  28. 0.1F,
  29. 0.1F,
  30. 0.1F,
  31. };
  32. static const float y_axis_channel_sens[3] = {
  33. 0.1F,
  34. 0.1F,
  35. 0.1F,
  36. };
  37. const uint8_t MATRIX_CHANNEL_NUM_X = sizeof(x_axis_channel) / sizeof(touch_pad_t);
  38. const uint8_t MATRIX_CHANNEL_NUM_Y = sizeof(y_axis_channel) / sizeof(touch_pad_t);
  39. typedef struct {
  40. QueueHandle_t valid_msg_handle;
  41. SemaphoreHandle_t response_sig_handle;
  42. } test_monitor_t;
  43. /* ------------------------------------------------------------------------------------------------------------------ */
  44. void test_matrix_event_simulator(touch_matrix_handle_t matrix_handle, touch_matrix_event_t matrix_event, uint32_t pos_index);
  45. static void test_matrix_channel_simulator(touch_pad_t channel, touch_matrix_event_t matrix_event);
  46. void test_matrix_event_check(touch_elem_message_t *valid_message, touch_elem_message_t *current_message);
  47. static void test_matrix_callback_check(touch_matrix_handle_t current_handle, touch_matrix_message_t *current_message, touch_elem_message_t *valid_message);
  48. void test_matrix_event_trigger_and_check(touch_matrix_handle_t handle, touch_matrix_event_t matrix_event, uint32_t pos_index);
  49. void test_matrix_callback_trigger_and_check(touch_matrix_handle_t handle, touch_matrix_event_t matrix_event, uint32_t pos_index, bool should_trigger, test_monitor_t *monitor);
  50. /* ------------------------------------------------ Dispatch method test -------------------------------------------- */
  51. static void test_matrix_disp_event(void);
  52. static void test_matrix_disp_callback(void);
  53. static void test_matrix_handler(touch_matrix_handle_t handle, touch_matrix_message_t *message, void *arg);
  54. /* ------------------------------------------------ Run-time test --------------------------------------------------- */
  55. static void test_matrix_event_change_lp(void);
  56. static void test_matrix_callback_change_lp(void);
  57. static void test_matrix_change_lp_handler(touch_matrix_handle_t out_handle, touch_matrix_message_t *out_message, void *arg);
  58. /* ----------------------------------------------- Random channel trigger test -------------------------------------- */
  59. static void test_matrix_random_channel_trigger(void);
  60. /* ------------------------------------------------------------------------------------------------------------------ */
  61. TEST_CASE("Touch matrix dispatch methods test", "[matrix][touch_element]")
  62. {
  63. touch_elem_global_config_t global_config = TOUCH_ELEM_GLOBAL_DEFAULT_CONFIG();
  64. TEST_ESP_OK(touch_element_install(&global_config));
  65. test_matrix_disp_event();
  66. test_matrix_disp_callback();
  67. touch_element_uninstall();
  68. }
  69. TEST_CASE("Touch matrix run-time test", "[matrix][touch_element]")
  70. {
  71. touch_elem_global_config_t global_config = TOUCH_ELEM_GLOBAL_DEFAULT_CONFIG();
  72. TEST_ESP_OK(touch_element_install(&global_config));
  73. test_matrix_event_change_lp();
  74. test_matrix_callback_change_lp();
  75. touch_element_uninstall();
  76. }
  77. TEST_CASE("Touch matrix random channel trigger test", "[matrix][touch_element]")
  78. {
  79. touch_elem_global_config_t global_config = TOUCH_ELEM_GLOBAL_DEFAULT_CONFIG();
  80. TEST_ESP_OK(touch_element_install(&global_config));
  81. test_matrix_random_channel_trigger();
  82. touch_element_uninstall();
  83. }
  84. void test_matrix_event_simulator(touch_matrix_handle_t matrix_handle, touch_matrix_event_t matrix_event, uint32_t pos_index)
  85. {
  86. te_matrix_handle_t te_matrix = (te_matrix_handle_t) matrix_handle;
  87. touch_pad_t x_channel = te_matrix->device[pos_index / te_matrix->y_channel_num]->channel;
  88. touch_pad_t y_channel = te_matrix->device[te_matrix->x_channel_num + (pos_index % te_matrix->y_channel_num)]->channel;
  89. if (matrix_event == TOUCH_MATRIX_EVT_ON_PRESS) {
  90. touch_pad_set_cnt_mode(x_channel, TOUCH_PAD_SLOPE_3, TOUCH_PAD_TIE_OPT_DEFAULT);
  91. touch_pad_set_cnt_mode(y_channel, TOUCH_PAD_SLOPE_3, TOUCH_PAD_TIE_OPT_DEFAULT);
  92. } else if (matrix_event == TOUCH_MATRIX_EVT_ON_RELEASE) {
  93. touch_pad_set_cnt_mode(x_channel, TOUCH_PAD_SLOPE_7, TOUCH_PAD_TIE_OPT_DEFAULT);
  94. touch_pad_set_cnt_mode(y_channel, TOUCH_PAD_SLOPE_7, TOUCH_PAD_TIE_OPT_DEFAULT);
  95. } else {
  96. touch_pad_set_cnt_mode(x_channel, TOUCH_PAD_SLOPE_3, TOUCH_PAD_TIE_OPT_DEFAULT); // LongPress
  97. touch_pad_set_cnt_mode(y_channel, TOUCH_PAD_SLOPE_3, TOUCH_PAD_TIE_OPT_DEFAULT);
  98. }
  99. }
  100. static void test_matrix_channel_simulator(touch_pad_t channel, touch_matrix_event_t matrix_event)
  101. {
  102. if (matrix_event == TOUCH_MATRIX_EVT_ON_PRESS) {
  103. touch_pad_set_cnt_mode(channel, TOUCH_PAD_SLOPE_3, TOUCH_PAD_TIE_OPT_DEFAULT);
  104. } else if (matrix_event == TOUCH_MATRIX_EVT_ON_RELEASE) {
  105. touch_pad_set_cnt_mode(channel, TOUCH_PAD_SLOPE_7, TOUCH_PAD_TIE_OPT_DEFAULT);
  106. }
  107. }
  108. void test_matrix_event_check(touch_elem_message_t *valid_message, touch_elem_message_t *current_message)
  109. {
  110. TEST_ASSERT_MESSAGE(current_message->handle == valid_message->handle, "check handle failed");
  111. TEST_ASSERT_MESSAGE(current_message->element_type == valid_message->element_type, "check element type failed");
  112. const touch_matrix_message_t *valid_matrix_message = touch_matrix_get_message(valid_message);
  113. const touch_matrix_message_t *current_matrix_message = touch_matrix_get_message(current_message);
  114. TEST_ASSERT_MESSAGE(current_matrix_message->event == valid_matrix_message->event, "check event failed");
  115. TEST_ASSERT_MESSAGE(current_matrix_message->position.index == valid_matrix_message->position.index, "check index failed");
  116. TEST_ASSERT_MESSAGE(current_matrix_message->position.x_axis == valid_matrix_message->position.x_axis, "check x_axis failed");
  117. TEST_ASSERT_MESSAGE(current_matrix_message->position.y_axis == valid_matrix_message->position.y_axis, "check y_axis failed");
  118. }
  119. static inline void test_matrix_callback_check(touch_matrix_handle_t current_handle, touch_matrix_message_t *current_message, touch_elem_message_t *valid_message)
  120. {
  121. const touch_matrix_message_t *valid_matrix_message = touch_matrix_get_message(valid_message);
  122. TEST_ASSERT_MESSAGE(valid_message->handle == current_handle, "check handle failed");
  123. TEST_ASSERT_MESSAGE(valid_message->element_type == TOUCH_ELEM_TYPE_MATRIX, "check element type failed");
  124. TEST_ASSERT_MESSAGE(valid_matrix_message->event == current_message->event, "check event failed");
  125. TEST_ASSERT_MESSAGE(valid_matrix_message->position.index == current_message->position.index, "check index failed");
  126. TEST_ASSERT_MESSAGE(valid_matrix_message->position.x_axis == current_message->position.x_axis, "check x_axis failed");
  127. TEST_ASSERT_MESSAGE(valid_matrix_message->position.y_axis == current_message->position.y_axis, "check y_axis failed");
  128. }
  129. void test_matrix_event_trigger_and_check(touch_matrix_handle_t handle, touch_matrix_event_t matrix_event, uint32_t pos_index)
  130. {
  131. touch_elem_message_t valid_message = {
  132. .handle = handle,
  133. .element_type = TOUCH_ELEM_TYPE_MATRIX,
  134. .arg = NULL
  135. };
  136. touch_matrix_message_t matrix_message = {
  137. .event = matrix_event,
  138. .position.index = pos_index,
  139. .position.x_axis = pos_index / MATRIX_CHANNEL_NUM_Y,
  140. .position.y_axis = pos_index % MATRIX_CHANNEL_NUM_Y
  141. };
  142. memcpy(valid_message.child_msg, &matrix_message, sizeof(touch_matrix_message_t)); //Construct valid_message
  143. test_matrix_event_simulator(handle, matrix_event, pos_index); //Trigger signal
  144. touch_elem_message_t current_message;
  145. te_matrix_handle_t te_matrix = handle;
  146. esp_err_t ret = touch_element_message_receive(&current_message, pdMS_TO_TICKS(2 * te_matrix->trigger_thr * 10)); //Get current message for verification
  147. TEST_ASSERT_MESSAGE(ret == ESP_OK, "matrix event receive timeout");
  148. test_matrix_event_check(&valid_message, &current_message); //Verification
  149. }
  150. void test_matrix_callback_trigger_and_check(touch_matrix_handle_t handle, touch_matrix_event_t matrix_event, uint32_t pos_index, bool should_trigger, test_monitor_t *monitor)
  151. {
  152. if (should_trigger) {
  153. touch_elem_message_t valid_message = {
  154. .handle = handle,
  155. .element_type = TOUCH_ELEM_TYPE_MATRIX,
  156. .arg = NULL
  157. };
  158. touch_matrix_message_t matrix_message = {
  159. .event = matrix_event,
  160. .position.index = pos_index,
  161. .position.x_axis = pos_index / MATRIX_CHANNEL_NUM_Y,
  162. .position.y_axis = pos_index % MATRIX_CHANNEL_NUM_Y
  163. };
  164. memcpy(valid_message.child_msg, &matrix_message, sizeof(touch_matrix_message_t)); //Construct valid_message
  165. xQueueSend(monitor->valid_msg_handle, &valid_message, portMAX_DELAY);
  166. }
  167. test_matrix_event_simulator(handle, matrix_event, pos_index); //Trigger signal
  168. te_matrix_handle_t te_matrix = handle;
  169. if (should_trigger) {
  170. BaseType_t os_ret = xSemaphoreTake(monitor->response_sig_handle, pdMS_TO_TICKS(2 * te_matrix->trigger_thr * 10));
  171. TEST_ASSERT_MESSAGE(os_ret == pdPASS, "Matrix queue timeout");
  172. } else {
  173. BaseType_t os_ret = xSemaphoreTake(monitor->response_sig_handle, pdMS_TO_TICKS(500));
  174. TEST_ASSERT_MESSAGE(os_ret == pdFALSE, "Matrix invalid trigger");
  175. }
  176. }
  177. static void test_matrix_disp_event(void)
  178. {
  179. touch_matrix_handle_t matrix_handle = NULL;
  180. touch_matrix_global_config_t global_config = TOUCH_MATRIX_GLOBAL_DEFAULT_CONFIG();
  181. TEST_ESP_OK(touch_matrix_install(&global_config));
  182. touch_matrix_config_t matrix_config = {
  183. .x_channel_array = x_axis_channel,
  184. .y_channel_array = y_axis_channel,
  185. .x_sensitivity_array = x_axis_channel_sens,
  186. .y_sensitivity_array = y_axis_channel_sens,
  187. .x_channel_num = MATRIX_CHANNEL_NUM_X,
  188. .y_channel_num = MATRIX_CHANNEL_NUM_Y
  189. };
  190. TEST_ESP_OK(touch_matrix_create(&matrix_config, &matrix_handle));
  191. TEST_ESP_OK(touch_matrix_subscribe_event(matrix_handle, TOUCH_ELEM_EVENT_ON_PRESS | TOUCH_ELEM_EVENT_ON_LONGPRESS | TOUCH_ELEM_EVENT_ON_RELEASE, NULL));
  192. TEST_ESP_OK(touch_matrix_set_longpress(matrix_handle, 300));
  193. TEST_ESP_OK(touch_matrix_set_dispatch_method(matrix_handle, TOUCH_ELEM_DISP_EVENT));
  194. TEST_ESP_OK(touch_element_start());
  195. vTaskDelay(pdMS_TO_TICKS(500)); //Mention in README, code-block-1
  196. srandom((unsigned int)time(NULL));
  197. printf("Touch matrix event test start\n");
  198. for (int i = 0; i < 10; i++) {
  199. printf("Touch matrix event test... (%d/10)\n", i + 1);
  200. uint32_t button_num = random() % ( MATRIX_CHANNEL_NUM_X * MATRIX_CHANNEL_NUM_Y );
  201. test_matrix_event_trigger_and_check(matrix_handle, TOUCH_MATRIX_EVT_ON_PRESS, button_num);
  202. test_matrix_event_trigger_and_check(matrix_handle, TOUCH_MATRIX_EVT_ON_LONGPRESS, button_num);
  203. test_matrix_event_trigger_and_check(matrix_handle, TOUCH_MATRIX_EVT_ON_RELEASE, button_num);
  204. }
  205. printf("Touch matrix event test finish\n");
  206. TEST_ESP_OK(touch_element_stop());
  207. TEST_ESP_OK(touch_matrix_delete(matrix_handle));
  208. touch_matrix_uninstall();
  209. }
  210. static void test_matrix_disp_callback(void)
  211. {
  212. test_monitor_t monitor;
  213. touch_matrix_handle_t matrix_handle = NULL;
  214. monitor.valid_msg_handle = xQueueCreate(10, sizeof(touch_elem_message_t));
  215. monitor.response_sig_handle = xSemaphoreCreateBinary();
  216. TEST_ASSERT(monitor.valid_msg_handle != NULL || monitor.response_sig_handle != NULL);
  217. touch_matrix_global_config_t global_config = TOUCH_MATRIX_GLOBAL_DEFAULT_CONFIG();
  218. TEST_ESP_OK(touch_matrix_install(&global_config));
  219. touch_matrix_config_t matrix_config = {
  220. .x_channel_array = x_axis_channel,
  221. .y_channel_array = y_axis_channel,
  222. .x_sensitivity_array = x_axis_channel_sens,
  223. .y_sensitivity_array = y_axis_channel_sens,
  224. .x_channel_num = MATRIX_CHANNEL_NUM_X,
  225. .y_channel_num = MATRIX_CHANNEL_NUM_Y
  226. };
  227. TEST_ESP_OK(touch_matrix_create(&matrix_config, &matrix_handle));
  228. TEST_ESP_OK(touch_matrix_subscribe_event(matrix_handle, TOUCH_ELEM_EVENT_ON_PRESS | TOUCH_ELEM_EVENT_ON_LONGPRESS | TOUCH_ELEM_EVENT_ON_RELEASE, (void *)&monitor));
  229. TEST_ESP_OK(touch_matrix_set_longpress(matrix_handle, 300));
  230. TEST_ESP_OK(touch_matrix_set_dispatch_method(matrix_handle, TOUCH_ELEM_DISP_CALLBACK));
  231. TEST_ESP_OK(touch_matrix_set_callback(matrix_handle, test_matrix_handler));
  232. TEST_ESP_OK(touch_element_start());
  233. vTaskDelay(pdMS_TO_TICKS(500)); //Mention in README, code-block-1
  234. srandom((unsigned int)time(NULL));
  235. printf("Touch matrix callback test start\n");
  236. for (int i = 0; i < 10; i++) {
  237. printf("Touch matrix callback test... (%d/10)\n", i + 1);
  238. uint32_t button_num = random() % (MATRIX_CHANNEL_NUM_X * MATRIX_CHANNEL_NUM_Y);
  239. test_matrix_callback_trigger_and_check(matrix_handle, TOUCH_MATRIX_EVT_ON_PRESS, button_num, true, &monitor);
  240. test_matrix_callback_trigger_and_check(matrix_handle, TOUCH_MATRIX_EVT_ON_LONGPRESS, button_num, true, &monitor);
  241. test_matrix_callback_trigger_and_check(matrix_handle, TOUCH_MATRIX_EVT_ON_RELEASE, button_num, true, &monitor);
  242. }
  243. printf("Touch matrix callback test finish\n");
  244. TEST_ESP_OK(touch_element_stop());
  245. TEST_ESP_OK(touch_matrix_delete(matrix_handle));
  246. touch_matrix_uninstall();
  247. vQueueDelete(monitor.valid_msg_handle);
  248. vSemaphoreDelete(monitor.response_sig_handle);
  249. }
  250. static void test_matrix_handler(touch_matrix_handle_t handle, touch_matrix_message_t *message, void *arg)
  251. {
  252. test_monitor_t *monitor = (test_monitor_t *)arg;
  253. touch_elem_message_t valid_message;
  254. BaseType_t os_ret = xQueueReceive(monitor->valid_msg_handle, &valid_message, pdMS_TO_TICKS(200));
  255. TEST_ASSERT_MESSAGE(os_ret == pdPASS, "test_matrix_handler: queue timeout");
  256. test_matrix_callback_check(handle, message, &valid_message);
  257. xSemaphoreGive(monitor->response_sig_handle);
  258. }
  259. static void test_matrix_random_channel_trigger(void)
  260. {
  261. test_monitor_t monitor;
  262. touch_matrix_handle_t matrix_handle = NULL;
  263. monitor.valid_msg_handle = xQueueCreate(10, sizeof(touch_elem_message_t));
  264. monitor.response_sig_handle = xSemaphoreCreateBinary();
  265. TEST_ASSERT(monitor.valid_msg_handle != NULL || monitor.response_sig_handle != NULL);
  266. touch_matrix_global_config_t global_config = TOUCH_MATRIX_GLOBAL_DEFAULT_CONFIG();
  267. TEST_ESP_OK(touch_matrix_install(&global_config));
  268. touch_matrix_config_t matrix_config = {
  269. .x_channel_array = x_axis_channel,
  270. .y_channel_array = y_axis_channel,
  271. .x_sensitivity_array = x_axis_channel_sens,
  272. .y_sensitivity_array = y_axis_channel_sens,
  273. .x_channel_num = MATRIX_CHANNEL_NUM_X,
  274. .y_channel_num = MATRIX_CHANNEL_NUM_Y
  275. };
  276. TEST_ESP_OK(touch_matrix_create(&matrix_config, &matrix_handle));
  277. TEST_ESP_OK(touch_matrix_subscribe_event(matrix_handle, TOUCH_ELEM_EVENT_ON_PRESS | TOUCH_ELEM_EVENT_ON_RELEASE, (void *) &monitor));
  278. TEST_ESP_OK(touch_matrix_set_dispatch_method(matrix_handle, TOUCH_ELEM_DISP_CALLBACK));
  279. TEST_ESP_OK(touch_matrix_set_callback(matrix_handle, test_matrix_handler));
  280. TEST_ESP_OK(touch_element_start());
  281. vTaskDelay(pdMS_TO_TICKS(500)); //Mention in README, code-block-1
  282. srandom((unsigned int)time(NULL));
  283. printf("Touch matrix random channel trigger test start\n");
  284. for (int i = 0; i < 10; i++) {
  285. printf("Touch matrix random channel trigger test... (%d/10)\n", i + 1);
  286. uint32_t channel_index_1 = random() % (MATRIX_CHANNEL_NUM_X + MATRIX_CHANNEL_NUM_Y);
  287. uint32_t channel_index_2 = random() % (MATRIX_CHANNEL_NUM_X + MATRIX_CHANNEL_NUM_Y);
  288. touch_pad_t channel_1 = (channel_index_1 < MATRIX_CHANNEL_NUM_X) ? x_axis_channel[channel_index_1] : y_axis_channel[channel_index_1 - MATRIX_CHANNEL_NUM_X];
  289. touch_pad_t channel_2 = (channel_index_2 < MATRIX_CHANNEL_NUM_X) ? x_axis_channel[channel_index_2] : y_axis_channel[channel_index_2 - MATRIX_CHANNEL_NUM_X];
  290. if ((channel_index_1 <= 2 && channel_index_2 <= 2) || (channel_index_1 > 2 && channel_index_2 > 2)) { //all x channels triggered or all y channels triggered
  291. //Should not be triggered
  292. BaseType_t os_ret;
  293. test_matrix_channel_simulator(channel_1, TOUCH_MATRIX_EVT_ON_PRESS);
  294. test_matrix_channel_simulator(channel_2, TOUCH_MATRIX_EVT_ON_PRESS);
  295. os_ret = xSemaphoreTake(monitor.response_sig_handle, pdMS_TO_TICKS(500));
  296. TEST_ASSERT_MESSAGE(os_ret == pdFAIL, "Matrix Press event invalid trigger");
  297. test_matrix_channel_simulator(channel_1, TOUCH_MATRIX_EVT_ON_RELEASE);
  298. test_matrix_channel_simulator(channel_2, TOUCH_MATRIX_EVT_ON_RELEASE);
  299. os_ret = xSemaphoreTake(monitor.response_sig_handle, pdMS_TO_TICKS(500));
  300. TEST_ASSERT_MESSAGE(os_ret == pdFAIL, "Matrix Release event invalid trigger");
  301. } else {
  302. //Should be triggered
  303. uint8_t button_num;
  304. if (channel_index_1 <= 2) {
  305. button_num = channel_index_1 * matrix_config.y_channel_num + (channel_index_2 - MATRIX_CHANNEL_NUM_X);
  306. } else {
  307. button_num = channel_index_2 * matrix_config.x_channel_num + (channel_index_1 - MATRIX_CHANNEL_NUM_Y);
  308. }
  309. test_matrix_callback_trigger_and_check(matrix_handle, TOUCH_MATRIX_EVT_ON_PRESS, button_num, true, &monitor);
  310. test_matrix_callback_trigger_and_check(matrix_handle, TOUCH_MATRIX_EVT_ON_RELEASE, button_num, true, &monitor);
  311. }
  312. }
  313. printf("Touch matrix random channel trigger test finish\n");
  314. TEST_ESP_OK(touch_element_stop());
  315. TEST_ESP_OK(touch_matrix_delete(matrix_handle));
  316. touch_matrix_uninstall();
  317. vQueueDelete(monitor.valid_msg_handle);
  318. vSemaphoreDelete(monitor.response_sig_handle);
  319. }
  320. static void test_matrix_event_change_lp(void)
  321. {
  322. touch_matrix_handle_t matrix_handle = NULL;
  323. touch_matrix_global_config_t global_config = TOUCH_MATRIX_GLOBAL_DEFAULT_CONFIG();
  324. TEST_ESP_OK(touch_matrix_install(&global_config));
  325. touch_matrix_config_t matrix_config = {
  326. .x_channel_array = x_axis_channel,
  327. .y_channel_array = y_axis_channel,
  328. .x_sensitivity_array = x_axis_channel_sens,
  329. .y_sensitivity_array = y_axis_channel_sens,
  330. .x_channel_num = MATRIX_CHANNEL_NUM_X,
  331. .y_channel_num = MATRIX_CHANNEL_NUM_Y
  332. };
  333. TEST_ESP_OK(touch_matrix_create(&matrix_config, &matrix_handle));
  334. TEST_ESP_OK(touch_matrix_subscribe_event(matrix_handle, TOUCH_ELEM_EVENT_ON_LONGPRESS, NULL));
  335. TEST_ESP_OK(touch_matrix_set_dispatch_method(matrix_handle, TOUCH_ELEM_DISP_EVENT));
  336. TEST_ESP_OK(touch_element_start());
  337. vTaskDelay(pdMS_TO_TICKS(500)); //Mention in README, code-block-1
  338. srandom((unsigned int)time(NULL));
  339. //10 times random press/longpress/release test
  340. printf("Touch matrix event change longtime test start\n");
  341. for (int i = 0; i < 10; i++) {
  342. printf("Touch matrix event change longtime test... (%d/10)\n", i + 1);
  343. uint32_t button_num = random() % ( MATRIX_CHANNEL_NUM_X * MATRIX_CHANNEL_NUM_Y );
  344. TEST_ESP_OK(touch_matrix_set_longpress(matrix_handle, 200 + (i + 1) * 50));
  345. test_matrix_event_trigger_and_check(matrix_handle, TOUCH_MATRIX_EVT_ON_LONGPRESS, button_num);
  346. test_matrix_event_simulator(matrix_handle, TOUCH_MATRIX_EVT_ON_RELEASE, button_num); //Reset hardware
  347. vTaskDelay(pdMS_TO_TICKS(100)); //Fixme: Waiting for driver core handle release event
  348. }
  349. printf("Touch matrix event change longtime test finish\n");
  350. TEST_ESP_OK(touch_element_stop());
  351. TEST_ESP_OK(touch_matrix_delete(matrix_handle));
  352. touch_matrix_uninstall();
  353. }
  354. static void test_matrix_callback_change_lp(void)
  355. {
  356. test_monitor_t monitor;
  357. touch_matrix_handle_t matrix_handle = NULL;
  358. monitor.valid_msg_handle = xQueueCreate(10, sizeof(touch_elem_message_t));
  359. monitor.response_sig_handle = xSemaphoreCreateBinary();
  360. TEST_ASSERT(monitor.valid_msg_handle != NULL || monitor.response_sig_handle != NULL);
  361. touch_matrix_global_config_t global_config = TOUCH_MATRIX_GLOBAL_DEFAULT_CONFIG();
  362. TEST_ESP_OK(touch_matrix_install(&global_config));
  363. touch_matrix_config_t matrix_config = {
  364. .x_channel_array = x_axis_channel,
  365. .y_channel_array = y_axis_channel,
  366. .x_sensitivity_array = x_axis_channel_sens,
  367. .y_sensitivity_array = y_axis_channel_sens,
  368. .x_channel_num = MATRIX_CHANNEL_NUM_X,
  369. .y_channel_num = MATRIX_CHANNEL_NUM_Y
  370. };
  371. TEST_ESP_OK(touch_matrix_create(&matrix_config, &matrix_handle));
  372. TEST_ESP_OK(touch_matrix_subscribe_event(matrix_handle, TOUCH_ELEM_EVENT_ON_LONGPRESS, (void *)&monitor));
  373. TEST_ESP_OK(touch_matrix_set_longpress(matrix_handle, 300));
  374. TEST_ESP_OK(touch_matrix_set_dispatch_method(matrix_handle, TOUCH_ELEM_DISP_CALLBACK));
  375. TEST_ESP_OK(touch_matrix_set_callback(matrix_handle, test_matrix_change_lp_handler));
  376. TEST_ESP_OK(touch_element_start());
  377. vTaskDelay(pdMS_TO_TICKS(500)); //Mention in README, code-block-1
  378. srandom((unsigned int)time(NULL));
  379. printf("Touch matrix callback change longtime test start\n");
  380. for (int i = 0; i < 10; i++) {
  381. printf("Touch matrix callback change longtime test... (%d/10)\n", i + 1);
  382. uint32_t button_num = random() % (MATRIX_CHANNEL_NUM_X * MATRIX_CHANNEL_NUM_Y);
  383. test_matrix_callback_trigger_and_check(matrix_handle, TOUCH_MATRIX_EVT_ON_LONGPRESS, button_num, true, &monitor);
  384. test_matrix_event_simulator(matrix_handle, TOUCH_MATRIX_EVT_ON_RELEASE, button_num); //Reset hardware
  385. vTaskDelay(pdMS_TO_TICKS(100)); //Fixme: Waiting for driver core handle release event
  386. }
  387. printf("Touch matrix callback change longtime test finish\n");
  388. TEST_ESP_OK(touch_element_stop());
  389. TEST_ESP_OK(touch_matrix_delete(matrix_handle));
  390. touch_matrix_uninstall();
  391. vQueueDelete(monitor.valid_msg_handle);
  392. vSemaphoreDelete(monitor.response_sig_handle);
  393. }
  394. static void test_matrix_change_lp_handler(touch_matrix_handle_t out_handle, touch_matrix_message_t *out_message, void *arg)
  395. {
  396. test_monitor_t *monitor = (test_monitor_t *)arg;
  397. touch_elem_message_t valid_message;
  398. BaseType_t os_ret = xQueueReceive(monitor->valid_msg_handle, &valid_message, pdMS_TO_TICKS(200)); //500ms timeout
  399. TEST_ASSERT_MESSAGE(os_ret == pdPASS, "test_matrix_handler: queue timeout");
  400. test_matrix_callback_check(out_handle, out_message, &valid_message);
  401. xSemaphoreGive(monitor->response_sig_handle);
  402. TEST_ESP_OK(touch_matrix_set_longpress(valid_message.handle, 300)); // Always 300ms
  403. }