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