-
Notifications
You must be signed in to change notification settings - Fork 7.2k
/
touch_matrix.h
213 lines (194 loc) · 8.27 KB
/
touch_matrix.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* SPDX-FileCopyrightText: 2016-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#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()
*
*/
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
*
* @note Matrix message will be passed from the callback function and it will be destroyed when the callback function return.
*
* @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