From 0526aa923682bc51d0678c8e8fe800ed8518632a Mon Sep 17 00:00:00 2001 From: Ketan Padegaonkar Date: Wed, 18 Feb 2026 20:22:26 +0530 Subject: [PATCH] Add constructor for injecting devices in BasicChopperController This is useful for cases where the pins may be active low, instead of active high. --- .../motor/ChopperStepperController.java | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/diozero-core/src/main/java/com/diozero/devices/sandpit/motor/ChopperStepperController.java b/diozero-core/src/main/java/com/diozero/devices/sandpit/motor/ChopperStepperController.java index 242303c0b..900a77025 100644 --- a/diozero-core/src/main/java/com/diozero/devices/sandpit/motor/ChopperStepperController.java +++ b/diozero-core/src/main/java/com/diozero/devices/sandpit/motor/ChopperStepperController.java @@ -36,6 +36,8 @@ import com.diozero.api.RuntimeIOException; import com.diozero.devices.sandpit.motor.StepperMotorInterface.Direction; +import java.util.Objects; + /** * A basic device for a stepper driven by a "chopper" driver: uses 3 GPIO pins (enable, direction, frequency) to * control a motor. @@ -109,6 +111,21 @@ class BasicChopperController implements ChopperStepperController { private final DigitalOutputDevice directionSet; private final PwmOutputDevice stepControl; + /** + * Constructor that allows injecting the devices directly. + * + * @param enableDevice the device responsible to enable/disable the driver + * @param directionSet the device responsible to set the direction of rotation + * @param stepControl the device responsible to control the stepping frequency and whether stepping or not + * @implNote The provided devices will be closed when this controller is closed. + */ + public BasicChopperController(DigitalOutputDevice enableDevice, DigitalOutputDevice directionSet, + PwmOutputDevice stepControl) { + this.enableDevice = Objects.requireNonNull(enableDevice, "enableDevice must not be null"); + this.directionSet = Objects.requireNonNull(directionSet, "directionSet must not be null"); + this.stepControl = Objects.requireNonNull(stepControl, "stepControl must not be null"); + } + /** * Basic constructor for this type of driver. * @@ -117,9 +134,9 @@ class BasicChopperController implements ChopperStepperController { * @param stepPin controls the frequency of the motor and whether stepping or not */ public BasicChopperController(int enablePin, int directionPin, int stepPin) { - enableDevice = new DigitalOutputDevice(enablePin, true, false); - directionSet = new DigitalOutputDevice(directionPin, true, false); - stepControl = new PwmOutputDevice(stepPin); + this(new DigitalOutputDevice(enablePin, true, false), + new DigitalOutputDevice(directionPin, true, false), + new PwmOutputDevice(stepPin)); } @Override @@ -215,6 +232,11 @@ public int multiplier() { private Resolution resolution = Resolution.FULL; + public FrequencyMultiplierChopperController(DigitalOutputDevice enableDevice, DigitalOutputDevice directionSet, + PwmOutputDevice stepControl) { + super(enableDevice, directionSet, stepControl); + } + public FrequencyMultiplierChopperController(int enablePin, int directionPin, int stepPin) { super(enablePin, directionPin, stepPin); }