From 3833669c3a41a26e944ffe891cc259ceaf2b9de0 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Wed, 10 Jan 2018 18:34:42 -0500 Subject: pins: Check if the same pin is referenced via different aliases Change the update_command() call to use a new PinResolver class. In that new class, verify that the same pin isn't referenced in two different parts of the config using different aliases for the pin. Signed-off-by: Kevin O'Connor --- klippy/pins.py | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) (limited to 'klippy/pins.py') diff --git a/klippy/pins.py b/klippy/pins.py index c612432c..c7983280 100644 --- a/klippy/pins.py +++ b/klippy/pins.py @@ -152,21 +152,32 @@ def update_map_beaglebone(pins, mcu): # Command translation ###################################################################### -# Obtains the pin mappings -def get_pin_map(mcu, mapping_name=None): - pins = dict(MCU_PINS.get(mcu, {})) - if mapping_name == 'arduino': - update_map_arduino(pins, mcu) - elif mapping_name == 'beaglebone': - update_map_beaglebone(pins, mcu) - return pins - -# Translate pin names in a firmware command re_pin = re.compile(r'(?P[ _]pin=)(?P[^ ]*)') -def update_command(cmd, pmap): - def pin_fixup(m): - return m.group('prefix') + str(pmap[m.group('name')]) - return re_pin.sub(pin_fixup, cmd) + +class PinResolver: + def __init__(self, mcu_type, validate_aliases=True): + self.mcu_type = mcu_type + self.validate_aliases = validate_aliases + self.pins = dict(MCU_PINS.get(mcu_type, {})) + self.active_pins = {} + def update_aliases(self, mapping_name): + self.pins = dict(MCU_PINS.get(self.mcu_type, {})) + if mapping_name == 'arduino': + update_map_arduino(self.pins, self.mcu_type) + elif mapping_name == 'beaglebone': + update_map_beaglebone(self.pins, self.mcu_type) + def update_command(self, cmd): + def pin_fixup(m): + name = m.group('name') + if name not in self.pins: + raise error("Unable to translate pin name: %s" % (cmd,)) + pin_id = self.pins[name] + if (name != self.active_pins.setdefault(pin_id, name) + and self.validate_aliases): + raise error("pin %s is an alias for %s" % ( + name, self.active_pins[pin_id])) + return m.group('prefix') + str(pin_id) + return re_pin.sub(pin_fixup, cmd) ###################################################################### -- cgit v1.2.3-70-g09d2