Skip to content

Commit c4d6aaa

Browse files
authored
Merge pull request #6 from adafruit/axis_swap
adding axis swap and invert
2 parents 93cfa5b + 3f494c1 commit c4d6aaa

File tree

2 files changed

+67
-11
lines changed

2 files changed

+67
-11
lines changed

adafruit_ft5336.py

+64-8
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
__version__ = "0.0.0+auto.0"
3939
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FT5336.git"
40-
40+
# pylint: disable=too-many-instance-attributes
4141
_DEFAULT_ADDR = const(0x38)
4242
_REG_VENDID = const(0xA3)
4343
_REG_CHIPID = const(0xA8)
@@ -49,6 +49,8 @@
4949
_TOUCH1_XL = const(0x04)
5050
_TOUCH1_YH = const(0x05)
5151
_TOUCH1_YL = const(0x06)
52+
_SCREEN_WIDTH = 320
53+
_SCREEN_HEIGHT = 480
5254

5355

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

66+
# pylint: disable=too-many-arguments
6467
def __init__(
65-
self, i2c, i2c_addr: int = _DEFAULT_ADDR, max_touches: int = 5
68+
self,
69+
i2c,
70+
i2c_addr: int = _DEFAULT_ADDR,
71+
max_touches: int = 5,
72+
invert_x: bool = False,
73+
invert_y: bool = False,
74+
swap_xy: bool = False,
6675
) -> None:
6776
"""Initialization over I2C
6877
6978
:param int i2c_addr: I2C address (default 0x38)
7079
:param int max_touches: Maximum number of touch points to track. Defaults to 5.
80+
:param bool invert_x: Invert X axis. Defaults to False.
81+
:param bool invert_y: Invert Y axis. Defaults to False.
82+
:param bool swap_xy: Swap X and Y axes. Defaults to False.
7183
"""
7284
self.i2c_device = I2CDevice(i2c, i2c_addr) # I2C device instance
7385
self.i2c_addr = i2c_addr # Store the I2C address
@@ -79,6 +91,15 @@ def __init__(
7991
self._touch_y: List[int] = [0] * self.max_touches
8092
self._touch_id: List[int] = [0] * self.max_touches
8193

94+
# Inversion and swap properties
95+
self._invert_x = invert_x
96+
self._invert_y = invert_y
97+
self._swap_xy = swap_xy
98+
99+
# Set screen dimensions
100+
self._screen_width = _SCREEN_WIDTH
101+
self._screen_height = _SCREEN_HEIGHT
102+
82103
# Verify device identity by checking the vendor and chip IDs
83104
if self._vend_id != _VENDID:
84105
raise ValueError("Incorrect vendor ID")
@@ -95,12 +116,20 @@ def _read_data(self):
95116
self._touches = 0
96117

97118
for i in range(self._touches):
98-
self._touch_x[i] = (buffer[_TOUCH1_XH + i * 6] & 0x0F) << 8 | buffer[
99-
_TOUCH1_XL + i * 6
100-
]
101-
self._touch_y[i] = (buffer[_TOUCH1_YH + i * 6] & 0x0F) << 8 | buffer[
102-
_TOUCH1_YL + i * 6
103-
]
119+
x = (buffer[_TOUCH1_XH + i * 6] & 0x0F) << 8 | buffer[_TOUCH1_XL + i * 6]
120+
y = (buffer[_TOUCH1_YH + i * 6] & 0x0F) << 8 | buffer[_TOUCH1_YL + i * 6]
121+
122+
if self._invert_x:
123+
x = self._screen_width - 1 - x
124+
125+
if self._invert_y:
126+
y = self._screen_height - 1 - y
127+
128+
if self._swap_xy:
129+
x, y = y, x
130+
131+
self._touch_x[i] = x
132+
self._touch_y[i] = y
104133
self._touch_id[i] = buffer[_TOUCH1_YH + i * 6] >> 4
105134

106135
@property
@@ -141,3 +170,30 @@ def point(self, point_index: int) -> Tuple:
141170
else:
142171
value = (self._touch_x[point_index], self._touch_y[point_index], 1)
143172
return value
173+
174+
@property
175+
def invert_x(self) -> bool:
176+
"""Whether the X axis is inverted"""
177+
return self._invert_x
178+
179+
@invert_x.setter
180+
def invert_x(self, value: bool):
181+
self._invert_x = value
182+
183+
@property
184+
def invert_y(self) -> bool:
185+
"""Whether the Y axis is inverted"""
186+
return self._invert_y
187+
188+
@invert_y.setter
189+
def invert_y(self, value: bool):
190+
self._invert_y = value
191+
192+
@property
193+
def swap_xy(self) -> bool:
194+
"""Whether the X and Y axes are swapped"""
195+
return self._swap_xy
196+
197+
@swap_xy.setter
198+
def swap_xy(self, value: bool):
199+
self._swap_xy = value

examples/buttons_example.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
tft_dc = board.D10
2525

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

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

3333
splash = displayio.Group()
3434
display.root_group = splash

0 commit comments

Comments
 (0)