yum/FastTextPager
Compressed text paging over OSC.
git clone https://git.yummers.dev/yum/FastTextPager
73de7cb
master
1#!/usr/bin/env python3 2 3import openvr as vr 4import sys 5import time 6 7EVENT_NONE = 0 8EVENT_RISING_EDGE = 1 9EVENT_FALLING_EDGE = 2 10 11class InputEvent : 12def __init__ (self , 13opcode :int ): 14self .opcode = opcode 15 16# Checks if the given button on the given controller is pressed. 17def pollButtonPress ( 18hand :str = "right" , 19button :str = "b" , 20shared_data = None # SharedThreadData object 21 )-> int : 22hands = {} 23hands ["left" ]= vr .TrackedControllerRole_LeftHand 24hands ["right" ]= vr .TrackedControllerRole_RightHand 25 26buttons = {} 27buttons ["a" ]= vr .k_EButton_IndexController_A 28buttons ["b" ]= vr .k_EButton_IndexController_B 29buttons ["thumbstick" ]= vr .k_EButton_Axis0 30 31system = None 32first = True 33while not shared_data .exit_event .is_set ()and not system : 34try : 35system = vr .init (vr .VRApplication_Background ) 36except Exception as e : 37if first : 38f"Failed to start steamVR input thread: { repr ( e ) } " ,file = sys .stderr ) 39first = False 40time .sleep (1 ) 41last_packet = 0 42event_high = False 43 44while not shared_data .exit_event .is_set (): 45time .sleep (0.01 ) 46 47lh_idx = system .getTrackedDeviceIndexForControllerRole (hands [hand ]) 48#print("left hand device idx: {}".format(lh_idx)) 49 50got_state ,state = system .getControllerState (lh_idx ) 51if not got_state : 52continue 53 54if state .unPacketNum == last_packet : 55continue 56 57# Clicking joysticks and moving joysticks fire the same events. To 58# differentiate movement from clicking, we create a dead zone: if the event 59# fires while the stick isn't moved far from center, we assume it's a 60# click, not movement. 61dead_zone_radius = 0.7 62 63button_mask = (1 << buttons [button ]) 64ret = EVENT_NONE 65if (state .ulButtonPressed & button_mask )!= 0 and \ 66 (state .rAxis [0 ].x ** 2 + state .rAxis [0 ].y ** 2 < dead_zone_radius ** 2 ): 67#print("button pressed: %016x" % state.ulButtonPressed) 68#for i in range(0, 5): 69# print("axis {} x: {} y: {}".format(i, state.rAxis[i].x, state.rAxis[i].y)) 70if not event_high : 71yield InputEvent (EVENT_RISING_EDGE ) 72event_high = True 73elif event_high : 74event_high = False 75yield InputEvent (EVENT_FALLING_EDGE ) 76 77if __name__ == "__main__" : 78gen = pollButtonPress () 79while True : 80time .sleep (0.1 ) 81 82event = pollButtonPress (session_state ) 83if event == EVENT_RISING_EDGE : 84"rising edge" ) 85elif event == EVENT_FALLING_EDGE : 86"falling edge" ) 87