Skip to content

Commit 755ab72

Browse files
authored
Merge pull request #811 from InfiniTimeOrg/cst816-add-validity-check
Add data validity check and retries in CST816S driver
2 parents 76c43eb + 8d61419 commit 755ab72

File tree

2 files changed

+58
-22
lines changed

2 files changed

+58
-22
lines changed

src/drivers/Cst816s.cpp

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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

7068
Cst816S::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+
}

src/drivers/Cst816s.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace Pinetime {
2121
uint16_t y = 0;
2222
Gestures gesture = Gestures::None;
2323
bool touching = false;
24-
bool isValid = true;
24+
bool isValid = false;
2525
};
2626

2727
Cst816S(TwiMaster& twiMaster, uint8_t twiAddress);
@@ -45,6 +45,8 @@ namespace Pinetime {
4545
return fwVersion;
4646
}
4747
private:
48+
bool CheckDeviceIds();
49+
4850
// Unused/Unavailable commented out
4951
static constexpr uint8_t gestureIndex = 1;
5052
static constexpr uint8_t touchPointNumIndex = 2;
@@ -58,6 +60,9 @@ namespace Pinetime {
5860
//static constexpr uint8_t touchXYIndex = 7;
5961
//static constexpr uint8_t touchMiscIndex = 8;
6062

63+
static constexpr uint8_t maxX = 240;
64+
static constexpr uint8_t maxY = 240;
65+
6166
TwiMaster& twiMaster;
6267
uint8_t twiAddress;
6368

0 commit comments

Comments
 (0)