SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
TouchDrvCHSC5816.cpp
Go to the documentation of this file.
1
30#include "../SensorBuildOpt.h"
31#if !SENSORLIB_EXCLUDE_TOUCH_CHSC5816
32
33#include "TouchDrvCHSC5816.hpp"
34
36{
37 uint8_t CHSC5816_REG_SLEEP[] = {
38 0x20, 0x00, 0x00, 0x00, // CHSC5816_REG_CMD_BUFF
39 0xF8, 0x16, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00,
40 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xE9
41 };
42 writeBuff(CHSC5816_REG_SLEEP, arraySize(CHSC5816_REG_SLEEP));
43}
44
46{
47 uint8_t buffer[POINT_BUFFER_SIZE] = {0};
48
49 _touchPoints.clear(); // Clear cached touch points
50
52 if (writeThenRead(buffer, COMMAND_SIZE, buffer, POINT_BUFFER_SIZE) == 0) {
53
54 // Get number of touch points
55 uint8_t numPoints = buffer[1];
56
57 if (numPoints > MAX_FINGER_NUM || numPoints == 0) {
58 return _touchPoints;
59 }
60 const uint16_t x = buffer[2] | ((buffer[5] & 0x0F) << 8);
61 const uint16_t y = buffer[3] | (((buffer[5] >> 4) & 0x0F) << 8);
62 const uint8_t pressure = buffer[4];
63 const uint8_t eventFlag = (buffer[6] & 0x0F);
64 const uint8_t id = (buffer[6] >> 4) & 0x0F;
65 _touchPoints.addPoint(x, y, pressure, id, eventFlag);
66
67 // Swap XY or mirroring coordinates,if set
69 }
70 return _touchPoints;
71}
72
74{
75 return "CHSC5816";
76}
77
78bool TouchDrvCHSC5816::initImpl(uint8_t)
79{
80 hal->delay(10);
81
82 uint8_t buffer[16] = {0};
83
85 if (writeBuff(buffer, arraySize(buffer)) < 0) {
86 SENSORLIB_LOG_E("writeBuff clean boot state failed!");
87 return false;
88 }
89
90 hal->delay(10);
91
92 bool success = false;
93 int retry = 5;
94 do {
96 if (writeThenRead(buffer, 4, buffer, 4) < 0) {
97 return false;
98 }
99
100 hal->delay(20);
101
102 // Check if the signature matches
103 // When device is startup , reg will report 0x43 0x48 0x53 0x43
104 if (buffer[0] == 0x43 && buffer[1] == 0x48 && buffer[2] == 0x53 && buffer[3] == 0x43) {
105 SENSORLIB_LOG_D("Signature matched!");
106 success = true;
107 break;
108
109 }
110 } while (retry--);
111
112 if (success) {
113 _maxTouchPoints = 1;
114 _chipID = 0x5816; // Fixed chip ID
115 return true;
116 }
117
118 return false;
119}
120
121#endif
constexpr size_t arraySize(const T(&arr)[N])
Returns the number of elements in a C‑style array at compile time.
#define SENSORLIB_LOG_E(...)
#define SENSORLIB_LOG_D(...)
void addrToBeBuf(uint32_t addr, uint8_t buf[4])
SensorPressure pressure(bhy)
std::unique_ptr< SensorHal > hal
int writeBuff(uint8_t *buf, size_t len)
int writeThenRead(const uint8_t *write_buffer, size_t write_len, uint8_t *read_buffer, size_t read_len)
static constexpr uint32_t REG_BOOT_STATE
static constexpr uint8_t COMMAND_SIZE
const char * getModelName() override
Get the model name.
static constexpr uint8_t POINT_BUFFER_SIZE
static constexpr uint32_t REG_POINT_EVENT
static constexpr uint8_t MAX_FINGER_NUM
const TouchPoints & getTouchPoints() override
Get the touch points.
void sleep() override
Puts the touch driver to sleep.
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.
int16_t x[5]
int16_t y[5]