|
1 | | -# SPDX-FileCopyrightText: 2021 Liz Clark for Adafruit Industries |
| 1 | +# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries |
2 | 2 | # |
3 | 3 | # SPDX-License-Identifier: MIT |
4 | 4 |
|
5 | 5 | import board |
6 | 6 | import displayio |
7 | | -import digitalio |
| 7 | +import keypad |
8 | 8 | from adafruit_st7789 import ST7789 |
| 9 | +import adafruit_imageload |
9 | 10 | import usb_hid |
10 | 11 | from adafruit_hid.keyboard import Keyboard |
11 | 12 | from adafruit_hid.keycode import Keycode |
|
56 | 57 | # display setup |
57 | 58 | display = ST7789(display_bus, width=240, height=240, rowstart=80) |
58 | 59 |
|
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) |
62 | 63 |
|
63 | 64 | # 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) |
73 | 67 |
|
74 | 68 | # Create a Group to hold the TileGrid |
75 | | -group = displayio.Group() |
| 69 | +group = displayio.Group(scale=4, x = 64, y = 32) |
76 | 70 |
|
77 | 71 | # Add the TileGrid to the Group |
78 | 72 | group.append(parrot0_grid) |
79 | 73 |
|
80 | 74 | # Add the Group to the Display |
81 | 75 | display.show(group) |
82 | 76 |
|
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 | +) |
88 | 84 |
|
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) |
95 | 87 |
|
96 | 88 | 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] |
107 | 89 |
|
108 | 90 | 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 |
122 | 98 | 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 |
126 | 102 | 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 |
139 | 112 | 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)) |
0 commit comments