|
28 | 28 |
|
29 | 29 | # Create variables to store touch coordinates and state |
30 | 30 | x0, y0, x1, y1 = 0, 0, 0, 0 |
31 | | -touching = False |
| 31 | +touch_input = False |
32 | 32 |
|
33 | 33 | # Loop to continuously read touch input and draw on the image |
34 | 34 | 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 |
46 | 46 | 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() |
51 | 52 | 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 |
54 | 57 |
|
55 | | - # Draw a line if touching |
56 | | - if touching: |
| 58 | + # Draw a line if there was touch input |
| 59 | + if touch_input: |
57 | 60 | img = cv.line(img, (x0, y0), (x1, y1), (255, 255, 255), 2) |
58 | 61 |
|
59 | 62 | # Display the frame |
|
0 commit comments