touch_element_private.h 9.1 KB

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