Skip to content

Dualsense

TODO: SignalFlow microphone and speaker demo TODO: List available output methods for LEDs, rumble, etc.

DualSense

Source code in src/tolvera/dualsense.py
class DualSense:
    def __init__(self, **kwargs):
        self.device_infos = DualSenseController.enumerate_devices()
        self.is_running = False
        print(f"[DualSense] Device Infos: {self.device_infos}")
        self.init(**kwargs)

    def init(self, **kwargs):
        self.device_infos = DualSenseController.enumerate_devices()
        if len(self.device_infos) < 1:
            raise Exception('[DualSense.init] No DualSense Controller available.')
        print(f"[DualSense.init] Device Infos: {self.device_infos}")
        config = DEFAULTS
        config.update(kwargs)
        self.controller = DualSenseController(**config)
        self.is_running = False

    def start(self):
        if hasattr(self, 'update_thread') and self.update_thread is not None:
            raise Exception('[DualSense.start] Thread already exists.')
        self.update_thread = threading.Thread(target=self.update)
        self.update_thread.start()
        self.is_running = True
        print(f"[DualSense.start] Thread started.")

    def stop(self):
        if self.update_thread is None:
            return
        self.is_running = False
        self.update_thread.join()
        self.update_thread = None
        print(f"[DualSense.stop] Thread stopped.")

    def update(self):
        self.controller.exceptions.on_change(self.on_exception)
        self.controller.activate()
        while self.is_running:
            time.sleep(1)
        self.controller.deactivate()

    def on_exception(self, exception: Exception):
        print(f'[DualSense.on_exception] Exception occured:', exception)
        self.stop()

    def handle(self, event_type: str):
        """Decorator for handling DualSense events.
        It has a 'type' argument where you specify the event type.
        For event types, see:
        https://github.com/yesbotics/dualsense-controller-python/blob/main/src/examples/example.py
        https://github.com/yesbotics/dualsense-controller-python/blob/main/src/dualsense_controller/core/state/read_state/value_type.py
        To list available inputs, use `list_inputs()`.

        Args:
            event_type (str): The event type.

        Example:
            ```py
            @ds.handle('left_stick')
            def _(left_stick):
                print(left_stick) # JoyStick(x=0.0, y=0.0)
            ```
        """
        def decorator(func):
            if hasattr(self.controller, event_type):
                getattr(self.controller, event_type).on_change(func)
            return func
        return decorator

    def list_defaults(self):
        print(f"[DualSense] Default config:\n{DEFAULTS}")
        return DEFAULTS

    def list_inputs(self):
        print(f"[DualSense] Available inputs:")
        [print(f"  - {i}") for i in CONTROLLER_INPUTS]
        return CONTROLLER_INPUTS

handle(event_type)

Decorator for handling DualSense events. It has a 'type' argument where you specify the event type. For event types, see: https://github.com/yesbotics/dualsense-controller-python/blob/main/src/examples/example.py https://github.com/yesbotics/dualsense-controller-python/blob/main/src/dualsense_controller/core/state/read_state/value_type.py To list available inputs, use list_inputs().

Parameters:

Name Type Description Default
event_type str

The event type.

required
Example
@ds.handle('left_stick')
def _(left_stick):
    print(left_stick) # JoyStick(x=0.0, y=0.0)
Source code in src/tolvera/dualsense.py
def handle(self, event_type: str):
    """Decorator for handling DualSense events.
    It has a 'type' argument where you specify the event type.
    For event types, see:
    https://github.com/yesbotics/dualsense-controller-python/blob/main/src/examples/example.py
    https://github.com/yesbotics/dualsense-controller-python/blob/main/src/dualsense_controller/core/state/read_state/value_type.py
    To list available inputs, use `list_inputs()`.

    Args:
        event_type (str): The event type.

    Example:
        ```py
        @ds.handle('left_stick')
        def _(left_stick):
            print(left_stick) # JoyStick(x=0.0, y=0.0)
        ```
    """
    def decorator(func):
        if hasattr(self.controller, event_type):
            getattr(self.controller, event_type).on_change(func)
        return func
    return decorator