gcode: Convert input handling to use a reactor mutex
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
parent
afc10400e3
commit
962f7b98bd
|
@ -31,6 +31,7 @@ class GCodeParser:
|
||||||
self.input_log = collections.deque([], 50)
|
self.input_log = collections.deque([], 50)
|
||||||
# Command handling
|
# Command handling
|
||||||
self.is_printer_ready = False
|
self.is_printer_ready = False
|
||||||
|
self.mutex = self.reactor.mutex()
|
||||||
self.base_gcode_handlers = self.gcode_handlers = {}
|
self.base_gcode_handlers = self.gcode_handlers = {}
|
||||||
self.ready_gcode_handlers = {}
|
self.ready_gcode_handlers = {}
|
||||||
self.mux_commands = {}
|
self.mux_commands = {}
|
||||||
|
@ -267,34 +268,20 @@ class GCodeParser:
|
||||||
return
|
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 = []
|
||||||
|
with self.mutex:
|
||||||
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
|
||||||
if self.fd_handle is None:
|
if self.fd_handle is None:
|
||||||
self.fd_handle = self.reactor.register_fd(self.fd,
|
self.fd_handle = self.reactor.register_fd(self.fd,
|
||||||
self._process_data)
|
self._process_data)
|
||||||
def process_batch(self, commands):
|
def process_batch(self, commands):
|
||||||
if self.is_processing_data:
|
if self.mutex.test():
|
||||||
return False
|
return False
|
||||||
self.is_processing_data = True
|
with self.mutex:
|
||||||
try:
|
|
||||||
self._process_commands(commands, need_ack=False)
|
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
|
return True
|
||||||
def run_script_from_command(self, script):
|
def run_script_from_command(self, script):
|
||||||
prev_need_ack = self.need_ack
|
prev_need_ack = self.need_ack
|
||||||
|
@ -303,15 +290,8 @@ class GCodeParser:
|
||||||
finally:
|
finally:
|
||||||
self.need_ack = prev_need_ack
|
self.need_ack = prev_need_ack
|
||||||
def run_script(self, script):
|
def run_script(self, script):
|
||||||
commands = script.split('\n')
|
with self.mutex:
|
||||||
curtime = None
|
self._process_commands(script.split('\n'), need_ack=False)
|
||||||
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)
|
|
||||||
# Response handling
|
# Response handling
|
||||||
def ack(self, msg=None):
|
def ack(self, msg=None):
|
||||||
if not self.need_ack or self.is_fileinput:
|
if not self.need_ack or self.is_fileinput:
|
||||||
|
|
Loading…
Reference in New Issue