yum/FastTextPager

Compressed text paging over OSC.

git clone https://git.yummers.dev/yum/FastTextPager

yumMore stuff73de7cb

master
2.7 KiB87 linesraw
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:
12    def __init__(self,
13            opcode: int):
14        self.opcode = opcode
15
16# Checks if the given button on the given controller is pressed.
17def pollButtonPress(
18        hand: str = "right",
19        button: str = "b",
20        shared_data = None  # SharedThreadData object
21        ) -> int:
22    hands = {}
23    hands["left"] = vr.TrackedControllerRole_LeftHand
24    hands["right"] = vr.TrackedControllerRole_RightHand
25
26    buttons = {}
27    buttons["a"] = vr.k_EButton_IndexController_A
28    buttons["b"] = vr.k_EButton_IndexController_B
29    buttons["thumbstick"] = vr.k_EButton_Axis0
30
31    system = None
32    first = True
33    while not shared_data.exit_event.is_set() and not system:
34        try:
35            system = vr.init(vr.VRApplication_Background)
36        except Exception as e:
37            if first:
38                print(f"Failed to start steamVR input thread: {repr(e)}", file=sys.stderr)
39            first = False
40            time.sleep(1)
41    last_packet = 0
42    event_high = False
43
44    while not shared_data.exit_event.is_set():
45        time.sleep(0.01)
46
47        lh_idx = system.getTrackedDeviceIndexForControllerRole(hands[hand])
48        #print("left hand device idx: {}".format(lh_idx))
49
50        got_state, state = system.getControllerState(lh_idx)
51        if not got_state:
52            continue
53
54        if state.unPacketNum == last_packet:
55            continue
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.
61        dead_zone_radius = 0.7
62
63        button_mask = (1 << buttons[button])
64        ret = EVENT_NONE
65        if (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))
70            if not event_high:
71                yield InputEvent(EVENT_RISING_EDGE)
72            event_high = True
73        elif event_high:
74            event_high = False
75            yield InputEvent(EVENT_FALLING_EDGE)
76
77if __name__ == "__main__":
78    gen = pollButtonPress()
79    while True:
80        time.sleep(0.1)
81
82        event = pollButtonPress(session_state)
83        if event == EVENT_RISING_EDGE:
84            print("rising edge")
85        elif event == EVENT_FALLING_EDGE:
86            print("falling edge")
87