from machine import Pin, SPI
from ili934xnew import ILI9341, color565
from xpt2046 import Touch
import time
import glcdfont
# SPI setup (SPI1 with shared display + touch)
spi = SPI(1, baudrate=20000000, sck=Pin(10), mosi=Pin(11), miso=Pin(12))
# Display setup
display = ILI9341(
spi=spi,
cs=Pin(17),
dc=Pin(15),
rst=Pin(16),
w=240,
h=320,
r=0
)
display.set_font(glcdfont)
display.fill_rectangle(0, 0, 240, 320, color565(0, 0, 0)) # Clear screen
# Draw a green "Clear" button
def draw_button(x, y, w, h, col, label):
display.fill_rectangle(x, y, w, h, col)
display.set_pos(x + 5, y + h // 2 - 8)
display.print(label)
draw_button(0, 0, 60, 30, color565(0, 255, 0), "Clear")
# Touch setup
touch = Touch(spi=spi, cs=Pin(17), int_pin=Pin(14))
touch.calibrate(
xmin=200, xmax=3800,
ymin=200, ymax=3800,
width=240, height=320,
orientation=0
)
pen_color = color565(255, 255, 255) # White color for drawing
# Main loop
while True:
x, y = touch.get_touch()
if x is None:
time.sleep(0.02)
continue
# Check if touch is on "Clear" button
if 0 <= x < 60 and 0 <= y < 30:
display.fill_rectangle(0, 30, 240, 290, color565(0, 0, 0)) # Clear below button
draw_button(0, 0, 60, 30, color565(0, 255, 0), "Clear")
else:
display.fill_circle(x, y, 4, pen_color) # Draw circle at touch point
time.sleep(0.02)
Loading
ili9341-cap-touch
ili9341-cap-touch