touch_matrix.h 8.8 KB

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