SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
TouchDrvFT6X36.cpp
Go to the documentation of this file.
1
30#include "../SensorBuildOpt.h"
31#if !SENSORLIB_EXCLUDE_TOUCH_FT6X36
32
33#include "TouchDrvFT6X36.hpp"
34
36{
37 writeReg(FT6X36_REG_POWER_MODE, PMODE_DEEP_SLEEP);
38}
39
41{
42 static constexpr size_t POINT_BUFFER_SIZE = MAX_FINGER_NUM * BYTES_PER_POINT;
43 uint8_t buffer[POINT_BUFFER_SIZE] = {0};
44 _touchPoints.clear(); // Clear cached touch points
45 int numPoints = readReg(FT6X36_REG_STATUS);
46 if (numPoints < 0) {
47 SENSORLIB_LOG_E("Failed to read touch points: %d", numPoints);
48 return _touchPoints;
49 }
50 if (numPoints == 0 || numPoints > MAX_FINGER_NUM) {
51 return _touchPoints;
52 }
53 uint8_t expectedBytes = numPoints * BYTES_PER_POINT; // 6 bytes per touch point
54
55 // Read all touch points data in a single burst read for efficiency
56 if (readRegBuff(FT6X36_REG_TOUCH1_XH, buffer, expectedBytes) == 0) {
57 for (int i = 0; i < numPoints; i++) {
58 const uint8_t eventFlag = (buffer[0 + i * BYTES_PER_POINT] & 0xC0) >> 6;
59 const uint8_t touchId = (buffer[2 + i * BYTES_PER_POINT] >> 4) & 0x0F;
60 const uint16_t x = ((buffer[0 + i * BYTES_PER_POINT] & 0x0F) << 8) | buffer[1 + i * BYTES_PER_POINT];
61 const uint16_t y = ((buffer[2 + i * BYTES_PER_POINT] & 0x0F) << 8) | buffer[3 + i * BYTES_PER_POINT];
62 const uint8_t weight = buffer[4 + i * BYTES_PER_POINT];
63 // Not used in FT6206, but valid in FT6236/FT6336U, representing the touch area size.
64 // The value is typically 0-15, where higher values indicate a larger touch area,
65 // which can be useful for distinguishing between a fingertip and a larger object like a
66 // stylus or palm. In some applications, this information can be used to improve touch accuracy
67 // or to implement features like palm rejection.
68 // const uint8_t area = (buffer[5 + i * BYTES_PER_POINT] & 0xF0) >> 4;
69 // Serial.printf("Point %d: ID=%d, Event=%d, X=%d, Y=%d, Weight=%d, Area=%d\n", i, touchId, eventFlag, x, y, weight, area);
70 _touchPoints.addPoint(x, y, weight, touchId, eventFlag);
71 }
72 // Swap XY or mirroring coordinates, if set
74 }
75 return _touchPoints;
76}
77
79{
80 switch (_chipID) {
81 case FT6206_CHIP_ID: return "FT6206";
82 case FT6236_CHIP_ID: return "FT6236";
83 case FT6336U_CHIP_ID: return "FT6336U";
84 case FT3267_CHIP_ID: return "FT3267";
85 case FT5336_CHIP_ID: return "FT5336";
86 case FT3068_CHIP_ID: return "FT3068";
87 default: return "UNKNOWN";
88 }
89}
90
92{
93 return readReg(FT6X36_REG_MODE) & 0x03;
94}
95
97{
98 int val = readReg(FT6X36_REG_GEST);
99 switch (val) {
100 case 0x10: return MOVE_UP;
101 case 0x14: return MOVE_RIGHT;
102 case 0x18: return MOVE_DOWN;
103 case 0x1C: return MOVE_LEFT;
104 case 0x48: return ZOOM_IN;
105 case 0x49: return ZOOM_OUT;
106 default: break;
107 }
108 return NO_GESTURE;
109}
110
112{
113 writeReg(FT6X36_REG_MODE, value);
114}
115
117{
118 return readReg(FT6X36_REG_THRESHOLD);
119}
120
122{
123 writeReg(FT6X36_REG_THRESHOLD, value);
124}
125
127{
128 return readReg(FT6X36_REG_MONITOR_TIME);
129}
130
132{
133 writeReg(FT6X36_REG_MONITOR_TIME, sec);
134}
135
137{
138 uint8_t buffer[2] = {0};
139 readRegBuff(FT6X36_REG_LIB_VERSION_H, buffer, 2);
140 return (buffer[0] << 8) | buffer[1];
141}
142
144{
145 writeReg(FT6X36_REG_INT_STATUS, 0x00);
146}
147
149{
150 writeReg(FT6X36_REG_INT_STATUS, 0x01);
151}
152
154{
155 writeReg(FT6X36_REG_POWER_MODE, mode);
156}
157
159{
160 return readReg(FT6X36_REG_VENDOR1_ID);
161}
162
164{
165 return readReg(FT6X36_REG_ERROR_STATUS);
166}
167
168bool TouchDrvFT6X36::initImpl(uint8_t)
169{
170 uint8_t vendId = readReg(FT6X36_REG_VENDOR1_ID);
171
172 switch (vendId) {
173 case FT_VEND_ID1:
174 case FT_VEND_ID2:
175 case FT_VEND_ID3:
176 break;
177 default:
178 SENSORLIB_LOG_E("Vendor id: 0x%X not match!", vendId);
179 return false;
180 }
181
182 _chipID = readReg(FT6X36_REG_CHIP_ID);
183
184 switch (_chipID) {
185 case FT3267_CHIP_ID:
186 case FT5336_CHIP_ID:
187 case FT6206_CHIP_ID:
188 case FT6236_CHIP_ID:
189 case FT6336U_CHIP_ID:
190 case FT3068_CHIP_ID:
191 break;
192 default:
193 SENSORLIB_LOG_E("Chip ID: 0x%" PRIx32 " not match!", _chipID);
194 return false;
195 }
196
197 SENSORLIB_LOG_I("Vend ID: 0x%X", vendId);
198 SENSORLIB_LOG_I("Chip ID: 0x%" PRIx32, _chipID);
199 SENSORLIB_LOG_I("Firmware Version: 0x%X", readReg(FT6X36_REG_FIRM_VERS));
200 SENSORLIB_LOG_I("Report Rate Hz: %u", readReg(FT6X36_REG_PERIOD_ACTIVE));
201 SENSORLIB_LOG_I("Threshold : %u", readReg(FT6X36_REG_THRESHOLD));
202 SENSORLIB_LOG_I("Library version : 0x%x", getLibraryVersion());
203 // This register describes period of monitor status, it should not less than 30.
204 SENSORLIB_LOG_I("Chip period of monitor status : 0x%x", readReg(FT6X36_REG_PERIOD_MONITOR));
205 _maxTouchPoints = MAX_FINGER_NUM;
206
207 return true;
208}
209
210
211#endif
#define SENSORLIB_LOG_I(...)
#define SENSORLIB_LOG_E(...)
int readReg(uint8_t reg) const
int writeReg(uint8_t reg, uint8_t val)
int readRegBuff(uint8_t reg, uint8_t *buf, size_t len) const
void interruptPolling(void)
Polling mode for touch interrupt.
uint16_t getLibraryVersion()
Get the library version.
const char * getModelName() override
Get the model name.
void interruptTrigger(void)
Trigger mode for touch interrupt.
uint8_t getThreshold(void)
Get the touch threshold.
uint8_t getVendorID(void)
Get the vendor ID.
void setThreshold(uint8_t value)
Set the touch threshold.
const TouchPoints & getTouchPoints() override
Get the touch points.
uint8_t getMonitorTime(void)
Get the monitor time.
void setMonitorTime(uint8_t sec)
Set the monitor time.
void setPowerMode(PowerMode mode)
Set the touch interrupt mode.
uint8_t getDeviceMode(void)
Get the device mode.
void setDeviceMode(uint8_t value)
Set the device mode.
void sleep() override
Puts the touch driver to sleep.
uint8_t getErrorCode(void)
Get the error code.
uint32_t _chipID
Chip identification value.
virtual void updateXY(TouchPoints &points) const
Apply coordinate transformations (swap, scale, mirror) to all touch points.
TouchPoints _touchPoints
Touch points data.
uint8_t _maxTouchPoints
Maximum supported touch points.
Container for touch point data and optional gesture information.
bool addPoint(uint16_t x, uint16_t y, uint16_t pressure=0, uint8_t id=0, uint8_t event=0)
Adds a touch point to the container.
void clear()
Clears all stored touch points and gesture.
uint8_t i
int16_t x[5]
int16_t y[5]