|
1 |
| -# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries |
2 | 1 | # SPDX-FileCopyrightText: Copyright (c) 2023 Liz Clark for Adafruit Industries
|
3 | 2 | #
|
4 | 3 | # SPDX-License-Identifier: MIT
|
5 | 4 | """
|
6 | 5 | `adafruit_ft5336`
|
7 | 6 | ================================================================================
|
8 | 7 |
|
9 |
| -Touchscreen driver for the FT5336 touch controller |
| 8 | +CircuitPython driver for the FT5336 touch screen controller |
10 | 9 |
|
11 | 10 |
|
12 | 11 | * Author(s): Liz Clark
|
|
16 | 15 |
|
17 | 16 | **Hardware:**
|
18 | 17 |
|
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>`_ |
21 | 19 |
|
22 | 20 | **Software and Dependencies:**
|
23 | 21 |
|
24 | 22 | * Adafruit CircuitPython firmware for the supported boards:
|
25 | 23 | https://circuitpython.org/downloads
|
26 | 24 |
|
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 |
32 | 27 | """
|
33 | 28 |
|
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 |
35 | 37 |
|
36 | 38 | __version__ = "0.0.0+auto.0"
|
37 | 39 | __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 |
0 commit comments