Skip to content

Commit bc2f185

Browse files
committed
Add options to invert and swap axes plus add typing
1 parent da8fb1d commit bc2f185

File tree

1 file changed

+60
-2
lines changed

1 file changed

+60
-2
lines changed

adafruit_tsc2007.py

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@
3232
import digitalio
3333
from adafruit_bus_device import i2c_device
3434

35+
try:
36+
# Used only for typing
37+
from typing import Union
38+
import busio
39+
except ImportError:
40+
pass
41+
3542
__version__ = "0.0.0+auto.0"
3643
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_TSC2007.git"
3744

@@ -60,16 +67,31 @@ class TSC2007:
6067
A driver for the TSC2007 resistive touch sensor.
6168
"""
6269

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+
):
6480
self._i2c = i2c_device.I2CDevice(i2c, address)
6581
self._irq = irq
6682
if self._irq:
6783
self._irq.switch_to_input(pull=digitalio.Pull.UP)
6884
self._buf = bytearray(2)
6985
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+
7092
self.touch # pylint: disable=pointless-statement
7193

72-
def command(self, function, power, resolution) -> int:
94+
def command(self, function: int, power: int, resolution: int) -> int:
7395
"""
7496
Write a command byte to the TSC2007 and read the 2-byte response
7597
"""
@@ -105,5 +127,41 @@ def touch(self) -> dict:
105127
z = self.command(TSC2007_MEASURE_Z1, TSC2007_ADON_IRQOFF, TSC2007_ADC_12BIT)
106128
self.command(TSC2007_MEASURE_TEMP0, TSC2007_POWERDOWN_IRQON, TSC2007_ADC_12BIT)
107129

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+
108139
point = {"x": x, "y": y, "pressure": z}
109140
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

Comments
 (0)