Skip to content

Commit d9b31b7

Browse files
authored
Merge pull request #1 from ladyada/master
working!
2 parents 2be3bbc + 24b94fa commit d9b31b7

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

adafruit_touchscreen.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
from analogio import AnalogIn
2+
from digitalio import DigitalInOut, Direction, Pull
3+
4+
def map_range(x, in_min, in_max, out_min, out_max):
5+
"""
6+
Maps a number from one range to another.
7+
Note: This implementation handles values < in_min differently than arduino's map function does.
8+
:return: Returns value mapped to new range
9+
:rtype: float
10+
"""
11+
mapped = (x-in_min) * (out_max - out_min) / (in_max-in_min) + out_min
12+
if out_min <= out_max:
13+
return max(min(mapped, out_max), out_min)
14+
return min(max(mapped, out_max), out_min)
15+
16+
class Touchscreen:
17+
18+
def __init__(self, x1_pin, x2_pin, y1_pin, y2_pin, *,
19+
x_resistance=None, samples=4, z_threshhold=10000,
20+
calibration=None, size=None):
21+
"""Since we use the pins as both analog and digital, they must be pins
22+
not DigitalInOuts!"""
23+
self._xm_pin = x1_pin
24+
self._xp_pin = x2_pin
25+
self._ym_pin = y1_pin
26+
self._yp_pin = y2_pin
27+
self._rx_plate = x_resistance
28+
self._xsamples = [0] * samples
29+
self._ysamples = [0] * samples
30+
if not calibration:
31+
calibration = ((0, 65535), (0, 65535))
32+
self._calib = calibration
33+
self._size = size
34+
self._zthresh = z_threshhold
35+
36+
@property
37+
def touch_point(self):
38+
with DigitalInOut(self._yp_pin) as yp:
39+
with DigitalInOut(self._ym_pin) as ym:
40+
with AnalogIn(self._xp_pin) as xp:
41+
yp.switch_to_output(True)
42+
ym.switch_to_output(False)
43+
for i in range(len(self._xsamples)):
44+
self._xsamples[i] = xp.value
45+
x = sum(self._xsamples) / len(self._xsamples)
46+
x_size = 65535
47+
if self._size:
48+
x_size = self._size[0]
49+
x = int(map_range(x, self._calib[0][0], self._calib[0][1], 0, x_size))
50+
51+
with DigitalInOut(self._xp_pin) as xp:
52+
with DigitalInOut(self._xm_pin) as xm:
53+
with AnalogIn(self._yp_pin) as yp:
54+
xp.switch_to_output(True)
55+
xm.switch_to_output(False)
56+
for i in range(len(self._ysamples)):
57+
self._ysamples[i] = yp.value
58+
y = sum(self._ysamples) / len(self._ysamples)
59+
y_size = 65535
60+
if self._size:
61+
y_size = self._size[1]
62+
y = int(map_range(y, self._calib[1][0], self._calib[1][1], 0, y_size))
63+
64+
z1 = z2 = z = None
65+
with DigitalInOut(self._xp_pin) as xp:
66+
xp.switch_to_output(False)
67+
with DigitalInOut(self._ym_pin) as ym:
68+
ym.switch_to_output(True)
69+
with AnalogIn(self._xm_pin) as xm:
70+
z1 = xm.value
71+
with AnalogIn(self._yp_pin) as yp:
72+
z2 = yp.value
73+
#print(z1, z2)
74+
z = 65535 - (z2-z1)
75+
if z > self._zthresh:
76+
return (x, y, z)
77+
return None

examples/touchscreen_simpletest.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import adafruit_touchscreen
2+
import board
3+
4+
# These pins are used as both analog and digital! XR and YU must be analog
5+
# and digital capable. XL and YD just need to be digital
6+
ts = adafruit_touchscreen.Touchscreen(board.TOUCH_XL, board.TOUCH_XR,
7+
board.TOUCH_YD, board.TOUCH_YU,
8+
calibration=((5200, 59000), (5800, 57000)),
9+
size=(320,240))
10+
11+
while True:
12+
p = ts.touch_point
13+
if p:
14+
print(p)

0 commit comments

Comments
 (0)