From d161d667412c9a096a52b1e0b156c49158d857d3 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Tue, 22 Nov 2016 12:37:37 -0500 Subject: [PATCH] mcu: Handle adc initialization entirely in mcu code Don't expose the ADC initialization to the gcode and heater code - instead, register a callback within the MCU_adc class and call it directly from the MCU class after configuration completes. Signed-off-by: Kevin O'Connor --- klippy/gcode.py | 4 ---- klippy/heater.py | 4 +--- klippy/mcu.py | 16 ++++++++++++---- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/klippy/gcode.py b/klippy/gcode.py index 056f7c50..2c57ed49 100644 --- a/klippy/gcode.py +++ b/klippy/gcode.py @@ -58,10 +58,6 @@ class GCodeParser: self.gcode_handlers = dict((h, getattr(self, 'cmd_'+h)) for h in handlers) def run(self): - if self.heater_nozzle is not None: - self.heater_nozzle.run() - if self.heater_bed is not None: - self.heater_bed.run() self.fd_handle = self.reactor.register_fd(self.fd, self.process_data) self.reactor.run() def finish(self): diff --git a/klippy/heater.py b/klippy/heater.py index 06bc0cd1..b7987936 100644 --- a/klippy/heater.py +++ b/klippy/heater.py @@ -47,14 +47,12 @@ class PrinterHeater: max_adc = self.calc_adc(self.config.getfloat('min_temp')) self.mcu_adc.set_minmax( SAMPLE_TIME, SAMPLE_COUNT, minval=min_adc, maxval=max_adc) - self.mcu_adc.set_adc_callback(self.adc_callback) + self.mcu_adc.set_adc_callback(REPORT_TIME, self.adc_callback) control_algo = self.config.get('control', 'watermark') algos = {'watermark': ControlBangBang, 'pid': ControlPID} self.control = algos[control_algo](self, self.config) if self.printer.mcu.output_file_mode: self.can_extrude = True - def run(self): - self.mcu_adc.query_analog_in(REPORT_TIME) def set_pwm(self, read_time, value): if value: if self.target_temp <= 0.: diff --git a/klippy/mcu.py b/klippy/mcu.py index 0066d24b..200c6256 100644 --- a/klippy/mcu.py +++ b/klippy/mcu.py @@ -262,6 +262,7 @@ class MCU_adc: self._mcu_freq = mcu.get_mcu_freq() self._cmd_queue = mcu.alloc_command_queue() mcu.add_config_cmd("config_analog_in oid=%d pin=%s" % (self._oid, pin)) + mcu.add_init_callback(self._init_callback) mcu.register_msg(self._handle_analog_in_state, "analog_in_state" , self._oid) self._query_cmd = mcu.lookup_command( @@ -278,8 +279,7 @@ class MCU_adc: self._min_sample = int(minval * max_adc) self._max_sample = min(0xffff, int(math.ceil(maxval * max_adc))) self._inv_max_adc = 1.0 / max_adc - def query_analog_in(self, report_time): - self._report_clock = int(report_time * self._mcu_freq) + def _init_callback(self): cur_clock = self._mcu.get_last_clock() clock = cur_clock + int(self._mcu_freq * (1.0 + self._oid * 0.01)) # XXX msg = self._query_cmd.encode( @@ -292,8 +292,9 @@ class MCU_adc: last_read_time = (next_clock - self._report_clock) / self._mcu_freq if self._callback is not None: self._callback(last_read_time, last_value) - def set_adc_callback(self, cb): - self._callback = cb + def set_adc_callback(self, report_time, callback): + self._report_clock = int(report_time * self._mcu_freq) + self._callback = callback class MCU: def __init__(self, printer, config): @@ -310,6 +311,7 @@ class MCU: self._num_oids = 0 self._config_cmds = [] self._config_crc = None + self._init_callbacks = [] # Move command queuing ffi_main, self.ffi_lib = chelper.get_ffi() self._steppers = [] @@ -363,6 +365,8 @@ class MCU: for c in self._config_cmds: self.send(self.create_command(c)) self._init_steppersync(500) + for cb in self._init_callbacks: + cb() self._send_config = dummy_send_config if not pace: def dummy_set_print_start_time(eventtime): @@ -444,6 +448,8 @@ class MCU: break logging.info("Configured") self._init_steppersync(config_params['move_count']) + for cb in self._init_callbacks: + cb() # Config creation helpers def create_oid(self): oid = self._num_oids @@ -451,6 +457,8 @@ class MCU: return oid def add_config_cmd(self, cmd): self._config_cmds.append(cmd) + def add_init_callback(self, callback): + self._init_callbacks.append(callback) def register_msg(self, cb, msg, oid=None): self.serial.register_callback(cb, msg, oid) def register_stepper(self, stepper):