Add Harness class, simplify harness creation
This commit is contained in:
parent
a51da3731d
commit
548adcdfb7
@ -1,14 +1,14 @@
|
|||||||
import wireviz
|
from wireviz import Harness, Node, Cable
|
||||||
|
|
||||||
X1 = wireviz.Node('X1', type='D-Sub', gender='female', pinout=('DCD','RX','TX','DTR','GND','DSR','RTS','CTS','RI'), ports_right=True)
|
Harness = Harness()
|
||||||
X2 = wireviz.Node('X2', type='Molex KK 254', gender='female', pinout=('GND','RX','TX','NC','OUT','IN'), ports_left=True)
|
|
||||||
W1 = wireviz.Cable('W1', mm2=0.25, length=0.2, show_name=True, show_pinout=True, num_wires=3, color_code='DIN', shield=True)
|
Harness.add(Cable('W1', mm2=0.25, length=0.2, show_name=True, show_pinout=True, num_wires=3, color_code='DIN', shield=True))
|
||||||
|
Harness.add(Node('X1', type='D-Sub', gender='female', pinout=('DCD','RX','TX','DTR','GND','DSR','RTS','CTS','RI'), ports_right=True))
|
||||||
|
Harness.add(Node('X2', type='Molex KK 254', gender='female', pinout=('GND','RX','TX','NC','OUT','IN'), ports_left=True))
|
||||||
# Option 1: define wires and shield in one line
|
# Option 1: define wires and shield in one line
|
||||||
# W1.connect(X1,(5,2,3,5),(1,2,3,'s'),X2,(1,3,2,None))
|
Harness.objects['W1'].connect('X1',(5,2,3,5),(1,2,3,'s'),'X2',(1,3,2,None))
|
||||||
# Option 2: define wires and shield separately
|
# Option 2: define wires and shield separately
|
||||||
W1.connect(X1,(5,2,3),'auto',X2,(1,3,2)) # wires
|
# Harness.objects['W1'].connect('X1',(5,2,3),'auto','X2',(1,3,2)) # wires
|
||||||
W1.connect(X1,(5,),('s',),X2,(None,)) # shield
|
# Harness.objects['W1'].connect('X1',(5,),('s',),'X2',(None,)) # shield
|
||||||
X2.loop(5,6)
|
|
||||||
objects = [X1, X2, W1]
|
|
||||||
|
|
||||||
wireviz.output(objects)
|
Harness.graphviz()
|
||||||
|
|||||||
@ -1,32 +1,31 @@
|
|||||||
import wireviz
|
from wireviz import Harness, Node, Cable
|
||||||
|
|
||||||
|
# shortcuts for use during harness creation
|
||||||
PINOUT_I2C = ('GND','+5V','SCL','SDA')
|
PINOUT_I2C = ('GND','+5V','SCL','SDA')
|
||||||
COLORS_I2C = ('BK', 'RD', 'YE', 'GN')
|
COLORS_I2C = ('BK', 'RD', 'YE', 'GN')
|
||||||
|
|
||||||
PINOUT_SPI_DATAONLY = ('MISO','MOSI','SCK')
|
PINOUT_SPI_DATAONLY = ('MISO','MOSI','SCK')
|
||||||
|
|
||||||
X1 = wireviz.Node('X1',type='Molex KK 254', gender='female', pinout=(
|
Harness = Harness()
|
||||||
'GND',
|
|
||||||
'+5V',
|
|
||||||
'SCL',
|
|
||||||
'SDA',
|
|
||||||
'MISO',
|
|
||||||
'MOSI',
|
|
||||||
'SCK',
|
|
||||||
'N/C'
|
|
||||||
), ports_right=True)
|
|
||||||
X2 = wireviz.Node('X2', type='Molex KK 254', gender='female', pinout=PINOUT_I2C, ports_left=True)
|
|
||||||
X3 = wireviz.Node('X3', type='Molex KK 254', gender='female', pinout=PINOUT_I2C, ports_left=True)
|
|
||||||
X4 = wireviz.Node('X4', type='Molex KK 254', gender='female', pinout=('GND','+12V')+PINOUT_SPI_DATAONLY, ports_left=True)
|
|
||||||
X5 = wireviz.Node('X5', type='Molex Micro-Fit', gender='male', pinout=('GND','+12V'), ports_right=True)
|
|
||||||
W1 = wireviz.Cable('W1', mm2=0.14, length=0.2, colors=COLORS_I2C)
|
|
||||||
W2 = wireviz.Cable('W2', mm2=0.14, length=0.2, colors=COLORS_I2C)
|
|
||||||
W3 = wireviz.Cable('W3', mm2=0.14, length=0.2, colors=('BK','BU','OG','VT'))
|
|
||||||
W4 = wireviz.Cable('W4', mm2=0.5, length=0.35, colors=('BK','RD'))
|
|
||||||
W1.connect(X1,(1,2,3,4),'auto',X2,'auto')
|
|
||||||
W2.connect(X1,(1,2,3,4),'auto',X3,'auto')
|
|
||||||
W3.connect(X1,(1,5,6,7),'auto',X4,(1,3,4,5))
|
|
||||||
W4.connect(X5,'auto','auto',X4,'auto')
|
|
||||||
objects = [X1, X2, X3, X4, X5, W1, W2, W3, W4]
|
|
||||||
|
|
||||||
wireviz.output(objects)
|
Harness.add(Node('X1',type='Molex KK 254', gender='female', pinout=('GND',
|
||||||
|
'+5V',
|
||||||
|
'SCL',
|
||||||
|
'SDA',
|
||||||
|
'MISO',
|
||||||
|
'MOSI',
|
||||||
|
'SCK',
|
||||||
|
'N/C'), ports_right=True))
|
||||||
|
Harness.add(Node('X2', type='Molex KK 254', gender='female', pinout=PINOUT_I2C, ports_left=True))
|
||||||
|
Harness.add(Node('X3', type='Molex KK 254', gender='female', pinout=PINOUT_I2C, ports_left=True))
|
||||||
|
Harness.add(Node('X4', type='Molex KK 254', gender='female', pinout=('GND','+12V')+PINOUT_SPI_DATAONLY, ports_left=True))
|
||||||
|
Harness.add(Node('X5', type='Molex Micro-Fit', gender='male', pinout=('GND','+12V'), ports_right=True))
|
||||||
|
Harness.add(Cable('W1', mm2=0.14, length=0.2, colors=COLORS_I2C))
|
||||||
|
Harness.add(Cable('W2', mm2=0.14, length=0.2, colors=COLORS_I2C))
|
||||||
|
Harness.add(Cable('W3', mm2=0.14, length=0.2, colors=('BK','BU','OG','VT')))
|
||||||
|
Harness.add(Cable('W4', mm2=0.5, length=0.35, colors=('BK','RD')))
|
||||||
|
Harness.objects['W1'].connect('X1',(1,2,3,4),'auto','X2','auto')
|
||||||
|
Harness.objects['W2'].connect('X1',(1,2,3,4),'auto','X3','auto')
|
||||||
|
Harness.objects['W3'].connect('X1',(1,5,6,7),'auto','X4',(1,3,4,5))
|
||||||
|
Harness.objects['W4'].connect_all_straight('X5','X4')
|
||||||
|
|
||||||
|
Harness.graphviz()
|
||||||
|
|||||||
@ -1,15 +1,16 @@
|
|||||||
import wireviz
|
from wireviz import Harness, Node, Cable
|
||||||
|
|
||||||
wireviz.color_mode = 'full' # short/SHORT/full/FULL/hex/HEX
|
Harness = Harness()
|
||||||
|
Harness.color_mode = 'full'
|
||||||
|
|
||||||
X1 = wireviz.Node('X1', num_pins=10, ports_right=True)
|
Harness.add(Node('X1', num_pins=10, ports_right=True))
|
||||||
X2 = wireviz.Node('X2', num_pins=10, ports_left=True)
|
Harness.add(Node('X2', num_pins=10, ports_left=True))
|
||||||
W1 = wireviz.Cable('W1', num_wires=10, color_code='IEC')
|
Harness.add(Cable('W1', num_wires=10, color_code='IEC'))
|
||||||
W1.connect_all_straight(X1,X2)
|
Harness.objects['W1'].connect_all_straight('X1','X2')
|
||||||
X3 = wireviz.Node('X3', num_pins=10, ports_right=True)
|
|
||||||
X4 = wireviz.Node('X4', num_pins=10, ports_left=True)
|
|
||||||
W2 = wireviz.Cable('W2', num_wires=10, color_code='DIN')
|
|
||||||
W2.connect_all_straight(X3,X4)
|
|
||||||
objects = [X1, X2, W1, X3, X4, W2]
|
|
||||||
|
|
||||||
wireviz.output(objects)
|
Harness.add(Node('X3', num_pins=10, ports_right=True))
|
||||||
|
Harness.add(Node('X4', num_pins=10, ports_left=True))
|
||||||
|
Harness.add(Cable('W2', num_wires=10, color_code='DIN'))
|
||||||
|
Harness.objects['W2'].connect_all_straight('X3','X4')
|
||||||
|
|
||||||
|
Harness.graphviz()
|
||||||
|
|||||||
@ -32,7 +32,38 @@ color_full = {
|
|||||||
'BN': 'brown',
|
'BN': 'brown',
|
||||||
}
|
}
|
||||||
|
|
||||||
color_mode = 'SHORT'
|
class Harness:
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.color_mode = 'SHORT'
|
||||||
|
self.objects = {}
|
||||||
|
|
||||||
|
def add(self, object):
|
||||||
|
self.objects[object.name] = object
|
||||||
|
self.objects[object.name].color_mode = self.color_mode
|
||||||
|
|
||||||
|
def debug(self):
|
||||||
|
print(self.objects)
|
||||||
|
|
||||||
|
def graphviz(self, print_to_screen=False):
|
||||||
|
with open('output/output.dot','w') as f:
|
||||||
|
with open('input/header.dot','r') as infile:
|
||||||
|
for line in infile:
|
||||||
|
f.write(line)
|
||||||
|
f.write('\n\n')
|
||||||
|
|
||||||
|
for o in self.objects:
|
||||||
|
f.write(self.objects[o].graphviz() + '\n')
|
||||||
|
|
||||||
|
f.write('\n\n')
|
||||||
|
with open('input/footer.dot','r') as infile:
|
||||||
|
for line in infile:
|
||||||
|
f.write(line)
|
||||||
|
|
||||||
|
if print_to_screen == True:
|
||||||
|
with open('output/output.dot','r') as f:
|
||||||
|
for line in f:
|
||||||
|
print(line)
|
||||||
|
|
||||||
class Node:
|
class Node:
|
||||||
|
|
||||||
@ -44,6 +75,7 @@ class Node:
|
|||||||
self.ports_left = ports_left
|
self.ports_left = ports_left
|
||||||
self.ports_right = ports_right
|
self.ports_right = ports_right
|
||||||
self.loops = []
|
self.loops = []
|
||||||
|
self.color_mode = 'SHORT'
|
||||||
|
|
||||||
if pinout is None:
|
if pinout is None:
|
||||||
self.pinout = ('',) * num_pins
|
self.pinout = ('',) * num_pins
|
||||||
@ -66,12 +98,6 @@ class Node:
|
|||||||
loop_side = side
|
loop_side = side
|
||||||
self.loops.append((from_pin, to_pin, loop_side))
|
self.loops.append((from_pin, to_pin, loop_side))
|
||||||
|
|
||||||
def __repr__(self):
|
|
||||||
return '{} = {} {}'.format(self.name, len(self.pinout), self.pinout)
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
return '{}'.format(self.name)
|
|
||||||
|
|
||||||
def graphviz(self):
|
def graphviz(self):
|
||||||
s = ''
|
s = ''
|
||||||
# print header
|
# print header
|
||||||
@ -137,6 +163,7 @@ class Cable:
|
|||||||
self.show_pinout = show_pinout
|
self.show_pinout = show_pinout
|
||||||
self.shield = shield
|
self.shield = shield
|
||||||
self.connections = []
|
self.connections = []
|
||||||
|
self.color_mode = 'SHORT'
|
||||||
if color_code is None and colors is None:
|
if color_code is None and colors is None:
|
||||||
self.colors = ('',) * num_wires
|
self.colors = ('',) * num_wires
|
||||||
else:
|
else:
|
||||||
@ -172,9 +199,6 @@ class Cable:
|
|||||||
def connect_all_straight(self, from_name, to_name):
|
def connect_all_straight(self, from_name, to_name):
|
||||||
self.connect(from_name, 'auto', 'auto', to_name, 'auto')
|
self.connect(from_name, 'auto', 'auto', to_name, 'auto')
|
||||||
|
|
||||||
def __repr__(self):
|
|
||||||
return '{} = {} {}\n {}'.format(self.name, len(self.colors), self.colors, self.connections)
|
|
||||||
|
|
||||||
def debug(self):
|
def debug(self):
|
||||||
print(self.name)
|
print(self.name)
|
||||||
print(self.colors)
|
print(self.colors)
|
||||||
@ -231,17 +255,17 @@ class Cable:
|
|||||||
else:
|
else:
|
||||||
l = []
|
l = []
|
||||||
for i,x in enumerate(self.colors,1):
|
for i,x in enumerate(self.colors,1):
|
||||||
if color_mode == 'full':
|
if self.color_mode == 'full':
|
||||||
x = color_full[x].lower()
|
x = color_full[x].lower()
|
||||||
elif color_mode == 'FULL':
|
elif self.color_mode == 'FULL':
|
||||||
x = color_hex[x].upper()
|
x = color_hex[x].upper()
|
||||||
elif color_mode == 'hex':
|
elif self.color_mode == 'hex':
|
||||||
x = color_hex[x].lower()
|
x = color_hex[x].lower()
|
||||||
elif color_mode == 'HEX':
|
elif self.color_mode == 'HEX':
|
||||||
x = color_hex[x].upper()
|
x = color_hex[x].upper()
|
||||||
elif color_mode == 'short':
|
elif self.color_mode == 'short':
|
||||||
x = x.lower()
|
x = x.lower()
|
||||||
elif color_mode == 'SHORT':
|
elif self.color_mode == 'SHORT':
|
||||||
x = x.upper()
|
x = x.upper()
|
||||||
else:
|
else:
|
||||||
raise Exception('Unknown color mode')
|
raise Exception('Unknown color mode')
|
||||||
@ -281,23 +305,3 @@ class Cable:
|
|||||||
s = s + '}'
|
s = s + '}'
|
||||||
|
|
||||||
return s
|
return s
|
||||||
|
|
||||||
def output(objects, print_to_screen=False):
|
|
||||||
with open('output/output.dot','w') as f:
|
|
||||||
with open('input/header.dot','r') as infile:
|
|
||||||
for line in infile:
|
|
||||||
f.write(line)
|
|
||||||
f.write('\n\n')
|
|
||||||
|
|
||||||
for o in objects:
|
|
||||||
f.write(o.graphviz() + '\n')
|
|
||||||
|
|
||||||
f.write('\n\n')
|
|
||||||
with open('input/footer.dot','r') as infile:
|
|
||||||
for line in infile:
|
|
||||||
f.write(line)
|
|
||||||
|
|
||||||
if print_to_screen == True:
|
|
||||||
with open('output/output.dot','r') as f:
|
|
||||||
for line in f:
|
|
||||||
print(line)
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user