gcode: Convert input handling to use a reactor mutex

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2019-06-09 12:01:58 -04:00
parent afc10400e3
commit 962f7b98bd
1 changed files with 8 additions and 28 deletions

View File

@ -31,6 +31,7 @@ class GCodeParser:
self.input_log = collections.deque([], 50)
# Command handling
self.is_printer_ready = False
self.mutex = self.reactor.mutex()
self.base_gcode_handlers = self.gcode_handlers = {}
self.ready_gcode_handlers = {}
self.mux_commands = {}
@ -267,34 +268,20 @@ class GCodeParser:
return
# Process commands
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:
self.pending_commands = []
self._process_commands(pending_commands)
with self.mutex:
self._process_commands(pending_commands)
pending_commands = self.pending_commands
self.is_processing_data = False
if self.fd_handle is None:
self.fd_handle = self.reactor.register_fd(self.fd,
self._process_data)
def process_batch(self, commands):
if self.is_processing_data:
if self.mutex.test():
return False
self.is_processing_data = True
try:
with self.mutex:
self._process_commands(commands, need_ack=False)
except self.error as e:
if self.pending_commands:
self._process_pending()
self.is_processing_data = False
raise
if self.pending_commands:
self._process_pending()
self.is_processing_data = False
return True
def run_script_from_command(self, script):
prev_need_ack = self.need_ack
@ -303,15 +290,8 @@ class GCodeParser:
finally:
self.need_ack = prev_need_ack
def run_script(self, script):
commands = script.split('\n')
curtime = None
while 1:
res = self.process_batch(commands)
if res:
break
if curtime is None:
curtime = self.reactor.monotonic()
curtime = self.reactor.pause(curtime + 0.100)
with self.mutex:
self._process_commands(script.split('\n'), need_ack=False)
# Response handling
def ack(self, msg=None):
if not self.need_ack or self.is_fileinput: