001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package edu.wpi.first.wpilibj;
006
007import edu.wpi.first.wpilibj.util.Color;
008import edu.wpi.first.wpilibj.util.Color8Bit;
009
010/** Generic interface for writing data to an LED buffer. */
011@FunctionalInterface
012public interface LEDWriter {
013  /**
014   * Sets the RGB value for an LED at a specific index on a LED buffer.
015   *
016   * @param index the index of the LED to write to
017   * @param r the value of the red channel, in [0, 255]
018   * @param g the value of the green channel, in [0, 255]
019   * @param b the value of the blue channel, in [0, 255]
020   */
021  void setRGB(int index, int r, int g, int b);
022
023  /**
024   * Sets a specific led in the buffer.
025   *
026   * @param index the index to write
027   * @param h the h value [0-180)
028   * @param s the s value [0-255]
029   * @param v the v value [0-255]
030   */
031  default void setHSV(int index, int h, int s, int v) {
032    if (s == 0) {
033      setRGB(index, v, v, v);
034      return;
035    }
036
037    int packedRGB = Color.hsvToRgb(h, s, v);
038
039    setRGB(
040        index,
041        Color.unpackRGB(packedRGB, Color.RGBChannel.kRed),
042        Color.unpackRGB(packedRGB, Color.RGBChannel.kGreen),
043        Color.unpackRGB(packedRGB, Color.RGBChannel.kBlue));
044  }
045
046  /**
047   * Sets the RGB value for an LED at a specific index on a LED buffer.
048   *
049   * @param index the index of the LED to write to
050   * @param color the color to set
051   */
052  default void setLED(int index, Color color) {
053    setRGB(index, (int) (color.red * 255), (int) (color.green * 255), (int) (color.blue * 255));
054  }
055
056  /**
057   * Sets the RGB value for an LED at a specific index on a LED buffer.
058   *
059   * @param index the index of the LED to write to
060   * @param color the color to set
061   */
062  default void setLED(int index, Color8Bit color) {
063    setRGB(index, color.red, color.green, color.blue);
064  }
065}