Simple tests

Ensure your device works with these simple tests.

examples/focaltouch_print_touches.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4"""
 5Example for getting touch data from an FT6206 or FT6236 capacitive
 6touch driver, over I2C
 7"""
 8
 9import time
10
11import board
12import busio
13
14import adafruit_focaltouch
15
16# Create library object (named "ft") using a Bus I2C port
17i2c = busio.I2C(board.SCL, board.SDA)
18
19ft = adafruit_focaltouch.Adafruit_FocalTouch(i2c, debug=False)
20
21while True:
22    # if the screen is being touched print the touches
23    if ft.touched:
24        print(ft.touches)
25    else:
26        print("no touch")
27
28    time.sleep(0.15)
examples/focaltouch_paint_simpletest.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4"""
 5Simple painting demo that draws on an Adafruit capacitive touch shield with
 6ILI9341 display and FT6206 captouch driver
 7"""
 8
 9import board
10import busio
11import digitalio
12from adafruit_rgb_display import color565, ili9341
13
14import adafruit_focaltouch
15
16# Create library object using our Bus I2C & SPI port
17i2c = busio.I2C(board.SCL, board.SDA)
18spi = busio.SPI(clock=board.SCK, MOSI=board.MOSI, MISO=board.MISO)
19
20# Adafruit Metro M0 + 2.8" Capacitive touch shield
21cs_pin = digitalio.DigitalInOut(board.D10)
22dc_pin = digitalio.DigitalInOut(board.D9)
23
24# Initialize display
25display = ili9341.ILI9341(spi, cs=cs_pin, dc=dc_pin)
26# Fill with black!
27display.fill(color565(0, 0, 0))
28
29ft = adafruit_focaltouch.Adafruit_FocalTouch(i2c)
30
31while True:
32    if ft.touched:
33        ts = ft.touches
34        point = ts[0]  # the shield only supports one point!
35        # perform transformation to get into display coordinate system!
36        y = 320 - point["y"]
37        x = 240 - point["x"]
38        display.fill_rectangle(x - 2, y - 2, 4, 4, color565(255, 255, 255))
examples/focaltouch_print_touches_with_irq.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4"""
 5Example for getting touch data from an FT6206 or FT6236 capacitive
 6touch driver, over I2C.  This version uses an interrupt to prevent
 7read errors from the FocalTouch chip.
 8"""
 9
10import time
11
12import board
13from digitalio import DigitalInOut, Direction
14
15import adafruit_focaltouch
16
17IRQ_pin = board.IO39  # select a pin to connect to the display's interrupt pin ("IRQ")
18i2c = board.I2C()
19
20# Setup the interrupt (IRQ) pin for input
21irq = DigitalInOut(IRQ_pin)
22irq.direction = Direction.INPUT
23
24# Create library object (named "ft") using a Bus I2C port and using an interrupt pin (IRQ)
25ft = adafruit_focaltouch.Adafruit_FocalTouch(i2c, debug=False, irq_pin=irq)
26
27
28print("\n\nReady for touches...")
29
30while True:
31    # if the screen is being touched print the touches
32    if ft.touched:
33        print(ft.touches)
34    else:
35        print("no touch")
36
37    time.sleep(0.05)

Demos

These demos are a bit more complex.

examples/focaltouch_paint_rgb666.py
  1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
  2# SPDX-License-Identifier: MIT
  3"""
  4Simple painting demo that draws on the Adafruit Qualia ESP32-S3 RGB666
  5with the 4.0" square display and FT6206 captouch driver
  6"""
  7
  8import board
  9import busio
 10import displayio
 11import dotclockframebuffer
 12from framebufferio import FramebufferDisplay
 13
 14import adafruit_focaltouch
 15
 16displayio.release_displays()
 17
 18# Initialize the Display
 19tft_pins = dict(board.TFT_PINS)
 20
 21tft_timings = {
 22    "frequency": 16000000,
 23    "width": 720,
 24    "height": 720,
 25    "hsync_pulse_width": 2,
 26    "hsync_front_porch": 46,
 27    "hsync_back_porch": 44,
 28    "vsync_pulse_width": 2,
 29    "vsync_front_porch": 16,
 30    "vsync_back_porch": 18,
 31    "hsync_idle_low": False,
 32    "vsync_idle_low": False,
 33    "de_idle_high": False,
 34    "pclk_active_high": False,
 35    "pclk_idle_high": False,
 36}
 37
 38init_sequence_tl040hds20 = b""
 39
 40board.I2C().deinit()
 41i2c = busio.I2C(board.SCL, board.SDA, frequency=100_000)
 42tft_io_expander = dict(board.TFT_IO_EXPANDER)
 43# tft_io_expander['i2c_address'] = 0x38 # uncomment for rev B
 44dotclockframebuffer.ioexpander_send_init_sequence(i2c, init_sequence_tl040hds20, **tft_io_expander)
 45
 46fb = dotclockframebuffer.DotClockFramebuffer(**tft_pins, **tft_timings)
 47display = FramebufferDisplay(fb, auto_refresh=False)
 48
 49# Main Program
 50pixel_size = 6
 51palette_width = 160
 52palette_height = display.height // 8
 53
 54bitmap = displayio.Bitmap(display.width, display.height, 65535)
 55
 56# Create a TileGrid to hold the bitmap
 57tile_grid = displayio.TileGrid(
 58    bitmap,
 59    pixel_shader=displayio.ColorConverter(input_colorspace=displayio.Colorspace.RGB565),
 60)
 61
 62# Create a Group to hold the TileGrid
 63group = displayio.Group()
 64
 65# Add the TileGrid to the Group
 66group.append(tile_grid)
 67
 68# Add the Group to the Display
 69display.root_group = group
 70
 71display.auto_refresh = True
 72
 73ft = adafruit_focaltouch.Adafruit_FocalTouch(i2c, address=0x48)
 74
 75current_color = displayio.ColorConverter().convert(0xFFFFFF)
 76
 77for i in range(palette_width):
 78    color_index = i * 255 // palette_width
 79    rgb565 = displayio.ColorConverter().convert(color_index | color_index << 8 | color_index << 16)
 80    r_mask = 0xF800
 81    g_mask = 0x07E0
 82    b_mask = 0x001F
 83    for j in range(palette_height):
 84        bitmap[i, j + palette_height] = rgb565 & b_mask
 85        bitmap[i, j + palette_height * 2] = rgb565 & (b_mask | g_mask)
 86        bitmap[i, j + palette_height * 3] = rgb565 & g_mask
 87        bitmap[i, j + palette_height * 4] = rgb565 & (r_mask | g_mask)
 88        bitmap[i, j + palette_height * 5] = rgb565 & r_mask
 89        bitmap[i, j + palette_height * 6] = rgb565 & (r_mask | b_mask)
 90        bitmap[i, j + palette_height * 7] = rgb565
 91
 92while True:
 93    if ft.touched:
 94        try:
 95            for touch in ft.touches:
 96                x = touch["x"]
 97                y = touch["y"]
 98                if x < palette_width:
 99                    current_color = bitmap[x, y]
100                else:
101                    for i in range(pixel_size):
102                        for j in range(pixel_size):
103                            x_pixel = x - (pixel_size // 2) + i
104                            y_pixel = y - (pixel_size // 2) + j
105                            if 0 <= x_pixel < display.width and 0 <= y_pixel < display.height:
106                                bitmap[x_pixel, y_pixel] = current_color
107        except RuntimeError:
108            pass