SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
TouchDrvHI8561.cpp
Go to the documentation of this file.
1
30#include "../SensorBuildOpt.h"
31#if !SENSORLIB_EXCLUDE_TOUCH_HI8561
32
33#include "TouchDrvHI8561.hpp"
34
36{
37 // Unable to achieve, relies on screen sleep
38}
39
41{
42 static constexpr uint8_t POINT_BUFFER_SIZE = (MAX_FINGER_NUM * BYTES_PER_POINT);
43
44 uint8_t buffer[POINT_BUFFER_SIZE] = {0};
45
46 _touchPoints.clear(); // Clear cached touch points
47
48 uint8_t numPoints = 0;
49 if (!makePacketThenRead(REG_INFO, buffer, 1)) {
50 return _touchPoints;
51 }
52
53 numPoints = buffer[0];
54 if (numPoints == 0 || numPoints > MAX_FINGER_NUM) {
55 return _touchPoints;
56 }
57
58 uint8_t expectedBytes = numPoints * BYTES_PER_POINT; // 6 bytes per touch point
59
60 if (!makePacketThenRead(REG_POINT, buffer, expectedBytes)) {
61 return _touchPoints;
62 }
63
64 for (uint8_t i = 0; i < numPoints; i++) {
65 const uint8_t offset = i * BYTES_PER_POINT ;
66 uint16_t x = buffer[offset + 1] | (buffer[offset + 0] << 8);
67 uint16_t y = buffer[offset + 3] | (buffer[offset + 2] << 8);
68 uint8_t pressure = buffer[offset + 4];
69 // Once edge detection is triggered, subsequent touch points are untrusted and do not need to continue looping.
70 if (x == UINT16_MAX || y == UINT16_MAX) {
71 SENSORLIB_LOG_E("Edge trigger detected, id %d set to invalid coordinates", i);
72 break;
73 }
75 }
76 // Swap XY or mirroring coordinates,if set
78
79 return _touchPoints;
80}
81
83{
84 return "HI8561";
85}
86
87bool TouchDrvHI8561::initImpl(uint8_t)
88{
89 // Disable I2C stop bit
90 setAck(false);
91
92 // Check if the starting address matches
93 uint8_t buffer[6];
94 if (!makePacketThenRead(REG_SECTION_INFO, buffer, sizeof(buffer))) {
95 return false;
96 }
97 uint32_t reg = buffer[0] + (buffer[1] << 8) + (buffer[2] << 16) + (buffer[3] << 24);
98 if (reg != REG_INFO) {
99 SENSORLIB_LOG_E("Invalid touch info start address: 0x%08" PRIu32, reg);
100 return false;
101 }
102
103 // Use fixed values ​​to identify chip models.
104 _chipID = 0x8561;
105
106 SENSORLIB_LOG_D("HI8561 touch start address: 0x%08" PRIu32, reg);
107
108 return true;
109}
110
111#endif
#define SENSORLIB_LOG_E(...)
#define SENSORLIB_LOG_D(...)
SensorPressure pressure(bhy)
void setAck(bool enable)
static constexpr uint32_t REG_SECTION_INFO
static constexpr uint32_t REG_INFO
const TouchPoints & getTouchPoints() override
Get the touch points.
void sleep() override
Puts the touch driver to sleep.
static constexpr uint8_t BYTES_PER_POINT
static constexpr uint32_t REG_POINT
const char * getModelName() override
Get the model name.
static constexpr uint8_t MAX_FINGER_NUM
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.
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]