Skip to content

Commit 9c875a0

Browse files
committed
Clean up touch screen example
Also change CST816 driver to have is_tocuhed() method separate from get_touch_xy()
1 parent dc112ba commit 9c875a0

File tree

2 files changed

+38
-23
lines changed

2 files changed

+38
-23
lines changed

cv2_drivers/touch_screens/cst816.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,22 @@ def is_connected(self):
7070
def getChipID(self):
7171
return self.read_register_value(self._REG_CHIP_ID)
7272

73-
def get_touch(self):
73+
def is_touched(self):
74+
"""
75+
Check if the touch screen is currently being touched.
76+
77+
Returns:
78+
bool: True if touching, False otherwise
79+
"""
80+
# Read the number of touches
81+
touch_num = self.read_register_value(self._REG_FINGER_NUM)
82+
83+
# If there are any touches, return True
84+
return touch_num > 0
85+
86+
def get_touch_xy(self):
7487
x = self.read_register_value(self._REG_X_POS_H, 2) & 0x0FFF
7588
y = self.read_register_value(self._REG_Y_POS_H, 2) & 0x0FFF
76-
touch_num = self.read_register_value(self._REG_FINGER_NUM)
7789

7890
# Adjust for the rotation
7991
if self.rotation == 0:
@@ -85,7 +97,7 @@ def get_touch(self):
8597
elif self.rotation == 3:
8698
x,y = self.height - y, x
8799

88-
return (x, y, touch_num)
100+
return (x, y)
89101

90102
def read_register_value(self, reg, num_bytes=1):
91103
"""

examples/ex03_touch_screen.py

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,35 @@
2828

2929
# Create variables to store touch coordinates and state
3030
x0, y0, x1, y1 = 0, 0, 0, 0
31-
touching = False
31+
touch_input = False
3232

3333
# Loop to continuously read touch input and draw on the image
3434
while True:
35-
# Read touch input
36-
x, y, touch_num = touch_screen.get_touch()
37-
38-
# Update the touch coordinates and state
39-
if touch_num > 0:
40-
if not touching:
41-
x0 = x
42-
y0 = y
43-
x1 = x
44-
y1 = y
45-
touching = True
35+
# Check if there is touch input
36+
if touch_screen.is_touched():
37+
# Check if this is the first touch or a continuation
38+
if not touch_input:
39+
# This is the first touch, set both (x0, y0) and (x1, y1) to the
40+
# initial touch coordinates. This will draw a point at the touch
41+
# location if no further touch inputs are made
42+
x0, y0 = touch_screen.get_touch_xy()
43+
x1, y1 = x0, y0
44+
# Set the state to indicate there is touch input
45+
touch_input = True
4646
else:
47-
x0 = x1
48-
y0 = y1
49-
x1 = x
50-
y1 = y
47+
# This is a continuation of the touch, set (x0, y0) to the previous
48+
# coordinates and set (x1, y1) to the current touch coordinates so
49+
# we can draw a line between them
50+
x0, y0 = x1, y1
51+
x1, y1 = touch_screen.get_touch_xy()
5152
else:
52-
if touching:
53-
touching = False
53+
# Check if there was touch input before
54+
if touch_input:
55+
# There was touch input before, but not any more
56+
touch_input = False
5457

55-
# Draw a line if touching
56-
if touching:
58+
# Draw a line if there was touch input
59+
if touch_input:
5760
img = cv.line(img, (x0, y0), (x1, y1), (255, 255, 255), 2)
5861

5962
# Display the frame

0 commit comments

Comments
 (0)