Package com.diozero.devices
Class LED
java.lang.Object
com.diozero.api.GpioDevice
com.diozero.api.DigitalOutputDevice
com.diozero.devices.LED
- All Implemented Interfaces:
OutputDeviceInterface
,java.io.Closeable
,java.lang.AutoCloseable
public class LED extends DigitalOutputDevice
Provides utility methods for controlling a Light Emitting Diode (LED). Connect the cathode (short leg, flat side) of the LED to a ground pin; connect the anode (longer leg) to a limiting resistor; connect the other side of the limiting resistor to a GPIO pin (the limiting resistor can be placed either side of the LED). Example LED control, taken from LEDTest:
try (LED led = new LED(pin)) {
led.on();
SleepUtil.sleepSeconds(.5);
led.off();
SleepUtil.sleepSeconds(.5);
led.toggle();
SleepUtil.sleepSeconds(.5);
led.toggle();
SleepUtil.sleepSeconds(.5);
led.blink(0.5f, 0.5f, 10, false);
}
-
Field Summary
-
Constructor Summary
Constructors Constructor Description LED(int gpio)
LED(int gpio, boolean activeHigh)
LED(GpioDeviceFactoryInterface deviceFactory, int gpio)
LED(GpioDeviceFactoryInterface deviceFactory, int gpio, boolean activeHigh, boolean initialValue)
-
Method Summary
Modifier and Type Method Description void
blink()
Blink indefinitely with 1 second on and 1 second off.void
blink(float onTime, float offTime, int n, boolean background)
Blink.void
blink(float onTime, float offTime, int n, boolean background, Action stopAction)
Blink.void
blink(Action stopAction)
Blink indefinitely with 1 second on and 1 second off.boolean
isLit()
Return true if the LED is currently on.Methods inherited from class com.diozero.api.DigitalOutputDevice
close, isOn, off, on, onOffLoop, setOn, setValue, setValueUnsafe, toggle
-
Constructor Details
-
LED
- Parameters:
gpio
- GPIO to which the LED is connected.- Throws:
RuntimeIOException
- If an I/O error occurred.
-
LED
- Parameters:
gpio
- GPIO to which the LED is connected.activeHigh
- Set to true if a high output value represents on.- Throws:
RuntimeIOException
- If an I/O error occurred.
-
LED
- Parameters:
deviceFactory
- Device factory to use to construct the device.gpio
- GPIO to which the LED is connected.- Throws:
RuntimeIOException
- If an I/O error occurred.
-
LED
public LED(GpioDeviceFactoryInterface deviceFactory, int gpio, boolean activeHigh, boolean initialValue)- Parameters:
deviceFactory
- Device factory to use to construct the device.gpio
- GPIO to which the LED is connected.activeHigh
- Set to true if a high output value represents on.initialValue
- Initial value.- Throws:
RuntimeIOException
- If an I/O error occurred.
-
-
Method Details
-
blink
Blink indefinitely with 1 second on and 1 second off.- Throws:
RuntimeIOException
- If an I/O error occurred.
-
blink
Blink indefinitely with 1 second on and 1 second off.- Parameters:
stopAction
- Action to invoke when the animation stops.- Throws:
RuntimeIOException
- If an I/O error occurred.
-
blink
public void blink(float onTime, float offTime, int n, boolean background) throws RuntimeIOExceptionBlink.- Parameters:
onTime
- On time in seconds.offTime
- Off time in seconds.n
- Number of iterations. Set to <0 to blink indefinitely.background
- If true start a background thread to control the blink and return immediately. If false, only return once the blink iterations have finished.- Throws:
RuntimeIOException
- If an I/O error occurred.
-
blink
public void blink(float onTime, float offTime, int n, boolean background, Action stopAction) throws RuntimeIOExceptionBlink.- Parameters:
onTime
- On time in seconds.offTime
- Off time in seconds.n
- Number of iterations. Set to <0 to blink indefinitely.background
- If true start a background thread to control the blink and return immediately. If false, only return once the blink iterations have finished.stopAction
- Action to invoke when the animation stops.- Throws:
RuntimeIOException
- If an I/O error occurred.
-
isLit
Return true if the LED is currently on.- Returns:
- True if the LED is on, false if off.
- Throws:
RuntimeIOException
- If an I/O error occurred.
-