Skip to content

Commit 9483f9a

Browse files
authored
Merge pull request #6 from makermelissa/main
Add options to invert and swap axes plus add typing
2 parents da8fb1d + fd0c808 commit 9483f9a

File tree

3 files changed

+101
-2
lines changed

3 files changed

+101
-2
lines changed

adafruit_tsc2007.py

+60-2
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

docs/examples.rst

+9
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,12 @@ Ensure your device works with this simple test.
66
.. literalinclude:: ../examples/tsc2007_simpletest.py
77
:caption: examples/tsc2007_simpletest.py
88
:linenos:
9+
10+
Display Specific test
11+
----------------------
12+
13+
Initialize a display and set options so that axes are correct to rotation.
14+
15+
.. literalinclude:: ../examples/tsc2007_3.5_feather_v2.py
16+
:caption: examples/tsc2007_3.5_feather_v2.py
17+
:linenos:

examples/tsc2007_3.5_feather_v2.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2024 Melissa LeBlanc-Williams for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
5+
6+
import board
7+
import displayio
8+
from adafruit_hx8357 import HX8357
9+
import adafruit_tsc2007
10+
11+
# Initialize the Display
12+
displayio.release_displays()
13+
14+
spi = board.SPI()
15+
tft_cs = board.D9
16+
tft_dc = board.D10
17+
18+
display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs)
19+
display = HX8357(display_bus, width=480, height=320)
20+
21+
# Use for I2C
22+
i2c = board.I2C() # uses board.SCL and board.SDA
23+
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
24+
25+
tsc = adafruit_tsc2007.TSC2007(i2c, invert_x=True, swap_xy=True)
26+
27+
while True:
28+
if tsc.touched:
29+
point = tsc.touch
30+
if point["pressure"] < 100: # ignore touches with no 'pressure' as false
31+
continue
32+
print("Touchpoint: (%d, %d, %d)" % (point["x"], point["y"], point["pressure"]))

0 commit comments

Comments
 (0)