Package com.diozero.api
Class ServoDevice
- java.lang.Object
-
- com.diozero.api.GpioDevice
-
- com.diozero.api.ServoDevice
-
- All Implemented Interfaces:
DeviceInterface
,AutoCloseable
public class ServoDevice extends GpioDevice
Provide Servo device control.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ServoDevice.Builder
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_FREQUENCY
-
Fields inherited from class com.diozero.api.GpioDevice
pinInfo
-
-
Constructor Summary
Constructors Constructor Description ServoDevice(ServoDeviceFactoryInterface deviceFactory, PinInfo pinInfo, int frequency, ServoTrim trim, int initialPulseWidthUs, boolean inverted)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Close this devicefloat
getAngle()
Get the current servo angle where 90 degrees is the middle positionint
getPulseWidthUs()
Get the current servo pulse width in microseconds.int
getServoFrequency()
boolean
isOn()
Get the device on / off status.void
max()
void
mid()
void
min()
static ServoDevice.Builder
newBuilder(int pwmOrGpio)
static ServoDevice.Builder
newBuilder(PinInfo pinInfo)
void
setAngle(float angle)
Turn the servo to the specified angle where 90 is the middle positionvoid
setPulseWidthUs(int pulseWidthUs)
Set the servo pulse width value in microseconds.void
setServoFrequency(int frequency)
-
Methods inherited from class com.diozero.api.GpioDevice
getGpio
-
-
-
-
Field Detail
-
DEFAULT_FREQUENCY
public static final int DEFAULT_FREQUENCY
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ServoDevice
public ServoDevice(ServoDeviceFactoryInterface deviceFactory, PinInfo pinInfo, int frequency, ServoTrim trim, int initialPulseWidthUs, boolean inverted) throws RuntimeIOException
- Parameters:
deviceFactory
- Device factory to use to provision this device.pinInfo
- GPIO to which the output device is connected.frequency
- Servo frequency (Hz).trim
- Servo min / mid / max pulse widths and angle mappinginitialPulseWidthUs
- Initial servo pulse width in microsecondsinverted
- If the output is inverted, i.e. min is output as max and vice versa- Throws:
RuntimeIOException
- If an I/O error occurred.
-
-
Method Detail
-
newBuilder
public static ServoDevice.Builder newBuilder(int pwmOrGpio)
-
newBuilder
public static ServoDevice.Builder newBuilder(PinInfo pinInfo)
-
close
public void close()
Description copied from interface:DeviceInterface
Close this device
-
getPulseWidthUs
public int getPulseWidthUs() throws RuntimeIOException
Get the current servo pulse width in microseconds.- Returns:
- Current output pulse width value.
- Throws:
RuntimeIOException
- If an I/O error occurred.
-
setPulseWidthUs
public void setPulseWidthUs(int pulseWidthUs) throws RuntimeIOException
Set the servo pulse width value in microseconds.- Parameters:
pulseWidthUs
- New pulse width value in microseconds.- Throws:
RuntimeIOException
- If an I/O error occurred.
-
getServoFrequency
public int getServoFrequency()
-
setServoFrequency
public void setServoFrequency(int frequency) throws RuntimeIOException
- Throws:
RuntimeIOException
-
isOn
public boolean isOn() throws RuntimeIOException
Get the device on / off status.- Returns:
- Returns true if the device currently has a value > 0.
- Throws:
RuntimeIOException
- If an I/O error occurred.
-
getAngle
public float getAngle()
Get the current servo angle where 90 degrees is the middle position- Returns:
- Servo angle (90 degrees is middle)
-
setAngle
public void setAngle(float angle)
Turn the servo to the specified angle where 90 is the middle position- Parameters:
angle
- Servo angle (90 degrees is middle)
-
min
public void min()
-
mid
public void mid()
-
max
public void max()
-
-