touchpad  2.0.0.0
touchpad.h
Go to the documentation of this file.
1 /*
2  * MikroSDK - MikroE Software Development Kit
3  * Copyright© 2020 MikroElektronika d.o.o.
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without restriction,
8  * including without limitation the rights to use, copy, modify, merge,
9  * publish, distribute, sublicense, and/or sell copies of the Software,
10  * and to permit persons to whom the Software is furnished to do so,
11  * subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22  * OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
33 // ----------------------------------------------------------------------------
34 
35 #ifndef TOUCHPAD_H
36 #define TOUCHPAD_H
37 
38 #include "mikrosdk_version.h"
39 
40 #ifdef __GNUC__
41 #if mikroSDK_GET_VERSION < 20800ul
42 #include "rcu_delays.h"
43 #else
44 #include "delays.h"
45 #endif
46 #endif
47 
48 #include "drv_digital_out.h"
49 #include "drv_digital_in.h"
50 #include "drv_i2c_master.h"
51 
52 // -------------------------------------------------------------- PUBLIC MACROS
62 #define TOUCHPAD_MAP_MIKROBUS( cfg, mikrobus ) \
63  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
64  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
65  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
66  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
67 
73 #define TOUCHPAD_RETVAL uint8_t
74 
75 #define TOUCHPAD_OK 0x00
76 #define TOUCHPAD_INIT_ERROR 0xFF
77 
83 #define TOUCHPAD_DEVICE_SLAVE_ADDR 0x25 // Slave address
84 
91 #define TOUCHPAD_REG_FW_MAJOR 0x00
92 #define TOUCHPAD_REG_FW_MINOR 0x01
93 #define TOUCHPAD_REG_APP_ID_LSB 0x02
94 #define TOUCHPAD_REG_APP_ID_MSB 0x03
95 #define TOUCHPAD_REG_CMD 0x04
96 #define TOUCHPAD_REG_MODE 0x05
97 #define TOUCHPAD_REG_MODE_CON 0x06
98 
99 #define TOUCHPAD_REG_TOUCH_STATE 0x10 // Touch state
100 #define TOUCHPAD_REG_TOUCH_X 0x11 // Touch X
101 #define TOUCHPAD_REG_TOUCH_Y 0x12 // Touch Y
102 #define TOUCHPAD_REG_TOUCH_LSB 0x13 // Touch XY
103 #define TOUCHPAD_REG_GESTURE_STATE 0x14 // Gestrure state
104 #define TOUCHPAD_REG_GESTURE_DIAG 0x15 // Gestrure diagnostic
105 
106 #define TOUCHPAD_REG_NUM_OF_X_CHANNELS 0x20 // Number of x channels
107 #define TOUCHPAD_REG_NUM_OF_Y_CHANNELS 0x21 // Number of y channels
108 #define TOUCHPAD_REG_SCAN_COUNT 0x22
109 #define TOUCHPAD_REG_TOUCH_THR_X 0x23
110 #define TOUCHPAD_REG_TOUCH_THR_Y 0x24
111 
112 #define TOUCHPAD_REG_ACTIVE_PERIOD_LSB 0x25
113 #define TOUCHPAD_REG_ACTIVE_PERIOD_MSB 0x26
114 #define TOUCHPAD_REG_IDLE_PERIOD_LSB 0x27
115 #define TOUCHPAD_REG_IDLE_PERIOD_MSB 0x28
116 #define TOUCHPAD_REG_IDLE_TIMEOUT 0x29
117 #define TOUCHPAD_REG_HYSTERESIS 0x2A
118 
119 #define TOUCHPAD_REG_DEBOUNCE_UP 0x2B
120 #define TOUCHPAD_REG_DEBOUNCE_DOWN 0x2C
121 #define TOUCHPAD_REG_FILTER_TYPE 0x31
122 #define TOUCHPAD_REG_FILTER_THR 0x32
123 #define TOUCHPAD_REG_LARGE_ACTIVATION_THR_LSB 0x35
124 #define TOUCHPAD_REG_LARGE_ACTIVATION_THR_MSB 0x36
125 
126 #define TOUCHPAD_REG_HORIZONTAL_SWIPE_DISTANCE 0x37 // Horisontal swipe distance
127 #define TOUCHPAD_REG_VERTICAL_SWIPE_DISTANCE 0x38 // Vertical swipe distance
128 #define TOUCHPAD_REG_SWIPE_HOLD_BOUNDARY 0x39 // Swipe hold boundary
129 #define TOUCHPAD_REG_TAP_DISTANCE 0x3A // Tap distance
130 #define TOUCHPAD_REG_DISTANCE_BETWEEN_TAPS 0x3B // Distance between taps
131 #define TOUCHPAD_REG_TAP_HOLD_TIME_LSB 0x3C // Tap hold time (LSB)
132 #define TOUCHPAD_REG_TAP_HOLD_TIME_MSB 0x3D // Tap hold time (MSB)
133 #define TOUCHPAD_REG_GESTURE_CLICK_TIME 0x3E // Gesture click time
134 #define TOUCHPAD_REG_SWIPE_HOLD_THRESHOLD 0x3F // Swipe hold treshold
135 #define TOUCHPAD_REG_MIN_SWIPE_VELOCITY 0x40 // Min swipe velecity
136 #define TOUCHPAD_REG_HORIZONTAL_GESTURE_ANGLE 0x41 // Horisontal gesture angle
137 #define TOUCHPAD_REG_VERTICAL_GESTURE_ANGLE 0x42 // Vertical gesture angle
138 
144 #define TOUCHPAD_MODE_STANDBY 0x00
145 #define TOUCHPAD_MODE_GESTURE 0x01
146 #define TOUCHPAD_MODE_TOUCH_ONLY 0x02
147 #define TOUCHPAD_MODE_FULL 0x03
148 #define TOUCHPAD_MODE_RAW_ADC 0x04
149 
155 #define TOUCHPAD_RAWADC_TYPE_RES_1_2 0x00
156 #define TOUCHPAD_RAWADC_TYPE_RES_1 0x01
157 #define TOUCHPAD_RAWADC_TYPE_RES_2 0x02
158 
164 #define TOUCHPAD_CMD_NV_STORAGE_WRITE 0x80
165 #define TOUCHPAD_CMD_RESTORE_DEF_CONFIG 0x40
166 #define TOUCHPAD_CMD_CONFIG_CTRL 0x20
167 #define TOUCHPAD_CMD_EXECUTE_MANU_TEST 0x08
168 #define TOUCHPAD_CMD_FORCE_BASELINE 0x01
169 
175 #define TOUCHPAD_PERIOD_10ms 0x0142
176 #define TOUCHPAD_PERIOD_20ms 0x0284
177 #define TOUCHPAD_PERIOD_50ms 0x064C
178 #define TOUCHPAD_PERIOD_1000ms 0x0C99
179 
185 #define TOUCHPAD_TOUCH_IS_PRESENT 0x01
186 #define TOUCHPAD_GESTURE_IS_PRESENT 0x02
187 #define TOUCHPAD_LARGE_ACT_IS_PRESENT 0x04
188 
194 #define TOUCHPAD_GS_NO_GESTURE_PRESENT 0x00 // No Gesture Present
195 #define TOUCHPAD_GS_SINGLE_CLICK 0x10 // Single Click
196 #define TOUCHPAD_GS_CLICK_AND_HOLD 0x11 // Click and Hold
197 #define TOUCHPAD_GS_DOUBLE_CLICK 0x20 // Double Click
198 #define TOUCHPAD_GS_DOWN_SWIPE 0x31 // Down Swipe
199 #define TOUCHPAD_GS_DOWN_SWIPE_AND_HOLD 0x32 // Down Swipe and Hold
200 #define TOUCHPAD_GS_RIGHT_SWIPE 0x41 // Right Swipe
201 #define TOUCHPAD_GS_RIGHT_SWIPE_AND_HOLD 0x42 // Right Swipe and Hold
202 #define TOUCHPAD_GS_UP_SWIPE 0x51 // Up Swipe
203 #define TOUCHPAD_GS_UP_SWIPE_AND_HOLD 0x52 // Up Swipe and Hold
204 #define TOUCHPAD_GS_LEFT_SWIPE 0x61 // Left Swipe
205 #define TOUCHPAD_GS_LEFT_SWIPE_AND_HOLD 0x62 // Left Swipe and Hold
206 
212 #define TOUCHPAD_EVENT_TOUCH 1
213 #define TOUCHPAD_EVENT_GESTURE 2
214 #define TOUCHPAD_NO_EVENT 0
215 
221 #define TOUCHPAD_GD_CLICK_TIMEOUT 0x01 // Click Timeout
222 #define TOUCHPAD_GD_SWIPE_TIMEOUT 0x02 // Swipe Timeout
223 #define TOUCHPAD_GD_GENERAL_TIMEOUT 0x03 // General Timeout
224 #define TOUCHPAD_GD_CLICK_THR_EXCEEDED 0x04 // Click Threshold Exceeded
225 #define TOUCHPAD_GD_SWIPE_THR_EXCEEDED 0x05 // Swipe Threshold Exceeded
226 #define TOUCHPAD_GD_SWIPE_HOLD_THR 0x06 // Swipe and Hold Threshold Exceeded
227 #define TOUCHPAD_GD_SWIPE_OPPOSITE_DIR_THR 0x07 // Swipe Opposite Direction Threshold Exceeded
228 #define TOUCHPAD_GD_SWIPE_HOLD_VALUE 0x09 // Swipe and Hold Value Exceeded
229 #define TOUCHPAD_GD_OUTSIDE_SWIPE 0x0A // Outside Swipe Angle
230  // End group macro
233 // --------------------------------------------------------------- PUBLIC TYPES
242 typedef struct
243 {
244  // Output pins
245 
246  digital_out_t rst;
247 
248  // Input pins
249 
250  digital_in_t int_pin;
251 
252  // Modules
253 
254  i2c_master_t i2c;
255 
256  // ctx variable
257 
258  uint8_t slave_address;
259 
260 } touchpad_t;
261 
265 typedef struct
266 {
267  // Communication gpio pins
268 
269  pin_name_t scl;
270  pin_name_t sda;
271 
272  // Additional gpio pins
273 
274  pin_name_t rst;
275  pin_name_t int_pin;
276 
277  // static variable
278 
279  uint8_t i2c_speed;
280  uint32_t i2c_address;
281 
283 
287 typedef struct
288 {
289  uint16_t x; // X position
290  uint16_t y; // Y position
291 
293  // End types group
295 
296 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
297 
303 #ifdef __cplusplus
304 extern "C"{
305 #endif
306 
316 
326 
336 
347 void touchpad_generic_write ( touchpad_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
348 
360 void touchpad_generic_read ( touchpad_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
361 
371 
379 
386 
394 
418 
419 #ifdef __cplusplus
420 }
421 #endif
422 #endif // _TOUCHPAD_H_
423  // End public_function group
426 
427 // ------------------------------------------------------------------------- END
touchpad_cfg_t::rst
pin_name_t rst
Definition: touchpad.h:274
touchpad_cfg_t
Click configuration structure definition.
Definition: touchpad.h:266
touchpad_cord_t::x
uint16_t x
Definition: touchpad.h:289
touchpad_t::i2c
i2c_master_t i2c
Definition: touchpad.h:254
touchpad_cfg_t::i2c_address
uint32_t i2c_address
Definition: touchpad.h:280
touchpad_get_event_state
uint8_t touchpad_get_event_state(touchpad_t *ctx)
Get event state.
touchpad_generic_read
void touchpad_generic_read(touchpad_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
touchpad_get_gesture_type
uint8_t touchpad_get_gesture_type(touchpad_t *ctx)
Gesture type.
touchpad_cfg_t::int_pin
pin_name_t int_pin
Definition: touchpad.h:275
touchpad_default_cfg
void touchpad_default_cfg(touchpad_t *ctx)
Click Default Configuration function.
touchpad_t::rst
digital_out_t rst
Definition: touchpad.h:246
touchpad_cfg_setup
void touchpad_cfg_setup(touchpad_cfg_t *cfg)
Config Object Initialization function.
touchpad_get_interrupt_state
uint8_t touchpad_get_interrupt_state(touchpad_t *ctx)
Get interrupt state.
touchpad_cfg_t::i2c_speed
uint8_t i2c_speed
Definition: touchpad.h:279
touchpad_cord_t::y
uint16_t y
Definition: touchpad.h:290
touchpad_init
TOUCHPAD_RETVAL touchpad_init(touchpad_t *ctx, touchpad_cfg_t *cfg)
Initialization function.
touchpad_reset
void touchpad_reset(touchpad_t *ctx)
Hardware Reset.
touchpad_t::int_pin
digital_in_t int_pin
Definition: touchpad.h:250
touchpad_generic_write
void touchpad_generic_write(touchpad_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
touchpad_t::slave_address
uint8_t slave_address
Definition: touchpad.h:258
touchpad_cfg_t::sda
pin_name_t sda
Definition: touchpad.h:270
touchpad_get_touch_coordinate
void touchpad_get_touch_coordinate(touchpad_t *ctx, touchpad_cord_t *cord)
Get touch coordinate.
touchpad_cfg_t::scl
pin_name_t scl
Definition: touchpad.h:269
TOUCHPAD_RETVAL
#define TOUCHPAD_RETVAL
Definition: touchpad.h:73
touchpad_cord_t
Coordinate structure definition.
Definition: touchpad.h:288
touchpad_t
Click ctx object definition.
Definition: touchpad.h:243