diff --git a/klippy/console.py b/klippy/console.py index 1dd99ca9..81dc87b2 100755 --- a/klippy/console.py +++ b/klippy/console.py @@ -24,6 +24,8 @@ class KeyboardReader: self.eval_globals = {} def connect(self, eventtime): self.ser.connect() + mcu = self.ser.msgparser.get_constant('MCU') + self.pins = pins.get_pin_map(mcu) self.reactor.unregister_timer(self.connect_timer) return self.reactor.NEVER def update_evals(self, eventtime): @@ -32,8 +34,8 @@ class KeyboardReader: self.eval_globals['freq'] = f self.eval_globals['clock'] = int(c) def set_pin_map(self, parts): - mcu = self.ser.msgparser.config['MCU'] - self.pins = pins.map_pins(parts[1], mcu) + mcu = self.ser.msgparser.get_constant('MCU') + self.pins = pins.get_pin_map(mcu, parts[1]) def set_var(self, parts): val = parts[2] try: @@ -44,10 +46,6 @@ class KeyboardReader: except ValueError: pass self.eval_globals[parts[1]] = val - def lookup_pin(self, value): - if self.pins is None: - self.pins = pins.mcu_to_pins(self.ser.msgparser.config['MCU']) - return self.pins[value] def translate(self, line, eventtime): evalparts = re_eval.split(line) if len(evalparts) > 1: @@ -60,8 +58,6 @@ class KeyboardReader: return None line = ''.join(evalparts) print "Eval:", line - if self.pins is None and self.ser.msgparser.config: - self.pins = pins.mcu_to_pins(self.ser.msgparser.config['MCU']) if self.pins is not None: try: line = pins.update_command(line, self.pins).strip() diff --git a/klippy/mcu.py b/klippy/mcu.py index c7fde801..00eef1df 100644 --- a/klippy/mcu.py +++ b/klippy/mcu.py @@ -357,6 +357,7 @@ class MCU: self._config_cmds = [] self._config_crc = None self._init_callbacks = [] + self._pin_map = config.get('pin_map', None) # Move command queuing ffi_main, self.ffi_lib = chelper.get_ffi() self._steppers = [] @@ -456,11 +457,7 @@ class MCU: # Resolve pin names mcu = self.serial.msgparser.get_constant('MCU') - pin_map = self._config.get('pin_map', None) - if pin_map is None: - pnames = pins.mcu_to_pins(mcu) - else: - pnames = pins.map_pins(pin_map, mcu) + pnames = pins.get_pin_map(mcu, self._pin_map) updated_cmds = [] for cmd in self._config_cmds: try: diff --git a/klippy/pins.py b/klippy/pins.py index bffe580c..597e59b3 100644 --- a/klippy/pins.py +++ b/klippy/pins.py @@ -16,25 +16,13 @@ def port_pins(port_count, bit_count=8): pins['P%c%d' % (portchr, portbit)] = port * bit_count + portbit return pins -PINS_atmega164 = port_pins(4) -PINS_atmega1280 = port_pins(12) - MCU_PINS = { - "atmega168": PINS_atmega164, "atmega644p": PINS_atmega164, + "atmega168": port_pins(4), "atmega644p": port_pins(4), "at90usb1286": port_pins(5), - "atmega1280": PINS_atmega1280, "atmega2560": PINS_atmega1280, + "atmega1280": port_pins(12), "atmega2560": port_pins(12), "sam3x8e": port_pins(4, 32) } -def mcu_to_pins(mcu): - return MCU_PINS.get(mcu, {}) - -re_pin = re.compile(r'(?P[ _]pin=)(?P[^ ]*)') -def update_command(cmd, pmap): - def fixup(m): - return m.group('prefix') + str(pmap[m.group('name')]) - return re_pin.sub(fixup, cmd) - ###################################################################### # Arduino mappings @@ -96,12 +84,25 @@ Arduino_from_mcu = { "sam3x8e": (Arduino_Due, Arduino_Due_analog), } -def map_pins(name, mcu): + +###################################################################### +# External commands +###################################################################### + +# Obtains the pin mappings +def get_pin_map(mcu, mapping_name=None): pins = MCU_PINS.get(mcu, {}) - if name == 'arduino': + if mapping_name == 'arduino': dpins, apins = Arduino_from_mcu.get(mcu, []) for i in range(len(dpins)): pins['ar' + str(i)] = pins[dpins[i]] for i in range(len(apins)): pins['analog%d' % (i,)] = pins[apins[i]] return pins + +# Translate pin names in a firmware command +re_pin = re.compile(r'(?P[ _]pin=)(?P[^ ]*)') +def update_command(cmd, pmap): + def fixup(m): + return m.group('prefix') + str(pmap[m.group('name')]) + return re_pin.sub(fixup, cmd)