klippy: Create the /tmp/printer pseudo tty before opening config file

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2016-11-20 20:40:31 -05:00
parent 4eb21a71ae
commit 7e7e607e3f
2 changed files with 18 additions and 21 deletions

View File

@ -9,10 +9,10 @@ import homing
# Parse out incoming GCode and find and translate head movements # Parse out incoming GCode and find and translate head movements
class GCodeParser: class GCodeParser:
RETRY_TIME = 0.100 RETRY_TIME = 0.100
def __init__(self, printer, fd, inputfile=False): def __init__(self, printer, fd, is_fileinput=False):
self.printer = printer self.printer = printer
self.fd = fd self.fd = fd
self.inputfile = inputfile self.is_fileinput = is_fileinput
# Input handling # Input handling
self.reactor = printer.reactor self.reactor = printer.reactor
self.fd_handle = None self.fd_handle = None
@ -123,11 +123,11 @@ class GCodeParser:
lines[0] = self.input_commands[0] + lines[0] lines[0] = self.input_commands[0] + lines[0]
self.input_commands = lines self.input_commands = lines
self.process_commands(eventtime) self.process_commands(eventtime)
if not data and self.inputfile: if not data and self.is_fileinput:
self.finish() self.finish()
# Response handling # Response handling
def ack(self, msg=None): def ack(self, msg=None):
if not self.need_ack or self.inputfile: if not self.need_ack or self.is_fileinput:
return return
if msg: if msg:
os.write(self.fd, "ok %s\n" % (msg,)) os.write(self.fd, "ok %s\n" % (msg,))
@ -136,7 +136,7 @@ class GCodeParser:
self.need_ack = False self.need_ack = False
def respond(self, msg): def respond(self, msg):
logging.debug(msg) logging.debug(msg)
if self.inputfile: if self.is_fileinput:
return return
os.write(self.fd, msg+"\n") os.write(self.fd, msg+"\n")
# Busy handling # Busy handling
@ -188,7 +188,7 @@ class GCodeParser:
self.gcode.respond(self.gcode.get_temp()) self.gcode.respond(self.gcode.get_temp())
self.last_temp_time = eventtime self.last_temp_time = eventtime
return self.cur_heater.check_busy(eventtime) return self.cur_heater.check_busy(eventtime)
if self.inputfile: if self.is_fileinput:
return return
self.set_busy(temp_busy_handler_wrapper()) self.set_busy(temp_busy_handler_wrapper())
def set_temp(self, heater, params, wait=False): def set_temp(self, heater, params, wait=False):
@ -249,7 +249,7 @@ class GCodeParser:
if not axes: if not axes:
axes = [0, 1, 2] axes = [0, 1, 2]
homing_state = homing.Homing(self.toolhead, axes) homing_state = homing.Homing(self.toolhead, axes)
if self.inputfile: if self.is_fileinput:
homing_state.set_no_verify_retract() homing_state.set_no_verify_retract()
self.toolhead.home(homing_state) self.toolhead.home(homing_state)
def axes_update(homing_state): def axes_update(homing_state):
@ -307,7 +307,7 @@ class GCodeParser:
kinpos[0], kinpos[1], kinpos[2])) kinpos[0], kinpos[1], kinpos[2]))
def cmd_M119(self, params): def cmd_M119(self, params):
# Get Endstop Status # Get Endstop Status
if self.inputfile: if self.is_fileinput:
return return
print_time = self.toolhead.get_last_move_time() print_time = self.toolhead.get_last_move_time()
query_state = homing.QueryEndstops(print_time, self.respond) query_state = homing.QueryEndstops(print_time, self.respond)

View File

@ -31,20 +31,12 @@ class ConfigWrapper:
return ConfigWrapper(self.printer, section) return ConfigWrapper(self.printer, section)
class Printer: class Printer:
def __init__(self, conffile, debuginput=None): def __init__(self, conffile, input_fd, is_fileinput=False):
self.fileconfig = ConfigParser.RawConfigParser() self.fileconfig = ConfigParser.RawConfigParser()
self.fileconfig.read(conffile) self.fileconfig.read(conffile)
self.reactor = reactor.Reactor() self.reactor = reactor.Reactor()
self._pconfig = ConfigWrapper(self, 'printer') self.gcode = gcode.GCodeParser(self, input_fd, is_fileinput)
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.mcu = mcu.MCU(self, ConfigWrapper(self, 'mcu')) self.mcu = mcu.MCU(self, ConfigWrapper(self, 'mcu'))
self.stats_timer = self.reactor.register_timer( self.stats_timer = self.reactor.register_timer(
self.stats, self.reactor.NOW) self.stats, self.reactor.NOW)
@ -63,7 +55,7 @@ class Printer:
self.objects['heater_bed'] = heater.PrinterHeater( self.objects['heater_bed'] = heater.PrinterHeater(
self, ConfigWrapper(self, 'heater_bed')) self, ConfigWrapper(self, 'heater_bed'))
self.objects['toolhead'] = toolhead.ToolHead( self.objects['toolhead'] = toolhead.ToolHead(
self, self._pconfig) self, ConfigWrapper(self, 'printer'))
def set_fileoutput(self, debugoutput, dictionary): def set_fileoutput(self, debugoutput, dictionary):
self.debugoutput = debugoutput self.debugoutput = debugoutput
self.dictionary = dictionary self.dictionary = dictionary
@ -115,6 +107,8 @@ def main():
help="write output to file instead of to serial port") help="write output to file instead of to serial port")
opts.add_option("-i", "--debuginput", dest="inputfile", opts.add_option("-i", "--debuginput", dest="inputfile",
help="read commands from file instead of from tty port") 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", opts.add_option("-l", "--logfile", dest="logfile",
help="write log to file instead of stderr") help="write log to file instead of stderr")
opts.add_option("-v", action="store_true", dest="verbose", opts.add_option("-v", action="store_true", dest="verbose",
@ -126,13 +120,16 @@ def main():
opts.error("Incorrect number of arguments") opts.error("Incorrect number of arguments")
conffile = args[0] conffile = args[0]
debuginput = debugoutput = bglogger = None input_fd = debuginput = debugoutput = bglogger = None
debuglevel = logging.INFO debuglevel = logging.INFO
if options.verbose: if options.verbose:
debuglevel = logging.DEBUG debuglevel = logging.DEBUG
if options.inputfile: if options.inputfile:
debuginput = open(options.inputfile, 'rb') debuginput = open(options.inputfile, 'rb')
input_fd = debuginput.fileno()
else:
input_fd = util.create_pty(options.inputtty)
if options.outputfile: if options.outputfile:
debugoutput = open(options.outputfile, 'wb') debugoutput = open(options.outputfile, 'wb')
if options.logfile: if options.logfile:
@ -142,7 +139,7 @@ def main():
logging.info("Starting Klippy...") logging.info("Starting Klippy...")
# Start firmware # Start firmware
printer = Printer(conffile, debuginput=debuginput) printer = Printer(conffile, input_fd, is_fileinput=debuginput is not None)
if debugoutput: if debugoutput:
proto_dict = read_dictionary(options.read_dictionary) proto_dict = read_dictionary(options.read_dictionary)
printer.set_fileoutput(debugoutput, proto_dict) printer.set_fileoutput(debugoutput, proto_dict)