Skip to content

Add option for IRQ pin to wait for _read #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions adafruit_focaltouch.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,14 @@ class Adafruit_FocalTouch:
_debug = False
chip = None

def __init__(self, i2c, address=_FT6206_DEFAULT_I2C_ADDR, debug=False):
def __init__(
self, i2c, address=_FT6206_DEFAULT_I2C_ADDR, debug=False, irq_pin=None
):
self._i2c = I2CDevice(i2c, address)
self._debug = debug
self._irq_pin = irq_pin

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

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

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

# pylint: enable=unused-variable

def _read(self, register, length):
def _read(self, register, length, irq_pin=None):
"""Returns an array of 'length' bytes from the 'register'"""
with self._i2c as i2c:

if irq_pin is not None:
while irq_pin.value:
pass

i2c.write(bytes([register & 0xFF]))
result = bytearray(length)

i2c.readinto(result)
if self._debug:
print("\t$%02X => %s" % (register, [hex(i) for i in result]))
Expand Down
47 changes: 47 additions & 0 deletions examples/focaltouch_print_touches_with_irq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""
Example for getting touch data from an FT6206 or FT6236 capacitive
touch driver, over I2C. This version uses an interrupt to prevent
read errors from the FocalTouch chip.
"""

import time
import busio
import board
from digitalio import DigitalInOut, Direction
import adafruit_focaltouch


if hasattr(
board, "SCL"
): # if SCL and SDA pins are defined by the board definition, use them.
SCL_pin = board.SCL
SDA_pin = board.SDA
else:
SCL_pin = board.IO42 # set to a pin that you want to use for SCL
SDA_pin = board.IO41 # set to a pin that you want to use for SDA

IRQ_pin = board.IO39 # select a pin to connect to the display's interrupt pin ("IRQ")

i2c = busio.I2C(SCL_pin, SDA_pin)

# Setup the interrupt (IRQ) pin for input
irq = DigitalInOut(board.IO39)
irq.direction = Direction.INPUT

# Create library object (named "ft") using a Bus I2C port and using an interrupt pin (IRQ)
ft = adafruit_focaltouch.Adafruit_FocalTouch(i2c, debug=False, irq_pin=irq)


print("\n\nReady for touches...")

while True:
# if the screen is being touched print the touches
if ft.touched:
print(ft.touches)
else:
print("no touch")

time.sleep(0.05)