touch_slider.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 slider instance default configuration --------------------------------- */
  20. #define TOUCH_SLIDER_GLOBAL_DEFAULT_CONFIG() \
  21. { \
  22. .quantify_lower_threshold = 0.3, \
  23. .threshold_divider = 0.8, \
  24. .filter_reset_time = 50, \
  25. .benchmark_update_time = 500, \
  26. .position_filter_size = 10, \
  27. .position_filter_factor = 2, \
  28. .calculate_channel_count = 3 \
  29. }
  30. /* ------------------------------------------------------------------------------------------------------------------ */
  31. /**
  32. * @brief Slider initialization configuration passed to touch_slider_install
  33. */
  34. typedef struct {
  35. float quantify_lower_threshold; //!< Slider signal quantification threshold
  36. float threshold_divider; //!< Slider channel threshold divider
  37. uint16_t filter_reset_time; //!< Slider position filter reset time (Unit is esp_timer callback tick)
  38. uint16_t benchmark_update_time; //!< Slider benchmark update time (Unit is esp_timer callback tick)
  39. uint8_t position_filter_size; //!< Moving window filter buffer size
  40. uint8_t position_filter_factor; //!< One-order IIR filter factor
  41. uint8_t calculate_channel_count; //!< The number of channels which will take part in calculation
  42. } touch_slider_global_config_t;
  43. /**
  44. * @brief Slider configuration (for new instance) passed to touch_slider_create()
  45. */
  46. typedef struct {
  47. const touch_pad_t *channel_array; //!< Slider channel array
  48. const float *sensitivity_array; //!< Slider channel sensitivity array
  49. uint8_t channel_num; //!< The number of slider channels
  50. uint8_t position_range; //!< The right region of touch slider position range, [0, position_range (less than or equal to 255)]
  51. } touch_slider_config_t;
  52. /**
  53. * @brief Slider event type
  54. */
  55. typedef enum {
  56. TOUCH_SLIDER_EVT_ON_PRESS, //!< Slider on Press event
  57. TOUCH_SLIDER_EVT_ON_RELEASE, //!< Slider on Release event
  58. TOUCH_SLIDER_EVT_ON_CALCULATION, //!< Slider on Calculation event
  59. TOUCH_SLIDER_EVT_MAX
  60. } touch_slider_event_t;
  61. typedef uint32_t touch_slider_position_t; //!< Slider position data type
  62. /**
  63. * @brief Slider message type
  64. */
  65. typedef struct {
  66. touch_slider_event_t event; //!< Slider event
  67. touch_slider_position_t position; //!< Slider position
  68. } touch_slider_message_t;
  69. typedef touch_elem_handle_t touch_slider_handle_t; //!< Slider instance handle
  70. typedef void(*touch_slider_callback_t)(touch_slider_handle_t, touch_slider_message_t *, void *); //!< Slider callback type
  71. /**
  72. * @brief Touch slider initialize
  73. *
  74. * This function initializes touch slider object and acts on all
  75. * touch slider instances.
  76. *
  77. * @param[in] global_config Touch slider global initialization configuration
  78. *
  79. * @return
  80. * - ESP_OK: Successfully initialized touch slider
  81. * - ESP_ERR_INVALID_STATE: Touch element library was not initialized
  82. * - ESP_ERR_INVALID_ARG: slider_init is NULL
  83. * - ESP_ERR_NO_MEM: Insufficient memory
  84. */
  85. esp_err_t touch_slider_install(const touch_slider_global_config_t *global_config);
  86. /**
  87. * @brief Release resources allocated using touch_slider_install()
  88. *
  89. * @return
  90. * - ESP_OK: Successfully released resources
  91. */
  92. void touch_slider_uninstall(void);
  93. /**
  94. * @brief Create a new touch slider instance
  95. *
  96. * @param[in] slider_config Slider configuration
  97. * @param[out] slider_handle Slider handle
  98. *
  99. * @note The index of Channel array and sensitivity array must be one-one correspondence
  100. *
  101. * @return
  102. * - ESP_OK: Successfully create touch slider
  103. * - ESP_ERR_INVALID_STATE: Touch slider driver was not initialized
  104. * - ESP_ERR_INVALID_ARG: Invalid configuration struct or arguments is NULL
  105. * - ESP_ERR_NO_MEM: Insufficient memory
  106. */
  107. esp_err_t touch_slider_create(const touch_slider_config_t *slider_config, touch_slider_handle_t *slider_handle);
  108. /**
  109. * @brief Release resources allocated using touch_slider_create
  110. *
  111. * @param[in] slider_handle Slider handle
  112. * @return
  113. * - ESP_OK: Successfully released resources
  114. * - ESP_ERR_INVALID_STATE: Touch slider driver was not initialized
  115. * - ESP_ERR_INVALID_ARG: slider_handle is null
  116. * - ESP_ERR_NOT_FOUND: Input handle is not a slider handle
  117. */
  118. esp_err_t touch_slider_delete(touch_slider_handle_t slider_handle);
  119. /**
  120. * @brief Touch slider subscribes event
  121. *
  122. * This function uses event mask to subscribe to touch slider events, once one of
  123. * the subscribed events occurs, the event message could be retrieved by calling
  124. * touch_element_message_receive() or input callback routine.
  125. *
  126. * @param[in] slider_handle Slider handle
  127. * @param[in] event_mask Slider subscription event mask
  128. * @param[in] arg User input argument
  129. *
  130. * @note Touch slider only support three kind of event masks, they are
  131. * TOUCH_ELEM_EVENT_ON_PRESS, TOUCH_ELEM_EVENT_ON_RELEASE. You can use those event masks in any
  132. * combination to achieve the desired effect.
  133. *
  134. * @return
  135. * - ESP_OK: Successfully subscribed touch slider event
  136. * - ESP_ERR_INVALID_STATE: Touch slider driver was not initialized
  137. * - ESP_ERR_INVALID_ARG: slider_handle is null or event is not supported
  138. */
  139. esp_err_t touch_slider_subscribe_event(touch_slider_handle_t slider_handle, uint32_t event_mask, void *arg);
  140. /**
  141. * @brief Touch slider set dispatch method
  142. *
  143. * This function sets a dispatch method that the driver core will use
  144. * this method as the event notification method.
  145. *
  146. * @param[in] slider_handle Slider handle
  147. * @param[in] dispatch_method Dispatch method (By callback/event)
  148. *
  149. * @return
  150. * - ESP_OK: Successfully set dispatch method
  151. * - ESP_ERR_INVALID_STATE: Touch slider driver was not initialized
  152. * - ESP_ERR_INVALID_ARG: slider_handle is null or dispatch_method is invalid
  153. */
  154. esp_err_t touch_slider_set_dispatch_method(touch_slider_handle_t slider_handle, touch_elem_dispatch_t dispatch_method);
  155. /**
  156. * @brief Touch slider set callback
  157. *
  158. * This function sets a callback routine into touch element driver core,
  159. * when the subscribed events occur, the callback routine will be called.
  160. *
  161. * @param[in] slider_handle Slider handle
  162. * @param[in] slider_callback User input callback
  163. *
  164. * @note Slider message will be passed from the callback function and it will be destroyed when the callback function return.
  165. *
  166. * @warning Since this input callback routine runs on driver core (esp-timer callback routine),
  167. * it should not do something that attempts to Block, such as calling vTaskDelay().
  168. *
  169. * @return
  170. * - ESP_OK: Successfully set callback
  171. * - ESP_ERR_INVALID_STATE: Touch slider driver was not initialized
  172. * - ESP_ERR_INVALID_ARG: slider_handle or slider_callback is null
  173. */
  174. esp_err_t touch_slider_set_callback(touch_slider_handle_t slider_handle, touch_slider_callback_t slider_callback);
  175. /**
  176. * @brief Touch slider get message
  177. *
  178. * This function decodes the element message from touch_element_message_receive() and return
  179. * a slider message pointer.
  180. *
  181. * @param[in] element_message element message
  182. *
  183. * @return Touch slider message pointer
  184. */
  185. const touch_slider_message_t* touch_slider_get_message(const touch_elem_message_t* element_message);
  186. #ifdef __cplusplus
  187. }
  188. #endif