Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options to invert and swap axes plus add typing #6

Merged
merged 2 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 60 additions & 2 deletions adafruit_tsc2007.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
import digitalio
from adafruit_bus_device import i2c_device

try:
# Used only for typing
from typing import Union
import busio
except ImportError:
pass

__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_TSC2007.git"

Expand Down Expand Up @@ -60,16 +67,31 @@ class TSC2007:
A driver for the TSC2007 resistive touch sensor.
"""

def __init__(self, i2c, address=0x48, irq=None):
# pylint: disable=too-many-arguments
def __init__(
self,
i2c: busio.I2C,
address: int = 0x48,
irq: Union[int | None] = None,
invert_x: bool = False,
invert_y: bool = False,
swap_xy: bool = False,
):
self._i2c = i2c_device.I2CDevice(i2c, address)
self._irq = irq
if self._irq:
self._irq.switch_to_input(pull=digitalio.Pull.UP)
self._buf = bytearray(2)
self._cmd = bytearray(1)

# Settable Properties
self._invert_x = invert_x
self._invert_y = invert_y
self._swap_xy = swap_xy

self.touch # pylint: disable=pointless-statement

def command(self, function, power, resolution) -> int:
def command(self, function: int, power: int, resolution: int) -> int:
"""
Write a command byte to the TSC2007 and read the 2-byte response
"""
Expand Down Expand Up @@ -105,5 +127,41 @@ def touch(self) -> dict:
z = self.command(TSC2007_MEASURE_Z1, TSC2007_ADON_IRQOFF, TSC2007_ADC_12BIT)
self.command(TSC2007_MEASURE_TEMP0, TSC2007_POWERDOWN_IRQON, TSC2007_ADC_12BIT)

if self._invert_x:
x = 4095 - x

if self._invert_y:
y = 4095 - y

if self._swap_xy:
x, y = y, x

point = {"x": x, "y": y, "pressure": z}
return point

@property
def invert_x(self) -> bool:
"""Whether the X axis is inverted"""
return self._invert_x

@invert_x.setter
def invert_x(self, value: bool):
self._invert_x = value

@property
def invert_y(self) -> bool:
"""Whether the Y axis is inverted"""
return self._invert_y

@invert_y.setter
def invert_y(self, value: bool):
self._invert_y = value

@property
def swap_xy(self) -> bool:
"""Whether the X and Y axes are swapped"""
return self._swap_xy

@swap_xy.setter
def swap_xy(self, value: bool):
self._swap_xy = value
9 changes: 9 additions & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,12 @@ Ensure your device works with this simple test.
.. literalinclude:: ../examples/tsc2007_simpletest.py
:caption: examples/tsc2007_simpletest.py
:linenos:

Display Specific test
----------------------

Initialize a display and set options so that axes are correct to rotation.

.. literalinclude:: ../examples/tsc2007_3.5_feather_v2.py
:caption: examples/tsc2007_3.5_feather_v2.py
:linenos:
32 changes: 32 additions & 0 deletions examples/tsc2007_3.5_feather_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# SPDX-FileCopyrightText: Copyright (c) 2024 Melissa LeBlanc-Williams for Adafruit Industries
#
# SPDX-License-Identifier: Unlicense


import board
import displayio
from adafruit_hx8357 import HX8357
import adafruit_tsc2007

# Initialize the Display
displayio.release_displays()

spi = board.SPI()
tft_cs = board.D9
tft_dc = board.D10

display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs)
display = HX8357(display_bus, width=480, height=320)

# Use for I2C
i2c = board.I2C() # uses board.SCL and board.SDA
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller

tsc = adafruit_tsc2007.TSC2007(i2c, invert_x=True, swap_xy=True)

while True:
if tsc.touched:
point = tsc.touch
if point["pressure"] < 100: # ignore touches with no 'pressure' as false
continue
print("Touchpoint: (%d, %d, %d)" % (point["x"], point["y"], point["pressure"]))
Loading