Skip to content

Commit b415fea

Browse files
authored
Merge pull request #1874 from dhalbert/speed-up-Kitty_Paw_Keypad
Speed up Kitty_Paw_Keypad
2 parents b9ec8de + 46c814a commit b415fea

3 files changed

Lines changed: 39 additions & 66 deletions

File tree

Kitty_Paw_Keypad/code.py

Lines changed: 39 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
# SPDX-FileCopyrightText: 2021 Liz Clark for Adafruit Industries
1+
# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries
22
#
33
# SPDX-License-Identifier: MIT
44

55
import board
66
import displayio
7-
import digitalio
7+
import keypad
88
from adafruit_st7789 import ST7789
9+
import adafruit_imageload
910
import usb_hid
1011
from adafruit_hid.keyboard import Keyboard
1112
from adafruit_hid.keycode import Keycode
@@ -56,86 +57,58 @@
5657
# display setup
5758
display = ST7789(display_bus, width=240, height=240, rowstart=80)
5859

59-
# CircuitPython 6 & 7 compatible
60-
# bitmap setup
61-
bitmap = displayio.OnDiskBitmap(open("/parrot-240-sheet.bmp", "rb"))
60+
bitmap, palette = adafruit_imageload.load("/partyParrotsSmol.bmp",
61+
bitmap=displayio.Bitmap,
62+
palette=displayio.Palette)
6263

6364
# Create a TileGrid to hold the bitmap
64-
parrot0_grid = displayio.TileGrid(bitmap, pixel_shader=getattr(bitmap, 'pixel_shader', displayio.ColorConverter()),
65-
tile_height=240, tile_width=240)
66-
67-
# # CircuitPython 7+ compatible
68-
# bitmap = displayio.OnDiskBitmap("/parrot-240-sheet.bmp")
69-
70-
# Create a TileGrid to hold the bitmap
71-
# parrot0_grid = displayio.TileGrid(bitmap, pixel_shader=bitmap.pixel_shader,
72-
# tile_height=240, tile_width=240)
65+
parrot0_grid = displayio.TileGrid(bitmap, pixel_shader=palette,
66+
tile_height=32, tile_width=32)
7367

7468
# Create a Group to hold the TileGrid
75-
group = displayio.Group()
69+
group = displayio.Group(scale=4, x = 64, y = 32)
7670

7771
# Add the TileGrid to the Group
7872
group.append(parrot0_grid)
7973

8074
# Add the Group to the Display
8175
display.show(group)
8276

83-
# digital pins for the buttons
84-
key_pins = [board.A0, board.A1, board.A2, board.A3]
85-
86-
# array for buttons
87-
keys = []
77+
# setup button pins
78+
key_pins = (
79+
board.A0,
80+
board.A1,
81+
board.A2,
82+
board.A3,
83+
)
8884

89-
# setup buttons as inputs
90-
for key in key_pins:
91-
key_pin = digitalio.DigitalInOut(key)
92-
key_pin.direction = digitalio.Direction.INPUT
93-
key_pin.pull = digitalio.Pull.UP
94-
keys.append(key_pin)
85+
# create keypad
86+
keys = keypad.Keys(key_pins, value_when_pressed=False, pull=True)
9587

9688
p = 0 # variable for tilegrid index
97-
a = 0 # variable for tile position
98-
99-
# states for buttons
100-
key0_pressed = False
101-
key1_pressed = False
102-
key2_pressed = False
103-
key3_pressed = False
104-
105-
# array for button states
106-
key_states = [key0_pressed, key1_pressed, key2_pressed, key3_pressed]
10789

10890
while True:
109-
# default tile grid position
110-
parrot0_grid[a] = p
111-
112-
# iterate through 4 buttons
113-
for i in range(4):
114-
inputs = keys[i]
115-
# if button is pressed...
116-
if not inputs.value and key_states[i] is False:
117-
# tile grid advances by 1 frame
118-
p += 1
119-
# update button state
120-
key_states[i] = True
121-
# if a midi keyboard...
91+
92+
# get keypad inputs
93+
event = keys.events.get()
94+
if event:
95+
# if a key is pressed..
96+
if event.pressed:
97+
# if a midi keyboard
12298
if midi_mode:
123-
# send NoteOn for corresponding MIDI note
124-
midi.send(NoteOn(midi_notes[i], 120))
125-
# if an HID keyboard...
99+
# send note number
100+
midi.send(NoteOn(midi_notes[event.key_number], 120))
101+
# if hid keyboard
126102
if keyboard_mode:
127-
# send keyboard output for corresponding keycode
128-
# the default includes a modifier along with the keycode
129-
keyboard.send(ctrl, shortcuts[i])
130-
# if the tile grid's index is at 9...
131-
if p > 9:
132-
# reset the index to 0
133-
p = 0
134-
# if the button is released...
135-
if inputs.value and key_states[i] is True:
136-
# update button state
137-
key_states[i] = False
138-
# if a midi keyboard...
103+
# send hid keyboard shortcut
104+
keyboard.send(ctrl, shortcuts[event.key_number])
105+
# advance parrot index
106+
p = (p + 1) % 10
107+
# update parrot bitmap
108+
parrot0_grid[0] = p
109+
# if a key is released
110+
if event.released:
111+
# if a midi keyboard
139112
if midi_mode:
140-
# send NoteOff for corresponding MIDI note
141-
midi.send(NoteOff(midi_notes[i], 120))
113+
# send note off message
114+
midi.send(NoteOff(midi_notes[event.key_number], 120))
-563 KB
Binary file not shown.
11.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)