Class LED

java.lang.Object
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

    Fields inherited from class com.diozero.api.DigitalOutputDevice

    INFINITE_ITERATIONS

    Fields inherited from class com.diozero.api.GpioDevice

    gpio
  • 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

    Methods inherited from class com.diozero.api.GpioDevice

    getGpio

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • LED

      public LED​(int gpio) throws RuntimeIOException
      Parameters:
      gpio - GPIO to which the LED is connected.
      Throws:
      RuntimeIOException - If an I/O error occurred.
    • LED

      public LED​(int gpio, boolean activeHigh) throws RuntimeIOException
      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

      public LED​(GpioDeviceFactoryInterface deviceFactory, int gpio) throws RuntimeIOException
      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

      public void blink() throws RuntimeIOException
      Blink indefinitely with 1 second on and 1 second off.
      Throws:
      RuntimeIOException - If an I/O error occurred.
    • blink

      public void blink​(Action stopAction) throws RuntimeIOException
      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 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.
      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

      public boolean isLit() throws RuntimeIOException
      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.