from machine import Pin, SPI
import st7789 # Library for ST7789 TFT display
import time
# Initialize SPI for TFT Display
spi = SPI(1, baudrate=40000000, sck=Pin(2), mosi=Pin(3))
display = st7789.ST7789(
spi,
width=240, # Display width
height=240, # Display height
cs=Pin(5), # Chip Select
dc=Pin(4), # Data/Command
rst=Pin(6), # Reset
rotation=1 # Rotate display (0-3)
)
# Initialize Buttons
up = Pin(12, Pin.IN, Pin.PULL_UP)
down = Pin(13, Pin.IN, Pin.PULL_UP)
left = Pin(14, Pin.IN, Pin.PULL_UP)
right = Pin(15, Pin.IN, Pin.PULL_UP)
select = Pin(16, Pin.IN, Pin.PULL_UP)
# Display Initialization
display.fill(st7789.BLACK)
display.text("Mini Laptop", 10, 10, st7789.WHITE)
display.text("Press Select", 10, 30, st7789.GREEN)
# Main Menu
menu_items = ["Text Editor", "Paint", "Settings"]
selected_item = 0
def draw_menu():
display.fill(st7789.BLACK)
for i, item in enumerate(menu_items):
color = st7789.YELLOW if i == selected_item else st7789.WHITE
display.text(item, 10, 10 + i * 20, color)
# Text Editor
def text_editor():
display.fill(st7789.BLACK)
display.text("Text Editor", 10, 10, st7789.WHITE)
display.text("Type with buttons:", 10, 30, st7789.GREEN)
text_buffer = ""
cursor_x, cursor_y = 10, 50
while True:
if not up.value(): # Up button
text_buffer += "U"
if not down.value(): # Down button
text_buffer += "D"
if not left.value(): # Left button
text_buffer += "L"
if not right.value(): # Right button
text_buffer += "R"
if not select.value(): # Select button
break
display.fill_rect(cursor_x, cursor_y, 240, 20, st7789.BLACK)
display.text(text_buffer, cursor_x, cursor_y, st7789.WHITE)
time.sleep(0.2)
# Paint App
def paint():
display.fill(st7789.BLACK)
display.text("Paint App", 10, 10, st7789.WHITE)
display.text("Draw with buttons:", 10, 30, st7789.GREEN)
x, y = 120, 120
while True:
if not up.value(): # Up button
y -= 1
if not down.value(): # Down button
y += 1
if not left.value(): # Left button
x -= 1
if not right.value(): # Right button
x += 1
if not select.value(): # Select button
break
display.pixel(x, y, st7789.RED)
time.sleep(0.01)
# Main Loop
while True:
draw_menu()
# Handle Button Input
if not up.value() and selected_item > 0:
selected_item -= 1
time.sleep(0.2)
if not down.value() and selected_item < len(menu_items) - 1:
selected_item += 1
time.sleep(0.2)
if not select.value():
if selected_item == 0:
text_editor()
elif selected_item == 1:
paint()
elif selected_item == 2:
display.fill(st7789.BLACK)
display.text("Settings", 10, 10, st7789.WHITE)
time.sleep(2)