Module diozero.core

Class LED

All Implemented Interfaces:
DeviceInterface, 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);
}
 
  • Constructor Details

    • LED

      public LED(PinInfo pinInfo, boolean activeHigh, boolean initialValue) throws RuntimeIOException
      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

      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.