SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
TouchDrvCST816.cpp
Go to the documentation of this file.
1
29#include "../SensorBuildOpt.h"
30#if !SENSORLIB_EXCLUDE_TOUCH_CST816
31
32#include "TouchDrvCST816.hpp"
33
35{
36 static constexpr uint8_t POINT_BUFFER_SIZE = 13;
37 uint8_t buffer[POINT_BUFFER_SIZE] = {0};
38 uint16_t x = 0, y = 0;
39
40 // Clear cached touch points
42
43 if (readRegBuff(CST8xx_REG_STATUS, buffer, POINT_BUFFER_SIZE) == 0) {
44
45 // Some CST816T will return all 0xFF after turning off automatic sleep.
46 if (buffer[2] == 0x00 || buffer[2] == 0xFF) {
47 return _touchPoints;
48 }
49
50 uint8_t numPoints = buffer[2] & 0x0F; // Get number of touch points (lower 4 bits)
51
52 // CST816 only supports single touch
53 if (numPoints > MAX_FINGER_NUM || numPoints == 0 ) {
54 return _touchPoints;
55 }
56
57 x = ((buffer[3] & 0x0F) << 8 | buffer[4]);
58 y = ((buffer[5] & 0x0F) << 8 | buffer[6]);
59
60 // Depends on touch screen firmware
63 return _touchPoints; // Return zero points
64 }
65 // If not center button, add point
67 // Swap XY or mirroring coordinates,if set
69 }
70
71 return _touchPoints;
72}
73
75{
76 switch (_chipID) {
77 case CST816S_CHIP_ID:
78 return "CST816S";
79 case CST816T_CHIP_ID:
80 return "CST816T";
81 case CST716_CHIP_ID:
82 return "CST716";
83 case CST820_CHIP_ID:
84 return "CST820";
85 case CST816D_CHIP_ID:
86 return "CST816D";
87 default:
88 break;
89 }
90 return "UNKNOWN";
91}
92
97
99{
100 switch (_chipID) {
101 case CST816S_CHIP_ID:
102 case CST816T_CHIP_ID:
103 case CST820_CHIP_ID:
104 case CST816D_CHIP_ID:
105 reset();
106 hal->delay(50);
108 break;
109 case CST716_CHIP_ID:
110 default:
111 break;
112 }
113}
114
116{
117 switch (_chipID) {
118 case CST816S_CHIP_ID:
119 case CST816T_CHIP_ID:
120 case CST820_CHIP_ID:
121 case CST816D_CHIP_ID:
122 reset();
123 hal->delay(50);
124 writeReg(CST8xx_REG_DIS_AUTOSLEEP, (uint8_t)0x00);
125 break;
126 case CST716_CHIP_ID:
127 default:
128 break;
129 }
130}
131
132bool TouchDrvCST816::initImpl(uint8_t)
133{
134 int chip_id = readReg(CST8xx_REG_CHIP_ID);
135 SENSORLIB_LOG_I("Chip ID:0x%x", chip_id);
136
137 int version = readReg(CST8xx_REG_FW_VERSION);
138 SENSORLIB_LOG_I("Version :0x%x", version);
139
140 // CST716 : 0x20
141 // CST816S : 0xB4
142 // CST816T : 0xB5
143 // CST816D : 0xB6
144 // CST226SE : A7 = 0X20
145 if (chip_id != CST816S_CHIP_ID &&
146 chip_id != CST816T_CHIP_ID &&
147 chip_id != CST820_CHIP_ID &&
148 chip_id != CST816D_CHIP_ID &&
149 (chip_id != CST716_CHIP_ID || version == 0)) {
150 SENSORLIB_LOG_E("Chip ID does not match, should be CST816S:0X%02X , CST816T:0X%02X , CST816D:0X%02X , CST820:0X%02X , CST716:0X%02X",
152 return false;
153 }
154
155 _chipID = chip_id;
156
157 SENSORLIB_LOG_I("Touch type:%s", getModelName());
158
159 _maxTouchPoints = 1;
160
161 return true;
162}
163
164#endif
#define SENSORLIB_LOG_I(...)
#define SENSORLIB_LOG_E(...)
@license MIT License
std::unique_ptr< SensorHal > hal
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 sleep() override
Puts the touch driver to sleep.
static constexpr uint8_t CST8xx_REG_SLEEP
static constexpr uint8_t MAX_FINGER_NUM
const TouchPoints & getTouchPoints() override
Get the touch points.
void enableAutoSleep()
Enable auto sleep mode.
static constexpr uint8_t CST820_CHIP_ID
static constexpr uint8_t CST8xx_REG_DIS_AUTOSLEEP
static constexpr uint8_t CST816D_CHIP_ID
static constexpr uint8_t CST816S_CHIP_ID
static constexpr uint8_t CST8xx_REG_FW_VERSION
void disableAutoSleep()
Disable auto sleep mode.
static constexpr uint8_t CST8xx_REG_STATUS
static constexpr uint8_t CST8xx_REG_CHIP_ID
static constexpr uint8_t CST716_CHIP_ID
const char * getModelName() override
Get the model name.
static constexpr uint8_t CST816T_CHIP_ID
uint32_t _chipID
Chip identification value.
virtual void reset()
Reset the touch controller.
void * _userData
User data for home button callback.
virtual void updateXY(TouchPoints &points) const
Apply coordinate transformations (swap, scale, mirror) to all touch points.
int16_t _center_btn_y
Y coordinate of home button center.
int16_t _center_btn_x
X coordinate of home button center.
TouchPoints _touchPoints
Touch points data.
uint8_t _maxTouchPoints
Maximum supported touch points.
HomeButtonCallback _HButtonCallback
Home button callback function.
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.
int16_t x[5]
int16_t y[5]