board_pins: Add a new "extras" module to support per-board pin aliases

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2019-08-09 11:53:09 -04:00 committed by KevinOConnor
parent 395429be14
commit bfbcb90751
3 changed files with 51 additions and 2 deletions

View File

@ -1366,6 +1366,19 @@
# be provided. # 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=<GND>" would reserve "EXP1_9"). This parameter must be
# provided.
# Support for a display attached to the micro-controller. # Support for a display attached to the micro-controller.
#[display] #[display]
#lcd_type: #lcd_type:

View File

@ -0,0 +1,24 @@
# Support for custom board pin aliases
#
# Copyright (C) 2019 Kevin O'Connor <kevin@koconnor.net>
#
# 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])

View File

@ -161,13 +161,25 @@ class PinResolver:
raise error("Pin %s reserved for %s - can't reserve for %s" % ( raise error("Pin %s reserved for %s - can't reserve for %s" % (
pin, self.reserved[pin], reserve_name)) pin, self.reserved[pin], reserve_name))
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): def add_pin_mapping(self, mcu_type, mapping_name):
if mapping_name == 'arduino': if mapping_name == 'arduino':
self.aliases = get_aliases_arduino(mcu_type) pin_mapping = get_aliases_arduino(mcu_type)
elif mapping_name == 'beaglebone': elif mapping_name == 'beaglebone':
self.aliases = get_aliases_beaglebone(mcu_type) pin_mapping = get_aliases_beaglebone(mcu_type)
else: else:
raise error("Unknown pin alias mapping '%s'" % (mapping_name,)) 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 update_command(self, cmd):
def pin_fixup(m): def pin_fixup(m):
name = m.group('name') name = m.group('name')