|
| 1 | +# Copyright (c) 2024 - 2025 Kevin G. Schlosser |
| 2 | + |
| 3 | +# this driver uses a special i2c bus implimentation I have written. |
| 4 | +# This implimentation takes into consideration the ESP32 and it having |
| 5 | +# threading available. It also has some convience methods built into it |
| 6 | +# that figure out what is wanting to be done automatically. |
| 7 | +# read more about it's use in the stub files. |
| 8 | + |
| 9 | +from micropython import const # NOQA |
| 10 | +import pointer_framework |
| 11 | +import machine # NOQA |
| 12 | +import time |
| 13 | + |
| 14 | + |
| 15 | +_CMD_REG = const(0x8040) |
| 16 | +_CMD_CHECK_REG = const(0x8046) |
| 17 | +_CMD_READ_DATA = const(0x01) |
| 18 | + |
| 19 | +_ESD_CHECK_REG = const(0x8041) |
| 20 | + |
| 21 | +_STATUS_REG = const(0x814E) |
| 22 | +_POINT_1_REG = const(0x8150) |
| 23 | + |
| 24 | +_PRODUCT_ID_REG = const(0x8140) |
| 25 | +_FIRMWARE_VERSION_REG = const(0x8144) |
| 26 | +_VENDOR_ID_REG = const(0x814A) |
| 27 | + |
| 28 | +_X_CORD_RES_REG = const(0x8146) |
| 29 | +_Y_CORD_RES_REG = const(0x8148) |
| 30 | + |
| 31 | +I2C_ADDR = 0x5D |
| 32 | +BITS = 16 |
| 33 | + |
| 34 | +_ADDR2 = const(0x14) |
| 35 | + |
| 36 | + |
| 37 | +class GT911(pointer_framework.PointerDriver): |
| 38 | + |
| 39 | + def _read_reg(self, reg, num_bytes=None, buf=None): |
| 40 | + self._tx_buf[0] = reg >> 8 |
| 41 | + self._tx_buf[1] = reg & 0xFF |
| 42 | + try: |
| 43 | + if num_bytes is not None: |
| 44 | + self._device.write_readinto(self._tx_mv[:2], self._rx_mv[:num_bytes]) |
| 45 | + else: |
| 46 | + self._device.write_readinto(self._tx_mv[:2], buf) |
| 47 | + except Exception as e: |
| 48 | + print(f"GT911 _read_reg got exception: {e}") |
| 49 | + |
| 50 | + def _write_reg(self, reg, value=None, buf=None): |
| 51 | + try: |
| 52 | + if value is not None: |
| 53 | + self._tx_buf[0] = value |
| 54 | + self._device.write_mem(reg, self._tx_mv[:1]) |
| 55 | + elif buf is not None: |
| 56 | + self._device.write_mem(reg, buf) |
| 57 | + except Exception as e: |
| 58 | + print(f"GT911 _write_reg got exception: {e}") |
| 59 | + |
| 60 | + def __init__( |
| 61 | + self, |
| 62 | + device, |
| 63 | + reset_pin=None, |
| 64 | + interrupt_pin=None, |
| 65 | + touch_cal=None, |
| 66 | + startup_rotation=pointer_framework.lv.DISPLAY_ROTATION._0, # NOQA |
| 67 | + debug=False |
| 68 | + ): |
| 69 | + self._tx_buf = bytearray(3) |
| 70 | + self._tx_mv = memoryview(self._tx_buf) |
| 71 | + self._rx_buf = bytearray(6) |
| 72 | + self._rx_mv = memoryview(self._rx_buf) |
| 73 | + |
| 74 | + self._device = device |
| 75 | + |
| 76 | + self.__x = 0 |
| 77 | + self.__y = 0 |
| 78 | + self.__last_state = self.RELEASED |
| 79 | + |
| 80 | + if isinstance(reset_pin, int): |
| 81 | + reset_pin = machine.Pin(reset_pin, machine.Pin.OUT) |
| 82 | + |
| 83 | + if isinstance(interrupt_pin, int): |
| 84 | + interrupt_pin = machine.Pin(interrupt_pin, machine.Pin.OUT) |
| 85 | + |
| 86 | + self._reset_pin = reset_pin |
| 87 | + self._interrupt_pin = interrupt_pin |
| 88 | + |
| 89 | + self.hw_reset() |
| 90 | + super().__init__( |
| 91 | + touch_cal=touch_cal, startup_rotation=startup_rotation, debug=debug |
| 92 | + ) |
| 93 | + |
| 94 | + def hw_reset(self): |
| 95 | + if self._interrupt_pin and self._reset_pin: |
| 96 | + self._interrupt_pin.init(self._interrupt_pin.OUT) |
| 97 | + self._interrupt_pin(0) |
| 98 | + self._reset_pin(0) |
| 99 | + time.sleep_ms(10) # NOQA |
| 100 | + self._interrupt_pin(0) |
| 101 | + time.sleep_ms(1) # NOQA |
| 102 | + self._reset_pin(1) |
| 103 | + time.sleep_ms(5) # NOQA |
| 104 | + self._interrupt_pin(0) |
| 105 | + time.sleep_ms(50) # NOQA |
| 106 | + self._interrupt_pin.init(self._interrupt_pin.IN) |
| 107 | + time.sleep_ms(50) # NOQA |
| 108 | + |
| 109 | + self._write_reg(_ESD_CHECK_REG, 0x00) |
| 110 | + self._write_reg(_CMD_CHECK_REG, _CMD_READ_DATA) |
| 111 | + self._write_reg(_CMD_REG, _CMD_READ_DATA) |
| 112 | + |
| 113 | + self._read_reg(_PRODUCT_ID_REG, 4) |
| 114 | + |
| 115 | + product_id = '' |
| 116 | + for item in self._rx_buf[:4]: |
| 117 | + try: |
| 118 | + product_id += chr(item) |
| 119 | + except: # NOQA |
| 120 | + break |
| 121 | + |
| 122 | + print('Touch Product id:', product_id) |
| 123 | + |
| 124 | + self._read_reg(_FIRMWARE_VERSION_REG, 2) |
| 125 | + print( |
| 126 | + 'Touch Firmware version:', |
| 127 | + hex(self._rx_buf[0] + (self._rx_buf[1] << 8)) |
| 128 | + ) |
| 129 | + |
| 130 | + self._read_reg(_VENDOR_ID_REG, 1) |
| 131 | + print(f'Touch Vendor id: 0x{hex(self._rx_buf[0])[2:].upper()}') |
| 132 | + x, y = self.hw_size |
| 133 | + print(f'Touch resolution: width={x}, height={y}') |
| 134 | + |
| 135 | + @property |
| 136 | + def hw_size(self): |
| 137 | + self._read_reg(_X_CORD_RES_REG, 2) |
| 138 | + x = self._rx_buf[0] + (self._rx_buf[1] << 8) |
| 139 | + |
| 140 | + self._read_reg(_Y_CORD_RES_REG, 2) |
| 141 | + y = self._rx_buf[0] + (self._rx_buf[1] << 8) |
| 142 | + |
| 143 | + return x, y |
| 144 | + |
| 145 | + @property |
| 146 | + def firmware_config(self): |
| 147 | + try: |
| 148 | + import gt911_extension |
| 149 | + except ImportError: |
| 150 | + raise ImportError( |
| 151 | + 'you need to upload the gt911_extension.py file to the MCU' |
| 152 | + ) |
| 153 | + return gt911_extension.GT911Extension(self, self._device) |
| 154 | + |
| 155 | + def _get_coords(self): |
| 156 | + self._read_reg(_STATUS_REG, 1) |
| 157 | + touch_cnt = self._rx_buf[0] & 0x0F |
| 158 | + status = self._rx_buf[0] & 0x80 |
| 159 | + |
| 160 | + if status: |
| 161 | + if touch_cnt == 1: |
| 162 | + self._read_reg(_POINT_1_REG, 6) |
| 163 | + |
| 164 | + x = self._rx_buf[0] + (self._rx_buf[1] << 8) |
| 165 | + y = self._rx_buf[2] + (self._rx_buf[3] << 8) |
| 166 | + |
| 167 | + self._write_reg(_STATUS_REG, 0x00) |
| 168 | + |
| 169 | + self.__x = x |
| 170 | + self.__y = y |
| 171 | + self.__last_state = self.PRESSED |
| 172 | + |
| 173 | + elif touch_cnt == 0: |
| 174 | + self.__last_state = self.RELEASED |
| 175 | + |
| 176 | + self._write_reg(_STATUS_REG, 0x00) |
| 177 | + |
| 178 | + return self.__last_state, self.__x, self.__y |
0 commit comments