Zuzu-Typ / XInput-Python

Simple access to the DirectX input API for Python
zlib License
29 stars 6 forks source link

"You need to specify at least one controller" #9

Open TheBlizWiz opened 2 years ago

TheBlizWiz commented 2 years ago

Forgive my inexperience working with the library here, but I'm trying to use this library to connect my XBox Controller to my VISCA Camera. When I try to run the attached script in Powershell I get this error message:

import XInput

class Handler(XInput.EventHandler):

    ALL_FACE_BUTTONS = XInput.BUTTON_A | XInput.BUTTON_B | XInput.BUTTON_Y | XInput.BUTTON_X
    ALL_DPAD_BUTTONS = XInput.BUTTON_DPAD_UP | XInput.BUTTON_DPAD_DOWN | XInput.BUTTON_DPAD_LEFT | XInput.BUTTON_DPAD_RIGHT
    ALL_MENU_BUTTONS = XInput.BUTTON_START | XInput.BUTTON_BACK
    ALL_THUMB_BUTTONS = XInput.BUTTON_LEFT_THUMB | XInput.BUTTON_RIGHT_THUMB
    ALL_SHOULDER_BUTTONS = XInput.BUTTON_LEFT_SHOULDER | XInput.BUTTON_RIGHT_SHOULDER
    ALL_BUTTONS = ALL_FACE_BUTTONS | ALL_DPAD_BUTTONS | ALL_MENU_BUTTONS | ALL_THUMB_BUTTONS | ALL_SHOULDER_BUTTONS

    # last_stickL_pos = (0.0, 0.0)
    # last_stickR_pos = (0.0, 0.0)

    def __init__(self, *controllers):
        super().__init__(*controllers, filter = Handler.ALL_BUTTONS + XInput.STICK_LEFT + XInput.STICK_RIGHT + XInput.TRIGGER_LEFT + XInput.TRIGGER_RIGHT)

    def process_button_event(self, event):
        print("(" + str(Event.button) + ") button pressed")
        # TODO: Connect button press to set preset command in VISCA
        # TODO: Connect shoulder button press to focus in/out in VISCA

    def process_trigger_event(self, event):
        if(Event.trigger == XInput.LEFT):
            print("Trigger L pressed to " + str(Event.value))
        if(Event.trigger == XInput.RIGHT):
            print("Trigger R pressed to " + str(Event.value))
        # TODO: Connect trigger push to camera zoom in/out in VISCA

    def process_stick_event(self, event):
        if(Event.stick == XInput.LEFT):
            print("Stick L moved to " + str(Event.value))
        if(Event.stick == XInput.RIGHT):
            print("Stick R moved to " + str(Event.value))
        # TODO: Check last_stick_pos to see if we need to send a new packet or not
        # TODO: Connect stick tilt to camera tilt/pan in VISCA

    def process_connection_event(self, event):
        print("Controller connected on port " + str(Event.user_index))

my_handler = Handler()
my_handler.add_filter(filter)
my_gamepad_thread = XInput.GamepadThread(my_handler)
Traceback (most recent call last):

  File "D:\Church Camera Python Controller\XBoxController.py", line 33, in <module>
    my_handler = Handler()

  File "D:\Church Camera Python Controller\XBoxController.py", line 13, in __init__
    super().__init__(*controllers, filter = Handler.ALL_BUTTONS + XInput.STICK_LEFT + XInput.STICK_RIGHT + XInput.TRIGGER_LEFT + XInput.TRIGGER_RIGHT)

  File "C:\Users\thebl\AppData\Local\Programs\Python\Python310\lib\site-packages\XInput.py", line 556, in __init__
    self.set_controllers(*controllers)

  File "C:\Users\thebl\AppData\Local\Programs\Python\Python310\lib\site-packages\XInput.py", line 582, in set_controllers
    raise ValueError("You need to specify at least one controller")

ValueError: You need to specify at least one controller

How do I specify a controller in the script?

baderj commented 2 years ago

There are four possible controllers: 0,1,2,3. You can see which one is connected with get_connected. That should return a list of booleans like this: (True, False, False, False). The first position is controller 0, up to controller 3.

To set the controller, you need to pass the numbers to Handler(), for instance Handler(0). You can also use multiple controllers, for instance 0 and 4 with Handler(0,4).