touch_element_private.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * SPDX-FileCopyrightText: 2016-2021 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. #include "touch_element/touch_button.h"
  9. #include "touch_element/touch_slider.h"
  10. #include "touch_element/touch_matrix.h"
  11. #include "esp_pm.h"
  12. #include "sdkconfig.h"
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. #define TE_TAG "Touch Element"
  17. #define TE_DEBUG_TAG "Touch Element Debug"
  18. #define TE_UNUSED(arg) (void)arg
  19. #define TE_CHECK(cond, ret_val) ({ \
  20. if (!(cond)) { \
  21. ESP_LOGE(TE_TAG, "%s(%d)", __FUNCTION__, __LINE__); \
  22. return (ret_val); \
  23. } \
  24. })
  25. #define TE_CHECK_GOTO(cond, label) ({ \
  26. if (!(cond)) { \
  27. goto label; \
  28. } \
  29. })
  30. #define TE_FREE_AND_NULL(ptr) ({ \
  31. free(ptr); \
  32. (ptr) = NULL; \
  33. })
  34. #define TE_DEFAULT_THRESHOLD_DIVIDER(obj) ((obj)->global_config->threshold_divider)
  35. #define TE_DEFAULT_LONGPRESS_TIME(obj) ((obj)->global_config->default_lp_time)
  36. typedef enum {
  37. TE_STATE_IDLE = 0,
  38. TE_STATE_PRESS,
  39. TE_STATE_RELEASE,
  40. } te_state_t;
  41. typedef te_state_t te_dev_state_t;
  42. typedef touch_elem_type_t te_dev_type_t;
  43. typedef struct {
  44. float sens; //!< Touch channel sensitivity
  45. touch_pad_t channel; //!< Touch channel number(index)
  46. te_dev_type_t type; //!< Touch channel type TODO: need to refactor as te_class_type_t
  47. te_dev_state_t state; //!< Touch channel current state
  48. bool is_use_last_threshold;
  49. } te_dev_t;
  50. typedef enum {
  51. TE_CLS_TYPE_BUTTON = 0,
  52. TE_CLS_TYPE_SLIDER,
  53. TE_CLS_TYPE_MATRIX,
  54. TE_CLS_TYPE_MAX //TODO: add waterproof class
  55. } te_class_type_t;
  56. typedef struct {
  57. touch_elem_handle_t handle;
  58. bool (*check_channel) (touch_pad_t);
  59. esp_err_t (*set_threshold) (void);
  60. void (*process_state) (void);
  61. void (*update_state) (touch_pad_t, te_state_t);
  62. } te_object_methods_t;
  63. /* -------------------------------------------- Waterproof basic type --------------------------------------------- */
  64. struct te_waterproof_s {
  65. te_dev_t *guard_device; //Waterproof guard channel device
  66. touch_elem_handle_t *mask_handle; //Waterproof masked handle array
  67. touch_pad_t shield_channel; //Waterproof shield channel
  68. bool is_shield_level_set; //Waterproof shield level setting bit
  69. };
  70. typedef struct te_waterproof_s* te_waterproof_handle_t;
  71. /* -------------------------------------------- Sleep basic type --------------------------------------------- */
  72. struct te_sleep_s {
  73. touch_elem_handle_t wakeup_handle;
  74. #ifdef CONFIG_PM_ENABLE
  75. esp_pm_lock_handle_t pm_lock;
  76. #endif
  77. uint32_t *non_volatile_threshold;
  78. };
  79. typedef struct te_sleep_s* te_sleep_handle_t;
  80. /* -------------------------------------------- Button basic type --------------------------------------------- */
  81. typedef struct {
  82. touch_elem_dispatch_t dispatch_method; //Button dispatch method
  83. touch_button_callback_t callback; //Button callback routine
  84. uint32_t event_mask; //Button subscribed event mask
  85. void *arg; //User input argument
  86. } te_button_handle_config_t;
  87. typedef te_state_t te_button_state_t; //TODO: add Long Press state
  88. struct te_button_s {
  89. te_button_handle_config_t *config; //Button configuration
  90. te_dev_t *device; //Base device information
  91. te_button_state_t current_state; //Button current state
  92. te_button_state_t last_state; //Button last state
  93. touch_button_event_t event; //Button outside state(for application layer)
  94. uint32_t trigger_cnt; //Button long time trigger counter
  95. uint32_t trigger_thr; //Button long time trigger counter threshold
  96. };
  97. typedef struct te_button_s* te_button_handle_t;
  98. /* -------------------------------------------- Slider basic type --------------------------------------------- */
  99. typedef struct {
  100. touch_elem_dispatch_t dispatch_method; //Slider dispatch method
  101. touch_slider_callback_t callback; //Slider callback routine
  102. uint32_t event_mask; //Slider subscribed event mask
  103. void *arg; //User input argument
  104. } te_slider_handle_config_t;
  105. typedef te_state_t te_slider_state_t;
  106. struct te_slider_s {
  107. te_slider_handle_config_t *config; //Slider configuration
  108. te_dev_t **device; //Base device information set
  109. te_slider_state_t current_state; //Slider current state
  110. te_slider_state_t last_state; //Slider last state
  111. touch_slider_event_t event; //Slider outside state(for application layer)
  112. float position_scale; //Slider position scale(step size)
  113. float *quantify_signal_array; //Slider re-quantization array
  114. uint32_t *channel_bcm; //Channel benchmark array
  115. uint32_t channel_bcm_update_cnt; //Channel benchmark update counter
  116. uint32_t filter_reset_cnt; //Slider reset counter
  117. uint32_t last_position; //Slider last position
  118. touch_slider_position_t position; //Slider position
  119. uint8_t position_range; //Slider position range([0, position_range])
  120. uint8_t channel_sum; //Slider channel sum
  121. uint8_t *pos_filter_window; //Slider position moving average filter window
  122. uint8_t pos_window_idx; //Slider position moving average filter window index
  123. bool is_first_sample; //Slider first time sample record bit
  124. };
  125. typedef struct te_slider_s* te_slider_handle_t;
  126. /* -------------------------------------------- Matrix basic type --------------------------------------------- */
  127. typedef struct {
  128. touch_elem_dispatch_t dispatch_method; //Matrix button dispatch method
  129. touch_matrix_callback_t callback; //Matrix button callback routine
  130. uint32_t event_mask; //Matrix button subscribed event mask
  131. void *arg; //User input argument
  132. } te_matrix_handle_config_t;
  133. typedef te_state_t te_matrix_state_t; //TODO: add Long Press state
  134. struct te_matrix_s {
  135. te_matrix_handle_config_t *config; //Matrix button configuration
  136. te_dev_t **device; //Base device information
  137. te_matrix_state_t current_state; //Matrix button current state
  138. te_matrix_state_t last_state; //Matrix button current state
  139. touch_matrix_event_t event; //Matrix button outside state(for application layer)
  140. uint32_t trigger_cnt; //Matrix button long time trigger counter
  141. uint32_t trigger_thr; //Matrix button long time trigger counter threshold
  142. touch_matrix_position_t position; //Matrix button position
  143. uint8_t x_channel_num; //The number of touch sensor channel in x axis
  144. uint8_t y_channel_num; //The number of touch sensor channel in y axis
  145. };
  146. typedef struct te_matrix_s* te_matrix_handle_t;
  147. /* ------------------------------------------------------------------------------------------------------------------ */
  148. /* --------------------------------------------- Global system methods ---------------------------------------------- */
  149. uint32_t te_read_smooth_signal(touch_pad_t channel_num);
  150. bool te_system_check_state(void);
  151. //TODO: Refactor this function with function overload
  152. esp_err_t te_dev_init(te_dev_t **device, uint8_t device_num, te_dev_type_t type, const touch_pad_t *channel, const float *sens, float divider);
  153. void te_dev_deinit(te_dev_t **device, uint8_t device_num);
  154. esp_err_t te_dev_set_threshold(te_dev_t *device);
  155. esp_err_t te_event_give(touch_elem_message_t te_message);
  156. uint8_t te_get_timer_period(void);
  157. void te_object_method_register(te_object_methods_t *object_methods, te_class_type_t object_type);
  158. void te_object_method_unregister(te_class_type_t object_type);
  159. bool te_object_check_channel(const touch_pad_t *channel_array, uint8_t channel_sum);
  160. bool waterproof_check_mask_handle(touch_elem_handle_t te_handle);
  161. bool te_is_touch_dsleep_wakeup(void);
  162. touch_pad_t te_get_sleep_channel(void);
  163. bool is_button_object_handle(touch_elem_handle_t element_handle);
  164. bool is_slider_object_handle(touch_elem_handle_t element_handle);
  165. bool is_matrix_object_handle(touch_elem_handle_t element_handle);
  166. void button_enable_wakeup_calibration(te_button_handle_t button_handle, bool en);
  167. void slider_enable_wakeup_calibration(te_slider_handle_t slider_handle, bool en);
  168. void matrix_enable_wakeup_calibration(te_matrix_handle_t matrix_handle, bool en);
  169. /* ------------------------------------------------------------------------------------------------------------------ */
  170. #ifdef __cplusplus
  171. }
  172. #endif