From e5c84a0ae9d2039ecddbadfb6d37ccc867d7ca87 Mon Sep 17 00:00:00 2001 From: Adam Mayer Date: Fri, 15 Dec 2017 21:57:49 -0500 Subject: [PATCH] refactor to add mock plotter added CLI for plot to svg set appropriate doc size for svg --- apple410/__init__.py | 50 +++++++++++++++++++++++++++++------------ apple410/cmdline.py | 15 +++++++++++++ apple410/plot_to_svg.py | 4 ++-- setup.py | 1 + 4 files changed, 54 insertions(+), 16 deletions(-) diff --git a/apple410/__init__.py b/apple410/__init__.py index a4838f3..557d9c2 100755 --- a/apple410/__init__.py +++ b/apple410/__init__.py @@ -9,12 +9,27 @@ def plot_to_svg(instream, outstream): p.read(instream) p.write(outstream) -class Apple410: - """A simple class for queing up commands for the Apple 410""" - def __init__(self, portname, baud=9600, flow_control_safe=False, cts_hack=False): - self.serial = serial.Serial(portname, baud, rtscts=False, dsrdtr=True, timeout=0.1) +class Apple410Common: + def __init__(self): self.pos = (0,0) self.wd = self.vp = (0,0,2394,1759) + + def move_to(self, coords): + self.send('MA{},{}'.format(coords[0],coords[1])) + self.pos = coords + + def draw_to(self, coords): + self.send('DA{:.2f},{:.2f}'.format(coords[0],coords[1])) + self.pos = coords + + def pen_select(self, index): + self.send('PS{}'.format(index)) + +class Apple410(Apple410Common): + """A simple class for queing up commands for the Apple 410""" + def __init__(self, portname, baud=9600, flow_control_safe=False, cts_hack=False): + Apple410Common.__init__(self) + self.serial = serial.Serial(portname, baud, rtscts=False, dsrdtr=True, timeout=0.1) self.flow_control_safe = flow_control_safe self.cts_hack = cts_hack @@ -39,14 +54,21 @@ class Apple410: self.sendchar(c) self.sendchar('\x03') - def move_to(self, coords): - self.send('MA{},{}'.format(coords[0],coords[1])) - self.pos = coords + def close(self): + self.serial.close() + +class MockApple410(Apple410Common): + def __init__(self, path): + Apple410Common.__init__(self) + if type(path) == type(''): + self.f = open(path,'w') + else: + self.f = path + + def send(self, command): + self.f.write(command) + self.f.write('\n') + + def close(self): + self.f.close() - def draw_to(self, coords): - self.send('DA{:.2f},{:.2f}'.format(coords[0],coords[1])) - self.pos = coords - - def pen_select(self, index): - self.send('PS{}'.format(index)) - diff --git a/apple410/cmdline.py b/apple410/cmdline.py index b9e038e..974630d 100644 --- a/apple410/cmdline.py +++ b/apple410/cmdline.py @@ -47,3 +47,18 @@ def svg2plot(): f = open(args.SVG) plot_svg(f,center=args.center) +from .plot_to_svg import Plotter + +def plot2svg(): + parser = argparse.ArgumentParser("plot2svg", + description="Convert a set of Apple 410 Color Plotter commands to an SVG") + parser.add_argument('PLOT', help='The plotter file to process. "-" will read plotter commands from standard input.') + args = parser.parse_args() + if args.PLOT == '-': + f = sys.stdin + else: + f = open(args.PLOT) + p = Plotter() + p.read(f) + p.write(sys.stdout) + diff --git a/apple410/plot_to_svg.py b/apple410/plot_to_svg.py index eb35c02..c9aade4 100755 --- a/apple410/plot_to_svg.py +++ b/apple410/plot_to_svg.py @@ -26,7 +26,7 @@ class Plotter: self.window = (0, 0, W, H) self.cur_g = None self.cur_d = None - self.d = svgwrite.Drawing(profile='tiny') + self.d = svgwrite.Drawing(profile='tiny',size=(W,H)) self.text_theta = 0 self.text_size = 1 self.pos = (0,0) @@ -161,7 +161,7 @@ class Plotter: def read(self,inf): for line in inf.readlines(): line = line.strip() - p.process_cmd(line) + self.process_cmd(line) if __name__=='__main__': p = Plotter() diff --git a/setup.py b/setup.py index 66cd333..649a6b6 100644 --- a/setup.py +++ b/setup.py @@ -54,6 +54,7 @@ setup( 'console_scripts': [ 'apple410=apple410.cmdline:main', 'a410svg2plot=apple410.cmdline:svg2plot', + 'a410plot2svg=apple410.cmdline:plot2svg', ], }, )