1 /*
2 * Copyright 2020 NXP
3 * All rights reserved.
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
8 #include "fsl_ft3267.h"
9
10 /*******************************************************************************
11 * Definitions
12 ******************************************************************************/
13 typedef struct _ft3267_touch_point
14 {
15 uint8_t XH;
16 uint8_t XL;
17 uint8_t YH;
18 uint8_t YL;
19 uint8_t RESERVED[2];
20 } ft3267_touch_point_t;
21
22 typedef struct _ft3267_touch_data
23 {
24 uint8_t GEST_ID;
25 uint8_t TD_STATUS;
26 ft3267_touch_point_t TOUCH[FT3267_MAX_TOUCHES];
27 } ft3267_touch_data_t;
28
29 #define TOUCH_POINT_GET_EVENT(T) ((touch_event_t)(uint8_t)((T).XH >> 6U))
30 #define TOUCH_POINT_GET_ID(T) ((T).YH >> 4)
31 #define TOUCH_POINT_GET_X(T) (int)(uint16_t)((((uint16_t)(T).XH & 0x0fU) << 8) | (uint16_t)(T).XL)
32 #define TOUCH_POINT_GET_Y(T) (int)(uint16_t)((((uint16_t)(T).YH & 0x0fU) << 8) | (uint16_t)(T).YL)
33
34 #define FT3267_SUBADDR_SIZE (1U)
35
36 /*******************************************************************************
37 * Code
38 ******************************************************************************/
FT3267_Init(ft3267_handle_t * handle,const ft3267_config_t * config)39 status_t FT3267_Init(ft3267_handle_t *handle, const ft3267_config_t *config)
40 {
41 if ((NULL == handle) || (NULL == config))
42 {
43 return kStatus_InvalidArgument;
44 }
45
46 (void)memset(handle, 0, sizeof(*handle));
47
48 handle->I2C_ReceiveFunc = config->I2C_ReceiveFunc;
49 handle->pullResetPinFunc = config->pullResetPinFunc;
50 handle->pullPowerPinFunc = config->pullPowerPinFunc;
51
52 /* Power on sequence */
53 if (NULL != handle->pullPowerPinFunc)
54 {
55 handle->pullPowerPinFunc(true);
56 }
57
58 /* Reset the controller. */
59 if (NULL != handle->pullResetPinFunc)
60 {
61 handle->pullResetPinFunc(false);
62 config->timeDelayMsFunc(5U);
63 handle->pullResetPinFunc(true);
64 }
65
66 return kStatus_Success;
67 }
68
FT3267_Deinit(ft3267_handle_t * handle)69 status_t FT3267_Deinit(ft3267_handle_t *handle)
70 {
71 status_t status;
72
73 if (NULL == handle)
74 {
75 status = kStatus_InvalidArgument;
76 }
77 else
78 {
79 if (NULL != handle->pullResetPinFunc)
80 {
81 handle->pullResetPinFunc(false);
82 }
83
84 if (NULL != handle->pullPowerPinFunc)
85 {
86 handle->pullPowerPinFunc(true);
87 }
88
89 status = kStatus_Success;
90 }
91
92 return status;
93 }
94
FT3267_GetSingleTouch(ft3267_handle_t * handle,touch_event_t * touch_event,int * touch_x,int * touch_y)95 status_t FT3267_GetSingleTouch(ft3267_handle_t *handle, touch_event_t *touch_event, int *touch_x, int *touch_y)
96 {
97 status_t status;
98 touch_event_t touch_event_local;
99
100 status =
101 handle->I2C_ReceiveFunc(FT3267_I2C_ADDRESS, 1U, FT3267_SUBADDR_SIZE, handle->touchBuf, FT3267_TOUCH_DATA_LEN);
102
103 if (status == kStatus_Success)
104 {
105 ft3267_touch_data_t *touch_data = (ft3267_touch_data_t *)(void *)(handle->touchBuf);
106
107 touch_event_local = TOUCH_POINT_GET_EVENT(touch_data->TOUCH[0]);
108
109 /* Update coordinates only if there is touch detected */
110 if ((touch_event_local == kTouch_Down) || (touch_event_local == kTouch_Contact))
111 {
112 if (NULL != touch_x)
113 {
114 *touch_x = TOUCH_POINT_GET_X(touch_data->TOUCH[0]);
115 }
116 if (NULL != touch_y)
117 {
118 *touch_y = TOUCH_POINT_GET_Y(touch_data->TOUCH[0]);
119 }
120 }
121
122 if (touch_event != NULL)
123 {
124 *touch_event = touch_event_local;
125 }
126 }
127
128 return status;
129 }
130
FT3267_GetMultiTouch(ft3267_handle_t * handle,int * touch_count,touch_point_t touch_array[FT3267_MAX_TOUCHES])131 status_t FT3267_GetMultiTouch(ft3267_handle_t *handle, int *touch_count, touch_point_t touch_array[FT3267_MAX_TOUCHES])
132 {
133 status_t status;
134 uint32_t i;
135
136 status =
137 handle->I2C_ReceiveFunc(FT3267_I2C_ADDRESS, 1U, FT3267_SUBADDR_SIZE, handle->touchBuf, FT3267_TOUCH_DATA_LEN);
138
139 if (status == kStatus_Success)
140 {
141 ft3267_touch_data_t *touch_data = (ft3267_touch_data_t *)(void *)(handle->touchBuf);
142
143 /* Check for valid number of touches - otherwise ignore touch information */
144 if (touch_data->TD_STATUS > FT3267_MAX_TOUCHES)
145 {
146 touch_data->TD_STATUS = 0;
147 }
148
149 /* Decode number of touches */
150 if (NULL != touch_count)
151 {
152 *touch_count = (int)touch_data->TD_STATUS;
153 }
154
155 /* Decode valid touch points */
156 for (i = 0; i < touch_data->TD_STATUS; i++)
157 {
158 touch_array[i].TOUCH_ID = TOUCH_POINT_GET_ID(touch_data->TOUCH[i]);
159 touch_array[i].TOUCH_EVENT = TOUCH_POINT_GET_EVENT(touch_data->TOUCH[i]);
160 touch_array[i].TOUCH_X = TOUCH_POINT_GET_X(touch_data->TOUCH[i]);
161 touch_array[i].TOUCH_Y = TOUCH_POINT_GET_Y(touch_data->TOUCH[i]);
162 }
163
164 /* Clear vacant elements of touch_array */
165 for (; i < FT3267_MAX_TOUCHES; i++)
166 {
167 touch_array[i].TOUCH_ID = 0;
168 touch_array[i].TOUCH_EVENT = kTouch_Reserved;
169 touch_array[i].TOUCH_X = 0;
170 touch_array[i].TOUCH_Y = 0;
171 }
172 }
173
174 return status;
175 }
176