diff options
Diffstat (limited to 'Scripts')
| -rw-r--r-- | Scripts/requirements.txt | 2 | ||||
| -rw-r--r-- | Scripts/steamvr.py | 147 | ||||
| -rw-r--r-- | Scripts/steamvr_v2.py | 84 | ||||
| -rw-r--r-- | Scripts/transcribe.py | 22 |
4 files changed, 90 insertions, 165 deletions
diff --git a/Scripts/requirements.txt b/Scripts/requirements.txt index 80bf93d..bdc93a1 100644 --- a/Scripts/requirements.txt +++ b/Scripts/requirements.txt @@ -5,7 +5,7 @@ future==0.18.2 keyboard langcodes language-data -openvr +pyopenxr pillow pyaudio python-osc diff --git a/Scripts/steamvr.py b/Scripts/steamvr.py index c231184..df8f422 100644 --- a/Scripts/steamvr.py +++ b/Scripts/steamvr.py @@ -1,80 +1,99 @@ -#!/usr/bin/env python3 - -# python3 -m pip install openvr -# License: BSD-3.0 (requires showing notice in binary distributions) -import openvr as vr +import ctypes import time +import xr EVENT_NONE = 0 EVENT_RISING_EDGE = 1 EVENT_FALLING_EDGE = 2 -hands = {} -hands["left"] = vr.TrackedControllerRole_LeftHand -hands["right"] = vr.TrackedControllerRole_RightHand - -buttons = {} -buttons["a"] = vr.k_EButton_IndexController_A -buttons["b"] = vr.k_EButton_IndexController_B -buttons["joystick"] = vr.k_EButton_Axis0 +# hand: either "right" or "left" +# button: either "a" or "b" +def pollButtonPress(hand: str = "right", button: str = "b") -> int: + # ContextObject is a high level pythonic class meant to keep simple cases simple. + with xr.ContextObject( + instance_create_info=xr.InstanceCreateInfo( + enabled_extension_names=[ + xr.KHR_OPENGL_ENABLE_EXTENSION_NAME, # A graphics extension is mandatory + ], + ), + ) as context: + controller_path_str = f"/user/hand/{hand}" + binding_path_str = f"/user/hand/{hand}/input/{button}/click" + print(f"Controller path: {controller_path_str}") + print(f"Binding path: {binding_path_str}") -class SessionState: - def __init__(self): - self.system = vr.init(vr.VRApplication_Background) - self.last_packet = 0 - # Whether the configured input event is high or low. - self.event_high = False + # Set up the B button action + controller_paths = (xr.Path * 1)( + xr.string_to_path(context.instance, controller_path_str,) + ) + b_button_action = xr.create_action( + action_set=context.default_action_set, + create_info=xr.ActionCreateInfo( + action_type=xr.ActionType.BOOLEAN_INPUT, + action_name="tastt_button_press", + localized_action_name="TaSTT Button Press", + count_subaction_paths=len(controller_paths), + subaction_paths=controller_paths, + ), + ) + suggested_bindings = (xr.ActionSuggestedBinding * 1)( + xr.ActionSuggestedBinding( + action=b_button_action, + binding=xr.string_to_path( + instance=context.instance, + path_string=binding_path_str, + ), + ), + ) + xr.suggest_interaction_profile_bindings( + instance=context.instance, + suggested_bindings=xr.InteractionProfileSuggestedBinding( + interaction_profile=xr.string_to_path( + context.instance, + "/interaction_profiles/valve/index_controller", + ), + count_suggested_bindings=len(suggested_bindings), + suggested_bindings=suggested_bindings, + ), + ) -# Checks if the given button on the given controller is pressed. -# Defaults to joystick click / left hand. -# Returns three values: -# 0 - button not pressed -# 1 - button rising edge -# 2 - button falling edge -def pollButtonPress( - session_state: SessionState, - hand_id: vr.ETrackedControllerRole = hands["left"], - button_id: vr.EVRButtonId = buttons["joystick"], - ) -> int: - lh_idx = session_state.system.getTrackedDeviceIndexForControllerRole(hand_id) - #print("left hand device idx: {}".format(lh_idx)) + last_change_time = 0 + for frame_index, frame_state in enumerate(context.frame_loop()): + if context.session_state != xr.SessionState.FOCUSED: + yield EVENT_NONE + continue - got_state, state = session_state.system.getControllerState(lh_idx) - if not got_state: - return EVENT_NONE + active_action_set = xr.ActiveActionSet( + action_set=context.default_action_set, + subaction_path=xr.NULL_PATH, + ) + xr.sync_actions( + session=context.session, + sync_info=xr.ActionsSyncInfo( + count_active_action_sets=1, + active_action_sets=ctypes.pointer(active_action_set), + ), + ) - if state.unPacketNum == session_state.last_packet: - return EVENT_NONE + action_info = xr.ActionStateGetInfo(action=b_button_action) + action_bool = xr.get_action_state_boolean( + session=context.session, get_info=action_info + ) - # Clicking joysticks and moving joysticks fire the same events. To - # differentiate movement from clicking, we create a dead zone: if the event - # fires while the stick isn't moved far from center, we assume it's a - # click, not movement. - dead_zone_radius = 0.7 + if action_bool.changed_since_last_sync == 0: + yield EVENT_NONE + continue - button_mask = (1 << button_id) - ret = EVENT_NONE - if (state.ulButtonPressed & button_mask) != 0 and\ - (state.rAxis[0].x**2 + state.rAxis[0].y**2 < dead_zone_radius**2): - #print("button pressed: %016x" % state.ulButtonPressed) - #for i in range(0, 5): - # print("axis {} x: {} y: {}".format(i, state.rAxis[i].x, state.rAxis[i].y)) - if not session_state.event_high: - ret = EVENT_RISING_EDGE - session_state.event_high = True - elif session_state.event_high: - session_state.event_high = False - ret = EVENT_FALLING_EDGE - return ret + if action_bool.current_state == 1: + yield EVENT_RISING_EDGE + continue + else: + yield EVENT_FALLING_EDGE + continue if __name__ == "__main__": - session_state = SessionState() + gen = run() while True: - time.sleep(0.1) - - event = pollButtonPress(session_state, hand_id = hands["left"], button_id = buttons["joystick"]) - if event == EVENT_RISING_EDGE: - print("rising edge") - elif event == EVENT_FALLING_EDGE: - print("falling edge") + event = next(gen) + print(f"event: {event}") diff --git a/Scripts/steamvr_v2.py b/Scripts/steamvr_v2.py deleted file mode 100644 index bd23014..0000000 --- a/Scripts/steamvr_v2.py +++ /dev/null @@ -1,84 +0,0 @@ -from ctypes import cast, c_void_p, pointer -import xr - -inst = xr.create_instance() -print(f"inst: {inst}") - -system_get_info = xr.SystemGetInfo( - form_factor=xr.FormFactor.HEAD_MOUNTED_DISPLAY) -system_id = xr.get_system( - instance=inst, - get_info=system_get_info) -print(f"system_id: {system_id}") - -gfx_binding = xr.GraphicsBindingOpenGLWin32KHR() -gfx_binding_ptr = cast(pointer(gfx_binding), c_void_p) -session_info = xr.SessionCreateInfo( - system_id=system_id, - next=gfx_binding_ptr) -# TODO some issue with graphics binding. -session = xr.create_session( - instance=inst, - create_info=session_info) - -action_set_info = xr.ActionSetCreateInfo( - action_set_name="tastt_actions", - localized_action_set_name="TaSTT_Actions", # ignore internationalization for now - priority=0) -print(f"action_set_info: {action_set_info}") - -action_set = xr.create_action_set( - instance=inst, - create_info=action_set_info) -print(f"action_set: {action_set}") - -action_create_info = xr.ActionCreateInfo( - action_name="tastt_click", - localized_action_name="TaSTT_Click", - action_type=xr.ActionType.BOOLEAN_INPUT) -print(f"action_create_info: {action_create_info}") - -print(dir(xr.create_action)) -action = xr.create_action( - action_set=action_set, - create_info=action_create_info) -print(f"action: {action}") - -input_path = xr.string_to_path(instance=inst, path_string="/user/hand/right/input/trigger/click") -print(f"input_path: {input_path}") - -actions = xr.ActionSuggestedBinding( - action=action, - binding=input_path) -print(f"actions: {actions}") - -interaction_profile_path = xr.string_to_path(instance=inst, path_string="/interaction_profiles/valve/index_controller") -print(f"interaction_profile_path: {interaction_profile_path}") - -bindings = xr.InteractionProfileSuggestedBinding( - interaction_profile=interaction_profile_path, - count_suggested_bindings=1, - suggested_bindings=[actions]) -print(f"bindings: {bindings}") - -xr.suggest_interaction_profile_bindings( - instance=inst, - suggested_bindings=bindings) - -while True: - action_info = xr.ActionStateGetInfo( - action=action) - - action_bool = xr.get_action_state_boolean( - session=session, - get_info=action_info) - - break - -xr.destroy_instance(inst) - -# Paths: -# /usr/hand/{left,right}/input/{a,b,thumbstick}/{click,touch} - -print("Done!") - diff --git a/Scripts/transcribe.py b/Scripts/transcribe.py index 8a10783..e9873e9 100644 --- a/Scripts/transcribe.py +++ b/Scripts/transcribe.py @@ -559,23 +559,12 @@ def readKeyboardInput(audio_state, enable_local_beep: bool, def readControllerInput(audio_state, enable_local_beep: bool, use_builtin: bool, button: str): - session = None - first = True - while session == None and audio_state.run_app == True: - try: - session = steamvr.SessionState() - except: - if audio_state.enable_debug_mode: - print("steamvr is off, no controller input") - session = None - time.sleep(5) - RECORD_STATE = 0 PAUSE_STATE = 1 state = PAUSE_STATE - hand_id = steamvr.hands[button.split()[0]] - button_id = steamvr.buttons[button.split()[1]] + hand_id = button.split()[0] + button_id = button.split()[1] # Rough description of state machine: # Single short press: toggle transcription @@ -585,11 +574,12 @@ def readControllerInput(audio_state, enable_local_beep: bool, last_rising = time.time() last_medium_press_end = 0 + + button_generator = steamvr.pollButtonPress(hand=hand_id, button=button_id) while audio_state.run_app == True: - time.sleep(0.05) + time.sleep(0.01) - event = steamvr.pollButtonPress(session, hand_id=hand_id, - button_id=button_id) + event = next(button_generator) if event == steamvr.EVENT_RISING_EDGE: last_rising = time.time() |
