Skip to content

Commit 71a1253

Browse files
committed
Add constructor for injecting devices in BasicChopperController
This is useful for cases where the pins may be active low, instead of active high.
1 parent b079066 commit 71a1253

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

diozero-core/src/main/java/com/diozero/devices/sandpit/motor/ChopperStepperController.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import com.diozero.api.RuntimeIOException;
3737
import com.diozero.devices.sandpit.motor.StepperMotorInterface.Direction;
3838

39+
import java.util.Objects;
40+
3941
/**
4042
* A basic device for a stepper driven by a "chopper" driver: uses 3 GPIO pins (enable, direction, frequency) to
4143
* control a motor.
@@ -109,6 +111,21 @@ class BasicChopperController implements ChopperStepperController {
109111
private final DigitalOutputDevice directionSet;
110112
private final PwmOutputDevice stepControl;
111113

114+
/**
115+
* Constructor that allows injecting the devices directly.
116+
*
117+
* @param enableDevice the device responsible to enable/disable the driver
118+
* @param directionSet the device responsible to set the direction of rotation
119+
* @param stepControl the device responsible to control the stepping frequency and whether stepping or not
120+
* @implNote The provided devices will be closed when this controller is closed.
121+
*/
122+
public BasicChopperController(DigitalOutputDevice enableDevice, DigitalOutputDevice directionSet,
123+
PwmOutputDevice stepControl) {
124+
this.enableDevice = Objects.requireNonNull(enableDevice, "enableDevice must not be null");
125+
this.directionSet = Objects.requireNonNull(directionSet, "directionSet must not be null");
126+
this.stepControl = Objects.requireNonNull(stepControl, "stepControl must not be null");
127+
}
128+
112129
/**
113130
* Basic constructor for this type of driver.
114131
*
@@ -117,9 +134,9 @@ class BasicChopperController implements ChopperStepperController {
117134
* @param stepPin controls the <i>frequency</i> of the motor and whether stepping or not
118135
*/
119136
public BasicChopperController(int enablePin, int directionPin, int stepPin) {
120-
enableDevice = new DigitalOutputDevice(enablePin, true, false);
121-
directionSet = new DigitalOutputDevice(directionPin, true, false);
122-
stepControl = new PwmOutputDevice(stepPin);
137+
this(new DigitalOutputDevice(enablePin, true, false),
138+
new DigitalOutputDevice(directionPin, true, false),
139+
new PwmOutputDevice(stepPin));
123140
}
124141

125142
@Override

0 commit comments

Comments
 (0)