XIAO Round Display

A sugestão do Chat GPT para converter a biblioteca de Micropython para Circuitpython é essa aqui. Eu ainda não fiz o teste.

Na pasta lib, crie uma nova pasta chamada chsc6x. Dentro dessa pasta, salve um arquivo init.py com o seguinte conteúdo:

import board
import busio
import digitalio
from time import sleep

class CHSC6X:
    CHSC6X_I2C_ID = 0x2e
    CHSC6X_READ_POINT_LEN = 5

    def __init__(self, i2c, irq_pin=None):
        self._i2c = i2c
        self._addr = self.CHSC6X_I2C_ID
        self._irq = digitalio.DigitalInOut(irq_pin) if irq_pin else None
        if self._irq:
            self._irq.switch_to_input(pull=digitalio.Pull.UP)
        self._buffer = bytearray(self.CHSC6X_READ_POINT_LEN)
        sleep(0.1)

    def is_touched(self):
        if self._irq:
            return not self._irq.value
        return self.touch_read() is not None

    def touch_read(self):
        if self._irq and not self._irq.value:
            self._i2c.readfrom_into(self._addr, self._buffer)
        else:
            try:
                self._i2c.readfrom_into(self._addr, self._buffer)
            except OSError:  # Thrown when reading too fast
                return None

        results = [i for i in self._buffer]
        # first byte is non-zero when touched, 3rd byte is x, 5th byte is y
        if results[0]:
            return results[2], results[4]
        return None

def main():
    print("Started...")
    i2c = busio.I2C(board.SCL, board.SDA, frequency=400000)
    irq_pin = digitalio.DigitalInOut(board.D7)
    touch = CHSC6X(i2c, irq_pin=irq_pin)

    while True:
        if touch.is_touched():
            print("Touched: ", touch.touch_read())
        sleep(0.1)

if __name__ == "__main__":
    main()