-
Notifications
You must be signed in to change notification settings - Fork 0
/
tagClient.py
69 lines (60 loc) · 1.61 KB
/
tagClient.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
67
68
69
from joy import JoyApp, progress
from joy.plans import AnimatorPlan
from joy.decl import KEYDOWN
from socket import socket, AF_INET, SOCK_DGRAM, error as SocketError
from numpy import array, mean
from json import loads as json_loads
from time import time as now
if __name__ != "__main__":
raise RuntimeError("Run this as a script")
try:
s.close()
except Exception:
pass
def _animation(fig):
s = socket(AF_INET, SOCK_DGRAM )
s.bind(("",0xB00))
s.setblocking(0)
fig.clf()
ax = fig.add_subplot(111)
msg = None
src = None
last = now()
while True:
try:
# read data as fast as possible
m,msrc = s.recvfrom(1<<16)
if not (msrc == src):
src = msrc
progress("New tag data source: %s:%d"%src)
if m and len(m)>2:
msg = m
continue
except SocketError as se:
# until we've run out; last message remains in m
pass
# make sure we got something
if not msg:
yield
continue
# display it
ax.cla()
ax.set_title("%.1f fps" % (1/(now()-last)))
last = now()
for d in json_loads(msg):
if type(d) is not dict:
continue
a = array(d['p'])
ax.plot( a[[0,1,2,3,0],0], a[[0,1,2,3,0],1], '.-b' )
ax.plot( a[[0],0], a[[0],1], 'og' )
ax.text( mean(a[:,0]), mean(a[:,1]), d['i'], ha='center',va='center' )
ax.axis([0,1600,0,1200])
yield
class App(JoyApp):
def onStart(self):
AnimatorPlan(self,_animation).start()
def onEvent(self,evt):
if evt.type == KEYDOWN:
return JoyApp.onEvent(self,evt)
app = App(cfg={'windowSize':(1080,740)})
app.run()