diff options
author | Kevin O'Connor <kevin@koconnor.net> | 2018-01-10 18:34:42 -0500 |
---|---|---|
committer | Kevin O'Connor <kevin@koconnor.net> | 2018-01-10 19:26:06 -0500 |
commit | 3833669c3a41a26e944ffe891cc259ceaf2b9de0 (patch) | |
tree | 5a692dad035fba835d582f2755b0cc8ad463cdb7 /klippy/pins.py | |
parent | df6528715e86305d77456efb8b76dad734ffda9f (diff) | |
download | kutter-3833669c3a41a26e944ffe891cc259ceaf2b9de0.tar.gz kutter-3833669c3a41a26e944ffe891cc259ceaf2b9de0.tar.xz kutter-3833669c3a41a26e944ffe891cc259ceaf2b9de0.zip |
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 <kevin@koconnor.net>
Diffstat (limited to 'klippy/pins.py')
-rw-r--r-- | klippy/pins.py | 39 |
1 files changed, 25 insertions, 14 deletions
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<prefix>[ _]pin=)(?P<name>[^ ]*)') -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) ###################################################################### |