Skip to content

Commit fd6bbd4

Browse files
committed
Accept host; mac parsing
1 parent 5f88eab commit fd6bbd4

File tree

8 files changed

+147
-7
lines changed

8 files changed

+147
-7
lines changed

find_dac.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ def find_dac_with_mac(mac):
99
s = socket(AF_INET, SOCK_DGRAM)
1010
s.bind(('0.0.0.0', BCAST_PORT))
1111

12+
if type(mac) is str:
13+
mac = Mac(mac)
14+
1215
print 'Looking for DAC (%s)' % mac.macStr
1316
macsFound = set()
1417

main.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
def main():
1515

16-
p1 = VirtualDac()
16+
p1 = VirtualDac(
17+
host='255.255.255.255'
18+
)
1719
q = p1.get_queue()
1820
p1.start()
1921

@@ -23,7 +25,6 @@ def main():
2325
p1.join()
2426
p2.join()
2527

26-
2728
if __name__ == '__main__':
2829
main()
2930

net/macs.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
1+
import struct
12

23
def mac_to_str(mac):
4+
"""
5+
Convert a `mac-type` to a colon-separated user-friendly string.
6+
* uuid.getnode (long)
7+
* EtherDream packet subset/parsed struct (str)
8+
"""
9+
10+
# Mac returned by uuid module's `getnode` is a long
11+
if type(mac) is long:
12+
byts = []
13+
while mac > 0:
14+
d = mac & 0xFF
15+
byts.append('%02x' %d)
16+
mac >>= 8
17+
18+
byteStr = int(''.join(byts), 16)
19+
mac = struct.pack('<' + 'Q', byteStr)[:6] # Two wasted bits
20+
321
# From Jacob Potter's EtherDream dac.py
422
return ':'.join('%02x' % (ord(x), ) for x in mac)
523

square.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/usr/bin/env python
2+
3+
from oldlib import dac
4+
from oldlib.common import *
5+
from find_dac import find_dac_with_mac
6+
7+
import math
8+
import itertools
9+
import sys
10+
import time
11+
12+
COLOR_R = CMAX
13+
COLOR_G = CMAX
14+
COLOR_B = CMAX
15+
16+
SIZE = 9000
17+
XOFF = 0
18+
YOFF = 0
19+
20+
def line_generator(pt1, pt2, backward=False, steps = 100):
21+
xdiff = pt1.x - pt2.x
22+
ydiff = pt1.y - pt2.y
23+
if not backward:
24+
for i in xrange(0, steps, 1):
25+
j = float(i)/steps
26+
x = pt1.x + (xdiff * j)
27+
y = pt1.y + (ydiff * j)
28+
yield (x, y, COLOR_R, COLOR_G, COLOR_B)
29+
30+
else:
31+
for i in xrange(steps, 0, -1):
32+
j = float(i)/steps
33+
x = pt1.x + (xdiff * j)
34+
y = pt1.y + (ydiff * j)
35+
yield (x, y, COLOR_R, COLOR_G, COLOR_B)
36+
37+
class LinePointStream(object):
38+
39+
def __init__(self, size=500, r=0, g=0, b=0, x=0, y=0):
40+
self.size = size
41+
self.r = r
42+
self.g = g
43+
self.b = b
44+
45+
self.stream = self.produce()
46+
47+
self.linePts = [
48+
(Point(0+x, 0+y), Point(0+x, size+y)),
49+
(Point(0+x, -size+y), Point(-size+x, -size+y)),
50+
(Point(size+x, -size+y), Point(size+x, -2*size+y)),
51+
(Point(size+x, 0+y), Point(2*size+x, 0+y))
52+
]
53+
54+
self.curLineIdx = len(self.linePts)-1
55+
self.advanceLine()
56+
57+
def advanceLine(self):
58+
self.curLineIdx = (self.curLineIdx+1) % len(self.linePts)
59+
self.curLine = line_generator(
60+
self.linePts[self.curLineIdx][0],
61+
self.linePts[self.curLineIdx][1])
62+
63+
def produce(self):
64+
while True:
65+
try:
66+
yield self.curLine.next()
67+
except:
68+
self.advanceLine()
69+
70+
"""
71+
# Line out
72+
for i in xrange(0, steps, 1):
73+
j = float(i)/steps
74+
x = self.x1 + (xdiff * j)
75+
y = self.y1 + (ydiff * j)
76+
yield (x, y, self.r, self.g, self.b)
77+
78+
# Line back in
79+
for i in xrange(0, steps, 1):
80+
j = float(i)/steps
81+
x = self.x2 - xdiff * j
82+
y = self.y2 - ydiff * j
83+
yield (x, y, self.r, self.g, self.b)
84+
"""
85+
86+
def read(self, n):
87+
d = [self.stream.next() for i in xrange(n)]
88+
return d
89+
90+
while True:
91+
macaddr = sys.argv[1]
92+
print 'Going to find DAC w/ mac %s' % macaddr
93+
m = find_dac_with_mac(macaddr)
94+
try:
95+
d = dac.DAC(m)
96+
ps = LinePointStream(SIZE, x=XOFF, y=YOFF)
97+
print 'Play stream...'
98+
d.play_stream(ps)
99+
100+
except KeyboardInterrupt:
101+
sys.exit()
102+
103+
except Exception as e:
104+
# Hopefully the galvos aren't melting...
105+
print "EXCEPTION"
106+
print e
107+
time.sleep(0.01)
108+
continue
109+

util/macs.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

vdac/broadcast.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ def run(self):
3333
s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
3434
s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
3535

36-
bp = BroadcastPacket().toStruct()
36+
bp = BroadcastPacket()
37+
38+
print 'Broadcasting as available MAC %s' % bp.getMacStr()
39+
40+
bp = bp.toStruct()
3741

3842
running = True
3943

vdac/packets.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import struct
22
from uuid import getnode as get_mac
3+
from net.macs import mac_to_str
34

45
class BroadcastPacket(object):
56
"""
@@ -32,6 +33,9 @@ def __init__(self):
3233
self.point_rate = 0
3334
self.point_count = 0
3435

36+
def getMacStr(self):
37+
return mac_to_str(self.mac)
38+
3539
def toStruct(self):
3640
byts = []
3741
mac = self.mac

vdac/vdac.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class VirtualDac(Process):
1414
usage.
1515
"""
1616

17-
def __init__(self, queue=None):
17+
def __init__(self, queue=None, host=''):
1818
super(VirtualDac, self).__init__()
1919

2020
if not queue:
@@ -24,6 +24,8 @@ def __init__(self, queue=None):
2424
self._is_running = False
2525
self._lock = RLock()
2626

27+
self._host = host
28+
2729
def get_queue(self):
2830
return self._queue
2931

@@ -42,7 +44,7 @@ def get_client():
4244
# Listen for client connection
4345
s = socket(AF_INET, SOCK_STREAM)
4446
s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
45-
s.bind(('localhost', 7765))
47+
s.bind(('', 7765))
4648
s.listen(3)
4749
return s.accept() # return (csock, addr)
4850

@@ -52,7 +54,7 @@ def get_client():
5254
running = True
5355

5456
while running:
55-
bt = BroadcastThread()
57+
bt = BroadcastThread(self._host)
5658

5759
try:
5860
print 'Broadcasting...'

0 commit comments

Comments
 (0)