java.lang.Object
com.diozero.api.GpioDevice
com.diozero.api.DigitalOutputDevice
com.diozero.devices.LED
- All Implemented Interfaces:
DeviceInterface
,AutoCloseable
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);
}
-
Nested Class Summary
Nested classes/interfaces inherited from class com.diozero.api.DigitalOutputDevice
DigitalOutputDevice.Builder
-
Field Summary
Fields inherited from class com.diozero.api.DigitalOutputDevice
INFINITE_ITERATIONS
Fields inherited from class com.diozero.api.GpioDevice
pinInfo
-
Constructor Summary
ConstructorDescriptionLED
(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 TypeMethodDescriptionvoid
blink()
Blink indefinitely with 1 second on and 1 second off.void
blink
(float onTime, float offTime, int n, boolean background) Blink.void
Blink.void
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, getCycleCount, isActiveHigh, isOn, off, on, onOffLoop, setOn, setValue, stopOnOffLoop, toggle
Methods inherited from class com.diozero.api.GpioDevice
getGpio
-
Constructor Details
-
LED
- Parameters:
pinInfo
- 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.
-
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
Blink.- 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 RuntimeIOException Blink.- 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.
-