|
| 1 | +# SPDX-FileCopyrightText: 2022 john park for Adafruit Industries |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +# Pico Four Step Switch Keypad Demo |
| 4 | +import time |
| 5 | +import board |
| 6 | +from digitalio import Direction, DigitalInOut, Pull |
| 7 | +from adafruit_debouncer import Debouncer |
| 8 | + |
| 9 | +board_led = DigitalInOut(board.LED) |
| 10 | +board_led.direction = Direction.OUTPUT |
| 11 | +board_led.value = True |
| 12 | + |
| 13 | +switch_pins = (board.GP6, board.GP7, board.GP8, board.GP9) |
| 14 | +step_switches = [] |
| 15 | +for switch_pin in switch_pins: |
| 16 | + tmp_switch_pin = DigitalInOut(switch_pin) |
| 17 | + tmp_switch_pin.pull = Pull.UP |
| 18 | + step_switches.append(Debouncer(tmp_switch_pin)) |
| 19 | + |
| 20 | +led_pins = (board.GP2, board.GP3, board.GP4, board.GP5) |
| 21 | +leds = [] |
| 22 | +for led_pin in led_pins: |
| 23 | + tmp_led_pin = DigitalInOut(led_pin) |
| 24 | + tmp_led_pin.direction = Direction.OUTPUT |
| 25 | + tmp_led_pin.value = False |
| 26 | + leds.append(tmp_led_pin) |
| 27 | + |
| 28 | +def blink_led(led_num, pause, repeat): |
| 29 | + for __ in range(repeat * 2): |
| 30 | + leds[led_num].value = not leds[led_num].value |
| 31 | + time.sleep(pause) |
| 32 | + |
| 33 | +def blink_all_leds(pause, repeat): |
| 34 | + for __ in range(repeat * 2): |
| 35 | + for led in leds: |
| 36 | + led.value = not led.value |
| 37 | + time.sleep(pause) |
| 38 | + |
| 39 | +blink_all_leds(0.1, 4) |
| 40 | + |
| 41 | + |
| 42 | +mode_picked = False # state of mode selection |
| 43 | +mode_choice = 0 # MIDI mode, desk switcher mode, etc. |
| 44 | +modes = (0, 1, 2, 3) |
| 45 | +mode_names = ("MIDI", "DESK", "SELECTOR", "COPY-PASTE") |
| 46 | + |
| 47 | +print("Select the mode by pressing a button...") |
| 48 | +while not mode_picked: # program waits for a mode to be picked |
| 49 | + for i in range(len(step_switches)): |
| 50 | + step_switches[i].update() |
| 51 | + if step_switches[i].fell: |
| 52 | + mode_choice = i |
| 53 | + print(mode_names[mode_choice], "mode") |
| 54 | + mode_picked = True |
| 55 | + |
| 56 | +if mode_choice == 0: # MIDI mode |
| 57 | + import usb_midi |
| 58 | + import adafruit_midi |
| 59 | + from adafruit_midi.control_change import ControlChange |
| 60 | + midi = adafruit_midi.MIDI( |
| 61 | + midi_in=usb_midi.ports[0], |
| 62 | + in_channel=0, |
| 63 | + midi_out=usb_midi.ports[1], |
| 64 | + out_channel=0 |
| 65 | + ) |
| 66 | + cc_num = [16, 17, 18, 19] |
| 67 | + cc_state = [False, False, False, False] |
| 68 | + |
| 69 | +else: # HID modes |
| 70 | + import usb_hid |
| 71 | + from adafruit_hid.keyboard import Keyboard |
| 72 | + from adafruit_hid.keycode import Keycode |
| 73 | + |
| 74 | +if mode_choice == 1: # Mac Desktop switcher mode |
| 75 | + kpd = Keyboard(usb_hid.devices) |
| 76 | + MODIFIER = Keycode.CONTROL |
| 77 | + KEYMAP = ( |
| 78 | + ("Desktop 1", [MODIFIER, Keycode.ONE]), |
| 79 | + ("Desktop 2", [MODIFIER, Keycode.TWO]), |
| 80 | + ("Desktop 3", [MODIFIER, Keycode.THREE]), |
| 81 | + ("Desktop 4", [MODIFIER, Keycode.FOUR]), |
| 82 | + ) |
| 83 | + |
| 84 | +if mode_choice == 2: # SELECTOR mode for game weapon slot, Wirecast, etc. |
| 85 | + kpd = Keyboard(usb_hid.devices) |
| 86 | + MODIFIER = Keycode.SHIFT |
| 87 | + KEYMAP = ( |
| 88 | + ("Selector 1", [MODIFIER, Keycode.ONE]), |
| 89 | + ("Selector 2", [MODIFIER, Keycode.TWO]), |
| 90 | + ("Selector 3", [MODIFIER, Keycode.THREE]), |
| 91 | + ("Selector 4", [MODIFIER, Keycode.FOUR]), |
| 92 | + ) |
| 93 | + |
| 94 | +if mode_choice == 3: # Copy/Paste mode |
| 95 | + kpd = Keyboard(usb_hid.devices) |
| 96 | + # Choose the correct modifier key for Windows or Mac. |
| 97 | + # MODIFIER = Keycode.CONTROL # For Windows |
| 98 | + MODIFIER = Keycode.COMMAND |
| 99 | + KEYMAP = ( |
| 100 | + ("wire 1", [MODIFIER, Keycode.A]), # select all |
| 101 | + ("wire 2", [MODIFIER, Keycode.X]), # cut |
| 102 | + ("wire 3", [MODIFIER, Keycode.C]), # copy |
| 103 | + ("wire 4", [MODIFIER, Keycode.V]), # paste |
| 104 | + ) |
| 105 | + |
| 106 | +blink_led(mode_choice, 0.1, 3) |
| 107 | + |
| 108 | + |
| 109 | +while True: |
| 110 | + for i in range(len(step_switches)): |
| 111 | + step_switches[i].update() |
| 112 | + if step_switches[i].fell: |
| 113 | + print(i, "pressed") |
| 114 | + if mode_choice == 0: |
| 115 | + leds[i].value = not leds[i].value |
| 116 | + if cc_state[i] is False: |
| 117 | + midi.send(ControlChange(cc_num[i], 127)) |
| 118 | + cc_state[i] = True |
| 119 | + else: |
| 120 | + midi.send(ControlChange(cc_num[i], 0)) |
| 121 | + cc_state[i] = False |
| 122 | + else: |
| 123 | + print(KEYMAP[i][0]) |
| 124 | + kpd.send(*KEYMAP[i][1]) |
| 125 | + for switch_led in leds: # blank the LEDs first |
| 126 | + switch_led.value = False |
| 127 | + leds[i].value = True # light selected switch LED |
0 commit comments