Skip to content

Commit 83f86d1

Browse files
committed
initial library file
1 parent 0f5dee0 commit 83f86d1

File tree

1 file changed

+92
-4
lines changed

1 file changed

+92
-4
lines changed

adafruit_ft5336.py

Lines changed: 92 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
21
# SPDX-FileCopyrightText: Copyright (c) 2023 Liz Clark for Adafruit Industries
32
#
43
# SPDX-License-Identifier: MIT
@@ -27,11 +26,100 @@
2726
.. todo:: Uncomment or remove the Bus Device and/or the Register library dependencies
2827
based on the library's use of either.
2928
30-
# * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
31-
# * Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register
29+
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
30+
* Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register
3231
"""
3332

34-
# imports
33+
from adafruit_register.i2c_bits import ROBits
34+
from adafruit_bus_device.i2c_device import I2CDevice
35+
from micropython import const
36+
try:
37+
from typing import List
38+
except ImportError:
39+
pass
3540

3641
__version__ = "0.0.0+auto.0"
3742
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FT5336.git"
43+
44+
_DEFAULT_ADDR = const(0x38)
45+
_REG_VENDID = const(0xA3)
46+
_REG_CHIPID = const(0xA8)
47+
_VENDID = const(0x11)
48+
_CHIPID = const(0x79)
49+
_REG_NUMTOUCHES = const(0x02)
50+
_TD_STATUS = const(0x02)
51+
_TOUCH1_XH = const(0x03)
52+
_TOUCH1_XL = const(0x04)
53+
_TOUCH1_YH = const(0x05)
54+
_TOUCH1_YL = const(0x06)
55+
56+
class Adafruit_FT5336:
57+
# Define read-only register bits for vendor ID, chip ID, and number of touches.
58+
_vend_id = ROBits(8, _REG_VENDID, 0) # 8-bit read-only register for vendor ID
59+
_chip_id = ROBits(8, _REG_CHIPID, 0) # 8-bit read-only register for chip ID
60+
_num_touches = ROBits(8, _REG_NUMTOUCHES, 0) # 8-bit read-only register for number of touches
61+
62+
def __init__(self, i2c, i2c_addr: int = _DEFAULT_ADDR, max_touches: int = 5):
63+
"""
64+
Initializes the FT5336 touchscreen driver.
65+
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.
73+
"""
74+
self.i2c_device = I2CDevice(i2c, i2c_addr) # I2C device instance
75+
self.i2c_addr = i2c_addr # Store the I2C address
76+
self._touches = 0 # Number of current touches
77+
self.max_touches = max_touches # Maximum number of touches to track
78+
79+
# Initialize touch point arrays
80+
self._touchX: List[int] = [0] * self.max_touches
81+
self._touchY: List[int] = [0] * self.max_touches
82+
self._touchID: List[int] = [0] * self.max_touches
83+
84+
# Verify device identity by checking the vendor and chip IDs
85+
if self._vend_id != _VENDID:
86+
raise ValueError("Incorrect vendor ID")
87+
if self._chip_id != _CHIPID:
88+
raise ValueError("Incorrect chip ID")
89+
90+
@property
91+
def touched(self):
92+
n = self._num_touches
93+
return 0 if n > 5 else n
94+
95+
def _read_data(self):
96+
buffer = bytearray(32)
97+
with self.i2c_device as i2c:
98+
i2c.write_then_readinto(bytearray([0]), buffer, in_end=32)
99+
100+
self._touches = buffer[_TD_STATUS]
101+
if self._touches > 5 or self._touches == 0:
102+
self._touches = 0
103+
104+
for i in range(self._touches):
105+
self._touchX[i] = (buffer[_TOUCH1_XH + i * 6] & 0x0F) << 8 | buffer[_TOUCH1_XL + i * 6]
106+
self._touchY[i] = (buffer[_TOUCH1_YH + i * 6] & 0x0F) << 8 | buffer[_TOUCH1_YL + i * 6]
107+
self._touchID[i] = buffer[_TOUCH1_YH + i * 6] >> 4
108+
109+
@property
110+
def points(self):
111+
self._read_data()
112+
113+
points = []
114+
for i in range(min(self._touches, self.max_touches)):
115+
point = (self._touchX[i], self._touchY[i], 1) # 1 indicates touch is active
116+
points.append(point)
117+
118+
return points
119+
120+
def point(self, point_index: int):
121+
self._read_data()
122+
if self._touches == 0 or point_index >= self._touches:
123+
return (0, 0, 0)
124+
else:
125+
return (self._touchX[point_index], self._touchY[point_index], 1)

0 commit comments

Comments
 (0)