Skip to content

add tsc2007 touch driver#532

Open
FoamyGuy wants to merge 1 commit intolvgl-micropython:mainfrom
FoamyGuy:add_tsc2007
Open

add tsc2007 touch driver#532
FoamyGuy wants to merge 1 commit intolvgl-micropython:mainfrom
FoamyGuy:add_tsc2007

Conversation

@FoamyGuy
Copy link

This adds a new touch driver for TSC2007 device. The initial version of the new driver was drafted by Codex and then reviewed and refined by me.

I tested it successfully with a build of MicropythonOS which is built on top of lvgl_micropython and this more basic example code. Hardware was Metro S3 + 2.8" TFT Shield

from micropython import const
import machine
import lcd_bus
import lvgl as lv
import ili9341
import task_handler
import i2c
import tsc2007


# Adjust these pins for your ESP32_GENERIC_S3 wiring
_WIDTH = const(240)
_HEIGHT = const(320)
_BL = const(5)
_RST = const(4)
_DC = const(9)

_MOSI = const(42)
_MISO = const(21)
_SCK = const(39)
_HOST = const(1)  # SPI2

_LCD_CS = const(10)
_LCD_FREQ = const(40000000)

# TSC2007 I2C touch pins (update these for your board)
_TP_SCL = const(48)
_TP_SDA = const(47)
_TP_FREQ = const(100000)


lv.init()

spi_bus = machine.SPI.Bus(
    host=_HOST,
    mosi=_MOSI,
    miso=_MISO,
    sck=_SCK,
)

display_bus = lcd_bus.SPIBus(
    spi_bus=spi_bus,
    freq=_LCD_FREQ,
    dc=_DC,
    cs=_LCD_CS,
)

display = ili9341.ILI9341(
    data_bus=display_bus,
    display_width=_WIDTH,
    display_height=_HEIGHT,
    backlight_pin=_BL,
    reset_pin=_RST,
    color_space=lv.COLOR_FORMAT.RGB565,
    color_byte_order=ili9341.BYTE_ORDER_BGR,
    rgb565_byte_swap=True,
)

display.set_power(True)
display.init(1)
display.set_backlight(100)

# TSC2007 touch setup
i2c_bus = i2c.I2C.Bus(host=0, scl=_TP_SCL, sda=_TP_SDA, freq=_TP_FREQ, use_locks=False)
touch_dev = i2c.I2C.Device(bus=i2c_bus, dev_id=tsc2007.I2C_ADDR, reg_bits=tsc2007.BITS)
indev = tsc2007.TSC2007(touch_dev)

# Resistive touch generally needs calibration
if not indev.is_calibrated:
    indev.calibrate()

th = task_handler.TaskHandler()

scrn = lv.screen_active()
scrn.set_style_bg_color(lv.color_hex(0x202020), 0)

status_label = lv.label(scrn)
status_label.set_text("Touch test: tap the button")
status_label.align(lv.ALIGN.TOP_MID, 0, 12)

btn = lv.button(scrn)
btn.set_size(180, 70)
btn.center()

btn_label = lv.label(btn)
btn_label.set_text("Touch me")
btn_label.center()

count_label = lv.label(scrn)
count_label.set_text("Clicks: 0")
count_label.align(lv.ALIGN.BOTTOM_MID, 0, -20)

click_count = 0


def on_btn_event(evt):
    global click_count

    if evt.get_code() == lv.EVENT.CLICKED:
        click_count += 1
        count_label.set_text("Clicks: {}".format(click_count))
        btn_label.set_text("Touched!")
        btn_label.center()


btn.add_event_cb(on_btn_event, lv.EVENT.ALL, None)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant