gcode: Convert busy handler from timer to greenlet

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2016-11-16 10:56:10 -05:00
parent 35428f2e04
commit e169f70bfb
1 changed files with 22 additions and 25 deletions

View File

@ -15,14 +15,13 @@ class GCodeParser:
self.is_fileinput = is_fileinput self.is_fileinput = is_fileinput
# Input handling # Input handling
self.reactor = printer.reactor self.reactor = printer.reactor
self.is_processing_data = False
self.fd_handle = None self.fd_handle = None
if not is_fileinput: if not is_fileinput:
self.fd_handle = self.reactor.register_fd(self.fd, self.process_data) self.fd_handle = self.reactor.register_fd(self.fd, self.process_data)
self.input_commands = [""] self.input_commands = [""]
self.bytes_read = 0 self.bytes_read = 0
self.input_log = collections.deque([], 50) self.input_log = collections.deque([], 50)
# Busy handling
self.busy_timer = self.reactor.register_timer(self.busy_handler)
self.busy_state = None self.busy_state = None
# Command handling # Command handling
self.gcode_handlers = {} self.gcode_handlers = {}
@ -117,14 +116,14 @@ class GCodeParser:
self.respond_error('Internal error on command:"%s"' % (cmd,)) self.respond_error('Internal error on command:"%s"' % (cmd,))
# Check if machine can process next command or must stall input # Check if machine can process next command or must stall input
if self.busy_state is not None: if self.busy_state is not None:
break self.busy_handler(eventtime)
if self.is_printer_ready and self.toolhead.check_busy(eventtime): if self.is_printer_ready and self.toolhead.check_busy(eventtime):
self.set_busy(self.toolhead) self.set_busy(self.toolhead)
break self.busy_handler(eventtime)
self.ack() self.ack()
del self.input_commands[:i+1] del self.input_commands[:i+1]
def process_data(self, eventtime): def process_data(self, eventtime):
if self.busy_state is not None: if self.is_processing_data:
self.reactor.unregister_fd(self.fd_handle) self.reactor.unregister_fd(self.fd_handle)
self.fd_handle = None self.fd_handle = None
return return
@ -134,7 +133,11 @@ class GCodeParser:
lines = data.split('\n') lines = data.split('\n')
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.is_processing_data = True
self.process_commands(eventtime) self.process_commands(eventtime)
self.is_processing_data = False
if self.fd_handle is None:
self.fd_handle = self.reactor.register_fd(self.fd, self.process_data)
if not data and self.is_fileinput: if not data and self.is_fileinput:
self.motor_heater_off() self.motor_heater_off()
self.printer.request_exit_eof() self.printer.request_exit_eof()
@ -160,29 +163,23 @@ class GCodeParser:
# Busy handling # Busy handling
def set_busy(self, busy_handler): def set_busy(self, busy_handler):
self.busy_state = busy_handler self.busy_state = busy_handler
self.reactor.update_timer(self.busy_timer, self.reactor.NOW)
def busy_handler(self, eventtime): def busy_handler(self, eventtime):
try: while 1:
busy = self.busy_state.check_busy(eventtime) try:
except homing.EndstopError, e: busy = self.busy_state.check_busy(eventtime)
self.respond_error(str(e)) except homing.EndstopError, e:
busy = False self.respond_error(str(e))
except: busy = False
logging.exception("Exception in busy handler") except:
self.toolhead.force_shutdown() logging.exception("Exception in busy handler")
self.respond_error('Internal error in busy handler') self.toolhead.force_shutdown()
busy = False self.respond_error('Internal error in busy handler')
if busy: busy = False
if not busy:
break
self.toolhead.reset_motor_off_time(eventtime) self.toolhead.reset_motor_off_time(eventtime)
return eventtime + self.RETRY_TIME eventtime = self.reactor.pause(eventtime + self.RETRY_TIME)
self.busy_state = None self.busy_state = None
self.ack()
self.process_commands(eventtime)
if self.busy_state is not None:
return self.reactor.NOW
if self.fd_handle is None:
self.fd_handle = self.reactor.register_fd(self.fd, self.process_data)
return self.reactor.NEVER
# Temperature wrappers # Temperature wrappers
def get_temp(self): def get_temp(self):
if not self.is_printer_ready: if not self.is_printer_ready: