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 <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2016-11-22 12:37:37 -05:00
parent 9755182adf
commit d161d66741
3 changed files with 13 additions and 11 deletions

View File

@ -58,10 +58,6 @@ class GCodeParser:
self.gcode_handlers = dict((h, getattr(self, 'cmd_'+h)) self.gcode_handlers = dict((h, getattr(self, 'cmd_'+h))
for h in handlers) for h in handlers)
def run(self): 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.fd_handle = self.reactor.register_fd(self.fd, self.process_data)
self.reactor.run() self.reactor.run()
def finish(self): def finish(self):

View File

@ -47,14 +47,12 @@ class PrinterHeater:
max_adc = self.calc_adc(self.config.getfloat('min_temp')) max_adc = self.calc_adc(self.config.getfloat('min_temp'))
self.mcu_adc.set_minmax( self.mcu_adc.set_minmax(
SAMPLE_TIME, SAMPLE_COUNT, minval=min_adc, maxval=max_adc) 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') control_algo = self.config.get('control', 'watermark')
algos = {'watermark': ControlBangBang, 'pid': ControlPID} algos = {'watermark': ControlBangBang, 'pid': ControlPID}
self.control = algos[control_algo](self, self.config) self.control = algos[control_algo](self, self.config)
if self.printer.mcu.output_file_mode: if self.printer.mcu.output_file_mode:
self.can_extrude = True self.can_extrude = True
def run(self):
self.mcu_adc.query_analog_in(REPORT_TIME)
def set_pwm(self, read_time, value): def set_pwm(self, read_time, value):
if value: if value:
if self.target_temp <= 0.: if self.target_temp <= 0.:

View File

@ -262,6 +262,7 @@ class MCU_adc:
self._mcu_freq = mcu.get_mcu_freq() self._mcu_freq = mcu.get_mcu_freq()
self._cmd_queue = mcu.alloc_command_queue() self._cmd_queue = mcu.alloc_command_queue()
mcu.add_config_cmd("config_analog_in oid=%d pin=%s" % (self._oid, pin)) 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" mcu.register_msg(self._handle_analog_in_state, "analog_in_state"
, self._oid) , self._oid)
self._query_cmd = mcu.lookup_command( self._query_cmd = mcu.lookup_command(
@ -278,8 +279,7 @@ class MCU_adc:
self._min_sample = int(minval * max_adc) self._min_sample = int(minval * max_adc)
self._max_sample = min(0xffff, int(math.ceil(maxval * max_adc))) self._max_sample = min(0xffff, int(math.ceil(maxval * max_adc)))
self._inv_max_adc = 1.0 / max_adc self._inv_max_adc = 1.0 / max_adc
def query_analog_in(self, report_time): def _init_callback(self):
self._report_clock = int(report_time * self._mcu_freq)
cur_clock = self._mcu.get_last_clock() cur_clock = self._mcu.get_last_clock()
clock = cur_clock + int(self._mcu_freq * (1.0 + self._oid * 0.01)) # XXX clock = cur_clock + int(self._mcu_freq * (1.0 + self._oid * 0.01)) # XXX
msg = self._query_cmd.encode( msg = self._query_cmd.encode(
@ -292,8 +292,9 @@ class MCU_adc:
last_read_time = (next_clock - self._report_clock) / self._mcu_freq last_read_time = (next_clock - self._report_clock) / self._mcu_freq
if self._callback is not None: if self._callback is not None:
self._callback(last_read_time, last_value) self._callback(last_read_time, last_value)
def set_adc_callback(self, cb): def set_adc_callback(self, report_time, callback):
self._callback = cb self._report_clock = int(report_time * self._mcu_freq)
self._callback = callback
class MCU: class MCU:
def __init__(self, printer, config): def __init__(self, printer, config):
@ -310,6 +311,7 @@ class MCU:
self._num_oids = 0 self._num_oids = 0
self._config_cmds = [] self._config_cmds = []
self._config_crc = None self._config_crc = None
self._init_callbacks = []
# Move command queuing # Move command queuing
ffi_main, self.ffi_lib = chelper.get_ffi() ffi_main, self.ffi_lib = chelper.get_ffi()
self._steppers = [] self._steppers = []
@ -363,6 +365,8 @@ class MCU:
for c in self._config_cmds: for c in self._config_cmds:
self.send(self.create_command(c)) self.send(self.create_command(c))
self._init_steppersync(500) self._init_steppersync(500)
for cb in self._init_callbacks:
cb()
self._send_config = dummy_send_config self._send_config = dummy_send_config
if not pace: if not pace:
def dummy_set_print_start_time(eventtime): def dummy_set_print_start_time(eventtime):
@ -444,6 +448,8 @@ class MCU:
break break
logging.info("Configured") logging.info("Configured")
self._init_steppersync(config_params['move_count']) self._init_steppersync(config_params['move_count'])
for cb in self._init_callbacks:
cb()
# Config creation helpers # Config creation helpers
def create_oid(self): def create_oid(self):
oid = self._num_oids oid = self._num_oids
@ -451,6 +457,8 @@ class MCU:
return oid return oid
def add_config_cmd(self, cmd): def add_config_cmd(self, cmd):
self._config_cmds.append(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): def register_msg(self, cb, msg, oid=None):
self.serial.register_callback(cb, msg, oid) self.serial.register_callback(cb, msg, oid)
def register_stepper(self, stepper): def register_stepper(self, stepper):