"""ILI9341 demo (fonts 8x8 background color)."""
from time import sleep
from ili9341 import Display, color565
from machine import Pin, SPI # type: ignore
from xct2046 import Touch
# Initialize SPI for Display (ILI9341)
spi_display = SPI(0, baudrate=40000000, sck=Pin(18), mosi=Pin(19))
display = Display(spi_display, dc=Pin(16), cs=Pin(17), rst=Pin(20))
# Initialize SPI for Touchscreen (XPT2046)
spi_touch = SPI(0, baudrate=1000000, sck=Pin(18), mosi=Pin(19), miso=Pin(16))
touch = Touch(spi_touch, cs=Pin(5))
# Colors
BLACK = color565(0, 0, 0)
WHITE = color565(255, 255, 255)
ORANGE = color565(255, 128, 0)
# Variables to store user input
user_choice = None
turn_degrees = 0
move_distance = 0
def get_touch_input():
"""Get touchscreen input."""
while True:
if touch.get_touch():
x, y = touch.get_touch()
# Flip Y-axis
y = display.height - y
return x, y
def menu():
spi = SPI(0, baudrate=40000000, sck=Pin(18), mosi=Pin(19))
display = Display(spi, dc=Pin(16), cs=Pin(17), rst=Pin(20))
y_center = display.height // 2
display.draw_text8x8(display.width - 180, y_center - 20, "Menu",
color565(255, 255, 255), rotate=270)
display.fill_rectangle(display.width - 155, 60, 18, 200,
color565(255, 128, 0))
display.draw_text8x8(display.width - 150, y_center - 48, "Rotate left?",
color565(255, 255, 255), rotate=270,
background=color565(255, 128, 0))
display.fill_rectangle(display.width - 105, 60, 18, 200,
color565(255, 128, 0))
display.draw_text8x8(display.width - 100, y_center - 48, "Rotate Right?",
color565(255, 255, 255), rotate=270,
background=color565(255, 128, 0))
while True:
x, y = get_touch_input()
global user_choice
# x=display.width - 100
# y=70
if display.width - 155 < x < display.width - 137 and 60 < y < 260:
user_choice = "Left"
return
elif display.width - 105 < x < display.width - 87 and 60 < y < 260:
user_choice = "Right"
return
def rotation():
global turn_degrees
spi = SPI(0, baudrate=40000000, sck=Pin(18), mosi=Pin(19))
display = Display(spi, dc=Pin(16), cs=Pin(17), rst=Pin(20))
y_center = display.height // 2
display.fill_rectangle(display.width - 155, 60, 18, 200,
color565(255, 128, 0))
display.draw_text8x8(display.width - 150, y_center - 48, "How many degree?",
color565(255, 255, 255), rotate=270,
background=color565(255, 128, 0))
display.draw_text8x8(display.width - 120, y_center - 40, "+",
color565(255, 255, 255), rotate=270,
background=color565(30, 128, 50))
display.draw_text8x8(display.width - 120, y_center , "-",
color565(255, 255, 255), rotate=270,
background=color565(100, 200, 50))
display.draw_text8x8(display.width - 120, y_center+30 , "OK",
color565(255, 255, 255), rotate=270,
background=color565(255, 128, 0))
while True:
x, y = get_touch_input()
# x=display.width - 100
# y=70
if display.width - 130 < x < y_center - 40 and 60 < y < 260:
turn_degrees +=10
display.draw_text8x8(display.width - 120, y_center+50 , f"Rotation={turn_degrees}",
color565(255, 255, 255), rotate=270,
background=color565(255, 128, 0))
elif display.width - 130 < x < y_center - 10 and 60 < y < 260:
turn_degrees -=10
display.draw_text8x8(display.width - 80, y_center-48 , f"Rotation={turn_degrees}",
color565(255, 255, 255), rotate=270,
background=color565(255, 128, 0))
if(turn_degrees<-20):
return
elif display.width - 120 < x < y_center+30 and 60 < y < 260:
return
def distance():
spi = SPI(0, baudrate=40000000, sck=Pin(18), mosi=Pin(19))
display = Display(spi, dc=Pin(16), cs=Pin(17), rst=Pin(20))
global move_distance
y_center = display.height // 2
display.fill_rectangle(display.width - 155, 60, 18, 200,
color565(255, 128, 0))
display.draw_text8x8(display.width - 150, y_center - 48, "How many cms?",
color565(255, 255, 255), rotate=270,
background=color565(255, 128, 0))
display.draw_text8x8(display.width - 120, y_center - 40, "+",
color565(255, 255, 255), rotate=270,
background=color565(30, 128, 50))
display.draw_text8x8(display.width - 120, y_center , "-",
color565(255, 255, 255), rotate=270,
background=color565(100, 200, 50))
display.draw_text8x8(display.width - 120, y_center+30 , "OK",
color565(255, 255, 255), rotate=270,
background=color565(255, 128, 0))
while True:
x, y = get_touch_input()
# x=display.width - 100
# y=70
if display.width - 130 < x < y_center - 40 and 60 < y < 260:
move_distance +=10
display.draw_text8x8(display.width - 120, y_center+50 , f"Distance={move_distance}cm",
color565(255, 255, 255), rotate=270,
background=color565(255, 128, 0))
elif display.width - 130 < x < y_center - 10 and 60 < y < 260:
move_distance -=10
display.draw_text8x8(display.width - 80, y_center-48 , f"Distance={move_distance}cm",
color565(255, 255, 255), rotate=270,
background=color565(255, 128, 0))
if(move_distance<-20):
return
elif display.width - 120 < x < y_center+30 and 60 < y < 260:
return
def run():
spi = SPI(0, baudrate=40000000, sck=Pin(18), mosi=Pin(19))
display = Display(spi, dc=Pin(16), cs=Pin(17), rst=Pin(20))
display.clear()
y_center = display.height // 2
display.fill_rectangle(display.width - 155, 60, 18, 200,
color565(255, 128, 0))
display.draw_text8x8(display.width - 150, y_center - 85, f"distans={move_distance} degree={turn_degrees}",
color565(255, 255, 255), rotate=270,
background=color565(255, 128, 0))
display.fill_rectangle(display.width - 105, 60, 18, 200,
color565(255, 128, 0))
display.draw_text8x8(display.width - 100, y_center - 50, f"Turn={user_choice}",
color565(255, 255, 255), rotate=270,
background=color565(255, 128, 0))
menu()
rotation()
distance()
run()