/ Kitty_Paw_Keypad / code.py
code.py
1 # SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 import board 6 import displayio 7 import keypad 8 from adafruit_st7789 import ST7789 9 import adafruit_imageload 10 import usb_hid 11 from adafruit_hid.keyboard import Keyboard 12 from adafruit_hid.keycode import Keycode 13 import usb_midi 14 import adafruit_midi 15 from adafruit_midi.note_on import NoteOn 16 from adafruit_midi.note_off import NoteOff 17 18 # if you want to use this as an HID keyboard, set keyboard_mode to True 19 # otherwise, set it to False 20 keyboard_mode = True 21 # if you want to use this as a MIDI keyboard, set midi_mode to True 22 # otherwise, set it to False 23 midi_mode = False 24 25 # change keyboard shortcuts here 26 # defaults are shortcuts for save, cut, copy & paste 27 # comment out ctrl depending on windows or macOS 28 if keyboard_mode: 29 keyboard = Keyboard(usb_hid.devices) 30 # modifier for windows 31 ctrl = Keycode.CONTROL 32 # modifier for macOS 33 # ctrl = Keycode.COMMAND 34 key0 = Keycode.S 35 key1 = Keycode.X 36 key2 = Keycode.C 37 key3 = Keycode.V 38 shortcuts = [key0, key1, key2, key3] 39 40 # change MIDI note numbers here 41 if midi_mode: 42 midi = adafruit_midi.MIDI(midi_out=usb_midi.ports[1], out_channel=0) 43 midi_notes = [60, 61, 62, 63] 44 45 # Release any resources currently in use for the displays 46 displayio.release_displays() 47 48 # spi display setup 49 spi = board.SPI() 50 tft_cs = board.D7 51 tft_dc = board.D5 52 53 display_bus = displayio.FourWire( 54 spi, command=tft_dc, chip_select=tft_cs, reset=board.D6 55 ) 56 57 # display setup 58 display = ST7789(display_bus, width=240, height=240, rowstart=80) 59 60 bitmap, palette = adafruit_imageload.load("/partyParrotsSmol.bmp", 61 bitmap=displayio.Bitmap, 62 palette=displayio.Palette) 63 64 # Create a TileGrid to hold the bitmap 65 parrot0_grid = displayio.TileGrid(bitmap, pixel_shader=palette, 66 tile_height=32, tile_width=32) 67 68 # Create a Group to hold the TileGrid 69 group = displayio.Group(scale=4, x = 64, y = 32) 70 71 # Add the TileGrid to the Group 72 group.append(parrot0_grid) 73 74 # Add the Group to the Display 75 display.show(group) 76 77 # setup button pins 78 key_pins = ( 79 board.A0, 80 board.A1, 81 board.A2, 82 board.A3, 83 ) 84 85 # create keypad 86 keys = keypad.Keys(key_pins, value_when_pressed=False, pull=True) 87 88 p = 0 # variable for tilegrid index 89 90 while True: 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 98 if midi_mode: 99 # send note number 100 midi.send(NoteOn(midi_notes[event.key_number], 120)) 101 # if hid keyboard 102 if keyboard_mode: 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 112 if midi_mode: 113 # send note off message 114 midi.send(NoteOff(midi_notes[event.key_number], 120))