gcode: Extract special cases from process_data()

Handle the unlikely case of pending commands in a new method
process_pending().

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2018-02-11 12:49:33 -05:00
parent 5abea041b4
commit 11c7c110a1
1 changed files with 28 additions and 20 deletions

View File

@ -182,34 +182,42 @@ class GCodeParser:
self.partial_input = lines.pop() self.partial_input = lines.pop()
pending_commands = self.pending_commands pending_commands = self.pending_commands
pending_commands.extend(lines) pending_commands.extend(lines)
# Check for M112 out-of-order # Special handling for debug file input EOF
if ((len(pending_commands) > 1 or self.is_processing_data) if not data and self.is_fileinput:
and len(pending_commands) < 20): if not self.is_processing_data:
for line in lines: self.motor_heater_off()
if self.m112_r.match(line) is not None: if self.toolhead is not None:
self.cmd_M112({}) self.toolhead.wait_moves()
# Check if already processing data self.printer.request_exit()
if self.is_processing_data: pending_commands.append("")
if len(pending_commands) >= 20 or not data: # Handle case where multiple commands pending
# Stop reading input if self.is_processing_data or len(pending_commands) > 1:
self.reactor.unregister_fd(self.fd_handle) if len(pending_commands) < 20:
self.fd_handle = None # Check for M112 out-of-order
return for line in lines:
if self.m112_r.match(line) is not None:
self.cmd_M112({})
if self.is_processing_data:
if len(pending_commands) >= 20:
# Stop reading input
self.reactor.unregister_fd(self.fd_handle)
self.fd_handle = None
return
# Process commands # Process commands
self.is_processing_data = True self.is_processing_data = True
self.pending_commands = []
self.process_commands(pending_commands)
if self.pending_commands:
self.process_pending()
self.is_processing_data = False
def process_pending(self):
pending_commands = self.pending_commands
while pending_commands: while pending_commands:
self.pending_commands = [] self.pending_commands = []
self.process_commands(pending_commands) self.process_commands(pending_commands)
pending_commands = self.pending_commands pending_commands = self.pending_commands
self.is_processing_data = False
# Reenable input reading if it was stopped
if self.fd_handle is None: if self.fd_handle is None:
self.fd_handle = self.reactor.register_fd(self.fd, self.process_data) self.fd_handle = self.reactor.register_fd(self.fd, self.process_data)
if not data and self.is_fileinput:
self.motor_heater_off()
if self.toolhead is not None:
self.toolhead.wait_moves()
self.printer.request_exit()
def run_script(self, script): def run_script(self, script):
prev_need_ack = self.need_ack prev_need_ack = self.need_ack
try: try: