touch_slider.h 7.9 KB

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