Skip to content

Commit 4ae52ac

Browse files
authored
Merge pull request #19 from kmatch98/add_irq
Add option for IRQ pin to wait for _read
2 parents af6f9c1 + e690864 commit 4ae52ac

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed

adafruit_focaltouch.py

100644100755
Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,14 @@ class Adafruit_FocalTouch:
6767
_debug = False
6868
chip = None
6969

70-
def __init__(self, i2c, address=_FT6206_DEFAULT_I2C_ADDR, debug=False):
70+
def __init__(
71+
self, i2c, address=_FT6206_DEFAULT_I2C_ADDR, debug=False, irq_pin=None
72+
):
7173
self._i2c = I2CDevice(i2c, address)
7274
self._debug = debug
75+
self._irq_pin = irq_pin
7376

74-
chip_data = self._read(_FT6XXX_REG_LIBH, 8)
77+
chip_data = self._read(_FT6XXX_REG_LIBH, 8) # don't wait for IRQ
7578
lib_ver, chip_id, _, _, firm_id, _, vend_id = struct.unpack(
7679
">HBBBBBB", chip_data
7780
)
@@ -93,7 +96,7 @@ def __init__(self, i2c, address=_FT6206_DEFAULT_I2C_ADDR, debug=False):
9396
@property
9497
def touched(self):
9598
""" Returns the number of touches currently detected """
96-
return self._read(_FT6XXX_REG_NUMTOUCHES, 1)[0]
99+
return self._read(_FT6XXX_REG_NUMTOUCHES, 1, irq_pin=self._irq_pin)[0]
97100

98101
# pylint: disable=unused-variable
99102
@property
@@ -103,7 +106,7 @@ def touches(self):
103106
touch coordinates, and 'id' as the touch # for multitouch tracking
104107
"""
105108
touchpoints = []
106-
data = self._read(_FT6XXX_REG_DATA, 32)
109+
data = self._read(_FT6XXX_REG_DATA, 32, irq_pin=self._irq_pin)
107110

108111
for i in range(2):
109112
point_data = data[i * 6 + 3 : i * 6 + 9]
@@ -121,11 +124,17 @@ def touches(self):
121124

122125
# pylint: enable=unused-variable
123126

124-
def _read(self, register, length):
127+
def _read(self, register, length, irq_pin=None):
125128
"""Returns an array of 'length' bytes from the 'register'"""
126129
with self._i2c as i2c:
130+
131+
if irq_pin is not None:
132+
while irq_pin.value:
133+
pass
134+
127135
i2c.write(bytes([register & 0xFF]))
128136
result = bytearray(length)
137+
129138
i2c.readinto(result)
130139
if self._debug:
131140
print("\t$%02X => %s" % (register, [hex(i) for i in result]))
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
"""
5+
Example for getting touch data from an FT6206 or FT6236 capacitive
6+
touch driver, over I2C. This version uses an interrupt to prevent
7+
read errors from the FocalTouch chip.
8+
"""
9+
10+
import time
11+
import busio
12+
import board
13+
from digitalio import DigitalInOut, Direction
14+
import adafruit_focaltouch
15+
16+
17+
if hasattr(
18+
board, "SCL"
19+
): # if SCL and SDA pins are defined by the board definition, use them.
20+
SCL_pin = board.SCL
21+
SDA_pin = board.SDA
22+
else:
23+
SCL_pin = board.IO42 # set to a pin that you want to use for SCL
24+
SDA_pin = board.IO41 # set to a pin that you want to use for SDA
25+
26+
IRQ_pin = board.IO39 # select a pin to connect to the display's interrupt pin ("IRQ")
27+
28+
i2c = busio.I2C(SCL_pin, SDA_pin)
29+
30+
# Setup the interrupt (IRQ) pin for input
31+
irq = DigitalInOut(board.IO39)
32+
irq.direction = Direction.INPUT
33+
34+
# Create library object (named "ft") using a Bus I2C port and using an interrupt pin (IRQ)
35+
ft = adafruit_focaltouch.Adafruit_FocalTouch(i2c, debug=False, irq_pin=irq)
36+
37+
38+
print("\n\nReady for touches...")
39+
40+
while True:
41+
# if the screen is being touched print the touches
42+
if ft.touched:
43+
print(ft.touches)
44+
else:
45+
print("no touch")
46+
47+
time.sleep(0.05)

0 commit comments

Comments
 (0)