test_touch_matrix.c 23 KB

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