// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #ifndef _DRIVER_TOUCH_PAD_H_ #define _DRIVER_TOUCH_PAD_H_ #ifdef __cplusplus extern "C" { #endif #include "esp_err.h" #include "esp_intr_alloc.h" #include "soc/touch_periph.h" #if CONFIG_IDF_TARGET_ESP32 typedef enum { TOUCH_PAD_NUM0 = 0, /*!< Touch pad channel 0 is GPIO4 */ TOUCH_PAD_NUM1, /*!< Touch pad channel 1 is GPIO0 */ TOUCH_PAD_NUM2, /*!< Touch pad channel 2 is GPIO2 */ TOUCH_PAD_NUM3, /*!< Touch pad channel 3 is GPIO15*/ TOUCH_PAD_NUM4, /*!< Touch pad channel 4 is GPIO13*/ TOUCH_PAD_NUM5, /*!< Touch pad channel 5 is GPIO12*/ TOUCH_PAD_NUM6, /*!< Touch pad channel 6 is GPIO14*/ TOUCH_PAD_NUM7, /*!< Touch pad channel 7 is GPIO27*/ TOUCH_PAD_NUM8, /*!< Touch pad channel 8 is GPIO33*/ TOUCH_PAD_NUM9, /*!< Touch pad channel 9 is GPIO32*/ TOUCH_PAD_MAX, } touch_pad_t; #elif CONFIG_IDF_TARGET_ESP32S2BETA typedef enum { TOUCH_PAD_NUM0 = 0, /*!< Internal channel, be used for denoise */ #define TOUCH_DENOISE_CHANNEL TOUCH_PAD_NUM0 /*!< T0 is an internal channel that does not have a corresponding external GPIO. T0 will work simultaneously with the measured channel Tn. Finally, the actual measured value of Tn is the value after subtracting lower bits of T0. */ TOUCH_PAD_NUM1, /*!< Touch channel 1 is GPIO1 */ TOUCH_PAD_NUM2, /*!< Touch channel 2 is GPIO2 */ TOUCH_PAD_NUM3, /*!< Touch channel 3 is GPIO3 */ TOUCH_PAD_NUM4, /*!< Touch channel 4 is GPIO4 */ TOUCH_PAD_NUM5, /*!< Touch channel 5 is GPIO5 */ TOUCH_PAD_NUM6, /*!< Touch channel 6 is GPIO6 */ TOUCH_PAD_NUM7, /*!< Touch channel 7 is GPIO7 */ TOUCH_PAD_NUM8, /*!< Touch channel 8 is GPIO8 */ TOUCH_PAD_NUM9, /*!< Touch channel 9 is GPIO9 */ TOUCH_PAD_NUM10, /*!< Touch channel 9 is GPIO10 */ TOUCH_PAD_NUM11, /*!< Touch channel 9 is GPIO11 */ TOUCH_PAD_NUM12, /*!< Touch channel 9 is GPIO12 */ TOUCH_PAD_NUM13, /*!< Touch channel 9 is GPIO13 */ TOUCH_PAD_NUM14, /*!< Touch channel 9 is GPIO14 */ #define TOUCH_SHIELD_CHANNEL TOUCH_PAD_NUM14 /*!< The waterproof function includes a shielded channel (TOUCH_PAD_NUM14) The shielded channel outputs the same signal as the channel being measured. It is generally designed as a grid and is placed around the touch buttons. */ TOUCH_PAD_MAX, } touch_pad_t; #endif typedef enum { TOUCH_HVOLT_KEEP = -1, /*! (touch threshold + hysteresis), the touch channel be touched. If (raw data - baseline) < (touch threshold - hysteresis), the touch channel be released. Range: 0 ~ 3. The coefficient is 0: 1/8; 1: 3/32; 2: 1/16; 3: 1/32 */ uint8_t noise_thr; /*! (noise), the baseline stop updating. If (raw data - baseline) < (noise), the baseline start updating. Range: 0 ~ 3. The coefficient is 0: 1/2; 1: 3/8; 2: 1/4; 3: 1/8; */ uint8_t noise_neg_thr; /*! (- negative noise), the baseline start updating. If (raw data - baseline) < (- negative noise), the baseline stop updating. Range: 0 ~ 3. The coefficient is 0: 1/2; 1: 3/8; 2: 1/4; 3: 1/8; */ uint8_t neg_noise_limit; /*! BIT(14) * @return * - ESP_OK on success */ esp_err_t touch_pad_set_group_mask(uint16_t enable_mask); /** * @brief Get the touch sensor scan group bit mask. * @param enable_mask Pointer to bitmask of touch sensor scan group. * e.g. TOUCH_PAD_NUM14 -> BIT(14) * @return * - ESP_OK on success */ esp_err_t touch_pad_get_group_mask(uint16_t *enable_mask); /** * @brief Clear touch channel from touch sensor scan group. * The working mode of the touch sensor is cyclically scanned. * This function will clear the scan bits according to the given bitmask. * @note If clear all mask, the FSM timer should be stop firsty. * @param enable_mask bitmask of touch sensor scan group. * e.g. TOUCH_PAD_NUM14 -> BIT(14) * @return * - ESP_OK on success */ esp_err_t touch_pad_clear_group_mask(uint16_t enable_mask); /** * @brief Configure parameter for each touch channel. * @note Touch num 0 is denoise channel, please use `touch_pad_denoise_enable` to set denoise function * @param touch_num touch pad index * @return * - ESP_OK Success * - ESP_ERR_INVALID_ARG if argument wrong * - ESP_FAIL if touch pad not initialized */ esp_err_t touch_pad_config(touch_pad_t touch_num); /** * @brief Reset the whole of touch module. * @note Call this funtion after `touch_pad_fsm_stop`, * @return * - ESP_OK Success */ esp_err_t touch_pad_reset(void); /** * @brief Check touch sensor measurement status. * If doing measurement, the flag will be clear. * If finish measurement. the flag will be set. * @return * - TRUE finish measurement * - FALSE doing measurement */ bool touch_pad_meas_is_done(void); /** * @brief Get the current scan channel. * usually used in ISR to decide channel scaning, and then, get the current measurement value. * The role of each bit is reference to type `touch_pad_intr_mask_t`. * @return * - touch channel number */ touch_pad_t touch_pad_get_scan_curr(void); /** * @brief Get the touch sensor interrupt status mask. usually used in ISR to decide interrupt type. * The role of each bit is reference to type `touch_pad_intr_mask_t`. * @return * - touch intrrupt bit */ uint32_t touch_pad_intr_status_get_mask(void); /** * @brief Enable touch sensor interrupt. * @param type interrupt type * @return * - ESP_OK on success */ esp_err_t touch_pad_intr_enable(touch_pad_intr_mask_t int_mask); /** * @brief Disable touch sensor interrupt. * @param type interrupt type * @return * - ESP_OK on success */ esp_err_t touch_pad_intr_disable(touch_pad_intr_mask_t int_mask); /** * @brief Register touch-pad ISR. * The handler will be attached to the same CPU core that this function is running on. * @param fn Pointer to ISR handler * @param arg Parameter for ISR * @return * - ESP_OK Success ; * - ESP_ERR_INVALID_ARG GPIO error * - ESP_ERR_NO_MEM No memory */ esp_err_t touch_pad_isr_register(intr_handler_t fn, void* arg, touch_pad_intr_mask_t intr_mask); /** * @brief get raw data of touch sensor. * @note After the initialization is complete, the "raw_data" is max value. You need to wait for a measurement * cycle before you can read the correct touch value. * @param touch_num touch pad index * @param raw_data pointer to accept touch sensor value * @return * - ESP_OK Success * - ESP_FAIL Touch channel 0 havent this parameter. */ esp_err_t touch_pad_read_raw_data(touch_pad_t touch_num, uint32_t *raw_data); /** * @brief get baseline of touch sensor. * @note After the initialization is complete, the "touch_value" is max value. You need to wait for a measurement * cycle before you can read the correct touch value. * @param touch_num touch pad index * @param touch_value pointer to accept touch sensor value * @return * - ESP_OK Success * - ESP_ERR_INVALID_ARG Touch channel 0 havent this parameter. */ esp_err_t touch_pad_filter_baseline_read(touch_pad_t touch_num, uint32_t *basedata); /** * @brief Reset baseline to raw data of touch sensor. * @param touch_num touch pad index * - TOUCH_PAD_MAX Reset basaline of all channels * @return * - ESP_OK Success */ esp_err_t touch_pad_filter_baseline_reset(touch_pad_t touch_num); /** * @brief get debounce count of touch sensor. * @param touch_num touch pad index * @param debounce pointer to debounce value * @return * - ESP_OK Success * - ESP_ERR_INVALID_ARG Touch channel 0 havent this parameter. */ esp_err_t touch_pad_filter_debounce_read(touch_pad_t touch_num, uint32_t *debounce); /** * @brief set parameter of touch sensor filter and detection algorithm. * For more details on the detection algorithm, please refer to the application documentation. * @param filter_info select filter type and threshold of detection algorithm * @return * - ESP_OK Success */ esp_err_t touch_pad_filter_set_config(touch_filter_config_t *filter_info); /** * @brief get parameter of touch sensor filter and detection algorithm. * For more details on the detection algorithm, please refer to the application documentation. * @param filter_info select filter type and threshold of detection algorithm * @return * - ESP_OK Success */ esp_err_t touch_pad_filter_get_config(touch_filter_config_t *filter_info); /** * @brief enable touch sensor filter and detection algorithm. * For more details on the detection algorithm, please refer to the application documentation. * @return * - ESP_OK Success */ esp_err_t touch_pad_filter_enable(void); /** * @brief diaable touch sensor filter and detection algorithm. * For more details on the detection algorithm, please refer to the application documentation. * @return * - ESP_OK Success */ esp_err_t touch_pad_filter_disable(void); /** * @brief set parameter of denoise pad (TOUCH_PAD_NUM0). * T0 is an internal channel that does not have a corresponding external GPIO. * T0 will work simultaneously with the measured channel Tn. Finally, the actual * measured value of Tn is the value after subtracting lower bits of T0. * This denoise function filters out interference introduced on all channels, * such as noise introduced by the power supply and external EMI. * @param denoise parameter of denoise * @return * - ESP_OK Success */ esp_err_t touch_pad_denoise_set_config(touch_pad_denoise_t denoise); /** * @brief get parameter of denoise pad (TOUCH_PAD_NUM0). * @param denoise Pointer to parameter of denoise * @return * - ESP_OK Success */ esp_err_t touch_pad_denoise_get_config(touch_pad_denoise_t *denoise); /** * @brief enable denoise function. * T0 is an internal channel that does not have a corresponding external GPIO. * T0 will work simultaneously with the measured channel Tn. Finally, the actual * measured value of Tn is the value after subtracting lower bits of T0. * This denoise function filters out interference introduced on all channels, * such as noise introduced by the power supply and external EMI. * @return * - ESP_OK Success */ esp_err_t touch_pad_denoise_enable(void); /** * @brief disable denoise function. * @return * - ESP_OK Success */ esp_err_t touch_pad_denoise_disable(void); /** * @brief get denoise measure value (TOUCH_PAD_NUM0). * @param denoise value of denoise * @return * - ESP_OK Success */ esp_err_t touch_pad_denoise_data_get(uint32_t *data); /** * @brief set parameter of waterproof function. * The waterproof function includes a shielded channel (TOUCH_PAD_NUM14) and a guard channel. * The shielded channel outputs the same signal as the channel being measured. * It is generally designed as a grid and is placed around the touch buttons. * The shielded channel does not follow the measurement signal of the protection channel. * So that the guard channel can detect a large area of water. * @param waterproof parameter of waterproof * @return * - ESP_OK Success */ esp_err_t touch_pad_waterproof_set_config(touch_pad_waterproof_t waterproof); /** * @brief get parameter of waterproof function. * @param waterproof parameter of waterproof * @return * - ESP_OK Success */ esp_err_t touch_pad_waterproof_get_config(touch_pad_waterproof_t *waterproof); /** * @brief Enable parameter of waterproof function. * The waterproof function includes a shielded channel (TOUCH_PAD_NUM14) and a guard channel. * The shielded channel outputs the same signal as the channel being measured. * It is generally designed as a grid and is placed around the touch buttons. * The shielded channel does not follow the measurement signal of the protection channel. * So that the guard channel can detect a large area of water. * @return * - ESP_OK Success */ esp_err_t touch_pad_waterproof_enable(void); /** * @brief Enable parameter of waterproof function. * The waterproof function includes a shielded channel (TOUCH_PAD_NUM14) and a guard channel. * The shielded channel outputs the same signal as the channel being measured. * It is generally designed as a grid and is placed around the touch buttons. * The shielded channel does not follow the measurement signal of the protection channel. * So that the guard channel can detect a large area of water. * @return * - ESP_OK Success */ esp_err_t touch_pad_waterproof_disable(void); /** * @brief Set parameter of proximity channel. Three proximity sensing channels can be set. * The proximity sensor measurement is the accumulation of touch channel measurements. * @note If stop the proximity function for the channel, point this proximity channel to `TOUCH_PAD_NUM0`. * @param proximity parameter of proximity * @return * - ESP_OK Success */ esp_err_t touch_pad_proximity_set_config(touch_pad_proximity_t proximity); /** * @brief Get parameter of proximity channel. Three proximity sensing channels can be set. * The proximity sensor measurement is the accumulation of touch channel measurements. * @param proximity parameter of proximity * @return * - ESP_OK Success */ esp_err_t touch_pad_proximity_get_config(touch_pad_proximity_t *proximity); /** * @brief Get measure count of proximity channel. * The proximity sensor measurement is the accumulation of touch channel measurements. * @param touch_num touch pad index * @param proximity parameter of proximity * @return * - ESP_OK Success * - ESP_ERR_INVALID_ARG parameter is NULL */ esp_err_t touch_pad_proximity_get_meas_cnt(touch_pad_t touch_num, uint32_t *cnt); /** * @brief Get the accumulated measurement of the proximity sensor. * The proximity sensor measurement is the accumulation of touch channel measurements. * @param touch_num touch pad index * @param measure_out If the accumulation process does not end, the `measure_out` is the process value. * @return * - ESP_OK Success */ esp_err_t touch_pad_proximity_data_get(touch_pad_t touch_num, uint32_t *measure_out); /** * @brief Set parameter of touch sensor in sleep mode. * In order to achieve low power consumption in sleep mode, other circuits except the RTC part of the register are in a power-off state. * Only one touch channel is supported in the sleep state, which can be used as a wake-up function. * If in non-sleep mode, the sleep parameters do not work. * @param slp_config touch pad config * @return * - ESP_OK Success */ esp_err_t touch_pad_sleep_channel_config(touch_pad_sleep_channel_t slp_config); /** * @brief get baseline of touch sensor in sleep mode. * @param baseline pointer to accept touch sensor baseline value * @return * - ESP_OK Success * - ESP_ERR_INVALID_ARG parameter is NULL */ esp_err_t touch_pad_sleep_channel_baseline_get(uint32_t *baseline); /** * @brief get debounce of touch sensor in sleep mode. * @param debounce pointer to accept touch sensor debounce value * @return * - ESP_OK Success * - ESP_ERR_INVALID_ARG parameter is NULL */ esp_err_t touch_pad_sleep_channel_debounce_get(uint32_t *debounce); /** * @brief get proximity count of touch sensor in sleep mode. * @param proximity_cnt pointer to accept touch sensor proximity count value * @return * - ESP_OK Success * - ESP_ERR_INVALID_ARG parameter is NULL */ esp_err_t touch_pad_sleep_channel_proximity_cnt_get(uint32_t *proximity_cnt); #endif // CONFIG_IDF_TARGET_ESP32S2BETA #ifdef __cplusplus } #endif #endif/*_DRIVER_TOUCH_PAD_H_*/