touch_matrix.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * SPDX-FileCopyrightText: 2016-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include "touch_element/touch_element.h"
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. /* ----------------------------- General matrix button instance default configuration ------------------------------ */
  12. #define TOUCH_MATRIX_GLOBAL_DEFAULT_CONFIG() \
  13. { \
  14. .threshold_divider = 0.8, \
  15. .default_lp_time = 1000 \
  16. }
  17. /* ------------------------------------------------------------------------------------------------------------------ */
  18. /**
  19. * @brief Matrix button initialization configuration passed to touch_matrix_install
  20. */
  21. typedef struct {
  22. float threshold_divider; //!< Matrix button channel threshold divider
  23. uint32_t default_lp_time; //!< Matrix button default LongPress event time (ms)
  24. } touch_matrix_global_config_t;
  25. /**
  26. * @brief Matrix button configuration (for new instance) passed to touch_matrix_create()
  27. */
  28. typedef struct {
  29. const touch_pad_t *x_channel_array; //!< Matrix button x-axis channels array
  30. const touch_pad_t *y_channel_array; //!< Matrix button y-axis channels array
  31. const float *x_sensitivity_array; //!< Matrix button x-axis channels sensitivity array
  32. const float *y_sensitivity_array; //!< Matrix button y-axis channels sensitivity array
  33. uint8_t x_channel_num; //!< The number of channels in x-axis
  34. uint8_t y_channel_num; //!< The number of channels in y-axis
  35. } touch_matrix_config_t;
  36. /**
  37. * @brief Matrix button event type
  38. */
  39. typedef enum {
  40. TOUCH_MATRIX_EVT_ON_PRESS, //!< Matrix button Press event
  41. TOUCH_MATRIX_EVT_ON_RELEASE, //!< Matrix button Press event
  42. TOUCH_MATRIX_EVT_ON_LONGPRESS, //!< Matrix button LongPress event
  43. TOUCH_MATRIX_EVT_MAX
  44. } touch_matrix_event_t;
  45. /**
  46. * @brief Matrix button position data type
  47. */
  48. typedef struct {
  49. uint8_t x_axis; //!< Matrix button x axis position
  50. uint8_t y_axis; //!< Matrix button y axis position
  51. uint8_t index; //!< Matrix button position index
  52. } touch_matrix_position_t;
  53. /**
  54. * @brief Matrix message type
  55. */
  56. typedef struct {
  57. touch_matrix_event_t event; //!< Matrix event
  58. touch_matrix_position_t position; //!< Matrix position
  59. } touch_matrix_message_t;
  60. typedef touch_elem_handle_t touch_matrix_handle_t; //!< Matrix button instance handle
  61. typedef void(*touch_matrix_callback_t)(touch_matrix_handle_t, touch_matrix_message_t *, void *); //!< Matrix button callback type
  62. /**
  63. * @brief Touch matrix button initialize
  64. *
  65. * This function initializes touch matrix button object and acts on all
  66. * touch matrix button instances.
  67. *
  68. * @param[in] global_config Touch matrix global initialization configuration
  69. *
  70. * @return
  71. * - ESP_OK: Successfully initialized touch matrix button
  72. * - ESP_ERR_INVALID_STATE: Touch element library was not initialized
  73. * - ESP_ERR_INVALID_ARG: matrix_init is NULL
  74. * - ESP_ERR_NO_MEM: Insufficient memory
  75. */
  76. esp_err_t touch_matrix_install(const touch_matrix_global_config_t *global_config);
  77. /**
  78. * @brief Release resources allocated using touch_matrix_install()
  79. *
  80. */
  81. void touch_matrix_uninstall(void);
  82. /**
  83. * @brief Create a new touch matrix button instance
  84. *
  85. * @param[in] matrix_config Matrix button configuration
  86. * @param[out] matrix_handle Matrix button handle
  87. *
  88. * @note Channel array and sensitivity array must be one-one correspondence in those array
  89. *
  90. * @note Touch matrix button does not support Multi-Touch now
  91. *
  92. * @return
  93. * - ESP_OK: Successfully create touch matrix button
  94. * - ESP_ERR_INVALID_STATE: Touch matrix driver was not initialized
  95. * - ESP_ERR_INVALID_ARG: Invalid configuration struct or arguments is NULL
  96. * - ESP_ERR_NO_MEM: Insufficient memory
  97. */
  98. esp_err_t touch_matrix_create(const touch_matrix_config_t *matrix_config, touch_matrix_handle_t *matrix_handle);
  99. /**
  100. * @brief Release resources allocated using touch_matrix_create()
  101. *
  102. * @param[in] matrix_handle Matrix handle
  103. * @return
  104. * - ESP_OK: Successfully released resources
  105. * - ESP_ERR_INVALID_STATE: Touch matrix driver was not initialized
  106. * - ESP_ERR_INVALID_ARG: matrix_handle is null
  107. * - ESP_ERR_NOT_FOUND: Input handle is not a matrix handle
  108. */
  109. esp_err_t touch_matrix_delete(touch_matrix_handle_t matrix_handle);
  110. /**
  111. * @brief Touch matrix button subscribes event
  112. *
  113. * This function uses event mask to subscribe to touch matrix events, once one of
  114. * the subscribed events occurs, the event message could be retrieved by calling
  115. * touch_element_message_receive() or input callback routine.
  116. *
  117. * @param[in] matrix_handle Matrix handle
  118. * @param[in] event_mask Matrix subscription event mask
  119. * @param[in] arg User input argument
  120. *
  121. * @note Touch matrix button only support three kind of event masks, they are
  122. * TOUCH_ELEM_EVENT_ON_PRESS, TOUCH_ELEM_EVENT_ON_RELEASE, TOUCH_ELEM_EVENT_ON_LONGPRESS. You can use those event
  123. * masks in any combination to achieve the desired effect.
  124. *
  125. * @return
  126. * - ESP_OK: Successfully subscribed touch matrix event
  127. * - ESP_ERR_INVALID_STATE: Touch matrix driver was not initialized
  128. * - ESP_ERR_INVALID_ARG: matrix_handle is null or event is not supported
  129. */
  130. esp_err_t touch_matrix_subscribe_event(touch_matrix_handle_t matrix_handle, uint32_t event_mask, void *arg);
  131. /**
  132. * @brief Touch matrix button set dispatch method
  133. *
  134. * This function sets a dispatch method that the driver core will use
  135. * this method as the event notification method.
  136. *
  137. * @param[in] matrix_handle Matrix button handle
  138. * @param[in] dispatch_method Dispatch method (By callback/event)
  139. *
  140. * @return
  141. * - ESP_OK: Successfully set dispatch method
  142. * - ESP_ERR_INVALID_STATE: Touch matrix driver was not initialized
  143. * - ESP_ERR_INVALID_ARG: matrix_handle is null or dispatch_method is invalid
  144. */
  145. esp_err_t touch_matrix_set_dispatch_method(touch_matrix_handle_t matrix_handle, touch_elem_dispatch_t dispatch_method);
  146. /**
  147. * @brief Touch matrix button set callback
  148. *
  149. * This function sets a callback routine into touch element driver core,
  150. * when the subscribed events occur, the callback routine will be called.
  151. *
  152. * @param[in] matrix_handle Matrix button handle
  153. * @param[in] matrix_callback User input callback
  154. *
  155. * @note Matrix message will be passed from the callback function and it will be destroyed when the callback function return.
  156. *
  157. * @warning Since this input callback routine runs on driver core (esp-timer callback routine),
  158. * it should not do something that attempts to Block, such as calling vTaskDelay().
  159. *
  160. * @return
  161. * - ESP_OK: Successfully set callback
  162. * - ESP_ERR_INVALID_STATE: Touch matrix driver was not initialized
  163. * - ESP_ERR_INVALID_ARG: matrix_handle or matrix_callback is null
  164. */
  165. esp_err_t touch_matrix_set_callback(touch_matrix_handle_t matrix_handle, touch_matrix_callback_t matrix_callback);
  166. /**
  167. * @brief Touch matrix button set long press trigger time
  168. *
  169. * This function sets the threshold time (ms) for a long press event. If a matrix button is pressed
  170. * and held for a period of time that exceeds the threshold time, a long press event is triggered.
  171. *
  172. * @param[in] matrix_handle Matrix button handle
  173. * @param[in] threshold_time Threshold time (ms) of long press event occur
  174. *
  175. * @return
  176. * - ESP_OK: Successfully set the time of long press event
  177. * - ESP_ERR_INVALID_STATE: Touch matrix driver was not initialized
  178. * - ESP_ERR_INVALID_ARG: matrix_handle is null or time (ms) is 0
  179. */
  180. esp_err_t touch_matrix_set_longpress(touch_matrix_handle_t matrix_handle, uint32_t threshold_time);
  181. /**
  182. * @brief Touch matrix get message
  183. *
  184. * This function decodes the element message from touch_element_message_receive() and return
  185. * a matrix message pointer.
  186. *
  187. * @param[in] element_message element message
  188. *
  189. * @return Touch matrix message pointer
  190. */
  191. const touch_matrix_message_t* touch_matrix_get_message(const touch_elem_message_t* element_message);
  192. #ifdef __cplusplus
  193. }
  194. #endif