Skip to content

Commit 8cad950

Browse files
committed
Add initial touch screen example
1 parent 808dd8d commit 8cad950

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

examples/ex04_touch_screen.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Import OpenCV and hardware initialization module
2+
import cv2
3+
from cv2_hardware_init import *
4+
5+
# Import NumPy
6+
from ulab import numpy as np
7+
8+
# Initialize an image to draw on
9+
img = np.zeros((240, 320, 3), dtype=np.uint8)
10+
11+
# Prompt the user to draw on the screen
12+
img = cv2.putText(img, "Touch to draw!", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
13+
14+
# Prompt the user to press a key to continue
15+
print("Press any key to continue")
16+
17+
# Create variables to store touch coordinates and state
18+
x0, y0, x1, y1 = 0, 0, 0, 0
19+
touching = False
20+
21+
# Loop to continuously read touch input and draw on the image
22+
while True:
23+
# Read touch input
24+
x, y, touch_num = touch_screen.get_touch()
25+
26+
# Update the touch coordinates and state
27+
if touch_num > 0:
28+
if not touching:
29+
x0 = x
30+
y0 = y
31+
x1 = x
32+
y1 = y
33+
touching = True
34+
else:
35+
x0 = x1
36+
y0 = y1
37+
x1 = x
38+
y1 = y
39+
else:
40+
if touching:
41+
touching = False
42+
43+
# Draw a line if touching
44+
if touching:
45+
img = cv2.line(img, (x0, y0), (x1, y1), (255, 255, 255), 2)
46+
47+
# Display the frame
48+
display.imshow(img)
49+
50+
# Check for key presses
51+
key = cv2.waitKey(1)
52+
53+
# If any key is pressed, exit the loop
54+
if key != -1:
55+
break

0 commit comments

Comments
 (0)