|
32 | 32 | import digitalio
|
33 | 33 | from adafruit_bus_device import i2c_device
|
34 | 34 |
|
| 35 | +try: |
| 36 | + # Used only for typing |
| 37 | + from typing import Union |
| 38 | + import busio |
| 39 | +except ImportError: |
| 40 | + pass |
| 41 | + |
35 | 42 | __version__ = "0.0.0+auto.0"
|
36 | 43 | __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_TSC2007.git"
|
37 | 44 |
|
@@ -60,16 +67,31 @@ class TSC2007:
|
60 | 67 | A driver for the TSC2007 resistive touch sensor.
|
61 | 68 | """
|
62 | 69 |
|
63 |
| - def __init__(self, i2c, address=0x48, irq=None): |
| 70 | + # pylint: disable=too-many-arguments |
| 71 | + def __init__( |
| 72 | + self, |
| 73 | + i2c: busio.I2C, |
| 74 | + address: int = 0x48, |
| 75 | + irq: Union[int | None] = None, |
| 76 | + invert_x: bool = False, |
| 77 | + invert_y: bool = False, |
| 78 | + swap_xy: bool = False, |
| 79 | + ): |
64 | 80 | self._i2c = i2c_device.I2CDevice(i2c, address)
|
65 | 81 | self._irq = irq
|
66 | 82 | if self._irq:
|
67 | 83 | self._irq.switch_to_input(pull=digitalio.Pull.UP)
|
68 | 84 | self._buf = bytearray(2)
|
69 | 85 | self._cmd = bytearray(1)
|
| 86 | + |
| 87 | + # Settable Properties |
| 88 | + self._invert_x = invert_x |
| 89 | + self._invert_y = invert_y |
| 90 | + self._swap_xy = swap_xy |
| 91 | + |
70 | 92 | self.touch # pylint: disable=pointless-statement
|
71 | 93 |
|
72 |
| - def command(self, function, power, resolution) -> int: |
| 94 | + def command(self, function: int, power: int, resolution: int) -> int: |
73 | 95 | """
|
74 | 96 | Write a command byte to the TSC2007 and read the 2-byte response
|
75 | 97 | """
|
@@ -105,5 +127,41 @@ def touch(self) -> dict:
|
105 | 127 | z = self.command(TSC2007_MEASURE_Z1, TSC2007_ADON_IRQOFF, TSC2007_ADC_12BIT)
|
106 | 128 | self.command(TSC2007_MEASURE_TEMP0, TSC2007_POWERDOWN_IRQON, TSC2007_ADC_12BIT)
|
107 | 129 |
|
| 130 | + if self._invert_x: |
| 131 | + x = 4095 - x |
| 132 | + |
| 133 | + if self._invert_y: |
| 134 | + y = 4095 - y |
| 135 | + |
| 136 | + if self._swap_xy: |
| 137 | + x, y = y, x |
| 138 | + |
108 | 139 | point = {"x": x, "y": y, "pressure": z}
|
109 | 140 | return point
|
| 141 | + |
| 142 | + @property |
| 143 | + def invert_x(self) -> bool: |
| 144 | + """Whether the X axis is inverted""" |
| 145 | + return self._invert_x |
| 146 | + |
| 147 | + @invert_x.setter |
| 148 | + def invert_x(self, value: bool): |
| 149 | + self._invert_x = value |
| 150 | + |
| 151 | + @property |
| 152 | + def invert_y(self) -> bool: |
| 153 | + """Whether the Y axis is inverted""" |
| 154 | + return self._invert_y |
| 155 | + |
| 156 | + @invert_y.setter |
| 157 | + def invert_y(self, value: bool): |
| 158 | + self._invert_y = value |
| 159 | + |
| 160 | + @property |
| 161 | + def swap_xy(self) -> bool: |
| 162 | + """Whether the X and Y axes are swapped""" |
| 163 | + return self._swap_xy |
| 164 | + |
| 165 | + @swap_xy.setter |
| 166 | + def swap_xy(self, value: bool): |
| 167 | + self._swap_xy = value |
0 commit comments