From bfbcb90751aa263f1ac87739dec05f7746c697ef Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Fri, 9 Aug 2019 11:53:09 -0400 Subject: [PATCH] board_pins: Add a new "extras" module to support per-board pin aliases Signed-off-by: Kevin O'Connor --- config/example-extras.cfg | 13 +++++++++++++ klippy/extras/board_pins.py | 24 ++++++++++++++++++++++++ klippy/pins.py | 16 ++++++++++++++-- 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 klippy/extras/board_pins.py diff --git a/config/example-extras.cfg b/config/example-extras.cfg index 184f690a..4f220502 100644 --- a/config/example-extras.cfg +++ b/config/example-extras.cfg @@ -1366,6 +1366,19 @@ # be provided. +# Board pin aliases. One may define aliases for the pins on a +# micro-controller. (If a micro-controller name is omitted in the +# board_pins config section name then it defaults to "mcu".) +#[board_pins mcu] +#aliases: +# A comma separated list of "name=value" aliases to create for the +# given micro-controller. For example, "EXP1_1=PE6" would create an +# "EXP1_1" alias for the "PE6" pin. However, if "value" is enclosed +# in "<>" then "name" is created as a reserved pin (for example, +# "EXP1_9=" would reserve "EXP1_9"). This parameter must be +# provided. + + # Support for a display attached to the micro-controller. #[display] #lcd_type: diff --git a/klippy/extras/board_pins.py b/klippy/extras/board_pins.py new file mode 100644 index 00000000..d8084b02 --- /dev/null +++ b/klippy/extras/board_pins.py @@ -0,0 +1,24 @@ +# Support for custom board pin aliases +# +# Copyright (C) 2019 Kevin O'Connor +# +# This file may be distributed under the terms of the GNU GPLv3 license. + +class PrinterBoardAliases: + def __init__(self, config, chip_name): + ppins = config.get_printer().lookup_object('pins') + pin_resolver = ppins.get_pin_resolver(chip_name) + aliases = config.get("aliases") + parts = [a.split('=', 1) for a in aliases.split(',')] + for name, value in parts: + name, value = name.strip(), value.strip() + if value.startswith('<') and value.endswith('>'): + pin_resolver.reserve_pin(name, value) + else: + pin_resolver.alias_pin(name, value) + +def load_config(config): + return PrinterBoardAliases(config, "mcu") + +def load_config_prefix(config): + return PrinterBoardAliases(config, config.get_name().split()[1]) diff --git a/klippy/pins.py b/klippy/pins.py index c1b4588f..a4b93762 100644 --- a/klippy/pins.py +++ b/klippy/pins.py @@ -161,13 +161,25 @@ class PinResolver: raise error("Pin %s reserved for %s - can't reserve for %s" % ( pin, self.reserved[pin], reserve_name)) self.reserved[pin] = reserve_name + def alias_pin(self, alias, pin): + if alias in self.aliases and self.aliases[alias] != pin: + raise error("Alias %s mapped to %s - can't alias to %s" % ( + alias, self.aliases[alias], pin)) + if pin in self.aliases: + pin = self.aliases[pin] + self.aliases[alias] = pin + for existing_alias, existing_pin in self.aliases.items(): + if existing_pin == alias: + self.aliases[existing_alias] = pin def add_pin_mapping(self, mcu_type, mapping_name): if mapping_name == 'arduino': - self.aliases = get_aliases_arduino(mcu_type) + pin_mapping = get_aliases_arduino(mcu_type) elif mapping_name == 'beaglebone': - self.aliases = get_aliases_beaglebone(mcu_type) + pin_mapping = get_aliases_beaglebone(mcu_type) else: raise error("Unknown pin alias mapping '%s'" % (mapping_name,)) + for alias, pin in pin_mapping.items(): + self.alias_pin(alias, pin) def update_command(self, cmd): def pin_fixup(m): name = m.group('name')