Skip to content

Commit 52e6386

Browse files
authored
Merge pull request #18 from CedarGroveStudios/main
Eliminate race condition to improve coordinate accuracy and reliability.
2 parents 304aa0b + bc3cd0f commit 52e6386

File tree

1 file changed

+33
-32
lines changed

1 file changed

+33
-32
lines changed

adafruit_touchscreen.py

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ class Touchscreen:
6262
:attr:`calibration` is a tuple of two tuples, the default is
6363
((0, 65535), (0, 65535)). The numbers are the min/max readings for the
6464
X and Y coordinate planes, respectively. To figure these out, pass in
65-
no calibration value and read the raw values out while touching the
66-
panel.
65+
no calibration value or size value and read the raw minimum and maximum
66+
values out while touching the panel.
6767
:attr:`size` is a tuple that gives the X and Y pixel size of the underlying
6868
screen. If passed in, we will automatically scale/rotate so touches
6969
correspond to the graphical coordinate system.
@@ -108,6 +108,7 @@ def __init__(
108108
self._rx_plate = x_resistance
109109
self._xsamples = [0] * samples
110110
self._ysamples = [0] * samples
111+
self._zsamples = [0] * samples
111112
if not calibration:
112113
calibration = ((0, 65535), (0, 65535))
113114
self._calib = calibration
@@ -118,36 +119,6 @@ def __init__(
118119
def touch_point(self): # pylint: disable=too-many-locals
119120
"""A tuple that represents the x, y and z (touch pressure) coordinates
120121
of a touch. Or, None if no touch is detected"""
121-
with DigitalInOut(self._yp_pin) as y_p:
122-
with DigitalInOut(self._ym_pin) as y_m:
123-
with AnalogIn(self._xp_pin) as x_p:
124-
y_p.switch_to_output(True)
125-
y_m.switch_to_output(False)
126-
for i in range( # pylint: disable=consider-using-enumerate
127-
len(self._xsamples)
128-
):
129-
self._xsamples[i] = x_p.value
130-
x = sum(self._xsamples) / len(self._xsamples)
131-
x_size = 65535
132-
if self._size:
133-
x_size = self._size[0]
134-
x = int(map_range(x, self._calib[0][0], self._calib[0][1], 0, x_size))
135-
136-
with DigitalInOut(self._xp_pin) as x_p:
137-
with DigitalInOut(self._xm_pin) as x_m:
138-
with AnalogIn(self._yp_pin) as y_p:
139-
x_p.switch_to_output(True)
140-
x_m.switch_to_output(False)
141-
for i in range( # pylint: disable=consider-using-enumerate
142-
len(self._ysamples)
143-
):
144-
self._ysamples[i] = y_p.value
145-
y = sum(self._ysamples) / len(self._ysamples)
146-
y_size = 65535
147-
if self._size:
148-
y_size = self._size[1]
149-
y = int(map_range(y, self._calib[1][0], self._calib[1][1], 0, y_size))
150-
151122
z_1 = z_2 = z = None
152123
with DigitalInOut(self._xp_pin) as x_p:
153124
x_p.switch_to_output(False)
@@ -160,5 +131,35 @@ def touch_point(self): # pylint: disable=too-many-locals
160131
# print(z_1, z_2)
161132
z = 65535 - (z_2 - z_1)
162133
if z > self._zthresh:
134+
with DigitalInOut(self._yp_pin) as y_p:
135+
with DigitalInOut(self._ym_pin) as y_m:
136+
with AnalogIn(self._xp_pin) as x_p:
137+
y_p.switch_to_output(True)
138+
y_m.switch_to_output(False)
139+
for i in range( # pylint: disable=consider-using-enumerate
140+
len(self._xsamples)
141+
):
142+
self._xsamples[i] = x_p.value
143+
x = sum(self._xsamples) / len(self._xsamples)
144+
x_size = 65535
145+
if self._size:
146+
x_size = self._size[0]
147+
x = int(map_range(x, self._calib[0][0], self._calib[0][1], 0, x_size))
148+
149+
with DigitalInOut(self._xp_pin) as x_p:
150+
with DigitalInOut(self._xm_pin) as x_m:
151+
with AnalogIn(self._yp_pin) as y_p:
152+
x_p.switch_to_output(True)
153+
x_m.switch_to_output(False)
154+
for i in range( # pylint: disable=consider-using-enumerate
155+
len(self._ysamples)
156+
):
157+
self._ysamples[i] = y_p.value
158+
y = sum(self._ysamples) / len(self._ysamples)
159+
y_size = 65535
160+
if self._size:
161+
y_size = self._size[1]
162+
y = int(map_range(y, self._calib[1][0], self._calib[1][1], 0, y_size))
163+
163164
return (x, y, z)
164165
return None

0 commit comments

Comments
 (0)