resolves linting errors for Super Nintendo USB controller guide code
This commit is contained in:
parent
8834e11d42
commit
956b146094
2 changed files with 82 additions and 82 deletions
|
|
@ -1,74 +1,74 @@
|
|||
# SPDX-FileCopyrightText: 2023 Robert Dale Smith for Adafruit Industries
|
||||
#
|
||||
# SPDX-License-Identifier: Unlicense
|
||||
|
||||
import usb_hid
|
||||
|
||||
# This is only one example of a gamepad descriptor, and may not suit your needs.
|
||||
GAMEPAD_REPORT_DESCRIPTOR = bytes((
|
||||
0x05, 0x01, # USAGE_PAGE (Generic Desktop)
|
||||
0x09, 0x05, # USAGE (Gamepad)
|
||||
0xa1, 0x01, # COLLECTION (Application)
|
||||
0x15, 0x00, # LOGICAL_MINIMUM (0)
|
||||
0x25, 0x01, # LOGICAL_MAXIMUM (1)
|
||||
0x35, 0x00, # PHYSICAL_MINIMUM (0)
|
||||
0x45, 0x01, # PHYSICAL_MAXIMUM (1)
|
||||
0x75, 0x01, # REPORT_SIZE (1)
|
||||
0x95, 0x0e, # REPORT_COUNT (14)
|
||||
0x05, 0x09, # USAGE_PAGE (Button)
|
||||
0x19, 0x01, # USAGE_MINIMUM (Button 1)
|
||||
0x29, 0x0e, # USAGE_MAXIMUM (Button 14)
|
||||
0x81, 0x02, # INPUT (Data,Var,Abs)
|
||||
0x95, 0x02, # REPORT_COUNT (3)
|
||||
0x81, 0x01, # INPUT (Cnst,Ary,Abs)
|
||||
0x05, 0x01, # USAGE_PAGE (Generic Desktop)
|
||||
0x25, 0x07, # LOGICAL_MAXIMUM (7)
|
||||
0x46, 0x3b, 0x01, # PHYSICAL_MAXIMUM (315)
|
||||
0x75, 0x04, # REPORT_SIZE (4)
|
||||
0x95, 0x01, # REPORT_COUNT (1)
|
||||
0x65, 0x14, # UNIT (Eng Rot:Angular Pos)
|
||||
0x09, 0x39, # USAGE (Hat switch)
|
||||
0x81, 0x42, # INPUT (Data,Var,Abs,Null)
|
||||
0x65, 0x00, # UNIT (None)
|
||||
0x95, 0x01, # REPORT_COUNT (1)
|
||||
0x81, 0x01, # INPUT (Cnst,Ary,Abs)
|
||||
0x26, 0xff, 0x00, # LOGICAL_MAXIMUM (255)
|
||||
0x46, 0xff, 0x00, # PHYSICAL_MAXIMUM (255)
|
||||
0x09, 0x30, # USAGE (X)
|
||||
0x09, 0x31, # USAGE (Y)
|
||||
0x09, 0x32, # USAGE (Z)
|
||||
0x09, 0x35, # USAGE (Rz)
|
||||
0x75, 0x08, # REPORT_SIZE (8)
|
||||
0x95, 0x04, # REPORT_COUNT (6)
|
||||
0x81, 0x02, # INPUT (Data,Var,Abs)
|
||||
0x06, 0x00, 0xff, # USAGE_PAGE (Vendor Specific)
|
||||
0x09, 0x20, # Unknown
|
||||
0x09, 0x21, # Unknown
|
||||
0x09, 0x22, # Unknown
|
||||
0x09, 0x23, # Unknown
|
||||
0x09, 0x24, # Unknown
|
||||
0x09, 0x25, # Unknown
|
||||
0x09, 0x26, # Unknown
|
||||
0x09, 0x27, # Unknown
|
||||
0x09, 0x28, # Unknown
|
||||
0x09, 0x29, # Unknown
|
||||
0x09, 0x2a, # Unknown
|
||||
0x09, 0x2b, # Unknown
|
||||
0x95, 0x0c, # REPORT_COUNT (12)
|
||||
0x81, 0x02, # INPUT (Data,Var,Abs)
|
||||
0x0a, 0x21, 0x26, # Unknown
|
||||
0x95, 0x08, # REPORT_COUNT (8)
|
||||
0xb1, 0x02, # FEATURE (Data,Var,Abs)
|
||||
0xc0 # END_COLLECTION
|
||||
))
|
||||
|
||||
gamepad = usb_hid.Device(
|
||||
report_descriptor=GAMEPAD_REPORT_DESCRIPTOR,
|
||||
usage_page=0x01, # Generic Desktop Control
|
||||
usage=0x05, # Gamepad
|
||||
report_ids=(0,), # Descriptor uses report ID 0.
|
||||
in_report_lengths=(19,), # This gamepad sends 6 bytes in its report.
|
||||
out_report_lengths=(0,), # It does not receive any reports.
|
||||
)
|
||||
|
||||
usb_hid.enable((gamepad,))
|
||||
# SPDX-FileCopyrightText: 2023 Robert Dale Smith for Adafruit Industries
|
||||
#
|
||||
# SPDX-License-Identifier: Unlicense
|
||||
|
||||
import usb_hid
|
||||
|
||||
# This is only one example of a gamepad descriptor, and may not suit your needs.
|
||||
GAMEPAD_REPORT_DESCRIPTOR = bytes((
|
||||
0x05, 0x01, # USAGE_PAGE (Generic Desktop)
|
||||
0x09, 0x05, # USAGE (Gamepad)
|
||||
0xa1, 0x01, # COLLECTION (Application)
|
||||
0x15, 0x00, # LOGICAL_MINIMUM (0)
|
||||
0x25, 0x01, # LOGICAL_MAXIMUM (1)
|
||||
0x35, 0x00, # PHYSICAL_MINIMUM (0)
|
||||
0x45, 0x01, # PHYSICAL_MAXIMUM (1)
|
||||
0x75, 0x01, # REPORT_SIZE (1)
|
||||
0x95, 0x0e, # REPORT_COUNT (14)
|
||||
0x05, 0x09, # USAGE_PAGE (Button)
|
||||
0x19, 0x01, # USAGE_MINIMUM (Button 1)
|
||||
0x29, 0x0e, # USAGE_MAXIMUM (Button 14)
|
||||
0x81, 0x02, # INPUT (Data,Var,Abs)
|
||||
0x95, 0x02, # REPORT_COUNT (3)
|
||||
0x81, 0x01, # INPUT (Cnst,Ary,Abs)
|
||||
0x05, 0x01, # USAGE_PAGE (Generic Desktop)
|
||||
0x25, 0x07, # LOGICAL_MAXIMUM (7)
|
||||
0x46, 0x3b, 0x01, # PHYSICAL_MAXIMUM (315)
|
||||
0x75, 0x04, # REPORT_SIZE (4)
|
||||
0x95, 0x01, # REPORT_COUNT (1)
|
||||
0x65, 0x14, # UNIT (Eng Rot:Angular Pos)
|
||||
0x09, 0x39, # USAGE (Hat switch)
|
||||
0x81, 0x42, # INPUT (Data,Var,Abs,Null)
|
||||
0x65, 0x00, # UNIT (None)
|
||||
0x95, 0x01, # REPORT_COUNT (1)
|
||||
0x81, 0x01, # INPUT (Cnst,Ary,Abs)
|
||||
0x26, 0xff, 0x00, # LOGICAL_MAXIMUM (255)
|
||||
0x46, 0xff, 0x00, # PHYSICAL_MAXIMUM (255)
|
||||
0x09, 0x30, # USAGE (X)
|
||||
0x09, 0x31, # USAGE (Y)
|
||||
0x09, 0x32, # USAGE (Z)
|
||||
0x09, 0x35, # USAGE (Rz)
|
||||
0x75, 0x08, # REPORT_SIZE (8)
|
||||
0x95, 0x04, # REPORT_COUNT (6)
|
||||
0x81, 0x02, # INPUT (Data,Var,Abs)
|
||||
0x06, 0x00, 0xff, # USAGE_PAGE (Vendor Specific)
|
||||
0x09, 0x20, # Unknown
|
||||
0x09, 0x21, # Unknown
|
||||
0x09, 0x22, # Unknown
|
||||
0x09, 0x23, # Unknown
|
||||
0x09, 0x24, # Unknown
|
||||
0x09, 0x25, # Unknown
|
||||
0x09, 0x26, # Unknown
|
||||
0x09, 0x27, # Unknown
|
||||
0x09, 0x28, # Unknown
|
||||
0x09, 0x29, # Unknown
|
||||
0x09, 0x2a, # Unknown
|
||||
0x09, 0x2b, # Unknown
|
||||
0x95, 0x0c, # REPORT_COUNT (12)
|
||||
0x81, 0x02, # INPUT (Data,Var,Abs)
|
||||
0x0a, 0x21, 0x26, # Unknown
|
||||
0x95, 0x08, # REPORT_COUNT (8)
|
||||
0xb1, 0x02, # FEATURE (Data,Var,Abs)
|
||||
0xc0 # END_COLLECTION
|
||||
))
|
||||
|
||||
gamepad = usb_hid.Device(
|
||||
report_descriptor=GAMEPAD_REPORT_DESCRIPTOR,
|
||||
usage_page=0x01, # Generic Desktop Control
|
||||
usage=0x05, # Gamepad
|
||||
report_ids=(0,), # Descriptor uses report ID 0.
|
||||
in_report_lengths=(19,), # This gamepad sends 6 bytes in its report.
|
||||
out_report_lengths=(0,), # It does not receive any reports.
|
||||
)
|
||||
|
||||
usb_hid.enable((gamepad,))
|
||||
|
|
|
|||
|
|
@ -4,9 +4,9 @@
|
|||
# Simple Super Nintendo controller to standard USB HID gamepad with DirectInput button mapping.
|
||||
# Tested on KB2040
|
||||
|
||||
import time
|
||||
import board
|
||||
import digitalio
|
||||
import time
|
||||
import usb_hid
|
||||
|
||||
# Update the SNES Controller pins based on your input
|
||||
|
|
@ -62,21 +62,21 @@ hat_map = {
|
|||
}
|
||||
|
||||
def read_snes_controller():
|
||||
button_states = []
|
||||
data_bits = []
|
||||
latch.value = True
|
||||
time.sleep(0.000012) # 12µs
|
||||
latch.value = False
|
||||
|
||||
for _ in range(16):
|
||||
time.sleep(0.000006) # Wait 6µs
|
||||
button_states.append(data.value)
|
||||
data_bits.append(data.value)
|
||||
|
||||
clock.value = True
|
||||
time.sleep(0.000006) # 6µs
|
||||
clock.value = False
|
||||
time.sleep(0.000006) # 6µs
|
||||
|
||||
return button_states
|
||||
return data_bits
|
||||
|
||||
# Find the gamepad device in the usb_hid devices
|
||||
gamepad_device = None
|
||||
|
|
@ -95,20 +95,20 @@ report[2] = 0x08 # default released hat switch value
|
|||
prev_report = bytearray(report)
|
||||
|
||||
while True:
|
||||
button_states = read_snes_controller()
|
||||
button_state = read_snes_controller()
|
||||
all_buttons = list(buttonmap.keys())
|
||||
|
||||
for idx, button in enumerate(all_buttons):
|
||||
index, byte_index, button_value = buttonmap[button]
|
||||
is_pressed = not button_states[index] # True if button is pressed
|
||||
is_pressed = not button_state [index] # True if button is pressed
|
||||
|
||||
if button in dpad_state: # If it's a direction button
|
||||
dpad_state[button] = 1 if is_pressed else 0
|
||||
else:
|
||||
if is_pressed:
|
||||
report[byte_index] |= button_value
|
||||
else:
|
||||
report[byte_index] &= ~button_value # Reset the button state in the report if not pressed
|
||||
else: # not pressed, reset button state
|
||||
report[byte_index] &= ~button_value
|
||||
|
||||
# SOCD (up priority and neutral horizontal)
|
||||
if (dpad_state["Up"] and dpad_state["Down"]):
|
||||
|
|
|
|||
Loading…
Reference in a new issue