forked from FAR-Lab/Interactive-Lab-Hub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
screen_test.py
60 lines (53 loc) · 1.98 KB
/
screen_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import digitalio
import board
from adafruit_rgb_display.rgb import color565
import adafruit_rgb_display.st7789 as st7789
import webcolors
# The display uses a communication protocol called SPI.
# SPI will not be covered in depth in this course.
# you can read more https://www.circuitbasics.com/basics-of-the-spi-communication-protocol/
cs_pin = digitalio.DigitalInOut(board.CE0)
dc_pin = digitalio.DigitalInOut(board.D25)
reset_pin = None
BAUDRATE = 64000000 # the rate the screen talks to the pi
# Create the ST7789 display:
display = st7789.ST7789(
board.SPI(),
cs=cs_pin,
dc=dc_pin,
rst=reset_pin,
baudrate=BAUDRATE,
width=135,
height=240,
x_offset=53,
y_offset=40,
)
# these setup the code for our buttons and the backlight and tell the pi to treat the GPIO pins as digitalIO vs analogIO
backlight = digitalio.DigitalInOut(board.D22)
backlight.switch_to_output()
backlight.value = True
buttonA = digitalio.DigitalInOut(board.D23)
buttonB = digitalio.DigitalInOut(board.D24)
buttonA.switch_to_input()
buttonB.switch_to_input()
# get a color from the user
screenColor = None
while not screenColor:
try:
# get a color from the user and convert it to RGB
screenColor = color565(*list(webcolors.name_to_rgb(input('Type the name of a color and hit enter: '))))
except ValueError:
# catch colors we don't recognize and go again
print("whoops I don't know that one")
# Main loop:
while True:
if buttonA.value and buttonB.value:
backlight.value = False # turn off backlight
else:
backlight.value = True # turn on backlight
if buttonB.value and not buttonA.value: # just button A pressed
display.fill(screenColor) # set the screen to the users color
if buttonA.value and not buttonB.value: # just button B pressed
display.fill(color565(255, 255, 255)) # set the screen to white
if not buttonA.value and not buttonB.value: # none pressed
display.fill(color565(0, 255, 0)) # green