Skip to content

Instantly share code, notes, and snippets.

@cjoshmartin
Last active March 13, 2024 22:39
Show Gist options
  • Save cjoshmartin/facb59b6688c1c901d77e49376b3d384 to your computer and use it in GitHub Desktop.
Save cjoshmartin/facb59b6688c1c901d77e49376b3d384 to your computer and use it in GitHub Desktop.
import hid
def button_check(report):
button_press = report[8]
# print("{:08b}".format(button_press))
if button_press == 0:
return
a = button_press & 0b00000001
if a:
print("Button A is pressed")
b = button_press & 0b00000010
if b:
print("Button B is pressed")
x = button_press & 0b00001000
if x:
print("Button X is pressed")
y = button_press & 0b00010000
if y:
print("Button y is pressed")
r1 = button_press & 0b10000000
if r1:
print("Button R1 is pressed")
l1 = button_press & 0b01000000
if l1:
print("Button L1 is pressed")
def start_or_select(report):
button_press = report[9]
if button_press == 0:
return
# print("{:08b}".format(button_press))
select = button_press & 0b00000100
if select:
print("Button Select is pressed")
start = button_press & 0b00001000
if start:
print("Button Start is pressed")
def dbpad_press(report):
button_press = report[1]
if button_press == 15:
return
# print("{:08b}".format(button_press))
if button_press == 0:
print("Button D-UP is pressed")
down = button_press == 4
if down:
print("Button D-Down is pressed")
left = button_press == 6
if left:
print("Button D-Left is pressed")
right = button_press == 2
if right:
print("Button D-Right is pressed")
def main():
print("Looking for device '8Bitdo FC30 Pro'")
address_of_device = []
for device in hid.enumerate():
product_string = device['product_string']
if len(product_string) > 0:
if product_string == "8Bitdo FC30 Pro":
print("Found 8Bitdo FC30 Pro")
address_of_device.append(f"0x{device['vendor_id']:04x}")
address_of_device.append(f"0x{device['product_id']:04x}")
break
else:
print("Didn't find device! :(")
return
print("connect to '8Bitdo FC30 Pro'")
gamepad = hid.device()
gamepad.open(int(address_of_device[0], 16), int(address_of_device[1], 16))
gamepad.set_nonblocking(True)
print("connected")
print("Waiting for inputs....")
while True:
report = gamepad.read(64)
if report:
button_check(report)
start_or_select(report)
dbpad_press(report)
# no_button_press = [3, 15, 128, 127, 128, 127, 0, 0, 0, 0]
# a_press = [3, 15, 128, 127, 128, 127, 0, 0, 1, 0]
# b_press = [3, 15, 128, 127, 128, 127, 0, 0, 2, 0]
# x_press = [3, 15, 128, 127, 128, 127, 0, 0, 8, 0]
# y_press = [3, 15, 128, 127, 128, 127, 0, 0, 16, 0]
# r1_press = [3, 15, 128, 127, 128, 127, 0, 0, 1, 28, 0]
# l1_press = [3, 15, 128, 127, 128, 127, 0, 0, 64, 0]
# r2_press = [3, 15, 128, 127, 128, 127, 255, 0, 0, 2]
# l2_press = [3, 15, 128, 127, 128, 127, 0, 255, 0, 1]
# select_press = [3, 15, 128, 127, 128, 127, 0, 0, 0, 4]
# start_press = [3, 15, 128, 127, 128, 127, 0, 0, 0, 8]
# dpad_up_press = [3, 0, 128, 127, 128, 127, 0, 0, 0, 0]
# dpad_down_press = [3, 4, 128, 127, 128, 127, 0, 0, 0, 0]
# dpad_left_press = [3, 6, 128, 127, 128, 127, 0, 0, 0, 0]
# dpad_right_press = [3, 2, 128, 127, 128, 127, 0, 0, 0, 0]
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment