Skip to content

Commit 956b146

Browse files
resolves linting errors for Super Nintendo USB controller guide code
1 parent 8834e11 commit 956b146

File tree

2 files changed

+82
-82
lines changed

2 files changed

+82
-82
lines changed

Super_Nintendo_USB_Controller/boot.py

Lines changed: 74 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,74 @@
1-
# SPDX-FileCopyrightText: 2023 Robert Dale Smith for Adafruit Industries
2-
#
3-
# SPDX-License-Identifier: Unlicense
4-
5-
import usb_hid
6-
7-
# This is only one example of a gamepad descriptor, and may not suit your needs.
8-
GAMEPAD_REPORT_DESCRIPTOR = bytes((
9-
0x05, 0x01, # USAGE_PAGE (Generic Desktop)
10-
0x09, 0x05, # USAGE (Gamepad)
11-
0xa1, 0x01, # COLLECTION (Application)
12-
0x15, 0x00, # LOGICAL_MINIMUM (0)
13-
0x25, 0x01, # LOGICAL_MAXIMUM (1)
14-
0x35, 0x00, # PHYSICAL_MINIMUM (0)
15-
0x45, 0x01, # PHYSICAL_MAXIMUM (1)
16-
0x75, 0x01, # REPORT_SIZE (1)
17-
0x95, 0x0e, # REPORT_COUNT (14)
18-
0x05, 0x09, # USAGE_PAGE (Button)
19-
0x19, 0x01, # USAGE_MINIMUM (Button 1)
20-
0x29, 0x0e, # USAGE_MAXIMUM (Button 14)
21-
0x81, 0x02, # INPUT (Data,Var,Abs)
22-
0x95, 0x02, # REPORT_COUNT (3)
23-
0x81, 0x01, # INPUT (Cnst,Ary,Abs)
24-
0x05, 0x01, # USAGE_PAGE (Generic Desktop)
25-
0x25, 0x07, # LOGICAL_MAXIMUM (7)
26-
0x46, 0x3b, 0x01, # PHYSICAL_MAXIMUM (315)
27-
0x75, 0x04, # REPORT_SIZE (4)
28-
0x95, 0x01, # REPORT_COUNT (1)
29-
0x65, 0x14, # UNIT (Eng Rot:Angular Pos)
30-
0x09, 0x39, # USAGE (Hat switch)
31-
0x81, 0x42, # INPUT (Data,Var,Abs,Null)
32-
0x65, 0x00, # UNIT (None)
33-
0x95, 0x01, # REPORT_COUNT (1)
34-
0x81, 0x01, # INPUT (Cnst,Ary,Abs)
35-
0x26, 0xff, 0x00, # LOGICAL_MAXIMUM (255)
36-
0x46, 0xff, 0x00, # PHYSICAL_MAXIMUM (255)
37-
0x09, 0x30, # USAGE (X)
38-
0x09, 0x31, # USAGE (Y)
39-
0x09, 0x32, # USAGE (Z)
40-
0x09, 0x35, # USAGE (Rz)
41-
0x75, 0x08, # REPORT_SIZE (8)
42-
0x95, 0x04, # REPORT_COUNT (6)
43-
0x81, 0x02, # INPUT (Data,Var,Abs)
44-
0x06, 0x00, 0xff, # USAGE_PAGE (Vendor Specific)
45-
0x09, 0x20, # Unknown
46-
0x09, 0x21, # Unknown
47-
0x09, 0x22, # Unknown
48-
0x09, 0x23, # Unknown
49-
0x09, 0x24, # Unknown
50-
0x09, 0x25, # Unknown
51-
0x09, 0x26, # Unknown
52-
0x09, 0x27, # Unknown
53-
0x09, 0x28, # Unknown
54-
0x09, 0x29, # Unknown
55-
0x09, 0x2a, # Unknown
56-
0x09, 0x2b, # Unknown
57-
0x95, 0x0c, # REPORT_COUNT (12)
58-
0x81, 0x02, # INPUT (Data,Var,Abs)
59-
0x0a, 0x21, 0x26, # Unknown
60-
0x95, 0x08, # REPORT_COUNT (8)
61-
0xb1, 0x02, # FEATURE (Data,Var,Abs)
62-
0xc0 # END_COLLECTION
63-
))
64-
65-
gamepad = usb_hid.Device(
66-
report_descriptor=GAMEPAD_REPORT_DESCRIPTOR,
67-
usage_page=0x01, # Generic Desktop Control
68-
usage=0x05, # Gamepad
69-
report_ids=(0,), # Descriptor uses report ID 0.
70-
in_report_lengths=(19,), # This gamepad sends 6 bytes in its report.
71-
out_report_lengths=(0,), # It does not receive any reports.
72-
)
73-
74-
usb_hid.enable((gamepad,))
1+
# SPDX-FileCopyrightText: 2023 Robert Dale Smith for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
5+
import usb_hid
6+
7+
# This is only one example of a gamepad descriptor, and may not suit your needs.
8+
GAMEPAD_REPORT_DESCRIPTOR = bytes((
9+
0x05, 0x01, # USAGE_PAGE (Generic Desktop)
10+
0x09, 0x05, # USAGE (Gamepad)
11+
0xa1, 0x01, # COLLECTION (Application)
12+
0x15, 0x00, # LOGICAL_MINIMUM (0)
13+
0x25, 0x01, # LOGICAL_MAXIMUM (1)
14+
0x35, 0x00, # PHYSICAL_MINIMUM (0)
15+
0x45, 0x01, # PHYSICAL_MAXIMUM (1)
16+
0x75, 0x01, # REPORT_SIZE (1)
17+
0x95, 0x0e, # REPORT_COUNT (14)
18+
0x05, 0x09, # USAGE_PAGE (Button)
19+
0x19, 0x01, # USAGE_MINIMUM (Button 1)
20+
0x29, 0x0e, # USAGE_MAXIMUM (Button 14)
21+
0x81, 0x02, # INPUT (Data,Var,Abs)
22+
0x95, 0x02, # REPORT_COUNT (3)
23+
0x81, 0x01, # INPUT (Cnst,Ary,Abs)
24+
0x05, 0x01, # USAGE_PAGE (Generic Desktop)
25+
0x25, 0x07, # LOGICAL_MAXIMUM (7)
26+
0x46, 0x3b, 0x01, # PHYSICAL_MAXIMUM (315)
27+
0x75, 0x04, # REPORT_SIZE (4)
28+
0x95, 0x01, # REPORT_COUNT (1)
29+
0x65, 0x14, # UNIT (Eng Rot:Angular Pos)
30+
0x09, 0x39, # USAGE (Hat switch)
31+
0x81, 0x42, # INPUT (Data,Var,Abs,Null)
32+
0x65, 0x00, # UNIT (None)
33+
0x95, 0x01, # REPORT_COUNT (1)
34+
0x81, 0x01, # INPUT (Cnst,Ary,Abs)
35+
0x26, 0xff, 0x00, # LOGICAL_MAXIMUM (255)
36+
0x46, 0xff, 0x00, # PHYSICAL_MAXIMUM (255)
37+
0x09, 0x30, # USAGE (X)
38+
0x09, 0x31, # USAGE (Y)
39+
0x09, 0x32, # USAGE (Z)
40+
0x09, 0x35, # USAGE (Rz)
41+
0x75, 0x08, # REPORT_SIZE (8)
42+
0x95, 0x04, # REPORT_COUNT (6)
43+
0x81, 0x02, # INPUT (Data,Var,Abs)
44+
0x06, 0x00, 0xff, # USAGE_PAGE (Vendor Specific)
45+
0x09, 0x20, # Unknown
46+
0x09, 0x21, # Unknown
47+
0x09, 0x22, # Unknown
48+
0x09, 0x23, # Unknown
49+
0x09, 0x24, # Unknown
50+
0x09, 0x25, # Unknown
51+
0x09, 0x26, # Unknown
52+
0x09, 0x27, # Unknown
53+
0x09, 0x28, # Unknown
54+
0x09, 0x29, # Unknown
55+
0x09, 0x2a, # Unknown
56+
0x09, 0x2b, # Unknown
57+
0x95, 0x0c, # REPORT_COUNT (12)
58+
0x81, 0x02, # INPUT (Data,Var,Abs)
59+
0x0a, 0x21, 0x26, # Unknown
60+
0x95, 0x08, # REPORT_COUNT (8)
61+
0xb1, 0x02, # FEATURE (Data,Var,Abs)
62+
0xc0 # END_COLLECTION
63+
))
64+
65+
gamepad = usb_hid.Device(
66+
report_descriptor=GAMEPAD_REPORT_DESCRIPTOR,
67+
usage_page=0x01, # Generic Desktop Control
68+
usage=0x05, # Gamepad
69+
report_ids=(0,), # Descriptor uses report ID 0.
70+
in_report_lengths=(19,), # This gamepad sends 6 bytes in its report.
71+
out_report_lengths=(0,), # It does not receive any reports.
72+
)
73+
74+
usb_hid.enable((gamepad,))

