Skip to content

Commit 2ee7f0e

Browse files
committed
refactor(touch): refactor the filter configuration
1 parent a775317 commit 2ee7f0e

File tree

3 files changed

+19
-89
lines changed

3 files changed

+19
-89
lines changed

components/esp_driver_touch_sens/hw_ver3/include/driver/touch_version_types.h

Lines changed: 5 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,7 @@ extern "C" {
5555
.benchmark = { \
5656
.filter_mode = TOUCH_BM_IIR_FILTER_4, \
5757
.jitter_step = 4, \
58-
.update = { \
59-
.pos_thresh = TOUCH_UPDATE_BM_POS_THRESH_1_4, \
60-
.neg_thresh = TOUCH_UPDATE_BM_NEG_THRESH_1_4, \
61-
.neg_limit = 5, \
62-
}, \
58+
.denoise_lvl = 1, \
6359
}, \
6460
.data = { \
6561
.smooth_filter = TOUCH_SMOOTH_IIR_FILTER_2, \
@@ -115,35 +111,6 @@ typedef enum {
115111
TOUCH_BM_JITTER_FILTER, /*!< Jitter Filter for benchmark, raw value +/- jitter_step */
116112
} touch_benchmark_filter_mode_t;
117113

118-
/**
119-
* @brief Positive noise limitation of benchmark
120-
* benchmark will only update gradually when
121-
* the smooth data within the positive noise limitation
122-
*
123-
*/
124-
typedef enum {
125-
TOUCH_UPDATE_BM_POS_ALWAYS = -1, /*!< Always update benchmark when (smooth_data - benchmark) > 0 */
126-
TOUCH_UPDATE_BM_POS_THRESH_1_2 = 0, /*!< Only update benchmark when the (smooth_data - benchmark) < 1/2 * activate_threshold */
127-
TOUCH_UPDATE_BM_POS_THRESH_3_8, /*!< Only update benchmark when the (smooth_data - benchmark) < 3/8 * activate_threshold */
128-
TOUCH_UPDATE_BM_POS_THRESH_1_4, /*!< Only update benchmark when the (smooth_data - benchmark) < 1/4 * activate_threshold */
129-
TOUCH_UPDATE_BM_POS_THRESH_1, /*!< Only update benchmark when the (smooth_data - benchmark) < 1 * activate_threshold */
130-
} touch_benchmark_pos_thresh_t;
131-
132-
/**
133-
* @brief Negative noise limitation of benchmark
134-
* benchmark will only update gradually when
135-
* the smooth data within the negative noise limitation
136-
*
137-
*/
138-
typedef enum {
139-
TOUCH_UPDATE_BM_NEG_NEVER = -2, /*!< Never update benchmark when (benchmark - smooth_data) > 0 */
140-
TOUCH_UPDATE_BM_NEG_ALWAYS = -1, /*!< Always update benchmark when (benchmark - smooth_data) > 0 */
141-
TOUCH_UPDATE_BM_NEG_THRESH_1_2 = 0, /*!< Only update benchmark when the (benchmark - smooth_data) < 1/2 * activate_threshold */
142-
TOUCH_UPDATE_BM_NEG_THRESH_3_8, /*!< Only update benchmark when the (benchmark - smooth_data) < 3/8 * activate_threshold */
143-
TOUCH_UPDATE_BM_NEG_THRESH_1_4, /*!< Only update benchmark when the (benchmark - smooth_data) < 1/4 * activate_threshold */
144-
TOUCH_UPDATE_BM_NEG_THRESH_1, /*!< Only update benchmark when the (benchmark - smooth_data) < 1 * activate_threshold */
145-
} touch_benchmark_neg_thresh_t;
146-
147114
/**
148115
* @brief Touch channel Infinite Impulse Response (IIR) filter for smooth data
149116
*
@@ -222,31 +189,11 @@ typedef struct {
222189
touch_benchmark_filter_mode_t filter_mode; /*!< Benchmark filter mode. IIR filter and Jitter filter can be selected,
223190
* TOUCH_BM_IIR_FILTER_16 is recommended
224191
*/
225-
uint32_t jitter_step; /*!< Jitter filter step size, only takes effect when the `filter_mode` is TOUCH_BM_JITTER_FILTER. Range: 0 ~ 15 */
226-
/**
227-
* @brief Benchmark update strategy
228-
*/
229-
struct {
230-
touch_benchmark_pos_thresh_t pos_thresh; /*!< Select the positive noise threshold. Higher = More noise resistance.
231-
* Range: [-1 ~ 3]. The coefficient of the positive threshold are -1: always; 0: 4/8; 1: 3/8; 2: 2/8; 3: 1;
232-
* Once the data of the channel exceeded the positive threshold (i.e., benchmark + coefficient * touch threshold),
233-
* the benchmark will stop updated to that value.
234-
* -1: ignore the positive threshold and always update the benchmark for positive noise
235-
*/
236-
touch_benchmark_neg_thresh_t neg_thresh; /*!< Select the negative noise threshold. Higher = More noise resistance.
237-
* Range: [-2 ~ 3]. The coefficient of the negative threshold are -2: never; -1: always; 0: 4/8; 1: 3/8; 2: 2/8; 3: 1;
238-
* Once the data of the channel below the negative threshold (i.e., benchmark - coefficient * touch threshold),
239-
* the benchmark will stop updated to that value,
240-
* unless the data keep below the negative threshold for more than the limitation of `neg_limit`
241-
* -1: ignore the negative threshold and always update the benchmark for negative noise
242-
* -2: never update the benchmark for negative noise
243-
*/
244-
uint32_t neg_limit; /*!< Set the time limitation of the negative threshold.
245-
* The benchmark will be updated to the value that below to the negative threshold after the limited time.
246-
* Normally the negative update is used at the beginning, as the initial benchmark is a very large value.
247-
* (the unit of `neg_limit` is the tick of the slow clock source)
192+
uint32_t jitter_step; /*!< Jitter filter step size, only takes effect when the `filter_mode` is TOUCH_BM_JITTER_FILTER. Range: [0 ~ 15] */
193+
int denoise_lvl; /*!< The denoise level, which determines the noise bouncing range that won't trigger benchmark update.
194+
* Range: [0 ~ 4]. The greater the denoise_lvl is, more noise resistance will be. Specially, `0` stands for no denoise
195+
* Typically, recommend to set this field to 1.
248196
*/
249-
} update; /*!< The benchmark update strategy */
250197
} benchmark; /*!< Benchmark filter */
251198
/**
252199
* @brief Data configuration

components/esp_driver_touch_sens/hw_ver3/touch_version_specific.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,8 @@ esp_err_t touch_sensor_config_filter(touch_sensor_handle_t sens_handle, const to
282282
{
283283
TOUCH_NULL_POINTER_CHECK(sens_handle);
284284
if (filter_cfg) {
285-
ESP_RETURN_ON_FALSE(filter_cfg->benchmark.update.neg_limit >= filter_cfg->data.debounce_cnt,
286-
ESP_ERR_INVALID_ARG, TAG, "The neg_limit should be greater than debounce_cnt");
285+
ESP_RETURN_ON_FALSE(filter_cfg->benchmark.denoise_lvl >= 0 && filter_cfg->benchmark.denoise_lvl <= 4,
286+
ESP_ERR_INVALID_ARG, TAG, "denoise_lvl is out of range");
287287
}
288288

289289
esp_err_t ret = ESP_OK;
@@ -297,8 +297,7 @@ esp_err_t touch_sensor_config_filter(touch_sensor_handle_t sens_handle, const to
297297
if (filter_cfg->benchmark.filter_mode == TOUCH_BM_JITTER_FILTER) {
298298
touch_ll_filter_set_jitter_step(filter_cfg->benchmark.jitter_step);
299299
}
300-
touch_ll_filter_set_pos_noise_thresh(filter_cfg->benchmark.update.pos_thresh);
301-
touch_ll_filter_set_neg_noise_thresh(filter_cfg->benchmark.update.neg_thresh, filter_cfg->benchmark.update.neg_limit);
300+
touch_ll_filter_set_denoise_level(filter_cfg->benchmark.denoise_lvl);
302301
/* Configure the touch data filter */
303302
touch_ll_filter_set_smooth_mode(filter_cfg->data.smooth_filter);
304303
touch_ll_filter_set_active_hysteresis(filter_cfg->data.active_hysteresis);

components/hal/esp32p4/include/hal/touch_sensor_ll.h

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -695,39 +695,23 @@ static inline void touch_ll_filter_set_debounce(uint32_t dbc_cnt)
695695
}
696696

