From 0036d9caf66b9560831bc2909a9236a59ab79d01 Mon Sep 17 00:00:00 2001 From: Kevin Matocha Date: Thu, 4 Feb 2021 20:23:28 -0600 Subject: [PATCH 1/5] Add option for IRQ pin to wait for _read --- adafruit_focaltouch.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) mode change 100644 => 100755 adafruit_focaltouch.py diff --git a/adafruit_focaltouch.py b/adafruit_focaltouch.py old mode 100644 new mode 100755 index 4d7c9c7..58f97f2 --- a/adafruit_focaltouch.py +++ b/adafruit_focaltouch.py @@ -67,11 +67,12 @@ 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 ) @@ -90,10 +91,11 @@ def __init__(self, i2c, address=_FT6206_DEFAULT_I2C_ADDR, debug=False): print("Point rate %d Hz" % self._read(_FT6XXX_REG_POINTRATE, 1)[0]) print("Thresh %d" % self._read(_FT6XXX_REG_THRESHHOLD, 1)[0]) + @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 +105,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 +123,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 (self.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])) From 0d871faa74c657b28bd6a65997be9363d4c138e2 Mon Sep 17 00:00:00 2001 From: Kevin Matocha Date: Thu, 4 Feb 2021 20:57:30 -0600 Subject: [PATCH 2/5] Ran black and pylint --- adafruit_focaltouch.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/adafruit_focaltouch.py b/adafruit_focaltouch.py index 58f97f2..cb22085 100755 --- a/adafruit_focaltouch.py +++ b/adafruit_focaltouch.py @@ -67,12 +67,14 @@ class Adafruit_FocalTouch: _debug = False chip = None - def __init__(self, i2c, address=_FT6206_DEFAULT_I2C_ADDR, debug=False, irq_pin=None): + 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) # don't wait for IRQ + 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 ) @@ -91,7 +93,6 @@ def __init__(self, i2c, address=_FT6206_DEFAULT_I2C_ADDR, debug=False, irq_pin=N print("Point rate %d Hz" % self._read(_FT6XXX_REG_POINTRATE, 1)[0]) print("Thresh %d" % self._read(_FT6XXX_REG_THRESHHOLD, 1)[0]) - @property def touched(self): """ Returns the number of touches currently detected """ @@ -127,8 +128,8 @@ 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 (self.irq_pin.value): + if irq_pin is not None: + while self.irq_pin.value: pass i2c.write(bytes([register & 0xFF])) From fda8c3236ba447a9a05da95f4d69e9f4ca0237e5 Mon Sep 17 00:00:00 2001 From: Kevin Matocha Date: Fri, 5 Feb 2021 10:55:53 -0600 Subject: [PATCH 3/5] Change self.irq_pin to internal variable self._irq_pin --- adafruit_focaltouch.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/adafruit_focaltouch.py b/adafruit_focaltouch.py index cb22085..2f6151c 100755 --- a/adafruit_focaltouch.py +++ b/adafruit_focaltouch.py @@ -72,7 +72,7 @@ def __init__( ): self._i2c = I2CDevice(i2c, address) self._debug = debug - self.irq_pin = irq_pin + self._irq_pin = irq_pin chip_data = self._read(_FT6XXX_REG_LIBH, 8) # don't wait for IRQ lib_ver, chip_id, _, _, firm_id, _, vend_id = struct.unpack( @@ -96,7 +96,7 @@ def __init__( @property def touched(self): """ Returns the number of touches currently detected """ - return self._read(_FT6XXX_REG_NUMTOUCHES, 1, irq_pin=self.irq_pin)[0] + return self._read(_FT6XXX_REG_NUMTOUCHES, 1, irq_pin=self._irq_pin)[0] # pylint: disable=unused-variable @property @@ -106,7 +106,7 @@ def touches(self): touch coordinates, and 'id' as the touch # for multitouch tracking """ touchpoints = [] - data = self._read(_FT6XXX_REG_DATA, 32, irq_pin=self.irq_pin) + 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] @@ -129,7 +129,7 @@ def _read(self, register, length, irq_pin=None): with self._i2c as i2c: if irq_pin is not None: - while self.irq_pin.value: + while self._irq_pin.value: pass i2c.write(bytes([register & 0xFF])) From 5ae40d5c5bdd3a4c6add8887b1049f62fdfa373b Mon Sep 17 00:00:00 2001 From: Kevin Matocha Date: Fri, 5 Feb 2021 10:58:24 -0600 Subject: [PATCH 4/5] Fix bug in _read function to use passed parameter rather than class variable --- adafruit_focaltouch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_focaltouch.py b/adafruit_focaltouch.py index 2f6151c..a738bde 100755 --- a/adafruit_focaltouch.py +++ b/adafruit_focaltouch.py @@ -129,7 +129,7 @@ def _read(self, register, length, irq_pin=None): with self._i2c as i2c: if irq_pin is not None: - while self._irq_pin.value: + while irq_pin.value: pass i2c.write(bytes([register & 0xFF])) From e6908648c6711077f67a0c2a174a86d74494f022 Mon Sep 17 00:00:00 2001 From: Kevin Matocha Date: Fri, 5 Feb 2021 12:51:55 -0600 Subject: [PATCH 5/5] Add example with interrupt (IRQ) --- examples/focaltouch_print_touches_with_irq.py | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 examples/focaltouch_print_touches_with_irq.py 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)