from machine import Pin, SPI
from ili9341 import Display, color565
from time import sleep
import xpt206 # Assuming you're using this library for the touchscreen
# Initialize SPI for Display (ILI9341)
spi = SPI(0, baudrate=10000000, polarity=1, phase=1, sck=Pin(18), mosi=Pin(19), miso=Pin(16))
display = Display(spi, dc=Pin(15), cs=Pin(17), rst=Pin(14), width=240, height=320)
# Initialize SPI for Touchscreen (XPT2046)
# Assuming the touchscreen uses the same SPI bus and CS pin is controlled separately
cs_touch = Pin(13,Pin.OUT) # CS pin for the touchscreen
touch = xpt206.Touch(spi, cs_touch)
def draw_text(display, text, x, y, color=color565(255, 255, 255), size=2):
"""Helper function to draw text on the display."""
display.draw_text8x8(x, y, text, color)
def draw_menu():
"""Display the main menu."""
display.clear()
draw_text(display, "Menu:", 10, 10, color565(0, 255, 0))
draw_text(display, "1. Turn Left", 10, 50, color565(255, 255, 0))
draw_text(display, "2. Turn Right", 10, 80, color565(255, 255, 0))
def get_touch_input():
"""Get touch input and return x, y coordinates."""
cs_touch.value(0) # Enable touchscreen (set CS to LOW)
while True:
p = touch.get_touch()
if p:
x, y = p
print(f"Touch detected at: {x}, {y}") # Debugging: Print touch coordinates
cs_touch.value(1) # Disable touchscreen (set CS to HIGH)
return x, y
def ask_degrees():
"""Ask the user for degrees to turn."""
display.clear()
draw_text(display, "Enter Degrees:", 10, 10, color565(0, 255, 255))
sleep(2) # Placeholder for user input
return 90 # Replace this with actual input handling
def ask_distance():
"""Ask the user for distance to move."""
display.clear()
draw_text(display, "Enter Distance (cm):", 10, 10, color565(0, 255, 255))
sleep(2) # Placeholder for user input
return 50 # Replace this with actual input handling
def main():
"""Main program logic."""
draw_menu()
# Wait for the user to choose the menu option by touching the screen
x, y = get_touch_input()
# Check if the user clicked on "Turn Left" or "Turn Right"
if 10 < x < 120 and 50 < y < 80:
action = "Turn Left"
elif 10 < x < 120 and 80 < y < 110:
action = "Turn Right"
else:
action = "Invalid"
degrees = ask_degrees()
distance = ask_distance()
display.clear()
draw_text(display, f"Action: {action}", 10, 10, color565(255, 0, 0))
draw_text(display, f"Degrees: {degrees}", 10, 50, color565(255, 0, 255))
draw_text(display, f"Distance: {distance} cm", 10, 90, color565(0, 255, 0))
sleep(5)
if __name__ == '__main__':
main()
Loading
pi-pico-w
pi-pico-w
Loading
ili9341-cap-touch
ili9341-cap-touch