From: Joseph Coffland Date: Thu, 22 Feb 2018 08:47:01 +0000 (-0800) Subject: Simplified state updates X-Git-Url: https://git.buildbotics.com/?a=commitdiff_plain;h=28658c70443bec85728f91c68061a64fdc5e0685;p=bbctrl-firmware Simplified state updates --- diff --git a/src/py/bbctrl/Comm.py b/src/py/bbctrl/Comm.py index 4422c1b..3be60bd 100644 --- a/src/py/bbctrl/Comm.py +++ b/src/py/bbctrl/Comm.py @@ -42,12 +42,11 @@ class Comm(): def __init__(self, ctrl): self.ctrl = ctrl - self.listeners = [] self.queue = deque() self.in_buf = '' self.command = None - ctrl.state.add_listener(self._update_state) + ctrl.state.add_listener(self._update) try: self.sp = serial.Serial(ctrl.args.serial, ctrl.args.baud, @@ -65,10 +64,6 @@ class Comm(): self.i2c_addr = ctrl.args.avr_addr - def add_listener(self, listener): - self.listeners.append(listener) - - def start_sending_gcode(self, path): self.ctrl.planner.load(path) self.set_write(True) @@ -108,7 +103,7 @@ class Comm(): self.ctrl.ioloop.update_handler(self.sp, flags) - def _update_state(self, update): + def _update(self, update): if 'xx' in update and update['xx'] == 'ESTOPPED': self.stop_sending_gcode() @@ -181,8 +176,6 @@ class Comm(): except Exception as e: log.warning('%s: %s', e, data) - update = {} - # Parse incoming serial data into lines while True: i = self.in_buf.find('\n') @@ -202,20 +195,15 @@ class Comm(): if 'variables' in msg: self._update_vars(msg) - continue - if 'msg' in msg: + elif 'msg' in msg: self._log_msg(msg) - continue - if 'firmware' in msg: + elif 'firmware' in msg: log.warning('firmware rebooted') self.connect() - update.update(msg) - - if update: - for listener in self.listeners: listener(update) + else: self.ctrl.state.update(msg) def _serial_handler(self, fd, events): diff --git a/src/py/bbctrl/Mach.py b/src/py/bbctrl/Mach.py index 367d7cf..9670b76 100644 --- a/src/py/bbctrl/Mach.py +++ b/src/py/bbctrl/Mach.py @@ -58,11 +58,9 @@ class Mach(): def __init__(self, ctrl): self.ctrl = ctrl self.comm = bbctrl.Comm(ctrl) - self.stopping = False - self.comm.add_listener(self._comm_update) - self.comm.add_listener(self.ctrl.state.update) + ctrl.state.add_listener(self._update) self.comm.queue_command(Cmd.REBOOT) @@ -70,7 +68,7 @@ class Mach(): def _is_busy(self): return self.ctrl.planner.is_running() - def _comm_update(self, update): + def _update(self, update): if self.stopping and 'xx' in update and update['xx'] == 'HOLDING': self.comm.stop_sending_gcode() # Resume once current queue of GCode commands has flushed @@ -140,10 +138,7 @@ class Mach(): def estop(self): self.comm.i2c_command(Cmd.ESTOP) def clear(self): self.comm.i2c_command(Cmd.CLEAR) - - - def start(self, path): - if path: self.comm.start_sending_gcode(path) + def start(self, path): self.comm.start_sending_gcode(path) def step(self, path):