697697
/**
698-
* Set the positive noise threshold coefficient. Higher = More noise resistance.
699-
* The benchmark will update to the new value if the touch data is within (benchmark + active_threshold * pos_coeff)
698+
* Set the denoise coefficient regarding the denoise level.
700699
*
701-
*
702-
* @param pos_noise_thresh Range [-1 ~ 3]. The coefficient is -1: always; 0: 4/8; 1: 3/8; 2: 2/8; 3: 1;
703-
* -1: the benchmark will always update to the new touch data without considering the positive noise threshold
700+
* @param denoise_lvl Range [0 ~ 4]. 0 = no noise resistance, otherwise higher denoise_lvl means more noise resistance.
704701
*/
705-
static inline void touch_ll_filter_set_pos_noise_thresh(int pos_noise_thresh)
702+
static inline void touch_ll_filter_set_denoise_level(int denoise_lvl)
706703
{
707-
bool always_update = pos_noise_thresh < 0;
704+
HAL_ASSERT(denoise_lvl >= 0 && denoise_lvl <= 4);
705+
bool always_update = denoise_lvl == 0;
706+
// Map denoise level to actual noise threshold coefficients
707+
uint32_t noise_thresh = denoise_lvl == 4 ? 3 : 3 - denoise_lvl;
708+
708709
LP_ANA_PERI.touch_filter2.touch_bypass_noise_thres = always_update;
709-
LP_ANA_PERI.touch_filter1.touch_noise_thres = always_update ? 0 : pos_noise_thresh;
710-
}
710+
LP_ANA_PERI.touch_filter1.touch_noise_thres = always_update ? 0 : noise_thresh;
711711

