Skip to content

Instantly share code, notes, and snippets.

@WhiteMagic
Created June 11, 2018 03:59
Show Gist options
  • Save WhiteMagic/8d4b495d30f29213cbfa9df2478e2dfe to your computer and use it in GitHub Desktop.
Save WhiteMagic/8d4b495d30f29213cbfa9df2478e2dfe to your computer and use it in GitHub Desktop.
import time
import gremlin
t16000 = gremlin.input_devices.JoystickDecorator(
"T.16000M",
72331530,
"Default"
)
g_current_velocity = 0.0
g_center_deadzone = 0.05
g_button_forward = 1
g_button_backward = 2
g_bin_width = 0.01
g_action_indicator = [False,] * int(1.0 / g_bin_width)
@gremlin.input_devices.periodic(0.01)
def digital_to_analog(vjoy):
# No motion
if abs(g_current_velocity) < g_center_deadzone:
vjoy[1].button(g_button_backward).is_pressed = False
vjoy[1].button(g_button_forward).is_pressed = False
return
# Index into button state mapping
time_now = time.time()
index = int(len(g_action_indicator) * (time_now - int(time_now)))
# Going forward
if g_current_velocity > 0:
vjoy[1].button(g_button_backward).is_pressed = False
vjoy[1].button(g_button_forward).is_pressed = g_action_indicator[index]
# Going backward
else:
vjoy[1].button(g_button_backward).is_pressed = g_action_indicator[index]
vjoy[1].button(g_button_forward).is_pressed = False
@t16000.axis(2)
def throttle_change(event, vjoy):
global g_current_velocity
global g_action_indicator
g_current_velocity = event.value
# Recompute the flags indicating whether or not to press a button
for i in range(len(g_action_indicator)):
if abs(event.value) < (i+1) * g_bin_width:
g_action_indicator[i] = False
else:
g_action_indicator[i] = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment