Skip to content

Commit c783533

Browse files
authored
Merge pull request #1 from BlitzCityDIY/main
Adding library, example and docs
2 parents 0f5dee0 + 3b07e32 commit c783533

File tree

6 files changed

+158
-38
lines changed

6 files changed

+158
-38
lines changed

README.rst

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Introduction
2121
:target: https://github.com/psf/black
2222
:alt: Code Style: Black
2323

24-
Touchscreen driver for the FT5336 touch controller
24+
CircuitPython driver for the FT5336 touch controller
2525

2626

2727
Dependencies
@@ -38,19 +38,10 @@ This is easily achieved by downloading
3838
or individual libraries can be installed using
3939
`circup <https://github.com/adafruit/circup>`_.
4040

41-
42-
43-
.. todo:: Describe the Adafruit product this library works with. For PCBs, you can also add the
44-
image from the assets folder in the PCB's GitHub repo.
45-
4641
`Purchase one from the Adafruit shop <http://www.adafruit.com/products/5846>`_
4742

4843
Installing from PyPI
4944
=====================
50-
.. note:: This library is not available on PyPI yet. Install documentation is included
51-
as a standard element. Stay tuned for PyPI availability!
52-
53-
.. todo:: Remove the above note if PyPI version is/will be available at time of release.
5445

5546
On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally `from
5647
PyPI <https://pypi.org/project/adafruit-circuitpython-ft5336/>`_.
@@ -101,8 +92,19 @@ Or the following command to update an existing version:
10192
Usage Example
10293
=============
10394

104-
.. todo:: Add a quick, simple example. It and other examples should live in the
105-
examples folder and be included in docs/examples.rst.
95+
.. code-block:: python
96+
97+
import time
98+
import board
99+
import adafruit_ft5336
100+
101+
i2c = board.I2C()
102+
touch = adafruit_ft5336.Adafruit_FT5336(i2c)
103+
104+
while True:
105+
t = touch.points
106+
print(t)
107+
time.sleep(0.1)
106108
107109
Documentation
108110
=============

adafruit_ft5336.py

Lines changed: 116 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
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
54
"""
65
`adafruit_ft5336`
76
================================================================================
87
9-
Touchscreen driver for the FT5336 touch controller
8+
CircuitPython driver for the FT5336 touch screen controller
109
1110
1211
* Author(s): Liz Clark
@@ -16,22 +15,129 @@
1615
1716
**Hardware:**
1817
19-
.. todo:: Add links to any specific hardware product page(s), or category page(s).
20-
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>`_
2119
2220
**Software and Dependencies:**
2321
2422
* Adafruit CircuitPython firmware for the supported boards:
2523
https://circuitpython.org/downloads
2624
27-
.. todo:: Uncomment or remove the Bus Device and/or the Register library dependencies
28-
based on the library's use of either.
29-
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
25+
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
26+
* Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register
3227
"""
3328

34-
# imports
29+
from adafruit_register.i2c_bits import ROBits
30+
from adafruit_bus_device.i2c_device import I2CDevice
31+
from micropython import const
32+
33+
try:
34+
from typing import List, Tuple
35+
except ImportError:
36+
pass
3537

