Skip to content

Commit

Permalink
Merge pull request #6 from adafruit/axis_swap
Browse files Browse the repository at this point in the history
adding axis swap and invert
  • Loading branch information
makermelissa authored Aug 1, 2024
2 parents 93cfa5b + 3f494c1 commit c4d6aaa
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 11 deletions.
72 changes: 64 additions & 8 deletions adafruit_ft5336.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

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

# pylint: disable=too-many-instance-attributes
_DEFAULT_ADDR = const(0x38)
_REG_VENDID = const(0xA3)
_REG_CHIPID = const(0xA8)
Expand All @@ -49,6 +49,8 @@
_TOUCH1_XL = const(0x04)
_TOUCH1_YH = const(0x05)
_TOUCH1_YL = const(0x06)
_SCREEN_WIDTH = 320
_SCREEN_HEIGHT = 480


class Adafruit_FT5336:
Expand All @@ -61,13 +63,23 @@ class Adafruit_FT5336:
8, _REG_NUMTOUCHES, 0
) # 8-bit read-only register for number of touches

# pylint: disable=too-many-arguments
def __init__(
self, i2c, i2c_addr: int = _DEFAULT_ADDR, max_touches: int = 5
self,
i2c,
i2c_addr: int = _DEFAULT_ADDR,
max_touches: int = 5,
invert_x: bool = False,
invert_y: bool = False,
swap_xy: bool = False,
) -> None:
"""Initialization over I2C
:param int i2c_addr: I2C address (default 0x38)
:param int max_touches: Maximum number of touch points to track. Defaults to 5.
:param bool invert_x: Invert X axis. Defaults to False.
:param bool invert_y: Invert Y axis. Defaults to False.
:param bool swap_xy: Swap X and Y axes. Defaults to False.
"""
self.i2c_device = I2CDevice(i2c, i2c_addr) # I2C device instance
self.i2c_addr = i2c_addr # Store the I2C address
Expand All @@ -79,6 +91,15 @@ def __init__(
self._touch_y: List[int] = [0] * self.max_touches
self._touch_id: List[int] = [0] * self.max_touches

# Inversion and swap properties
self._invert_x = invert_x
self._invert_y = invert_y
self._swap_xy = swap_xy

# Set screen dimensions
self._screen_width = _SCREEN_WIDTH
self._screen_height = _SCREEN_HEIGHT

# Verify device identity by checking the vendor and chip IDs
if self._vend_id != _VENDID:
raise ValueError("Incorrect vendor ID")
Expand All @@ -95,12 +116,20 @@ def _read_data(self):
self._touches = 0

for i in range(self._touches):
self._touch_x[i] = (buffer[_TOUCH1_XH + i * 6] & 0x0F) << 8 | buffer[
_TOUCH1_XL + i * 6
]
self._touch_y[i] = (buffer[_TOUCH1_YH + i * 6] & 0x0F) << 8 | buffer[
_TOUCH1_YL + i * 6
]
x = (buffer[_TOUCH1_XH + i * 6] & 0x0F) << 8 | buffer[_TOUCH1_XL + i * 6]
y = (buffer[_TOUCH1_YH + i * 6] & 0x0F) << 8 | buffer[_TOUCH1_YL + i * 6]

if self._invert_x:
x = self._screen_width - 1 - x

if self._invert_y:
y = self._screen_height - 1 - y

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

self._touch_x[i] = x
self._touch_y[i] = y
self._touch_id[i] = buffer[_TOUCH1_YH + i * 6] >> 4

@property
Expand Down Expand Up @@ -141,3 +170,30 @@ def point(self, point_index: int) -> Tuple:
else:
value = (self._touch_x[point_index], self._touch_y[point_index], 1)
return value

@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
6 changes: 3 additions & 3 deletions examples/buttons_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
tft_dc = board.D10

display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs)
# display is rotated to align x, y with touch screen x, y
display = HX8357(display_bus, width=320, height=480, rotation=270)
display = HX8357(display_bus, width=480, height=320, rotation=0)

i2c = board.I2C() # uses board.SCL and board.SDA
touch = adafruit_ft5336.Adafruit_FT5336(i2c)
# touch coordinates are adjusted to match display with 0 rotation
touch = adafruit_ft5336.Adafruit_FT5336(i2c, invert_x=True, swap_xy=True)

splash = displayio.Group()
display.root_group = splash
Expand Down

0 comments on commit c4d6aaa

Please sign in to comment.