pins: Move reserved pin handling into PinResolver class
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
parent
a482ddf582
commit
395429be14
|
@ -38,7 +38,7 @@ class KeyboardReader:
|
||||||
self.fd = sys.stdin.fileno()
|
self.fd = sys.stdin.fileno()
|
||||||
util.set_nonblock(self.fd)
|
util.set_nonblock(self.fd)
|
||||||
self.mcu_freq = 0
|
self.mcu_freq = 0
|
||||||
self.pins = None
|
self.pins = pins.PinResolver({}, validate_aliases=False)
|
||||||
self.data = ""
|
self.data = ""
|
||||||
reactor.register_fd(self.fd, self.process_kbd)
|
reactor.register_fd(self.fd, self.process_kbd)
|
||||||
reactor.register_callback(self.connect)
|
reactor.register_callback(self.connect)
|
||||||
|
@ -63,8 +63,6 @@ class KeyboardReader:
|
||||||
self.ser.handle_default = self.handle_default
|
self.ser.handle_default = self.handle_default
|
||||||
self.ser.register_response(self.handle_output, '#output')
|
self.ser.register_response(self.handle_output, '#output')
|
||||||
self.mcu_freq = msgparser.get_constant_float('CLOCK_FREQ')
|
self.mcu_freq = msgparser.get_constant_float('CLOCK_FREQ')
|
||||||
mcu_type = msgparser.get_constant('MCU')
|
|
||||||
self.pins = pins.PinResolver(mcu_type, {}, validate_aliases=False)
|
|
||||||
self.output("="*20 + " connected " + "="*20)
|
self.output("="*20 + " connected " + "="*20)
|
||||||
return self.reactor.NEVER
|
return self.reactor.NEVER
|
||||||
def output(self, msg):
|
def output(self, msg):
|
||||||
|
@ -83,7 +81,8 @@ class KeyboardReader:
|
||||||
self.eval_globals['freq'] = self.mcu_freq
|
self.eval_globals['freq'] = self.mcu_freq
|
||||||
self.eval_globals['clock'] = self.clocksync.get_clock(eventtime)
|
self.eval_globals['clock'] = self.clocksync.get_clock(eventtime)
|
||||||
def command_PINS(self, parts):
|
def command_PINS(self, parts):
|
||||||
self.pins.update_aliases(parts[1])
|
mcu_type = self.ser.get_msgparser().get_constant('MCU')
|
||||||
|
self.pins.add_pin_mapping(mcu_type, parts[1])
|
||||||
def command_SET(self, parts):
|
def command_SET(self, parts):
|
||||||
val = parts[2]
|
val = parts[2]
|
||||||
try:
|
try:
|
||||||
|
@ -164,12 +163,11 @@ class KeyboardReader:
|
||||||
return None
|
return None
|
||||||
line = ''.join(evalparts)
|
line = ''.join(evalparts)
|
||||||
self.output("Eval: %s" % (line,))
|
self.output("Eval: %s" % (line,))
|
||||||
if self.pins is not None:
|
try:
|
||||||
try:
|
line = self.pins.update_command(line).strip()
|
||||||
line = self.pins.update_command(line).strip()
|
except:
|
||||||
except:
|
self.output("Unable to map pin: %s" % (line,))
|
||||||
self.output("Unable to map pin: %s" % (line,))
|
return None
|
||||||
return None
|
|
||||||
if line:
|
if line:
|
||||||
parts = line.split()
|
parts = line.split()
|
||||||
if parts[0] in self.local_commands:
|
if parts[0] in self.local_commands:
|
||||||
|
|
|
@ -26,9 +26,10 @@ def resolve_bus_name(mcu, param, bus):
|
||||||
# Check for reserved bus pins
|
# Check for reserved bus pins
|
||||||
constants = mcu.get_constants()
|
constants = mcu.get_constants()
|
||||||
reserve_pins = constants.get('BUS_PINS_%s' % (bus,), None)
|
reserve_pins = constants.get('BUS_PINS_%s' % (bus,), None)
|
||||||
|
pin_resolver = ppins.get_pin_resolver(mcu_name)
|
||||||
if reserve_pins is not None:
|
if reserve_pins is not None:
|
||||||
for pin in reserve_pins.split(','):
|
for pin in reserve_pins.split(','):
|
||||||
ppins.reserve_pin(mcu_name, pin, bus)
|
pin_resolver.reserve_pin(pin, bus)
|
||||||
return bus
|
return bus
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -128,8 +128,9 @@ class servo_pwm:
|
||||||
pru_mcu = replicape.mcu_pwm_enable.get_mcu()
|
pru_mcu = replicape.mcu_pwm_enable.get_mcu()
|
||||||
printer = pru_mcu.get_printer()
|
printer = pru_mcu.get_printer()
|
||||||
ppins = printer.lookup_object('pins')
|
ppins = printer.lookup_object('pins')
|
||||||
ppins.reserve_pin(pru_mcu.get_name(), resv1, config_name)
|
pin_resolver = ppins.get_pin_resolver(pru_mcu.get_name())
|
||||||
ppins.reserve_pin(pru_mcu.get_name(), resv2, config_name)
|
pin_resolver.reserve_pin(resv1, config_name)
|
||||||
|
pin_resolver.reserve_pin(resv2, config_name)
|
||||||
def setup_cycle_time(self, cycle_time, hardware_pwm=False):
|
def setup_cycle_time(self, cycle_time, hardware_pwm=False):
|
||||||
self.mcu_pwm.setup_cycle_time(cycle_time, True);
|
self.mcu_pwm.setup_cycle_time(cycle_time, True);
|
||||||
|
|
||||||
|
|
|
@ -568,10 +568,9 @@ class MCU:
|
||||||
# Resolve pin names
|
# Resolve pin names
|
||||||
mcu_type = self._serial.get_msgparser().get_constant('MCU')
|
mcu_type = self._serial.get_msgparser().get_constant('MCU')
|
||||||
ppins = self._printer.lookup_object('pins')
|
ppins = self._printer.lookup_object('pins')
|
||||||
reserved_pins = ppins.get_reserved_pins(self._name)
|
pin_resolver = ppins.get_pin_resolver(self._name)
|
||||||
pin_resolver = pins.PinResolver(mcu_type, reserved_pins)
|
|
||||||
if self._pin_map is not None:
|
if self._pin_map is not None:
|
||||||
pin_resolver.update_aliases(self._pin_map)
|
pin_resolver.add_pin_mapping(mcu_type, self._pin_map)
|
||||||
for i, cmd in enumerate(self._config_cmds):
|
for i, cmd in enumerate(self._config_cmds):
|
||||||
self._config_cmds[i] = pin_resolver.update_command(cmd)
|
self._config_cmds[i] = pin_resolver.update_command(cmd)
|
||||||
for i, cmd in enumerate(self._init_cmds):
|
for i, cmd in enumerate(self._init_cmds):
|
||||||
|
@ -651,10 +650,11 @@ class MCU:
|
||||||
["%s=%s" % (k, v) for k, v in self.get_constants().items()]))]
|
["%s=%s" % (k, v) for k, v in self.get_constants().items()]))]
|
||||||
logging.info("\n".join(log_info))
|
logging.info("\n".join(log_info))
|
||||||
ppins = self._printer.lookup_object('pins')
|
ppins = self._printer.lookup_object('pins')
|
||||||
|
pin_resolver = ppins.get_pin_resolver(name)
|
||||||
for cname, value in self.get_constants().items():
|
for cname, value in self.get_constants().items():
|
||||||
if cname.startswith("RESERVE_PINS_"):
|
if cname.startswith("RESERVE_PINS_"):
|
||||||
for pin in value.split(','):
|
for pin in value.split(','):
|
||||||
ppins.reserve_pin(name, pin, cname[13:])
|
pin_resolver.reserve_pin(pin, cname[13:])
|
||||||
self._mcu_freq = self.get_constant_float('CLOCK_FREQ')
|
self._mcu_freq = self.get_constant_float('CLOCK_FREQ')
|
||||||
self._stats_sumsq_base = self.get_constant_float('STATS_SUMSQ_BASE')
|
self._stats_sumsq_base = self.get_constant_float('STATS_SUMSQ_BASE')
|
||||||
self._emergency_stop_cmd = self.lookup_command("emergency_stop")
|
self._emergency_stop_cmd = self.lookup_command("emergency_stop")
|
||||||
|
|
|
@ -151,17 +151,21 @@ def get_aliases_beaglebone(mcu):
|
||||||
re_pin = re.compile(r'(?P<prefix>[ _]pin=)(?P<name>[^ ]*)')
|
re_pin = re.compile(r'(?P<prefix>[ _]pin=)(?P<name>[^ ]*)')
|
||||||
|
|
||||||
class PinResolver:
|
class PinResolver:
|
||||||
def __init__(self, mcu_type, reserved_pins, validate_aliases=True):
|
def __init__(self, validate_aliases=True):
|
||||||
self.mcu_type = mcu_type
|
|
||||||
self.reserved_pins = reserved_pins
|
|
||||||
self.validate_aliases = validate_aliases
|
self.validate_aliases = validate_aliases
|
||||||
|
self.reserved = {}
|
||||||
self.aliases = {}
|
self.aliases = {}
|
||||||
self.active_pins = {}
|
self.active_pins = {}
|
||||||
def update_aliases(self, mapping_name):
|
def reserve_pin(self, pin, reserve_name):
|
||||||
|
if pin in self.reserved and self.reserved[pin] != reserve_name:
|
||||||
|
raise error("Pin %s reserved for %s - can't reserve for %s" % (
|
||||||
|
pin, self.reserved[pin], reserve_name))
|
||||||
|
self.reserved[pin] = reserve_name
|
||||||
|
def add_pin_mapping(self, mcu_type, mapping_name):
|
||||||
if mapping_name == 'arduino':
|
if mapping_name == 'arduino':
|
||||||
self.aliases = get_aliases_arduino(self.mcu_type)
|
self.aliases = get_aliases_arduino(mcu_type)
|
||||||
elif mapping_name == 'beaglebone':
|
elif mapping_name == 'beaglebone':
|
||||||
self.aliases = get_aliases_beaglebone(self.mcu_type)
|
self.aliases = 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,))
|
||||||
def update_command(self, cmd):
|
def update_command(self, cmd):
|
||||||
|
@ -172,9 +176,9 @@ class PinResolver:
|
||||||
and self.validate_aliases):
|
and self.validate_aliases):
|
||||||
raise error("pin %s is an alias for %s" % (
|
raise error("pin %s is an alias for %s" % (
|
||||||
name, self.active_pins[pin_id]))
|
name, self.active_pins[pin_id]))
|
||||||
if pin_id in self.reserved_pins:
|
if pin_id in self.reserved:
|
||||||
raise error("pin %s is reserved for %s" % (
|
raise error("pin %s is reserved for %s" % (
|
||||||
name, self.reserved_pins[pin_id]))
|
name, self.reserved[pin_id]))
|
||||||
return m.group('prefix') + str(pin_id)
|
return m.group('prefix') + str(pin_id)
|
||||||
return re_pin.sub(pin_fixup, cmd)
|
return re_pin.sub(pin_fixup, cmd)
|
||||||
|
|
||||||
|
@ -188,7 +192,7 @@ class PrinterPins:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.chips = {}
|
self.chips = {}
|
||||||
self.active_pins = {}
|
self.active_pins = {}
|
||||||
self.reserved_pins = {}
|
self.pin_resolvers = {}
|
||||||
def parse_pin(self, pin_desc, can_invert=False, can_pullup=False):
|
def parse_pin(self, pin_desc, can_invert=False, can_pullup=False):
|
||||||
desc = pin_desc.strip()
|
desc = pin_desc.strip()
|
||||||
pullup = invert = 0
|
pullup = invert = 0
|
||||||
|
@ -242,19 +246,16 @@ class PrinterPins:
|
||||||
def reset_pin_sharing(self, pin_params):
|
def reset_pin_sharing(self, pin_params):
|
||||||
share_name = "%s:%s" % (pin_params['chip_name'], pin_params['pin'])
|
share_name = "%s:%s" % (pin_params['chip_name'], pin_params['pin'])
|
||||||
del self.active_pins[share_name]
|
del self.active_pins[share_name]
|
||||||
def reserve_pin(self, chip_name, pin, reserve_name):
|
def get_pin_resolver(self, chip_name):
|
||||||
chip_reserve = self.reserved_pins.setdefault(chip_name, {})
|
if chip_name not in self.pin_resolvers:
|
||||||
if pin in chip_reserve and chip_reserve[pin] != reserve_name:
|
raise error("Unknown chip name '%s'" % (chip_name,))
|
||||||
raise error("Pin %s:%s reserved for %s - can't reserve for %s" % (
|
return self.pin_resolvers[chip_name]
|
||||||
chip_name, pin, chip_reserve[pin], reserve_name))
|
|
||||||
chip_reserve[pin] = reserve_name
|
|
||||||
def get_reserved_pins(self, chip_name):
|
|
||||||
return self.reserved_pins.get(chip_name, {})
|
|
||||||
def register_chip(self, chip_name, chip):
|
def register_chip(self, chip_name, chip):
|
||||||
chip_name = chip_name.strip()
|
chip_name = chip_name.strip()
|
||||||
if chip_name in self.chips:
|
if chip_name in self.chips:
|
||||||
raise error("Duplicate chip name '%s'" % (chip_name,))
|
raise error("Duplicate chip name '%s'" % (chip_name,))
|
||||||
self.chips[chip_name] = chip
|
self.chips[chip_name] = chip
|
||||||
|
self.pin_resolvers[chip_name] = PinResolver()
|
||||||
|
|
||||||
def add_printer_objects(config):
|
def add_printer_objects(config):
|
||||||
config.get_printer().add_object('pins', PrinterPins())
|
config.get_printer().add_object('pins', PrinterPins())
|
||||||
|
|
Loading…
Reference in New Issue