Skip to content

Instantly share code, notes, and snippets.

@AviiNL
Created March 6, 2024 10:06
Show Gist options
  • Save AviiNL/389ed3db0bfb1f0dd412bfafaf26d119 to your computer and use it in GitHub Desktop.
Save AviiNL/389ed3db0bfb1f0dd412bfafaf26d119 to your computer and use it in GitHub Desktop.
Accelerated encoder to Axis for Joystick Gremlin
import time
import math
import gremlin
from gremlin.user_plugin import *
def timestamp():
return math.floor(time.time() * 1000)
mode = ModeVariable(
"Mode",
"The mode to use for this mapping"
)
btn_up = PhysicalInputVariable(
"Increment button",
"Increment button",
[gremlin.common.InputType.JoystickButton]
)
btn_down = PhysicalInputVariable(
"Decrement button",
"Decrement button",
[gremlin.common.InputType.JoystickButton]
)
vjoy_axis = VirtualInputVariable(
"Virtual output axis",
"Virtual output axis",
[gremlin.common.InputType.JoystickAxis]
)
acceleration = FloatVariable(
"Acceleration",
"If the buttons are pressed in rapid succession (within 100ms) this value is added to the modifier that is added or subtracted from the axis value",
2.0,
1.0,
10.0,
)
resolution = IntegerVariable(
"Ouput resolution",
"If a single button press adds more than 1 unit, increase this value",
360,
1,
2048,
)
# Storage for axis values
g_last_time = 0
g_modifier = 1.0
decorator_1 = btn_up.create_decorator(mode.value)
decorator_2 = btn_down.create_decorator(mode.value)
def process(event, vjoy, increment):
global g_last_time
global g_modifier
now = timestamp()
device = vjoy_axis.vjoy_id
axis = vjoy_axis.input_id
axis_value = vjoy[device].axis(axis).value * float(resolution.value)
g_modifier += acceleration.value
time_diff = now - g_last_time
if time_diff > 100: # it's been too long, reset back to 1
g_modifier = 1.0
if increment:
new_axis_value = (axis_value + g_modifier) / float(resolution.value)
else:
new_axis_value = (axis_value - g_modifier) / float(resolution.value)
new_axis_value = max(-1.0, min(new_axis_value, 1.0))
vjoy[device].axis(axis).value = new_axis_value
g_last_time = timestamp()
@decorator_1.button(btn_up.input_id)
def button_up(event, vjoy):
process(event, vjoy, True)
@decorator_2.button(btn_down.input_id)
def button_down(event, vjoy):
process(event, vjoy, False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment