pimoroni-pico/micropython/examples/pico_display/button_test.py

84 wiersze
2.8 KiB
Python
Czysty Zwykły widok Historia

# This example shows you a simple, non-interrupt way of reading Pico Display's buttons with a loop that checks to see if buttons are pressed.
2024-07-15 14:23:22 +00:00
# If you have a Display Pack 2.0" or 2.8" use DISPLAY_PICO_DISPLAY_2 instead of DISPLAY_PICO_DISPLAY
import time
from pimoroni import Button
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY, PEN_P4
2024-07-15 14:23:22 +00:00
from pimoroni import RGBLED
# We're only using a few colours so we can use a 4 bit/16 colour palette and save RAM!
display = PicoGraphics(display=DISPLAY_PICO_DISPLAY, pen_type=PEN_P4, rotate=0)
display.set_backlight(0.5)
display.set_font("bitmap8")
2021-04-15 08:21:27 +00:00
button_a = Button(12)
button_b = Button(13)
button_x = Button(14)
button_y = Button(15)
2024-07-15 14:23:22 +00:00
# Set up the RGB LED For Display Pack and Display Pack 2.0":
led = RGBLED(6, 7, 8)
# For Display Pack 2.8" uncomment the line below and comment out the line above:
# led = RGBLED(26, 27, 28)
WHITE = display.create_pen(255, 255, 255)
BLACK = display.create_pen(0, 0, 0)
CYAN = display.create_pen(0, 255, 255)
MAGENTA = display.create_pen(255, 0, 255)
YELLOW = display.create_pen(255, 255, 0)
GREEN = display.create_pen(0, 255, 0)
# sets up a handy function we can call to clear the screen
2021-04-15 08:21:27 +00:00
def clear():
display.set_pen(BLACK)
2024-07-15 14:23:22 +00:00
led.set_rgb(0, 0, 0)
display.clear()
display.update()
2021-04-15 08:21:27 +00:00
# set up
clear()
while True:
if button_a.read(): # if a button press is detected then...
2021-02-16 16:52:48 +00:00
clear() # clear to black
display.set_pen(WHITE) # change the pen colour
2024-07-15 14:23:22 +00:00
led.set_rgb(255, 255, 255) # set the LED colour to match
display.text("Button A pressed", 10, 10, 240, 4) # display some text on the screen
display.update() # update the display
time.sleep(1) # pause for a sec
2021-02-16 16:52:48 +00:00
clear() # clear to black again
elif button_b.read():
2021-02-16 16:52:48 +00:00
clear()
display.set_pen(CYAN)
2024-07-15 14:23:22 +00:00
led.set_rgb(0, 255, 255)
display.text("Button B pressed", 10, 10, 240, 4)
display.update()
time.sleep(1)
2021-02-16 16:52:48 +00:00
clear()
elif button_x.read():
2021-02-16 16:52:48 +00:00
clear()
display.set_pen(MAGENTA)
2024-07-15 14:23:22 +00:00
led.set_rgb(255, 0, 255)
display.text("Button X pressed", 10, 10, 240, 4)
display.update()
time.sleep(1)
2021-02-16 16:52:48 +00:00
clear()
elif button_y.read():
2021-02-16 16:52:48 +00:00
clear()
display.set_pen(YELLOW)
2024-07-15 14:23:22 +00:00
led.set_rgb(255, 255, 0)
display.text("Button Y pressed", 10, 10, 240, 4)
display.update()
time.sleep(1)
2021-02-16 16:52:48 +00:00
clear()
else:
display.set_pen(GREEN)
2024-07-15 14:23:22 +00:00
led.set_rgb(0, 255, 0)
2021-02-16 16:52:48 +00:00
display.text("Press any button!", 10, 10, 240, 4)
display.update()
time.sleep(0.1) # this number is how frequently the Pico checks for button presses