touch_element: add new touch library component

This commit is contained in:
Kang Zuoling
2020-12-30 11:57:59 +08:00
parent 9e059689f2
commit d16861001f
10 changed files with 3699 additions and 0 deletions
@@ -0,0 +1,203 @@
// Copyright 2016-2020 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.
#pragma once
#include "touch_element/touch_element.h"
#ifdef __cplusplus
extern "C" {
#endif
/* --------------------------------- General button instance default configuration --------------------------------- */
#define TOUCH_BUTTON_GLOBAL_DEFAULT_CONFIG() \
{ \
.threshold_divider = 0.8, \
.default_lp_time = 1000 \
}
/* ------------------------------------------------------------------------------------------------------------------ */
/**
* @brief Button initialization configuration passed to touch_button_install
*/
typedef struct {
float threshold_divider; //!< Button channel threshold divider
uint32_t default_lp_time; //!< Button default LongPress event time (ms)
} touch_button_global_config_t;
/**
* @brief Button configuration (for new instance) passed to touch_button_create()
*/
typedef struct {
touch_pad_t channel_num; //!< Button channel number (index)
float channel_sens; //!< Button channel sensitivity
} touch_button_config_t;
/**
* @brief Button event type
*/
typedef enum {
TOUCH_BUTTON_EVT_ON_PRESS, //!< Button Press event
TOUCH_BUTTON_EVT_ON_RELEASE, //!< Button Release event
TOUCH_BUTTON_EVT_ON_LONGPRESS, //!< Button LongPress event
TOUCH_BUTTON_EVT_MAX
} touch_button_event_t;
/**
* @brief Button message type
*/
typedef struct {
touch_button_event_t event; //!< Button event
} touch_button_message_t;
typedef touch_elem_handle_t touch_button_handle_t; //!< Button handle
typedef void(*touch_button_callback_t)(touch_button_handle_t, touch_button_message_t, void *); //!< Button callback type
/**
* @brief Touch Button initialize
*
* This function initializes touch button global and acts on all
* touch button instances.
*
* @param[in] global_config Button object initialization configuration
*
* @return
* - ESP_OK: Successfully initialized touch button
* - ESP_ERR_INVALID_STATE: Touch element library was not initialized
* - ESP_ERR_INVALID_ARG: button_init is NULL
* - ESP_ERR_NO_MEM: Insufficient memory
*/
esp_err_t touch_button_install(const touch_button_global_config_t *global_config);
/**
* @brief Release resources allocated using touch_button_install()
*/
void touch_button_uninstall(void);
/**
* @brief Create a new touch button instance
*
* @param[in] button_config Button configuration
* @param[out] button_handle Button handle
*
* @note The sensitivity has to be explored in experiments,
* Sensitivity = (Raw(touch) - Raw(release)) / Raw(release) * 100%
*
* @return
* - ESP_OK: Successfully create touch button
* - ESP_ERR_INVALID_STATE: Touch button driver was not initialized
* - ESP_ERR_NO_MEM: Insufficient memory
* - ESP_ERR_INVALID_ARG: Invalid configuration struct or arguments is NULL
*/
esp_err_t touch_button_create(const touch_button_config_t *button_config, touch_button_handle_t *button_handle);
/**
* @brief Release resources allocated using touch_button_create()
*
* @param[in] button_handle Button handle
* @return
* - ESP_OK: Successfully released resources
* - ESP_ERR_INVALID_STATE: Touch button driver was not initialized
* - ESP_ERR_INVALID_ARG: button_handle is null
* - ESP_ERR_NOT_FOUND: Input handle is not a button handle
*/
esp_err_t touch_button_delete(touch_button_handle_t button_handle);
/**
* @brief Touch button subscribes event
*
* This function uses event mask to subscribe to touch button events, once one of
* the subscribed events occurs, the event message could be retrieved by calling
* touch_element_message_receive() or input callback routine.
*
* @param[in] button_handle Button handle
* @param[in] event_mask Button subscription event mask
* @param[in] arg User input argument
*
* @note Touch button only support three kind of event masks, they are
* TOUCH_ELEM_EVENT_ON_PRESS, TOUCH_ELEM_EVENT_ON_RELEASE, TOUCH_ELEM_EVENT_ON_LONGPRESS.
* You can use those event masks in any combination to achieve the desired effect.
*
* @return
* - ESP_OK: Successfully subscribed touch button event
* - ESP_ERR_INVALID_STATE: Touch button driver was not initialized
* - ESP_ERR_INVALID_ARG: button_handle is null or event is not supported
*/
esp_err_t touch_button_subscribe_event(touch_button_handle_t button_handle, uint32_t event_mask, void *arg);
/**
* @brief Touch button set dispatch method
*
* This function sets a dispatch method that the driver core will use
* this method as the event notification method.
*
* @param[in] button_handle Button handle
* @param[in] dispatch_method Dispatch method (By callback/event)
*
* @return
* - ESP_OK: Successfully set dispatch method
* - ESP_ERR_INVALID_STATE: Touch button driver was not initialized
* - ESP_ERR_INVALID_ARG: button_handle is null or dispatch_method is invalid
*/
esp_err_t touch_button_set_dispatch_method(touch_button_handle_t button_handle, touch_elem_dispatch_t dispatch_method);
/**
* @brief Touch button set callback
*
* This function sets a callback routine into touch element driver core,
* when the subscribed events occur, the callback routine will be called.
*
* @param[in] button_handle Button handle
* @param[in] button_callback User input callback
*
* @warning Since this input callback routine runs on driver core (esp-timer callback routine),
* it should not do something that attempts to Block, such as calling vTaskDelay().
*
* @return
* - ESP_OK: Successfully set callback
* - ESP_ERR_INVALID_STATE: Touch button driver was not initialized
* - ESP_ERR_INVALID_ARG: button_handle or button_callback is null
*/
esp_err_t touch_button_set_callback(touch_button_handle_t button_handle, touch_button_callback_t button_callback);
/**
* @brief Touch button set long press trigger time
*
* This function sets the threshold time (ms) for a long press event. If a button is pressed
* and held for a period of time that exceeds the threshold time, a long press event is triggered.
*
* @param[in] button_handle Button handle
* @param[in] threshold_time Threshold time (ms) of long press event occur
*
* @return
* - ESP_OK: Successfully set the threshold time of long press event
* - ESP_ERR_INVALID_STATE: Touch button driver was not initialized
* - ESP_ERR_INVALID_ARG: button_handle is null or time (ms) is not lager than 0
*/
esp_err_t touch_button_set_longpress(touch_button_handle_t button_handle, uint32_t threshold_time);
/**
* @brief Touch button get message
*
* This function decodes the element message from touch_element_message_receive() and return
* a button message pointer.
*
* @param[in] element_message element message
*
* @return Touch button message pointer
*/
const touch_button_message_t* touch_button_get_message(const touch_elem_message_t* element_message);
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,273 @@
// Copyright 2016-2020 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.
#pragma once
#include "driver/touch_sensor.h"
#ifdef __cplusplus
extern "C" {
#endif
/* -------------------------------- General hardware & system default configuration -------------------------------- */
/* Since those are important hardware and algorithm parameters, user should not change them before knowing all details*/
/* ------------------------------------------------------------------------------------------------------------------ */
#define TOUCH_ELEM_GLOBAL_DEFAULT_CONFIG() \
{ \
.hardware = { \
.upper_voltage = TOUCH_HVOLT_2V7, \
.voltage_attenuation = TOUCH_HVOLT_ATTEN_0V5, \
.lower_voltage = TOUCH_LVOLT_0V5, \
.suspend_channel_polarity = TOUCH_PAD_CONN_HIGHZ, \
.denoise_level = TOUCH_PAD_DENOISE_BIT4, \
.denoise_equivalent_cap = TOUCH_PAD_DENOISE_CAP_L0, \
.smooth_filter_mode = TOUCH_PAD_SMOOTH_IIR_2, \
.benchmark_filter_mode = TOUCH_PAD_FILTER_IIR_16, \
.sample_count = 500, \
.sleep_cycle = 0xf, \
.benchmark_debounce_count = 2, \
.benchmark_calibration_threshold = 2, \
.benchmark_jitter_step = 5 \
}, \
.software = { \
.waterproof_threshold_divider = 0.8, \
.processing_period = 10, \
.intr_message_size = 14, \
.event_message_size = 20 \
} \
}
/* ------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------- Event subscription ----------------------------------------------- */
#define TOUCH_ELEM_EVENT_NONE BIT(0) //!< None event
#define TOUCH_ELEM_EVENT_ON_PRESS BIT(1) //!< On Press event
#define TOUCH_ELEM_EVENT_ON_RELEASE BIT(2) //!< On Release event
#define TOUCH_ELEM_EVENT_ON_LONGPRESS BIT(3) //!< On LongPress event
#define TOUCH_ELEM_EVENT_ON_CALCULATION BIT(4) //!< On Calculation event
/* ------------------------------------------------------------------------------------------------------------------ */
#define TOUCH_WATERPROOF_GUARD_NOUSE (0) //!< Waterproof no use guard sensor
/* -------------------------------- Global hardware & software configuration struct --------------------------------- */
/**
* @brief Touch element software configuration
*/
typedef struct {
float waterproof_threshold_divider; //!< Waterproof guard channel threshold divider
uint8_t processing_period; //!< Processing period(ms)
uint8_t intr_message_size; //!< Interrupt message queue size
uint8_t event_message_size; //!< Event message queue size
} touch_elem_sw_config_t;
/**
* @brief Touch element hardware configuration
*/
typedef struct {
touch_high_volt_t upper_voltage; //!< Touch sensor channel upper charge voltage
touch_volt_atten_t voltage_attenuation; //!< Touch sensor channel upper charge voltage attenuation (Diff voltage is upper - attenuation - lower)
touch_low_volt_t lower_voltage; //!< Touch sensor channel lower charge voltage
touch_pad_conn_type_t suspend_channel_polarity; //!< Suspend channel polarity (High Impedance State or GND)
touch_pad_denoise_grade_t denoise_level; //!< Internal de-noise level
touch_pad_denoise_cap_t denoise_equivalent_cap; //!< Internal de-noise channel (Touch channel 0) equivalent capacitance
touch_smooth_mode_t smooth_filter_mode; //!< Smooth value filter mode (This only apply to touch_pad_filter_read_smooth())
touch_filter_mode_t benchmark_filter_mode; //!< Benchmark filter mode
uint16_t sample_count; //!< The count of sample in each measurement of touch sensor
uint16_t sleep_cycle; //!< The cycle (RTC slow clock) of sleep
uint8_t benchmark_debounce_count; //!< Benchmark debounce count
uint8_t benchmark_calibration_threshold; //!< Benchmark calibration threshold
uint8_t benchmark_jitter_step; //!< Benchmark jitter filter step (This only works at while benchmark filter mode is jitter filter)
} touch_elem_hw_config_t;
/**
* @brief Touch element global configuration passed to touch_element_install
*/
typedef struct {
touch_elem_hw_config_t hardware; //!< Hardware configuration
touch_elem_sw_config_t software; //!< Software configuration
} touch_elem_global_config_t;
/**
* @brief Touch element waterproof configuration passed to touch_element_waterproof_install
*/
typedef struct {
touch_pad_t guard_channel; //!< Waterproof Guard-Sensor channel number (index)
float guard_sensitivity; //!< Waterproof Guard-Sensor sensitivity
} touch_elem_waterproof_config_t;
/* ------------------------------------------------------------------------------------------------------------------ */
typedef void *touch_elem_handle_t; //!< Touch element handle type
typedef uint32_t touch_elem_event_t; //!< Touch element event type
/**
* @brief Touch element handle type
*/
typedef enum {
TOUCH_ELEM_TYPE_BUTTON, //!< Touch element button
TOUCH_ELEM_TYPE_SLIDER, //!< Touch element slider
TOUCH_ELEM_TYPE_MATRIX, //!< Touch element matrix button
} touch_elem_type_t;
/**
* @brief Touch element event dispatch methods (event queue/callback)
*/
typedef enum {
TOUCH_ELEM_DISP_EVENT, //!< Event queue dispatch
TOUCH_ELEM_DISP_CALLBACK, //!< Callback dispatch
TOUCH_ELEM_DISP_MAX
} touch_elem_dispatch_t;
/**
* @brief Touch element event message type from touch_element_message_receive()
*/
typedef struct {
touch_elem_handle_t handle; //!< Touch element handle
touch_elem_type_t element_type; //!< Touch element type
void *arg; //!< User input argument
uint8_t child_msg[8]; //!< Encoded message
} touch_elem_message_t;
/* ------------------------------------------------------------------------------------------------------------------ */
/**
* @brief Touch element processing initialization
*
* @param[in] global_config Global initialization configuration structure
*
* @note To reinitialize the touch element object, call touch_element_uninstall() first
*
* @return
* - ESP_OK: Successfully initialized
* - ESP_ERR_INVALID_ARG: Invalid argument
* - ESP_ERR_NO_MEM: Insufficient memory
* - ESP_ERR_INVALID_STATE: Touch element is already initialized
* - Others: Unknown touch driver layer or lower layer error
*/
esp_err_t touch_element_install(const touch_elem_global_config_t *global_config);
/**
* @brief Touch element processing start
*
* This function starts the touch element processing system
*
* @note This function must only be called after all the touch element instances finished creating
*
* @return
* - ESP_OK: Successfully started to process
* - Others: Unknown touch driver layer or lower layer error
*/
esp_err_t touch_element_start(void);
/**
* @brief Touch element processing stop
*
* This function stops the touch element processing system
*
* @note This function must be called before changing the system (hardware, software) parameters
*
* @return
* - ESP_OK: Successfully stopped to process
* - Others: Unknown touch driver layer or lower layer error
*/
esp_err_t touch_element_stop(void);
/**
* @brief Release resources allocated using touch_element_install
*
* @return
* - ESP_OK: Successfully released touch element object
* - ESP_ERR_INVALID_STATE: Touch element object is not initialized
* - Others: Unknown touch driver layer or lower layer error
*/
void touch_element_uninstall(void);
/**
* @brief Get current event message of touch element instance
*
* This function will receive the touch element message (handle, event type, etc...)
* from te_event_give(). It will block until a touch element event or a timeout occurs.
*
* @param[out] element_message Touch element event message structure
* @param[in] ticks_to_wait Number of FreeRTOS ticks to block for waiting event
* @return
* - ESP_OK: Successfully received touch element event
* - ESP_ERR_INVALID_STATE: Touch element library is not initialized
* - ESP_ERR_INVALID_ARG: element_message is null
* - ESP_ERR_TIMEOUT: Timed out waiting for event
*/
esp_err_t touch_element_message_receive(touch_elem_message_t *element_message, uint32_t ticks_to_wait);
/**
* @brief Touch element waterproof initialization
*
* This function enables the hardware waterproof, then touch element system uses Shield-Sensor
* and Guard-Sensor to mitigate the influence of water-drop and water-stream.
*
* @param[in] waterproof_config Waterproof configuration
*
* @note If the waterproof function is used, Shield-Sensor can not be disabled and it will use channel 14 as
* it's internal channel. Hence, the user can not use channel 14 for another propose. And the Guard-Sensor
* is not necessary since it is optional.
*
* @note Shield-Sensor: It always uses channel 14 as the shield channel, so user must connect
* the channel 14 and Shield-Layer in PCB since it will generate a synchronous signal automatically
*
* @note Guard-Sensor: This function is optional. If used, the user must connect the guard channel and Guard-Ring
* in PCB. Any channels user wants to protect should be added into Guard-Ring in PCB.
*
* @return
* - ESP_OK: Successfully initialized
* - ESP_ERR_INVALID_STATE: Touch element library is not initialized
* - ESP_ERR_INVALID_ARG: waterproof_config is null or invalid Guard-Sensor channel
* - ESP_ERR_NO_MEM: Insufficient memory
*/
esp_err_t touch_element_waterproof_install(const touch_elem_waterproof_config_t *waterproof_config);
/**
* @brief Release resources allocated using touch_element_waterproof_install()
*/
void touch_element_waterproof_uninstall(void);
/**
* @brief Add a masked handle to protect while Guard-Sensor has been triggered
*
* This function will add an application handle (button, slider, etc...) as a masked handle. While Guard-Sensor
* has been triggered, waterproof function will start working and lock the application internal state. While the
* influence of water is reduced, the application will be unlock and reset into IDLE state.
*
* @param[in] element_handle Touch element instance handle
*
* @note The waterproof protection logic must follow the real circuit in PCB, it means that all of the channels
* inside the input handle must be inside the Guard-Ring in real circuit.
*
* @return
* - ESP_OK: Successfully added a masked handle
* - ESP_ERR_INVALID_STATE: Waterproof is not initialized
* - ESP_ERR_INVALID_ARG: element_handle is null
*/
esp_err_t touch_element_waterproof_add(touch_elem_handle_t element_handle);
/**
* @brief Remove a masked handle to protect
*
* This function will remove an application handle from masked handle table.
*
* @param[in] element_handle Touch element instance handle
*
* @return
* - ESP_OK: Successfully removed a masked handle
* - ESP_ERR_INVALID_STATE: Waterproof is not initialized
* - ESP_ERR_INVALID_ARG: element_handle is null
* - ESP_ERR_NOT_FOUND: Failed to search element_handle from waterproof mask_handle list
*/
esp_err_t touch_element_waterproof_remove(touch_elem_handle_t element_handle);
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,185 @@
// Copyright 2016-2020 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.
#pragma once
#include "touch_element/touch_element.h"
#include "touch_element/touch_button.h"
#include "touch_element/touch_slider.h"
#include "touch_element/touch_matrix.h"
#ifdef __cplusplus
extern "C" {
#endif
#define TE_TAG "Touch Element"
#define TE_DEBUG_TAG "Touch Element Debug"
#define TE_UNUSED(arg) (void)arg
#define TE_CHECK(cond, ret_val) ({ \
if (!(cond)) { \
ESP_LOGE(TE_TAG, "%s:%d (%s)", __FILE__, __LINE__, __FUNCTION__); \
return (ret_val); \
} \
})
#define TE_CHECK_GOTO(cond, label) ({ \
if (!(cond)) { \
goto label; \
} \
})
#define TE_FREE_AND_NULL(ptr) ({ \
free(ptr); \
(ptr) = NULL; \
})
#define TE_DEFAULT_THRESHOLD_DIVIDER(obj) ((obj)->global_config->threshold_divider)
#define TE_DEFAULT_LONGPRESS_TIME(obj) ((obj)->global_config->default_lp_time)
typedef enum {
TE_STATE_IDLE = 0,
TE_STATE_PRESS,
TE_STATE_RELEASE,
} te_state_t;
typedef te_state_t te_dev_state_t;
typedef touch_elem_type_t te_dev_type_t;
typedef struct {
float sens; //!< Touch channel sensitivity
touch_pad_t channel; //!< Touch channel number(index)
te_dev_type_t type; //!< Touch channel type TODO: need to refactor as te_class_type_t
te_dev_state_t state; //!< Touch channel current state
} te_dev_t;
typedef enum {
TE_CLS_TYPE_BUTTON = 0,
TE_CLS_TYPE_SLIDER,
TE_CLS_TYPE_MATRIX,
TE_CLS_TYPE_MAX //TODO: add waterproof class
} te_class_type_t;
typedef struct {
touch_elem_handle_t handle;
bool (*check_channel) (touch_pad_t);
esp_err_t (*set_threshold) (void);
void (*process_state) (void);
void (*update_state) (touch_pad_t, te_state_t);
} te_object_methods_t;
/* -------------------------------------------- Waterproof basic type --------------------------------------------- */
struct te_waterproof_s {
te_dev_t *guard_device; //Waterproof guard channel device
touch_elem_handle_t *mask_handle; //Waterproof masked handle array
touch_pad_t shield_channel; //Waterproof shield channel
bool is_shield_level_set; //Waterproof shield level setting bit
};
typedef struct te_waterproof_s* te_waterproof_handle_t;
/* -------------------------------------------- Button basic type --------------------------------------------- */
typedef struct {
touch_elem_dispatch_t dispatch_method; //Button dispatch method
touch_button_callback_t callback; //Button callback routine
uint32_t event_mask; //Button subscribed event mask
void *arg; //User input argument
} te_button_handle_config_t;
typedef te_state_t te_button_state_t; //TODO: add Long Press state
struct te_button_s {
te_button_handle_config_t *config; //Button configuration
te_dev_t *device; //Base device information
te_button_state_t current_state; //Button current state
te_button_state_t last_state; //Button last state
touch_button_event_t event; //Button outside state(for application layer)
uint32_t trigger_cnt; //Button long time trigger counter
uint32_t trigger_thr; //Button long time trigger counter threshold
};
typedef struct te_button_s* te_button_handle_t;
/* -------------------------------------------- Slider basic type --------------------------------------------- */
typedef struct {
touch_elem_dispatch_t dispatch_method; //Slider dispatch method
touch_slider_callback_t callback; //Slider callback routine
uint32_t event_mask; //Slider subscribed event mask
void *arg; //User input argument
} te_slider_handle_config_t;
typedef te_state_t te_slider_state_t;
struct te_slider_s {
te_slider_handle_config_t *config; //Slider configuration
te_dev_t **device; //Base device information set
te_slider_state_t current_state; //Slider current state
te_slider_state_t last_state; //Slider last state
touch_slider_event_t event; //Slider outside state(for application layer)
float position_scale; //Slider position scale(step size)
float *quantify_signal_array; //Slider re-quantization array
uint32_t *channel_bcm; //Channel benchmark array
uint32_t channel_bcm_update_cnt; //Channel benchmark update counter
uint32_t filter_reset_cnt; //Slider reset counter
uint32_t last_position; //Slider last position
touch_slider_position_t position; //Slider position
uint8_t position_range; //Slider position range([0, position_range])
uint8_t channel_sum; //Slider channel sum
uint8_t *pos_filter_window; //Slider position moving average filter window
uint8_t pos_window_idx; //Slider position moving average filter window index
bool is_first_sample; //Slider first time sample record bit
};
typedef struct te_slider_s* te_slider_handle_t;
/* -------------------------------------------- Matrix basic type --------------------------------------------- */
typedef struct {
touch_elem_dispatch_t dispatch_method; //Matrix button dispatch method
touch_matrix_callback_t callback; //Matrix button callback routine
uint32_t event_mask; //Matrix button subscribed event mask
void *arg; //User input argument
} te_matrix_handle_config_t;
typedef te_state_t te_matrix_state_t; //TODO: add Long Press state
struct te_matrix_s {
te_matrix_handle_config_t *config; //Matrix button configuration
te_dev_t **device; //Base device information
te_matrix_state_t current_state; //Matrix button current state
te_matrix_state_t last_state; //Matrix button current state
touch_matrix_event_t event; //Matrix button outside state(for application layer)
uint32_t trigger_cnt; //Matrix button long time trigger counter
uint32_t trigger_thr; //Matrix button long time trigger counter threshold
touch_matrix_position_t position; //Matrix button position
uint8_t x_channel_num; //The number of touch sensor channel in x axis
uint8_t y_channel_num; //The number of touch sensor channel in y axis
};
typedef struct te_matrix_s* te_matrix_handle_t;
/* ------------------------------------------------------------------------------------------------------------------ */
/* --------------------------------------------- Global system methods ---------------------------------------------- */
uint32_t te_read_smooth_signal(touch_pad_t channel_num);
bool te_system_check_state(void);
//TODO: Refactor this function with function overload
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);
void te_dev_deinit(te_dev_t **device, uint8_t device_num);
esp_err_t te_dev_set_threshold(te_dev_t *device);
esp_err_t te_event_give(touch_elem_message_t te_message);
uint8_t te_get_timer_period(void);
void te_object_method_register(te_object_methods_t *object_methods, te_class_type_t object_type);
void te_object_method_unregister(te_class_type_t object_type);
bool te_object_check_channel(const touch_pad_t *channel_array, uint8_t channel_sum);
bool waterproof_check_mask_handle(touch_elem_handle_t te_handle);
/* ------------------------------------------------------------------------------------------------------------------ */
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,221 @@
// Copyright 2016-2020 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.
#pragma once
#include "touch_element/touch_element.h"
#ifdef __cplusplus
extern "C" {
#endif
/* ----------------------------- General matrix button instance default configuration ------------------------------ */
#define TOUCH_MATRIX_GLOBAL_DEFAULT_CONFIG() \
{ \
.threshold_divider = 0.8, \
.default_lp_time = 1000 \
}
/* ------------------------------------------------------------------------------------------------------------------ */
/**
* @brief Matrix button initialization configuration passed to touch_matrix_install
*/
typedef struct {
float threshold_divider; //!< Matrix button channel threshold divider
uint32_t default_lp_time; //!< Matrix button default LongPress event time (ms)
} touch_matrix_global_config_t;
/**
* @brief Matrix button configuration (for new instance) passed to touch_matrix_create()
*/
typedef struct {
const touch_pad_t *x_channel_array; //!< Matrix button x-axis channels array
const touch_pad_t *y_channel_array; //!< Matrix button y-axis channels array
const float *x_sensitivity_array; //!< Matrix button x-axis channels sensitivity array
const float *y_sensitivity_array; //!< Matrix button y-axis channels sensitivity array
uint8_t x_channel_num; //!< The number of channels in x-axis
uint8_t y_channel_num; //!< The number of channels in y-axis
} touch_matrix_config_t;
/**
* @brief Matrix button event type
*/
typedef enum {
TOUCH_MATRIX_EVT_ON_PRESS, //!< Matrix button Press event
TOUCH_MATRIX_EVT_ON_RELEASE, //!< Matrix button Press event
TOUCH_MATRIX_EVT_ON_LONGPRESS, //!< Matrix button LongPress event
TOUCH_MATRIX_EVT_MAX
} touch_matrix_event_t;
/**
* @brief Matrix button position data type
*/
typedef struct {
uint8_t x_axis; //!< Matrix button x axis position
uint8_t y_axis; //!< Matrix button y axis position
uint8_t index; //!< Matrix button position index
} touch_matrix_position_t;
/**
* @brief Matrix message type
*/
typedef struct {
touch_matrix_event_t event; //!< Matrix event
touch_matrix_position_t position; //!< Matrix position
} touch_matrix_message_t;
typedef touch_elem_handle_t touch_matrix_handle_t; //!< Matrix button instance handle
typedef void(*touch_matrix_callback_t)(touch_matrix_handle_t, touch_matrix_message_t, void *); //!< Matrix button callback type
/**
* @brief Touch matrix button initialize
*
* This function initializes touch matrix button object and acts on all
* touch matrix button instances.
*
* @param[in] global_config Touch matrix global initialization configuration
*
* @return
* - ESP_OK: Successfully initialized touch matrix button
* - ESP_ERR_INVALID_STATE: Touch element library was not initialized
* - ESP_ERR_INVALID_ARG: matrix_init is NULL
* - ESP_ERR_NO_MEM: Insufficient memory
*/
esp_err_t touch_matrix_install(const touch_matrix_global_config_t *global_config);
/**
* @brief Release resources allocated using touch_matrix_install()
*
* @return
* - ESP_OK: Successfully released resources
*/
void touch_matrix_uninstall(void);
/**
* @brief Create a new touch matrix button instance
*
* @param[in] matrix_config Matrix button configuration
* @param[out] matrix_handle Matrix button handle
*
* @note Channel array and sensitivity array must be one-one correspondence in those array
*
* @note Touch matrix button does not support Multi-Touch now
*
* @return
* - ESP_OK: Successfully create touch matrix button
* - ESP_ERR_INVALID_STATE: Touch matrix driver was not initialized
* - ESP_ERR_INVALID_ARG: Invalid configuration struct or arguments is NULL
* - ESP_ERR_NO_MEM: Insufficient memory
*/
esp_err_t touch_matrix_create(const touch_matrix_config_t *matrix_config, touch_matrix_handle_t *matrix_handle);
/**
* @brief Release resources allocated using touch_matrix_create()
*
* @param[in] matrix_handle Matrix handle
* @return
* - ESP_OK: Successfully released resources
* - ESP_ERR_INVALID_STATE: Touch matrix driver was not initialized
* - ESP_ERR_INVALID_ARG: matrix_handle is null
* - ESP_ERR_NOT_FOUND: Input handle is not a matrix handle
*/
esp_err_t touch_matrix_delete(touch_matrix_handle_t matrix_handle);
/**
* @brief Touch matrix button subscribes event
*
* This function uses event mask to subscribe to touch matrix events, once one of
* the subscribed events occurs, the event message could be retrieved by calling
* touch_element_message_receive() or input callback routine.
*
* @param[in] matrix_handle Matrix handle
* @param[in] event_mask Matrix subscription event mask
* @param[in] arg User input argument
*
* @note Touch matrix button only support three kind of event masks, they are
* TOUCH_ELEM_EVENT_ON_PRESS, TOUCH_ELEM_EVENT_ON_RELEASE, TOUCH_ELEM_EVENT_ON_LONGPRESS. You can use those event
* masks in any combination to achieve the desired effect.
*
* @return
* - ESP_OK: Successfully subscribed touch matrix event
* - ESP_ERR_INVALID_STATE: Touch matrix driver was not initialized
* - ESP_ERR_INVALID_ARG: matrix_handle is null or event is not supported
*/
esp_err_t touch_matrix_subscribe_event(touch_matrix_handle_t matrix_handle, uint32_t event_mask, void *arg);
/**
* @brief Touch matrix button set dispatch method
*
* This function sets a dispatch method that the driver core will use
* this method as the event notification method.
*
* @param[in] matrix_handle Matrix button handle
* @param[in] dispatch_method Dispatch method (By callback/event)
*
* @return
* - ESP_OK: Successfully set dispatch method
* - ESP_ERR_INVALID_STATE: Touch matrix driver was not initialized
* - ESP_ERR_INVALID_ARG: matrix_handle is null or dispatch_method is invalid
*/
esp_err_t touch_matrix_set_dispatch_method(touch_matrix_handle_t matrix_handle, touch_elem_dispatch_t dispatch_method);
/**
* @brief Touch matrix button set callback
*
* This function sets a callback routine into touch element driver core,
* when the subscribed events occur, the callback routine will be called.
*
* @param[in] matrix_handle Matrix button handle
* @param[in] matrix_callback User input callback
*
* @warning Since this input callback routine runs on driver core (esp-timer callback routine),
* it should not do something that attempts to Block, such as calling vTaskDelay().
*
* @return
* - ESP_OK: Successfully set callback
* - ESP_ERR_INVALID_STATE: Touch matrix driver was not initialized
* - ESP_ERR_INVALID_ARG: matrix_handle or matrix_callback is null
*/
esp_err_t touch_matrix_set_callback(touch_matrix_handle_t matrix_handle, touch_matrix_callback_t matrix_callback);
/**
* @brief Touch matrix button set long press trigger time
*
* This function sets the threshold time (ms) for a long press event. If a matrix button is pressed
* and held for a period of time that exceeds the threshold time, a long press event is triggered.
*
* @param[in] matrix_handle Matrix button handle
* @param[in] threshold_time Threshold time (ms) of long press event occur
*
* @return
* - ESP_OK: Successfully set the time of long press event
* - ESP_ERR_INVALID_STATE: Touch matrix driver was not initialized
* - ESP_ERR_INVALID_ARG: matrix_handle is null or time (ms) is 0
*/
esp_err_t touch_matrix_set_longpress(touch_matrix_handle_t matrix_handle, uint32_t threshold_time);
/**
* @brief Touch matrix get message
*
* This function decodes the element message from touch_element_message_receive() and return
* a matrix message pointer.
*
* @param[in] element_message element message
*
* @return Touch matrix message pointer
*/
const touch_matrix_message_t* touch_matrix_get_message(const touch_elem_message_t* element_message);
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,205 @@
// Copyright 2016-2020 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.
#pragma once
#include "touch_element/touch_element.h"
#ifdef __cplusplus
extern "C" {
#endif
/* --------------------------------- General slider instance default configuration --------------------------------- */
#define TOUCH_SLIDER_GLOBAL_DEFAULT_CONFIG() \
{ \
.quantify_lower_threshold = 0.3, \
.threshold_divider = 0.8, \
.filter_reset_time = 50, \
.benchmark_update_time = 500, \
.position_filter_size = 10, \
.position_filter_factor = 2, \
.calculate_channel_count = 3 \
}
/* ------------------------------------------------------------------------------------------------------------------ */
/**
* @brief Slider initialization configuration passed to touch_slider_install
*/
typedef struct {
float quantify_lower_threshold; //!< Slider signal quantification threshold
float threshold_divider; //!< Slider channel threshold divider
uint16_t filter_reset_time; //!< Slider position filter reset time (Unit is esp_timer callback tick)
uint16_t benchmark_update_time; //!< Slider benchmark update time (Unit is esp_timer callback tick)
uint8_t position_filter_size; //!< Moving window filter buffer size
uint8_t position_filter_factor; //!< One-order IIR filter factor
uint8_t calculate_channel_count; //!< The number of channels which will take part in calculation
} touch_slider_global_config_t;
/**
* @brief Slider configuration (for new instance) passed to touch_slider_create()
*/
typedef struct {
const touch_pad_t *channel_array; //!< Slider channel array
const float *sensitivity_array; //!< Slider channel sensitivity array
uint8_t channel_num; //!< The number of slider channels
uint8_t position_range; //!< The right region of touch slider position range, [0, position_range (less than or equal to 255)]
} touch_slider_config_t;
/**
* @brief Slider event type
*/
typedef enum {
TOUCH_SLIDER_EVT_ON_PRESS, //!< Slider on Press event
TOUCH_SLIDER_EVT_ON_RELEASE, //!< Slider on Release event
TOUCH_SLIDER_EVT_ON_CALCULATION, //!< Slider on Calculation event
TOUCH_SLIDER_EVT_MAX
} touch_slider_event_t;
typedef uint32_t touch_slider_position_t; //!< Slider position data type
/**
* @brief Slider message type
*/
typedef struct {
touch_slider_event_t event; //!< Slider event
touch_slider_position_t position; //!< Slider position
} touch_slider_message_t;
typedef touch_elem_handle_t touch_slider_handle_t; //!< Slider instance handle
typedef void(*touch_slider_callback_t)(touch_slider_handle_t, touch_slider_message_t, void *); //!< Slider callback type
/**
* @brief Touch slider initialize
*
* This function initializes touch slider object and acts on all
* touch slider instances.
*
* @param[in] global_config Touch slider global initialization configuration
*
* @return
* - ESP_OK: Successfully initialized touch slider
* - ESP_ERR_INVALID_STATE: Touch element library was not initialized
* - ESP_ERR_INVALID_ARG: slider_init is NULL
* - ESP_ERR_NO_MEM: Insufficient memory
*/
esp_err_t touch_slider_install(const touch_slider_global_config_t *global_config);
/**
* @brief Release resources allocated using touch_slider_install()
*
* @return
* - ESP_OK: Successfully released resources
*/
void touch_slider_uninstall(void);
/**
* @brief Create a new touch slider instance
*
* @param[in] slider_config Slider configuration
* @param[out] slider_handle Slider handle
*
* @note The index of Channel array and sensitivity array must be one-one correspondence
*
* @return
* - ESP_OK: Successfully create touch slider
* - ESP_ERR_INVALID_STATE: Touch slider driver was not initialized
* - ESP_ERR_INVALID_ARG: Invalid configuration struct or arguments is NULL
* - ESP_ERR_NO_MEM: Insufficient memory
*/
esp_err_t touch_slider_create(const touch_slider_config_t *slider_config, touch_slider_handle_t *slider_handle);
/**
* @brief Release resources allocated using touch_slider_create
*
* @param[in] slider_handle Slider handle
* @return
* - ESP_OK: Successfully released resources
* - ESP_ERR_INVALID_STATE: Touch slider driver was not initialized
* - ESP_ERR_INVALID_ARG: slider_handle is null
* - ESP_ERR_NOT_FOUND: Input handle is not a slider handle
*/
esp_err_t touch_slider_delete(touch_slider_handle_t slider_handle);
/**
* @brief Touch slider subscribes event
*
* This function uses event mask to subscribe to touch slider events, once one of
* the subscribed events occurs, the event message could be retrieved by calling
* touch_element_message_receive() or input callback routine.
*
* @param[in] slider_handle Slider handle
* @param[in] event_mask Slider subscription event mask
* @param[in] arg User input argument
*
* @note Touch slider only support three kind of event masks, they are
* TOUCH_ELEM_EVENT_ON_PRESS, TOUCH_ELEM_EVENT_ON_RELEASE. You can use those event masks in any
* combination to achieve the desired effect.
*
* @return
* - ESP_OK: Successfully subscribed touch slider event
* - ESP_ERR_INVALID_STATE: Touch slider driver was not initialized
* - ESP_ERR_INVALID_ARG: slider_handle is null or event is not supported
*/
esp_err_t touch_slider_subscribe_event(touch_slider_handle_t slider_handle, uint32_t event_mask, void *arg);
/**
* @brief Touch slider set dispatch method
*
* This function sets a dispatch method that the driver core will use
* this method as the event notification method.
*
* @param[in] slider_handle Slider handle
* @param[in] dispatch_method Dispatch method (By callback/event)
*
* @return
* - ESP_OK: Successfully set dispatch method
* - ESP_ERR_INVALID_STATE: Touch slider driver was not initialized
* - ESP_ERR_INVALID_ARG: slider_handle is null or dispatch_method is invalid
*/
esp_err_t touch_slider_set_dispatch_method(touch_slider_handle_t slider_handle, touch_elem_dispatch_t dispatch_method);
/**
* @brief Touch slider set callback
*
* This function sets a callback routine into touch element driver core,
* when the subscribed events occur, the callback routine will be called.
*
* @param[in] slider_handle Slider handle
* @param[in] slider_callback User input callback
*
* @warning Since this input callback routine runs on driver core (esp-timer callback routine),
* it should not do something that attempts to Block, such as calling vTaskDelay().
*
* @return
* - ESP_OK: Successfully set callback
* - ESP_ERR_INVALID_STATE: Touch slider driver was not initialized
* - ESP_ERR_INVALID_ARG: slider_handle or slider_callback is null
*/
esp_err_t touch_slider_set_callback(touch_slider_handle_t slider_handle, touch_slider_callback_t slider_callback);
/**
* @brief Touch slider get message
*
* This function decodes the element message from touch_element_message_receive() and return
* a slider message pointer.
*
* @param[in] element_message element message
*
* @return Touch slider message pointer
*/
const touch_slider_message_t* touch_slider_get_message(const touch_elem_message_t* element_message);
#ifdef __cplusplus
}
#endif