@@ -32,6 +32,18 @@ bool Cst816S::Init() {
3232 twiMaster.Read (twiAddress, 0xa7 , &dummy, 1 );
3333 vTaskDelay (5 );
3434
35+ static constexpr uint8_t maxRetries = 3 ;
36+ bool isDeviceOk;
37+ uint8_t retries = 0 ;
38+ do {
39+ isDeviceOk = CheckDeviceIds ();
40+ retries++;
41+ } while (!isDeviceOk && retries < maxRetries);
42+
43+ if (!isDeviceOk) {
44+ return false ;
45+ }
46+
3547 /*
3648 [2] EnConLR - Continuous operation can slide around
3749 [1] EnConUD - Slide up and down to enable continuous operation
@@ -50,21 +62,7 @@ bool Cst816S::Init() {
5062 static constexpr uint8_t irqCtl = 0b01110000 ;
5163 twiMaster.Write (twiAddress, 0xFA , &irqCtl, 1 );
5264
53- // There's mixed information about which register contains which information
54- if (twiMaster.Read (twiAddress, 0xA7 , &chipId, 1 ) == TwiMaster::ErrorCodes::TransactionFailed) {
55- chipId = 0xFF ;
56- return false ;
57- }
58- if (twiMaster.Read (twiAddress, 0xA8 , &vendorId, 1 ) == TwiMaster::ErrorCodes::TransactionFailed) {
59- vendorId = 0xFF ;
60- return false ;
61- }
62- if (twiMaster.Read (twiAddress, 0xA9 , &fwVersion, 1 ) == TwiMaster::ErrorCodes::TransactionFailed) {
63- fwVersion = 0xFF ;
64- return false ;
65- }
66-
67- return chipId == 0xb4 && vendorId == 0 && fwVersion == 1 ;
65+ return true ;
6866}
6967
7068Cst816S::TouchInfos Cst816S::GetTouchInfo () {
@@ -79,18 +77,33 @@ Cst816S::TouchInfos Cst816S::GetTouchInfo() {
7977
8078 // This can only be 0 or 1
8179 uint8_t nbTouchPoints = touchData[touchPointNumIndex] & 0x0f ;
82-
8380 uint8_t xHigh = touchData[touchXHighIndex] & 0x0f ;
8481 uint8_t xLow = touchData[touchXLowIndex];
85- info.x = (xHigh << 8 ) | xLow;
86-
82+ uint16_t x = (xHigh << 8 ) | xLow;
8783 uint8_t yHigh = touchData[touchYHighIndex] & 0x0f ;
8884 uint8_t yLow = touchData[touchYLowIndex];
89- info.y = (yHigh << 8 ) | yLow;
85+ uint16_t y = (yHigh << 8 ) | yLow;
86+ Gestures gesture = static_cast <Gestures>(touchData[gestureIndex]);
87+
88+ // Validity check
89+ if (x >= maxX || y >= maxY ||
90+ (gesture != Gestures::None &&
91+ gesture != Gestures::SlideDown &&
92+ gesture != Gestures::SlideUp &&
93+ gesture != Gestures::SlideLeft &&
94+ gesture != Gestures::SlideRight &&
95+ gesture != Gestures::SingleTap &&
96+ gesture != Gestures::DoubleTap &&
97+ gesture != Gestures::LongPress)) {
98+ info.isValid = false ;
99+ return info;
100+ }
90101
102+ info.x = x;
103+ info.y = y;
91104 info.touching = (nbTouchPoints > 0 );
92- info.gesture = static_cast <Gestures>(touchData[gestureIndex]) ;
93-
105+ info.gesture = gesture ;
106+ info. isValid = true ;
94107 return info;
95108}
96109
@@ -108,3 +121,21 @@ void Cst816S::Wakeup() {
108121 Init ();
109122 NRF_LOG_INFO (" [TOUCHPANEL] Wakeup" );
110123}
124+
125+ bool Cst816S::CheckDeviceIds () {
126+ // There's mixed information about which register contains which information
127+ if (twiMaster.Read (twiAddress, 0xA7 , &chipId, 1 ) == TwiMaster::ErrorCodes::TransactionFailed) {
128+ chipId = 0xFF ;
129+ return false ;
130+ }
131+ if (twiMaster.Read (twiAddress, 0xA8 , &vendorId, 1 ) == TwiMaster::ErrorCodes::TransactionFailed) {
132+ vendorId = 0xFF ;
133+ return false ;
134+ }
135+ if (twiMaster.Read (twiAddress, 0xA9 , &fwVersion, 1 ) == TwiMaster::ErrorCodes::TransactionFailed) {
136+ fwVersion = 0xFF ;
137+ return false ;
138+ }
139+
140+ return chipId == 0xb4 && vendorId == 0 && fwVersion == 1 ;
141+ }
0 commit comments