Super_Nintendo_USB_Controller/code.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
# Simple Super Nintendo controller to standard USB HID gamepad with DirectInput button mapping.
55
# Tested on KB2040
66

7+
import time
78
import board
89
import digitalio
9-
import time
1010
import usb_hid
1111

1212
# Update the SNES Controller pins based on your input
@@ -62,21 +62,21 @@
6262
}
6363

6464
def read_snes_controller():
65-
button_states = []
65+
data_bits = []
6666
latch.value = True
6767
time.sleep(0.000012) # 12µs
6868
latch.value = False
6969

7070
for _ in range(16):
7171
time.sleep(0.000006) # Wait 6µs
72-
button_states.append(data.value)
72+
data_bits.append(data.value)
7373

7474
clock.value = True
7575
time.sleep(0.000006) # 6µs
7676
clock.value = False
7777
time.sleep(0.000006) # 6µs
7878

79-
return button_states
79+
return data_bits
8080

8181
# Find the gamepad device in the usb_hid devices
8282
gamepad_device = None
@@ -95,20 +95,20 @@ def read_snes_controller():
9595
prev_report = bytearray(report)
9696

9797
while True:
98-
button_states = read_snes_controller()
98+
button_state = read_snes_controller()
9999
all_buttons = list(buttonmap.keys())
100100

101101
for idx, button in enumerate(all_buttons):
102102
index, byte_index, button_value = buttonmap[button]
103-
is_pressed = not button_states[index] # True if button is pressed
103+
is_pressed = not button_state [index] # True if button is pressed
104104

105105
if button in dpad_state: # If it's a direction button
106106
dpad_state[button] = 1 if is_pressed else 0
107107
else:
108108
if is_pressed:
109109
report[byte_index] |= button_value
110-
else:
111-
report[byte_index] &= ~button_value # Reset the button state in the report if not pressed
110+
else: # not pressed, reset button state
111+
report[byte_index] &= ~button_value
112112

113113
# SOCD (up priority and neutral horizontal)
114114
if (dpad_state["Up"] and dpad_state["Down"]):

0 commit comments

Comments
 (0)