Skip to content

Commit 366fd30

Browse files
committed
Adding code for Kitty Paw Keypad
Adding code and bitmap for the Kitty Paw Keypad Learn guide. Code has options to either be MIDI keyboard or HID keyboard. Each time a key is pressed, the party parrot tile grid advances by one frame.
1 parent 2bfaf56 commit 366fd30

2 files changed

Lines changed: 132 additions & 0 deletions

File tree

Kitty_Paw_Keypad/code.py

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import board
2+
import displayio
3+
import digitalio
4+
from adafruit_st7789 import ST7789
5+
import usb_hid
6+
from adafruit_hid.keyboard import Keyboard
7+
from adafruit_hid.keycode import Keycode
8+
import usb_midi
9+
import adafruit_midi
10+
from adafruit_midi.note_on import NoteOn
11+
from adafruit_midi.note_off import NoteOff
12+
13+
# if you want to use this as an HID keyboard, set keyboard_mode to True
14+
# otherwise, set it to False
15+
keyboard_mode = True
16+
# if you want to use this as a MIDI keyboard, set midi_mode to True
17+
# otherwise, set it to False
18+
midi_mode = False
19+
20+
# change keyboard shortcuts here
21+
# defaults are shortcuts for save, cut, copy & paste
22+
# comment out ctrl depending on windows or macOS
23+
if keyboard_mode:
24+
keyboard = Keyboard(usb_hid.devices)
25+
# modifier for windows
26+
ctrl = Keycode.CONTROL
27+
# modifier for macOS
28+
# ctrl = Keycode.COMMAND
29+
key0 = Keycode.S
30+
key1 = Keycode.X
31+
key2 = Keycode.C
32+
key3 = Keycode.V
33+
shortcuts = [key0, key1, key2, key3]
34+
35+
# change MIDI note numbers here
36+
if midi_mode:
37+
midi = adafruit_midi.MIDI(midi_out=usb_midi.ports[1], out_channel=0)
38+
midi_notes = [60, 61, 62, 63]
39+
40+
# Release any resources currently in use for the displays
41+
displayio.release_displays()
42+
43+
# spi display setup
44+
spi = board.SPI()
45+
tft_cs = board.D7
46+
tft_dc = board.D5
47+
48+
display_bus = displayio.FourWire(
49+
spi, command=tft_dc, chip_select=tft_cs, reset=board.D6
50+
)
51+
52+
# display setup
53+
display = ST7789(display_bus, width=240, height=240, rowstart=80)
54+
55+
# bitmap setup
56+
bitmap = displayio.OnDiskBitmap(open("/parrot-240-sheet.bmp", "rb"))
57+
58+
# Create a TileGrid to hold the bitmap
59+
parrot0_grid = displayio.TileGrid(bitmap, pixel_shader=displayio.ColorConverter(),
60+
width=1, height=1,
61+
tile_height=240, tile_width=240,
62+
default_tile=0,
63+
x=0, y=0)
64+
65+
# Create a Group to hold the TileGrid
66+
group = displayio.Group()
67+
68+
# Add the TileGrid to the Group
69+
group.append(parrot0_grid)
70+
71+
# Add the Group to the Display
72+
display.show(group)
73+
74+
# digital pins for the buttons
75+
key_pins = [board.A0, board.A1, board.A2, board.A3]
76+
77+
# array for buttons
78+
keys = []
79+
80+
# setup buttons as inputs
81+
for key in key_pins:
82+
key_pin = digitalio.DigitalInOut(key)
83+
key_pin.direction = digitalio.Direction.INPUT
84+
key_pin.pull = digitalio.Pull.UP
85+
keys.append(key_pin)
86+
87+
p = 0 # variable for tilegrid index
88+
a = 0 # variable for tile position
89+
90+
# states for buttons
91+
key0_pressed = False
92+
key1_pressed = False
93+
key2_pressed = False
94+
key3_pressed = False
95+
96+
# array for button states
97+
key_states = [key0_pressed, key1_pressed, key2_pressed, key3_pressed]
98+
99+
while True:
100+
# default tile grid position
101+
parrot0_grid[a] = p
102+
103+
# iterate through 4 buttons
104+
for i in range(4):
105+
inputs = keys[i]
106+
# if button is pressed...
107+
if not inputs.value and key_states[i] is False:
108+
# tile grid advances by 1 frame
109+
p += 1
110+
# update button state
111+
key_states[i] = True
112+
# if a midi keyboard...
113+
if midi_mode:
114+
# send NoteOn for corresponding MIDI note
115+
midi.send(NoteOn(midi_notes[i], 120))
116+
# if an HID keyboard...
117+
if keyboard_mode:
118+
# send keyboard output for corresponding keycode
119+
# the default includes a modifier along with the keycode
120+
keyboard.send(ctrl, shortcuts[i])
121+
# if the tile grid's index is at 9...
122+
if p > 9:
123+
# reset the index to 0
124+
p = 0
125+
# if the button is released...
126+
if inputs.value and key_states[i] is True:
127+
# update button state
128+
key_states[i] = False
129+
# if a midi keyboard...
130+
if midi_mode:
131+
# send NoteOff for corresponding MIDI note
132+
midi.send(NoteOff(midi_notes[i], 120))
563 KB
Binary file not shown.

0 commit comments

Comments
 (0)