klippy: Run the MCU connect code within the reactor

Setup the reactor and run the MCU connection code as a timer within
the reactor.  The connection code will make use of reactor greenlets
so that it can wait for events during the connection phase.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2016-11-28 13:14:56 -05:00
parent bafe796eeb
commit 5d805ba550
4 changed files with 28 additions and 24 deletions

View file

@ -342,12 +342,13 @@ class MCU:
self._steppersync = self.ffi_lib.steppersync_alloc(
self.serial.serialqueue, stepqueues, len(stepqueues), count)
def connect(self):
state_params = {}
def handle_serial_state(params):
if params['#state'] == 'connected':
self._printer.reactor.end()
state_params.update(params)
self.serial.register_callback(handle_serial_state, '#state')
self.serial.connect()
self._printer.reactor.run()
while state_params.get('#state') != 'connected':
self._printer.reactor.pause(time.time() + 0.05)
self.serial.unregister_callback('#state')
logging.info("serial connected")
self._mcu_freq = float(self.serial.msgparser.config['CLOCK_FREQ'])
@ -427,14 +428,15 @@ class MCU:
sent_config = False
def handle_get_config(params):
config_params.update(params)
done = not sent_config or params['is_config']
if done:
self._printer.reactor.end()
return done
return True
while 1:
self.serial.send_with_response(msg, handle_get_config, 'config')
self._printer.reactor.run()
if not config_params['is_config']:
while 1:
is_config = config_params.get('is_config')
if is_config is not None and (not sent_config or is_config):
break
self._printer.reactor.pause(time.time() + 0.05)
if not is_config:
# Send config commands
for c in self._config_cmds:
self.send(self.create_command(c))