@@ -40,28 +40,31 @@ class POINT(ctypes.Structure):
40
40
41
41
42
42
# initialize networking
43
- sock = socket .socket (socket .AF_INET , socket .SOCK_DGRAM ) # Read datagrams over UDP
44
- sock .settimeout (1 ) # Without a timeout, this script will "hang" if nothing is received
43
+ # Read datagrams over UDP
44
+ sock = socket .socket (socket .AF_INET , socket .SOCK_DGRAM )
45
+ # Without a timeout, this script will "hang" if nothing is received
46
+ sock .settimeout (1 )
45
47
sock .bind ((args .host , args .port )) # Register our socket
46
48
47
49
# How to get local IP address?
48
50
# Doesn't work for me: socket.gethostbyname(socket.gethostname())
49
- # For now, just manually go to settings -> Wi-fi and look up your *local* IP address.
50
- # It will be something like 192.x.x.x or 10.x.x.x
51
- # This is not your public Internet address. This is your local area network address.
52
- # On Windows, make sure it's set to a private network to allow discovery.
53
- # You'd think public would allow discovery, but when you are *in public* - like at a coffee shop - you don't want strangers to access your PC.
51
+ # For now, just manually go to settings -> Wi-fi and look up your *local* IP
52
+ # address. It will be something like 192.x.x.x or 10.x.x.x This is not your
53
+ # public Internet address. This is your local area network address. On Windows,
54
+ # make sure it's set to a private network to allow discovery. You'd think public
55
+ # would allow discovery, but when you are *in public* - like at a coffee shop -
56
+ # you don't want strangers to access your PC.
54
57
text_listening = (
55
- f"Listening on { sock .getsockname ()} for mouse data from Raspberry Pi server..."
58
+ f"Listening on {
59
+ sock .getsockname ()} for mouse data from Raspberry Pi server..."
56
60
)
57
61
print (ctime () + " - " + text_listening )
58
62
print ("\n Press Ctrl-C to exit\n " )
59
63
60
64
61
- # Stats for debugging & performance.
62
- # The goal is 60 frames per second, or 16.67ms per frame.
63
- # That leads to a very smooth mouse cursor. (SmartNav was 100 fps)
64
- # A standard non-gaming monitor is also 60Hz. (TV is 30 fps)
65
+ # Stats for debugging & performance. The goal is 60 frames per second, or
66
+ # 16.67ms per frame. That leads to a very smooth mouse cursor. (SmartNav was 100
67
+ # fps) A standard non-gaming monitor is also 60Hz. (TV is 30 fps)
65
68
@dataclass
66
69
class PhilNavDebug :
67
70
time_start = time ()
@@ -78,9 +81,11 @@ class PhilNavDebug:
78
81
# 3. Repeat forever until Ctrl-C
79
82
while True :
80
83
try :
81
- # 48 bytes of 6 doubles in binary C format. Why? Because it's OpenTrack's protocol.
84
+ # 48 bytes of 6 doubles in binary C format. Why? Because it's
85
+ # OpenTrack's protocol.
82
86
# x, y, z, pitch, yaw, roll = struct.unpack('dddddd', data)
83
- # PhilNav uses x, y as x_diff, y_diff and moves the mouse relative to its current position.
87
+ # PhilNav uses x, y as x_diff, y_diff and moves the mouse relative to
88
+ # its current position.
84
89
# https://github.com/opentrack/opentrack/issues/747
85
90
data , addr = sock .recvfrom (48 )
86
91
except TimeoutError :
@@ -101,13 +106,17 @@ class PhilNavDebug:
101
106
ctypes .windll .user32 .GetCursorPos (
102
107
ctypes .byref (pt )
103
108
) # get current mouse position by reference (C++ thing)
104
- # I'm moving the Y axis slightly faster because looking left and right is easier than nodding up and down.
105
- # Also, monitors are wider than they are tall.
109
+ # I'm moving the Y axis slightly faster because looking left and right
110
+ # is easier than nodding up and down. Also, monitors are wider than they
111
+ # are tall.
106
112
x_new = round (pt .x + x * args .speed )
107
113
y_new = round (pt .y + y * args .speed * 1.33 )
108
114
ctypes .windll .user32 .SetCursorPos (x_new , y_new ) # move mouse cursor
109
115
110
- # I'm trying to measure the total time from capturing the frame on the camera to moving the mouse cursor on my PC. This isn't super accurate. It's sometimes negative (TIME TRAVEL!!!). The clock difference between the Raspberry Pi and my PC seems to be around 10-20ms?
116
+ # I'm trying to measure the total time from capturing the frame on the
117
+ # camera to moving the mouse cursor on my PC. This isn't super accurate.
118
+ # It's sometimes negative (TIME TRAVEL!!!). The clock difference between
119
+ # the Raspberry Pi and my PC seems to be around 10-20ms?
111
120
time_diff_ms = int ((time () - roll ) * 1000 )
112
121
113
122
# it's 60 FPS, so only debug once per second
@@ -117,8 +126,10 @@ class PhilNavDebug:
117
126
# display legend every 5 seconds
118
127
if PhilNavDebug .debug_num % 5 == 1 :
119
128
logging .info (
120
- f" { ctime ()} - Received: ({ 'x_diff' :>8} ,{ 'y_diff' :>8} ,{ 'n/a' :>8} ,{ 'n/a' :>8} ,{ 'loc ns' :>8} ,{ 'net ms' :>8} )"
129
+ f" { ctime ()} - Received: ({ 'x_diff' :>8} ,{ 'y_diff' :>8} ,{
130
+ 'n/a' :>8} ,{ 'n/a' :>8} ,{ 'loc ns' :>8} ,{ 'net ms' :>8} )"
121
131
)
122
132
logging .info (
123
- f" { ctime ()} - Received: ({ x :> 8.2f} ,{ y :> 8.2f} ,{ z :> 8.2f} ,{ pitch :> 8.2f} ,{ (time () - PhilNavDebug .msg_time_start )* 1000 :> 8.2f} ,{ time_diff_ms :> 8} )"
133
+ f" { ctime ()} - Received: ({ x :> 8.2f} ,{ y :> 8.2f} ,{ z :> 8.2f} ,{ pitch :> 8.2f} ,{
134
+ (time () - PhilNavDebug .msg_time_start )* 1000 :> 8.2f} ,{ time_diff_ms :> 8} )"
124
135
)
0 commit comments