93 lines
2.5 KiB
Python
Executable File
93 lines
2.5 KiB
Python
Executable File
|
|
import serial
|
|
|
|
def getWin():
|
|
ser = serial.Serial('/dev/ttyUSB0', 9600)
|
|
ser.write('OW;')
|
|
s=[]
|
|
if not s:
|
|
print 'Datei Fehler'
|
|
while True:
|
|
x=ser.read()
|
|
if x == "\x0d":
|
|
break
|
|
s.append(x)
|
|
return tuple(int(x) for x in "".join(s).split(","))
|
|
|
|
def parse(filename):
|
|
with open(filename) as file:
|
|
contents = file.read()
|
|
commands = []
|
|
for command in contents.split(";"):
|
|
name, args = command[:2], command[2:]
|
|
args = [int(arg) for arg in args.split(",")] if args else []
|
|
commands.append((name, args))
|
|
return commands
|
|
|
|
def compile(commands):
|
|
return ";".join(name + ",".join(str(arg) for arg in args)
|
|
for name, args in commands)
|
|
|
|
def scale(commands, factor):
|
|
for name,args in commands:
|
|
if name in ("PU","PD","PA","PR"):
|
|
for i in range(len(args)):
|
|
args[i] = int(args[i]*factor)
|
|
return commands
|
|
|
|
def left(commands, left):
|
|
for name,args in commands:
|
|
if name in ("PU","PD","PA","PR"):
|
|
if len(args):
|
|
args[1] +=left
|
|
return commands
|
|
|
|
def up(commands, up):
|
|
for name,args in commands:
|
|
if name in ("PU","PD","PA","PR"):
|
|
if len(args):
|
|
args[0] +=up
|
|
return commands
|
|
|
|
|
|
def oob(commands):
|
|
xmin, ymin, xmax, ymax = getWin()
|
|
for name,args in commands:
|
|
if name in ("PU","PD","PA","PR"):
|
|
if xmin < args[0] <xmax or ymin < args[1] < ymax:
|
|
return False
|
|
else:
|
|
return False
|
|
|
|
def center(command):
|
|
xmin, ymin, xmax, ymax = getWin()
|
|
maxx = maxy = minx = miny = 0
|
|
for name,args in command:
|
|
if len(args)>1:
|
|
if args[0] > maxx:
|
|
maxx = args[0]
|
|
if args[0] < minx:
|
|
minx = args[0]
|
|
if args[1] < miny:
|
|
miny = args[1]
|
|
if args[1] > maxy:
|
|
maxy =args[1]
|
|
xcent= int((xmax-xmin)/2)
|
|
ycent= int((ymax-ymin)/2)
|
|
centy= int((maxy-miny)/2)
|
|
centx= int((maxx-minx)/2)
|
|
return up(left(command,centy-ycent),centx-xcent)
|
|
|
|
|
|
|
|
out = center(scale(parse("vishnu.hpgl"),2.5))
|
|
print out
|
|
print'test'
|
|
print oob(out)
|
|
|
|
|
|
# print name, args
|
|
ser = serial.Serial('/dev/ttyUSB0', 9600)
|
|
if not oob(out):
|
|
ser.write(compile(out))
|