|
| 1 | +# SPDX-FileCopyrightText: 2023 Robert Dale Smith for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +# Simple Super Nintendo controller to standard USB HID gamepad with DirectInput button mapping. |
| 5 | +# Tested on KB2040 |
| 6 | + |
| 7 | +import time |
| 8 | +import board |
| 9 | +import digitalio |
| 10 | +import usb_hid |
| 11 | + |
| 12 | +# Update the SNES Controller pins based on your input |
| 13 | +LATCH_PIN = board.D6 |
| 14 | +CLOCK_PIN = board.D5 |
| 15 | +DATA_PIN = board.D7 |
| 16 | + |
| 17 | +# Set up pins for SNES Controller |
| 18 | +latch = digitalio.DigitalInOut(LATCH_PIN) |
| 19 | +latch.direction = digitalio.Direction.OUTPUT |
| 20 | + |
| 21 | +clock = digitalio.DigitalInOut(CLOCK_PIN) |
| 22 | +clock.direction = digitalio.Direction.OUTPUT |
| 23 | + |
| 24 | +data = digitalio.DigitalInOut(DATA_PIN) |
| 25 | +data.direction = digitalio.Direction.INPUT |
| 26 | +data.pull = digitalio.Pull.UP # pull-up as a default |
| 27 | + |
| 28 | +# SNES to USB button mapping |
| 29 | +buttonmap = { |
| 30 | + "B": (0, 0, 0x2), # Button 1 |
| 31 | + "Y": (1, 0, 0x1), # Button 3 |
| 32 | + "Select": (2, 1, 0x01), # Button 9 |
| 33 | + "Start": (3, 1, 0x02), # Button 10 |
| 34 | + "Up": (4, 0, 0), # D-pad North |
| 35 | + "Down": (5, 0, 0), # D-pad South |
| 36 | + "Left": (6, 0, 0), # D-pad East |
| 37 | + "Right": (7, 0, 0), # D-pad West |
| 38 | + "A": (8, 0, 0x4), # Button 2 |
| 39 | + "X": (9, 0, 0x8), # Button 4 |
| 40 | + "L": (10, 0, 0x10), # Button 5 |
| 41 | + "R": (11, 0, 0x20) # Button 6 |
| 42 | +} |
| 43 | + |
| 44 | +# D-pad buttons to hat-switch value |
| 45 | +dpad_state = { |
| 46 | + "Up": 0, |
| 47 | + "Down": 0, |
| 48 | + "Left": 0, |
| 49 | + "Right": 0, |
| 50 | +} |
| 51 | + |
| 52 | +hat_map = { |
| 53 | + (0, 0, 0, 0): 0x08, # Released |
| 54 | + (1, 0, 0, 0): 0x00, # N |
| 55 | + (1, 1, 0, 0): 0x01, # NE |
| 56 | + (0, 1, 0, 0): 0x02, # E |
| 57 | + (0, 1, 0, 1): 0x03, # SE |
| 58 | + (0, 0, 0, 1): 0x04, # S |
| 59 | + (0, 0, 1, 1): 0x05, # SW |
| 60 | + (0, 0, 1, 0): 0x06, # W |
| 61 | + (1, 0, 1, 0): 0x07, # NW |
| 62 | +} |
| 63 | + |
| 64 | +def read_snes_controller(): |
| 65 | + data_bits = [] |
| 66 | + latch.value = True |
| 67 | + time.sleep(0.000012) # 12µs |
| 68 | + latch.value = False |
| 69 | + |
| 70 | + for _ in range(16): |
| 71 | + time.sleep(0.000006) # Wait 6µs |
| 72 | + data_bits.append(data.value) |
| 73 | + |
| 74 | + clock.value = True |
| 75 | + time.sleep(0.000006) # 6µs |
| 76 | + clock.value = False |
| 77 | + time.sleep(0.000006) # 6µs |
| 78 | + |
| 79 | + return data_bits |
| 80 | + |
| 81 | +# Find the gamepad device in the usb_hid devices |
| 82 | +gamepad_device = None |
| 83 | +for device in usb_hid.devices: |
| 84 | + if device.usage_page == 0x01 and device.usage == 0x05: |
| 85 | + gamepad_device = device |
| 86 | + break |
| 87 | + |
| 88 | +if gamepad_device is not None: |
| 89 | + print("Gamepad device found!") |
| 90 | +else: |
| 91 | + print("Gamepad device not found.") |
| 92 | + |
| 93 | +report = bytearray(19) |
| 94 | +report[2] = 0x08 # default released hat switch value |
| 95 | +prev_report = bytearray(report) |
| 96 | + |
| 97 | +while True: |
| 98 | + button_state = read_snes_controller() |
| 99 | + all_buttons = list(buttonmap.keys()) |
| 100 | + |
| 101 | + for idx, button in enumerate(all_buttons): |
| 102 | + index, byte_index, button_value = buttonmap[button] |
| 103 | + is_pressed = not button_state [index] # True if button is pressed |
| 104 | + |
| 105 | + if button in dpad_state: # If it's a direction button |
| 106 | + dpad_state[button] = 1 if is_pressed else 0 |
| 107 | + else: |
| 108 | + if is_pressed: |
| 109 | + report[byte_index] |= button_value |
| 110 | + else: # not pressed, reset button state |
| 111 | + report[byte_index] &= ~button_value |
| 112 | + |
| 113 | + # SOCD (up priority and neutral horizontal) |
| 114 | + if (dpad_state["Up"] and dpad_state["Down"]): |
| 115 | + dpad_state["Down"] = 0 |
| 116 | + if (dpad_state["Left"] and dpad_state["Right"]): |
| 117 | + dpad_state["Left"] = 0 |
| 118 | + dpad_state["Right"] = 0 |
| 119 | + |
| 120 | + # Extract the dpad_state to a tuple and get the corresponding hat value |
| 121 | + dpad_tuple = (dpad_state["Up"], dpad_state["Right"], dpad_state["Left"], dpad_state["Down"]) |
| 122 | + report[2] = hat_map[dpad_tuple] # Assuming report[2] is the byte for the hat switch |
| 123 | + |
| 124 | + if prev_report != report: |
| 125 | + gamepad_device.send_report(report) |
| 126 | + prev_report[:] = report |
| 127 | + |
| 128 | + time.sleep(0.1) |
0 commit comments