From 6c90998bb063fd92feb0c8812651adc66d43a49a Mon Sep 17 00:00:00 2001 From: Mel Date: Thu, 11 Dec 2025 23:01:34 +0100 Subject: [PATCH] started to add VTS API code --- src/vts-gampad-input/main.py | 30 ++++++++++- src/vts-gampad-input/vts-connection.py | 16 ------ src/vts-gampad-input/vts_connection.py | 72 ++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 18 deletions(-) delete mode 100644 src/vts-gampad-input/vts-connection.py create mode 100755 src/vts-gampad-input/vts_connection.py diff --git a/src/vts-gampad-input/main.py b/src/vts-gampad-input/main.py index 2900d59..b0ce1a7 100755 --- a/src/vts-gampad-input/main.py +++ b/src/vts-gampad-input/main.py @@ -1,7 +1,9 @@ #! /usr/bin/env python +import asyncio import inquirer from inputs import GamePad, devices +from vts_connection import VTSConnection def _get_gamepads() -> list[GamePad]: @@ -37,7 +39,7 @@ def select_gamepad() -> GamePad | None: return gamepads[0] -def _in_deadzone(analog_value: int, percentage: float = 5) -> bool: +def _in_deadzone(analog_value: int, percentage: float = 10) -> bool: center: float = 255 / 2 dead_range = center * (percentage / 100) @@ -47,8 +49,24 @@ def _in_deadzone(analog_value: int, percentage: float = 5) -> bool: return False +def _normalize_analog_sticks(analog_value: float) -> float: + """Normalize analog value to -1 - 1 range to comply with Nyarupad""" + minimum: int = 0 + maximum: int = 255 + formula = 2 * ((analog_value - minimum) / (maximum - minimum)) - 1 + return formula + + +def _normalize_analog_trigger(analog_value: float) -> float: + minimum: int = 0 + maximum: int = 255 + formula = (analog_value - minimum) / (maximum - minimum) + return formula + + def main() -> None: gamepad = select_gamepad() + connection = VTSConnection() while True: for event in gamepad.read(): @@ -56,10 +74,18 @@ def main() -> None: # Add some deadzone of about 5% to analog stick if event.code in ["ABS_X", "ABS_Y", "ABS_RX", "ABS_RY"]: if not _in_deadzone(analog_value=event.state): - print(event.ev_type, event.code, event.state) + print( + f"{event.ev_type} {event.code} {_normalize_analog_sticks(analog_value=event.state)}" + ) + elif event.code in ["ABS_Z", "ABS_RZ"]: + print( + f"{event.ev_type} {event.code} {_normalize_analog_trigger(event.state)}" + ) else: print(event.ev_type, event.code, event.state) + asyncio.run(connection.update_parameters(event.code, event.state)) + if __name__ == "__main__": try: diff --git a/src/vts-gampad-input/vts-connection.py b/src/vts-gampad-input/vts-connection.py deleted file mode 100644 index 6aab16f..0000000 --- a/src/vts-gampad-input/vts-connection.py +++ /dev/null @@ -1,16 +0,0 @@ -import pyvts -import asyncio - -plugin_info = { - "plugin_name": "vts-gampad-input", - "developer": "Melody LaFae", - "authentication_token_path": f"{os.environ['XDG_CONFIG_DIRS']}/.vts-gampad-authentication-token", -} - -class VTSConnection: - - def __init__(self): - pass - - async def connect(self): - vts = pyvts. \ No newline at end of file diff --git a/src/vts-gampad-input/vts_connection.py b/src/vts-gampad-input/vts_connection.py new file mode 100755 index 0000000..4a57aa9 --- /dev/null +++ b/src/vts-gampad-input/vts_connection.py @@ -0,0 +1,72 @@ +#! /usr/bin/env python + +import asyncio +import os +import pyvts + +plugin_info = { + "plugin_name": "vts-gampad-input", + "developer": "Melody LaFae", + "authentication_token_path": f"{os.environ['HOME']}/.vts-gampad-authentication-token", +} + + +class VTSConnection: + def __init__(self): + self.vts = pyvts.vts(plugin_info=plugin_info) + self.request = pyvts.VTSRequest(plugin_info=plugin_info) + if asyncio.run(self._connect()): + asyncio.run(self._create_vts_params()) + + async def _connect(self) -> bool: + try: + await self.vts.connect() + except OSError: + print("Error: Make sure the VTS API is running") + exit(1) + await self.vts.request_authenticate_token() + await self.vts.request_authenticate() + + connection_success = await self.vts.request( + self.request.requestCustomParameter( + parameter="NP_ON", + info="VTS Gamepad Input Enabled", + min=0.0, + max=1.0, + default_value=0.0, + ) + ) + + if not connection_success: + raise ConnectionError("Connection failed") + else: + return True + + async def _create_vts_params(self): + params: list[dict] = [ + self.request.requestCustomParameter( + parameter="NP_LButtonDown", + info="Left side face buttons down", + min=0.0, + max=1.0, + default_value=0.0, + ), + self.request.requestCustomParameter( + parameter="NP_LButtonDown", + info="Left side face buttons down", + min=0.0, + max=1.0, + default_value=0.0, + ), + ] + + async def update_parameters(self, button: str, value: float) -> None: + pass + + +def main(): + VTSConnection() + + +if __name__ == "__main__": + main()