diff --git a/adafruit_focaltouch.py b/adafruit_focaltouch.py old mode 100644 new mode 100755 index 4d7c9c7..a738bde --- a/adafruit_focaltouch.py +++ b/adafruit_focaltouch.py @@ -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 ) @@ -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 @@ -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] @@ -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])) diff --git a/examples/focaltouch_print_touches_with_irq.py b/examples/focaltouch_print_touches_with_irq.py new file mode 100644 index 0000000..2b896f8 --- /dev/null +++ b/examples/focaltouch_print_touches_with_irq.py @@ -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)