3638
__version__ = "0.0.0+auto.0"
3739
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FT5336.git"
40+
41+
_DEFAULT_ADDR = const(0x38)
42+
_REG_VENDID = const(0xA3)
43+
_REG_CHIPID = const(0xA8)
44+
_VENDID = const(0x11)
45+
_CHIPID = const(0x79)
46+
_REG_NUMTOUCHES = const(0x02)
47+
_TD_STATUS = const(0x02)
48+
_TOUCH1_XH = const(0x03)
49+
_TOUCH1_XL = const(0x04)
50+
_TOUCH1_YH = const(0x05)
51+
_TOUCH1_YL = const(0x06)
52+
53+
54+
class Adafruit_FT5336:
55+
"""Adafruit FT5336 touch screen driver"""
56+
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(
61+
8, _REG_NUMTOUCHES, 0
62+
) # 8-bit read-only register for number of touches
63+
64+
def __init__(
65+
self, i2c, i2c_addr: int = _DEFAULT_ADDR, max_touches: int = 5
66+
) -> None:
67+
"""Initialization over I2C
68+
69+
:param int i2c_addr: I2C address (default 0x38)
70+
:param int max_touches: Maximum number of touch points to track. Defaults to 5.
71+
"""
72+
self.i2c_device = I2CDevice(i2c, i2c_addr) # I2C device instance
73+
self.i2c_addr = i2c_addr # Store the I2C address
74+
self._touches = 0 # Number of current touches
75+
self.max_touches = max_touches # Maximum number of touches to track
76+
77+
# Initialize touch point arrays
78+
self._touch_x: List[int] = [0] * self.max_touches
79+
self._touch_y: List[int] = [0] * self.max_touches
80+
self._touch_id: List[int] = [0] * self.max_touches
81+
82+
# Verify device identity by checking the vendor and chip IDs
83+
if self._vend_id != _VENDID:
84+
raise ValueError("Incorrect vendor ID")
85+
if self._chip_id != _CHIPID:
86+
raise ValueError("Incorrect chip ID")
87+
88+
def _read_data(self):
89+
buffer = bytearray(32)
90+
with self.i2c_device as i2c:
91+
i2c.write_then_readinto(bytearray([0]), buffer, in_end=32)
92+
93+
self._touches = buffer[_TD_STATUS]
94+
if self._touches > self.max_touches or self._touches == 0:
95+
self._touches = 0
96+
97+
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+
]
104+
self._touch_id[i] = buffer[_TOUCH1_YH + i * 6] >> 4
105+
106+
@property
107+
def touched(self) -> int:
108+
"""Count of touch inputs detected
109+
110+
:return: Count of touch inputs detected (0-max_touches)
111+
:rtype: int
112+
"""
113+
n = self._num_touches
114+
return 0 if n > self.max_touches else n
115+
116+
@property
117+
def points(self) -> List:
118+
"""X, Y and Z values from each available touch input
119+
120+
:return: X, Y and Z values in a list
121+
:rtype: List
122+
"""
123+
self._read_data()
124+
points = []
125+
for i in range(min(self._touches, self.max_touches)):
126+
point = (self._touch_x[i], self._touch_y[i], 1)
127+
points.append(point)
128+
129+
return points
130+
131+
def point(self, point_index: int) -> Tuple:
132+
"""X, Y and Z value from a specified touch input
133+
134+
:param int point_index: Touch input to read (0 - max_touches)
135+
:return: X, Y and Z values
136+
:rtype: Tuple
137+
"""
138+
self._read_data()
139+
if self._touches == 0 or point_index >= self._touches:
140+
value = (0, 0, 0)
141+
else:
142+
value = (self._touch_x[point_index], self._touch_y[point_index], 1)
143+
return value

docs/conf.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,19 @@
2323
"sphinx.ext.todo",
2424
]
2525

26-
# TODO: Please Read!
27-
# Uncomment the below if you use native CircuitPython modules such as
28-
# digitalio, micropython and busio. List the modules you use. Without it, the
29-
# autodoc module docs will fail to generate with a warning.
30-
# autodoc_mock_imports = ["digitalio", "busio"]
26+
# autodoc_mock_imports = [
27+
# "micropython",
28+
# "busio",
29+
# "adafruit_bus_device",
30+
# "adafruit_register",
31+
# ]
3132

3233
autodoc_preserve_defaults = True
3334

3435

3536
intersphinx_mapping = {
36-
"python": ("https://docs.python.org/3", None),"BusDevice": ("https://docs.circuitpython.org/projects/busdevice/en/latest/", None),
37+
"python": ("https://docs.python.org/3", None),
38+
"BusDevice": ("https://docs.circuitpython.org/projects/busdevice/en/latest/", None),
3739
"Register": ("https://docs.circuitpython.org/projects/register/en/latest/", None),
3840
"CircuitPython": ("https://docs.circuitpython.org/en/latest/", None),
3941
}

docs/index.rst

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,12 @@ Table of Contents
2424
.. toctree::
2525
:caption: Tutorials
2626

27-
.. todo:: Add any Learn guide links here. If there are none, then simply delete this todo and leave
28-
the toctree above for use later.
27+
Adafruit 3.5" 320x480 Color TFT Touchscreen Breakout Learn Guide <https://learn.adafruit.com/adafruit-3-5-color-320x480-tft-touchscreen-breakout>
2928

3029
.. toctree::
3130
:caption: Related Products
3231

33-
.. todo:: Add any product links here. If there are none, then simply delete this todo and leave
34-
the toctree above for use later.
32+
Adafruit 3.5" TFT 320x480 with Capacitive Touch Breakout Board - EYESPI <https://www.adafruit.com/product/5846>
3533

3634
.. toctree::
3735
:caption: Other Links

examples/ft5336_simpletest.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
2-
# SPDX-FileCopyrightText: Copyright (c) 2023 Liz Clark for Adafruit Industries
3-
#
4-
# SPDX-License-Identifier: Unlicense
1+
# SPDX-FileCopyrightText: 2023 Liz Clark for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
"""
5+
Demo for the FT5336. Reads all available touch input coordinates.
6+
"""
7+
8+
import time
9+
import board
10+
import adafruit_ft5336
11+
12+
i2c = board.I2C()
13+
touch = adafruit_ft5336.Adafruit_FT5336(i2c)
14+
15+
while True:
16+
t = touch.points
17+
print(t)
18+
time.sleep(0.1)

requirements.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
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
54

65
Adafruit-Blinka
76
adafruit-circuitpython-busdevice
87
adafruit-circuitpython-register
9-
n

0 commit comments

Comments
 (0)