-
Notifications
You must be signed in to change notification settings - Fork 1
/
bg.py
66 lines (55 loc) · 1.86 KB
/
bg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#run this program in background to provide tracker data to the nvim plugin
import os, sys
import pysurvive
def main():
c_x = 0.495 #center offset x
c_y = 0.07 #center offset y
s_x = 2.45 #scale x
s_y = 2.45 #scale y
f1 = "/tmp/nvim_tracker_f1"
if not os.path.exists(f1):
os.mkfifo(f1)
r0 = 0
r1 = 0
screen_y, screen_x = 0, 0
W, H = 2560, 1440
actx = pysurvive.SimpleContext(sys.argv)
fifofile = open(f1, 'w')
running = True
while running:
try:
updated = actx.NextUpdated()
if updated:
poseObj = updated.Pose()
poseData = poseObj[0]
poseTimestamp = poseObj[1]
#WM0 is vive tracker, T20 is HMD
#when tracker is connected with usb, may be T21 instead of WM0
if str(updated.Name(), 'utf-8') == "WM0":
r0 = poseData.Rot[0]
r1 = poseData.Rot[1]
screen_y = H * (r0 + c_y)*s_y
screen_x = W * (1- (r1 + c_x)*s_x)
fifofile.write(str(screen_x) + ',' + str(screen_y) + '\n')
fifofile.flush()
except KeyboardInterrupt:
print("Stopping")
running = False
fifofile.close()
if os.path.exists(f1):
os.remove(f1)
quit()
except BrokenPipeError:
#remakes the fifo file if pipe is broken, so other instances of nvim plugin can get data without restarting bg script
print("Broken Pipe, restarting")
if os.path.exists(f1):
os.remove(f1)
os.mkfifo(f1)
fifofile = open(f1, 'w')
if __name__ == "__main__":
f1 = "/tmp/nvim_tracker_f1"
try:
main()
finally:
if os.path.exists(f1):
os.remove(f1)