Skip to content

Commit 8168409

Browse files
committed
Update adafruit_ft5336.py
1 parent e972374 commit 8168409

File tree

1 file changed

+33
-27
lines changed

1 file changed

+33
-27
lines changed

adafruit_ft5336.py

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
`adafruit_ft5336`
66
================================================================================
77
8-
Touchscreen driver for the FT5336 touch controller
8+
CircuitPython driver for the FT5336 touch screen controller
99
1010
1111
* Author(s): Liz Clark
@@ -15,17 +15,13 @@
1515
1616
**Hardware:**
1717
18-
.. todo:: Add links to any specific hardware product page(s), or category page(s).
19-
Use unordered list & hyperlink rST inline format: "* `Link Text <url>`_"
18+
* `Adafruit 3.5" TFT 320x480 with Capacitive Touch Breakout: <https://adafruit.com/product/5846>`_
2019
2120
**Software and Dependencies:**
2221
2322
* Adafruit CircuitPython firmware for the supported boards:
2423
https://circuitpython.org/downloads
2524
26-
.. todo:: Uncomment or remove the Bus Device and/or the Register library dependencies
27-
based on the library's use of either.
28-
2925
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
3026
* Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register
3127
"""
@@ -34,7 +30,7 @@
3430
from adafruit_bus_device.i2c_device import I2CDevice
3531
from micropython import const
3632
try:
37-
from typing import List
33+
from typing import List, Tuple
3834
except ImportError:
3935
pass
4036

@@ -54,22 +50,17 @@
5450
_TOUCH1_YL = const(0x06)
5551

5652
class Adafruit_FT5336:
53+
"""Adafruit FT5336 touch screen driver"""
5754
# Define read-only register bits for vendor ID, chip ID, and number of touches.
5855
_vend_id = ROBits(8, _REG_VENDID, 0) # 8-bit read-only register for vendor ID
5956
_chip_id = ROBits(8, _REG_CHIPID, 0) # 8-bit read-only register for chip ID
6057
_num_touches = ROBits(8, _REG_NUMTOUCHES, 0) # 8-bit read-only register for number of touches
6158

62-
def __init__(self, i2c, i2c_addr: int = _DEFAULT_ADDR, max_touches: int = 5):
63-
"""
64-
Initializes the FT5336 touchscreen driver.
59+
def __init__(self, i2c, i2c_addr: int = _DEFAULT_ADDR, max_touches: int = 5) -> None:
60+
"""Initialization over I2C
6561
66-
Args:
67-
i2c: The I2C bus object.
68-
i2c_addr (int): The I2C address of the device. Defaults to _DEFAULT_ADDR.
69-
max_touches (int): Maximum number of touch points to track. Defaults to 5.
70-
71-
Raises:
72-
ValueError: If the detected vendor ID or chip ID does not match the expected values.
62+
:param int i2c_addr: I2C address (default 0x38)
63+
:param int max_touches: Maximum number of touch points to track. Defaults to 5.
7364
"""
7465
self.i2c_device = I2CDevice(i2c, i2c_addr) # I2C device instance
7566
self.i2c_addr = i2c_addr # Store the I2C address
@@ -87,37 +78,52 @@ def __init__(self, i2c, i2c_addr: int = _DEFAULT_ADDR, max_touches: int = 5):
8778
if self._chip_id != _CHIPID:
8879
raise ValueError("Incorrect chip ID")
8980

90-
@property
91-
def touched(self):
92-
n = self._num_touches
93-
return 0 if n > 5 else n
94-
9581
def _read_data(self):
9682
buffer = bytearray(32)
9783
with self.i2c_device as i2c:
9884
i2c.write_then_readinto(bytearray([0]), buffer, in_end=32)
9985

10086
self._touches = buffer[_TD_STATUS]
101-
if self._touches > 5 or self._touches == 0:
87+
if self._touches > self.max_touches or self._touches == 0:
10288
self._touches = 0
10389

10490
for i in range(self._touches):
10591
self._touchX[i] = (buffer[_TOUCH1_XH + i * 6] & 0x0F) << 8 | buffer[_TOUCH1_XL + i * 6]
10692
self._touchY[i] = (buffer[_TOUCH1_YH + i * 6] & 0x0F) << 8 | buffer[_TOUCH1_YL + i * 6]
10793
self._touchID[i] = buffer[_TOUCH1_YH + i * 6] >> 4
94+
95+
@property
96+
def touched(self) -> int:
97+
"""Count of touch inputs detected
98+
99+
:return: Count of touch inputs detected (0-max_touches)
100+
:rtype: int
101+
"""
102+
n = self._num_touches
103+
return 0 if n > self.max_touches else n
108104

109105
@property
110-
def points(self):
106+
def points(self) -> List:
107+
"""X, Y and Z values from each available touch input
108+
109+
:return: X, Y and Z values in a list
110+
:rtype: List
111+
"""
111112
self._read_data()
112-
113113
points = []
114114
for i in range(min(self._touches, self.max_touches)):
115-
point = (self._touchX[i], self._touchY[i], 1) # 1 indicates touch is active
115+
point = (self._touchX[i], self._touchY[i], 1)
116116
points.append(point)
117117

118118
return points
119119

120-
def point(self, point_index: int):
120+
def point(self, point_index: int) -> Tuple:
121+
"""X, Y and Z value from a specified touch input
122+
123+
:param int point_index: Touch input to read (0 - max_touches)
124+
:return: X, Y and Z values
125+
:rtype: Tuple
126+
"""
121127
self._read_data()
122128
if self._touches == 0 or point_index >= self._touches:
123129
return (0, 0, 0)

0 commit comments

Comments
 (0)