From 7e7e607e3f6a3de5e7b833d028b77afa619c8024 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Sun, 20 Nov 2016 20:40:31 -0500 Subject: [PATCH] klippy: Create the /tmp/printer pseudo tty before opening config file Signed-off-by: Kevin O'Connor --- klippy/gcode.py | 16 ++++++++-------- klippy/klippy.py | 23 ++++++++++------------- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/klippy/gcode.py b/klippy/gcode.py index e4798159..36c552f4 100644 --- a/klippy/gcode.py +++ b/klippy/gcode.py @@ -9,10 +9,10 @@ import homing # Parse out incoming GCode and find and translate head movements class GCodeParser: RETRY_TIME = 0.100 - def __init__(self, printer, fd, inputfile=False): + def __init__(self, printer, fd, is_fileinput=False): self.printer = printer self.fd = fd - self.inputfile = inputfile + self.is_fileinput = is_fileinput # Input handling self.reactor = printer.reactor self.fd_handle = None @@ -123,11 +123,11 @@ class GCodeParser: lines[0] = self.input_commands[0] + lines[0] self.input_commands = lines self.process_commands(eventtime) - if not data and self.inputfile: + if not data and self.is_fileinput: self.finish() # Response handling def ack(self, msg=None): - if not self.need_ack or self.inputfile: + if not self.need_ack or self.is_fileinput: return if msg: os.write(self.fd, "ok %s\n" % (msg,)) @@ -136,7 +136,7 @@ class GCodeParser: self.need_ack = False def respond(self, msg): logging.debug(msg) - if self.inputfile: + if self.is_fileinput: return os.write(self.fd, msg+"\n") # Busy handling @@ -188,7 +188,7 @@ class GCodeParser: self.gcode.respond(self.gcode.get_temp()) self.last_temp_time = eventtime return self.cur_heater.check_busy(eventtime) - if self.inputfile: + if self.is_fileinput: return self.set_busy(temp_busy_handler_wrapper()) def set_temp(self, heater, params, wait=False): @@ -249,7 +249,7 @@ class GCodeParser: if not axes: axes = [0, 1, 2] homing_state = homing.Homing(self.toolhead, axes) - if self.inputfile: + if self.is_fileinput: homing_state.set_no_verify_retract() self.toolhead.home(homing_state) def axes_update(homing_state): @@ -307,7 +307,7 @@ class GCodeParser: kinpos[0], kinpos[1], kinpos[2])) def cmd_M119(self, params): # Get Endstop Status - if self.inputfile: + if self.is_fileinput: return print_time = self.toolhead.get_last_move_time() query_state = homing.QueryEndstops(print_time, self.respond) diff --git a/klippy/klippy.py b/klippy/klippy.py index e772b3ce..191e5eed 100644 --- a/klippy/klippy.py +++ b/klippy/klippy.py @@ -31,20 +31,12 @@ class ConfigWrapper: return ConfigWrapper(self.printer, section) class Printer: - def __init__(self, conffile, debuginput=None): + def __init__(self, conffile, input_fd, is_fileinput=False): self.fileconfig = ConfigParser.RawConfigParser() self.fileconfig.read(conffile) self.reactor = reactor.Reactor() - self._pconfig = ConfigWrapper(self, 'printer') - ptty = self._pconfig.get('pseudo_tty', '/tmp/printer') - if debuginput is None: - pseudo_tty = util.create_pty(ptty) - else: - pseudo_tty = debuginput.fileno() - - self.gcode = gcode.GCodeParser( - self, pseudo_tty, inputfile=debuginput is not None) + self.gcode = gcode.GCodeParser(self, input_fd, is_fileinput) self.mcu = mcu.MCU(self, ConfigWrapper(self, 'mcu')) self.stats_timer = self.reactor.register_timer( self.stats, self.reactor.NOW) @@ -63,7 +55,7 @@ class Printer: self.objects['heater_bed'] = heater.PrinterHeater( self, ConfigWrapper(self, 'heater_bed')) self.objects['toolhead'] = toolhead.ToolHead( - self, self._pconfig) + self, ConfigWrapper(self, 'printer')) def set_fileoutput(self, debugoutput, dictionary): self.debugoutput = debugoutput self.dictionary = dictionary @@ -115,6 +107,8 @@ def main(): help="write output to file instead of to serial port") opts.add_option("-i", "--debuginput", dest="inputfile", help="read commands from file instead of from tty port") + opts.add_option("-I", "--input-tty", dest="inputtty", default='/tmp/printer', + help="input tty name (default is /tmp/printer)") opts.add_option("-l", "--logfile", dest="logfile", help="write log to file instead of stderr") opts.add_option("-v", action="store_true", dest="verbose", @@ -126,13 +120,16 @@ def main(): opts.error("Incorrect number of arguments") conffile = args[0] - debuginput = debugoutput = bglogger = None + input_fd = debuginput = debugoutput = bglogger = None debuglevel = logging.INFO if options.verbose: debuglevel = logging.DEBUG if options.inputfile: debuginput = open(options.inputfile, 'rb') + input_fd = debuginput.fileno() + else: + input_fd = util.create_pty(options.inputtty) if options.outputfile: debugoutput = open(options.outputfile, 'wb') if options.logfile: @@ -142,7 +139,7 @@ def main(): logging.info("Starting Klippy...") # Start firmware - printer = Printer(conffile, debuginput=debuginput) + printer = Printer(conffile, input_fd, is_fileinput=debuginput is not None) if debugoutput: proto_dict = read_dictionary(options.read_dictionary) printer.set_fileoutput(debugoutput, proto_dict)