Skip to content

Instantly share code, notes, and snippets.

@kmatch98
Created August 31, 2023 00:08
Show Gist options
  • Save kmatch98/097a34177b68480e511179f9eeb8d909 to your computer and use it in GitHub Desktop.
Save kmatch98/097a34177b68480e511179f9eeb8d909 to your computer and use it in GitHub Desktop.
GT911 CircuitPython driver, barely working.
# SPDX-FileCopyrightText: 2017 Limor Fried for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""CircuitPython I2C Device Address Scan"""
# If you run this and it seems to hang, try manually unlocking
# your I2C bus from the REPL with
# >>> import board
# >>> board.I2C().unlock()
import time
import board
import busio
import adafruit_focaltouch
from adafruit_bus_device.i2c_device import I2CDevice
GT911_REG_ID = 0x8140
print("starting...")
# To use default I2C bus (most boards)
# i2c = board.I2C() # uses board.SCL and board.SDA
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
# To create I2C bus on specific pins
# import busio
# i2c = busio.I2C(board.SCL1, board.SDA1) # QT Py RP2040 STEMMA connector
# i2c = busio.I2C(board.GP1, board.GP0) # Pi Pico RP2040
i2c = busio.I2C(board.IO18, board.IO17)
print("try lock")
while not i2c.try_lock():
pass
print("locked")
# GT911_config= bytes([
# 0x42,
# 0xD0, 0x02, 0x00, 0x05, 0x05, 0x75, 0x01, 0x01, 0x0F, 0x24,
# 0x0F, 0x64, 0x3C, 0x03, 0x05, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00,
# 0x00, 0x16, 0x19, 0x1C, 0x14, 0x8C, 0x0E, 0x0E, 0x24, 0x00, 0x31,
# 0x0D, 0x00, 0x00, 0x00, 0x83, 0x33, 0x1D, 0x00, 0x41, 0x00, 0x00,
# 0x00, 0x00, 0x00, 0x08, 0x0A, 0x00, 0x2B, 0x1C, 0x3C, 0x94, 0xD5,
# 0x03, 0x08, 0x00, 0x00, 0x04, 0x93, 0x1E, 0x00, 0x82, 0x23, 0x00,
# 0x74, 0x29, 0x00, 0x69, 0x2F, 0x00, 0x5F, 0x37, 0x00, 0x5F, 0x20,
# 0x40, 0x60, 0x00, 0xF0, 0x40, 0x30, 0x55, 0x50, 0x27, 0x00, 0x00,
# 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
# 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 0x19, 0x00, 0x00,
# 0x50, 0x50, 0x02, 0x04, 0x06, 0x08, 0x0A, 0x0C, 0x0E, 0x10, 0x12,
# 0x14, 0x16, 0x18, 0x1A, 0x1C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
# 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1D,
# 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x24, 0x26, 0x28, 0x29, 0x2A, 0x1C,
# 0x18, 0x16, 0x14, 0x13, 0x12, 0x10, 0x0F, 0x0C, 0x0A, 0x08, 0x06,
# 0x04, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
# 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9C, 0x01])
# print("send config")
# i2c.writeto(0x5D, GT911_config)
time.sleep(0.2)
while True:
# i2c.writeto(0x5D, bytes([(0x80 & 0xFF), (0x40 & 0xFF), (0x00 & 0xFF)]))
# print("write 50")
touches = bytearray(1)
i2c.writeto(0x5D, bytes([(0x81 & 0xFF), (0x4E & 0xFF)]))
i2c.readfrom_into(0x5D, touches)
# print("write 50")
i2c.writeto(0x5D, bytes([(0x81 & 0xFF), (0x50 & 0xFF)]))
# print("read")
result = bytearray(4)
i2c.readfrom_into(0x5D, result)
i2c.writeto(0x5D, bytes([(0x81 & 0xFF), (0x4E & 0xFF), (0x00 & 0xFF)]))
print("touches: {} x: {}, y: {}".format(touches[0] & 0x0F, (result[1] << 8) + result[0], (result[3] << 8) + result[2]))
# print("")
time.sleep(0.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment