-
Notifications
You must be signed in to change notification settings - Fork 0
/
buttons.py
executable file
·139 lines (115 loc) · 2.72 KB
/
buttons.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python3
import digitalio
import board
from PIL import Image, ImageDraw, ImageFont
from adafruit_rgb_display.rgb import color565
import adafruit_rgb_display.st7789 as st7789
from gpiozero import Button, DigitalOutputDevice
from sys import exit
from time import sleep
from subprocess import run
BORDER = 20
FONTSIZE = 24
BAUDRATE = 64000000
BOUNCE = None
DELAY = 0.1
START = 95
spi = board.SPI()
disp = st7789.ST7789(
spi,
rotation=90,
width=135,
height=240,
x_offset=53,
y_offset=40,
cs=digitalio.DigitalInOut(board.CE0),
dc=digitalio.DigitalInOut(board.D25),
rst=None,
baudrate=BAUDRATE
)
if disp.rotation % 180 == 90:
height = disp.width
width = disp.height
else:
width = disp.width
height = disp.height
backlight = digitalio.DigitalInOut(board.D22)
backlight.switch_to_output()
backlight.value = True
output = '>'
val = START
cur = ''
caps = False
def do_select():
global caps, val
if val != START:
if caps and val <= 90:
val = val + 32
elif not caps and val <= 122:
val = val - 32
caps = not caps
def do_start():
global running
running = False
def do_up():
global val
if val == START:
val = 97
else:
val = val + 1
def do_down():
global val
if val == START:
val = 97
else:
val = val - 1
def do_left():
global output, val
if len(output) > 1:
if val == START:
output = output[0 : len(output) - 1]
else:
val = START
def do_right():
global output, val
if val == START:
val = 32
output = output + chr(val)
val = START
select = Button(23, bounce_time=BOUNCE)
select.when_pressed = do_select
start = Button(24, bounce_time=BOUNCE, hold_time=3)
start.when_held = do_start
# 13 is ground for 'up'
pin13 = DigitalOutputDevice(13)
pin13.off()
up = Button(6, bounce_time=BOUNCE, hold_time=0.25, hold_repeat=True)
up.when_pressed = do_up
up.when_held = do_up
down = Button(12, bounce_time=BOUNCE, hold_time=0.25, hold_repeat=True)
down.when_pressed = do_down
down.when_held = do_down
left = Button(5, bounce_time=BOUNCE)
left.when_pressed = do_left
right = Button(16, bounce_time=BOUNCE)
right.when_pressed = do_right
image = Image.new("RGB", (width, height))
draw = ImageDraw.Draw(image)
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", FONTSIZE)
def dprint(text):
draw.rectangle((0, 0, width, height), fill=(0, 0, 0))
draw.text(
(0, 0),
text,
font=font,
fill=(255, 255, 0)
)
disp.image(image)
running = True
while running:
try:
dprint(output + chr(val))
sleep(DELAY)
except KeyboardInterrupt:
exit(0)
exit(1)