|
| 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 |
0 commit comments