started to add VTS API code
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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.
|
||||
72
src/vts-gampad-input/vts_connection.py
Executable file
72
src/vts-gampad-input/vts_connection.py
Executable file
@@ -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()
|
||||
Reference in New Issue
Block a user