712-
/**
713-
* Set the negative noise threshold coefficient. Higher = More noise resistance.
714-
* The benchmark will update to the new value if the touch data is greater than (benchmark - active_threshold * neg_coeff)
715-
*
716-
* @param neg_noise_thresh Range [-2 ~ 3]. The coefficient is -2: never; -1: always; 0: 4/8; 1: 3/8; 2: 2/8; 3: 1;
717-
* -1: the benchmark will always update to the new touch data without considering the negative noise threshold
718-
* -2: the benchmark will never update to the new touch data with negative growth
719-
* @param neg_noise_limit Only when neg_noise_thresh >= 0, if the touch data keep blow the negative threshold for mare than neg_noise_limit ticks,
720-
* the benchmark will still update to the new value.
721-
* It is normally used for updating the benchmark at the first scanning
722-
*/
723-
static inline void touch_ll_filter_set_neg_noise_thresh(int neg_noise_thresh, uint8_t neg_noise_limit)
724-
{
725-
bool always_update = neg_noise_thresh == -1;
726-
bool stop_update = neg_noise_thresh == -2;
727712
LP_ANA_PERI.touch_filter2.touch_bypass_nn_thres = always_update;
728-
LP_ANA_PERI.touch_filter1.touch_nn_disupdate_baseline_en = stop_update;
729-
LP_ANA_PERI.touch_filter1.touch_nn_thres = always_update || stop_update ? 0 : neg_noise_thresh;
730-
LP_ANA_PERI.touch_filter1.touch_nn_limit = always_update || stop_update ? 5 : neg_noise_limit; // 5 is the default value
713+
LP_ANA_PERI.touch_filter1.touch_nn_thres = always_update ? 0 : noise_thresh;
714+
LP_ANA_PERI.touch_filter1.touch_nn_limit = 5; // 5 is the default value
731715
}
732716

733717
/**

0 commit comments

Comments
 (0)