From 1b0750597312625c23e3389226e418dda1373df2 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Mon, 18 Dec 2017 20:22:53 -0500 Subject: [PATCH] chipmisc: Add multi_pin capability Add the ability to alias multiple output pins from a single pin alias. This makes it possible to support some cases where a single logical output is driven by multiple output pins. Signed-off-by: Kevin O'Connor --- config/example-extras.cfg | 13 ++++++++++ klippy/chipmisc.py | 50 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/config/example-extras.cfg b/config/example-extras.cfg index 19405262..34a55d89 100644 --- a/config/example-extras.cfg +++ b/config/example-extras.cfg @@ -169,6 +169,19 @@ # See the 'pwm_output' section for details on these parameters. +# Multiple pin outputs (one may define any number of sections with a +# "multi_pin" prefix). A multi_pin output creates an internal pin +# alias that can modify multiple output pins each time the alias pin +# is set. For example, one could define a "[multi_pin my_fan]" object +# containing two pins and then set "pin=multi_pin:my_fan" in the +# "[fan]" section - on each fan change both output pins would be +# updated. These aliases may not be used with stepper motor pins. +#[multi_pin my_multi_pin] +#pins: +# A comma separated list of pins associated with this alias. This +# parameter must be provided. + + # Statically configured AD5206 digipots connected via SPI bus (one may # define any number of sections with an "ad5206" prefix). #[ad5206 my_digipot] diff --git a/klippy/chipmisc.py b/klippy/chipmisc.py index 6fae2868..658307c0 100644 --- a/klippy/chipmisc.py +++ b/klippy/chipmisc.py @@ -76,6 +76,54 @@ class PrinterPin: self.last_value = value self.last_value_time = print_time +class PrinterMultiPin: + def __init__(self, printer, config): + self.printer = printer + try: + pins.get_printer_pins(printer).register_chip('multi_pin', self) + except pins.error: + pass + self.pin_type = None + self.pin_list = [pin.strip() for pin in config.get('pins').split(',')] + self.mcu_pins = [] + def setup_pin(self, pin_params): + pin_name = pin_params['pin'] + pin = self.printer.objects.get('multi_pin ' + pin_name) + if pin is not self: + if pin is None: + raise pins.error("multi_pin %s not configured" % (pin_name,)) + return pin.setup_pin(pin_params) + if self.pin_type is not None: + raise pins.error("Can't setup multi_pin %s twice" % (pin_name,)) + self.pin_type = pin_params['type'] + invert = "" + if pin_params['invert']: + invert = "!" + self.mcu_pins = [ + pins.setup_pin(self.printer, self.pin_type, invert + pin_desc) + for pin_desc in self.pin_list] + return self + def get_mcu(self): + return self.mcu_pins[0].get_mcu() + def setup_max_duration(self, max_duration): + for mcu_pin in self.mcu_pins: + mcu_pin.setup_max_duration(max_duration) + def setup_start_value(self, start_value, shutdown_value): + for mcu_pin in self.mcu_pins: + mcu_pin.setup_start_value(start_value, shutdown_value) + def setup_cycle_time(self, cycle_time): + for mcu_pin in self.mcu_pins: + mcu_pin.setup_cycle_time(cycle_time) + def setup_hard_pwm(self, hard_cycle_ticks): + for mcu_pin in self.mcu_pins: + mcu_pin.setup_hard_pwm(hard_cycle_ticks) + def set_digital(self, print_time, value): + for mcu_pin in self.mcu_pins: + mcu_pin.set_digital(print_time, value) + def set_pwm(self, print_time, value): + for mcu_pin in self.mcu_pins: + mcu_pin.set_pwm(print_time, value) + ###################################################################### # Servos @@ -350,6 +398,8 @@ def add_printer_objects(printer, config): printer.add_object('pin' + s.section[17:], PrinterPin(printer, s)) for s in config.get_prefix_sections('pwm_output '): printer.add_object('pin' + s.section[10:], PrinterPin(printer, s)) + for s in config.get_prefix_sections('multi_pin '): + printer.add_object(s.section, PrinterMultiPin(printer, s)) for s in config.get_prefix_sections('servo '): printer.add_object(s.section, PrinterServo(printer, s)) for s in config.get_prefix_sections('ad5206 '):