from time import sleep
from ili9341 import Display, color565
from machine import Pin, SPI # type: ignore
from xct2046 import Touch
def rotation(touch):
"""Handle input for rotation using a touch-based interface."""
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))
turn_degrees = 0
coords=(60,60)
while True:
#coords = touch.get_touch()
if coords:
x, y = coords
if 50 <= x <= 150 and 50 <= y <= 100: # "Increase" touch zone
turn_degrees += 10
elif 50 <= x <= 150 and 100 <= y <= 150: # "Decrease" touch zone
turn_degrees = max(0, turn_degrees - 10)
elif 200 <= x <= 240 and 150 <= y <= 200: # "Done" touch zone
break
# Update display with the new value
display.fill_rectangle(display.width - 150, y_center, 50, 20,
color565(255, 128, 0)) # Clear previous value
display.draw_text8x8(display.width - 130, y_center,
f"{turn_degrees} deg", color565(255, 255, 255),
rotate=270, background=color565(255, 128, 0))
coords=(0,0)
return turn_degrees
def distance(touch):
"""Handle input for distance using a touch-based interface."""
global travel_distance
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(0, 128, 255))
display.draw_text8x8(display.width - 150, y_center - 48, "How many cm?",
color565(255, 255, 255), rotate=270,
background=color565(0, 128, 255))
travel_distance = 0
while True:
coords = touch.get_touch()
if coords:
x, y = coords
if 50 <= x <= 150 and 50 <= y <= 100: # "Increase" touch zone
travel_distance += 10
elif 50 <= x <= 150 and 100 <= y <= 150: # "Decrease" touch zone
travel_distance = max(0, travel_distance - 10)
elif 200 <= x <= 240 and 150 <= y <= 200: # "Done" touch zone
break
# Update display with the new value
display.fill_rectangle(display.width - 150, y_center, 50, 20,
color565(0, 128, 255)) # Clear previous value
display.draw_text8x8(display.width - 150, y_center,
f"{travel_distance} cm", color565(255, 255, 255),
rotate=270, background=color565(0, 128, 255))
return travel_distance
def main():
"""Main program for handling rotation and distance inputs."""
spi = SPI(0, baudrate=40000000, sck=Pin(18), mosi=Pin(19))
touch = Touch(spi, cs=Pin(22))
degrees = rotation(touch)
print(f"Turn Degrees: {degrees}")
distance_value = distance(touch)
print(f"Travel Distance: {distance_value}")
main()