Skip to content

Commit

Permalink
Add java user code
Browse files Browse the repository at this point in the history
  • Loading branch information
rzblue committed Oct 3, 2024
1 parent 994964b commit b3dfcf3
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
15 changes: 15 additions & 0 deletions hal/src/main/java/edu/wpi/first/hal/AddressableLEDJNI.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
* @see "hal/AddressableLED.h"
*/
public class AddressableLEDJNI extends JNIWrapper {
public static final int COLOR_ORDER_RBG = 0;
public static final int COLOR_ORDER_BGR = 1;
public static final int COLOR_ORDER_BRG = 2;
public static final int COLOR_ORDER_GRB = 3;
public static final int COLOR_ORDER_GBR = 4;
public static final int COLOR_ORDER_RGB = 5;

/**
* Initialize Addressable LED using a PWM Digital handle.
*
Expand All @@ -27,6 +34,14 @@ public class AddressableLEDJNI extends JNIWrapper {
*/
public static native void free(int handle);

/**
* Sets the color order for the addressable LED output
*
* @param handle the Addressable LED handle
* @param colorOrder the color order
*/
public static native void setColorOrder(int handle, int colorOrder);

/**
* Sets the length of the LED strip.
*
Expand Down
23 changes: 23 additions & 0 deletions hal/src/main/native/cpp/jni/AddressableLEDJNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ using namespace wpi::java;

static_assert(sizeof(jbyte) * 4 == sizeof(HAL_AddressableLEDData));

static_assert(edu_wpi_first_hal_AddressableLEDJNI_COLOR_ORDER_RBG == HAL_ALED_RBG);
static_assert(edu_wpi_first_hal_AddressableLEDJNI_COLOR_ORDER_BGR == HAL_ALED_BGR);
static_assert(edu_wpi_first_hal_AddressableLEDJNI_COLOR_ORDER_BRG == HAL_ALED_BRG);
static_assert(edu_wpi_first_hal_AddressableLEDJNI_COLOR_ORDER_GRB == HAL_ALED_GRB);
static_assert(edu_wpi_first_hal_AddressableLEDJNI_COLOR_ORDER_GBR == HAL_ALED_GBR);
static_assert(edu_wpi_first_hal_AddressableLEDJNI_COLOR_ORDER_RGB == HAL_ALED_RGB);

extern "C" {
/*
* Class: edu_wpi_first_hal_AddressableLEDJNI
Expand Down Expand Up @@ -46,6 +53,22 @@ Java_edu_wpi_first_hal_AddressableLEDJNI_free
}
}

/*
* Class: edu_wpi_first_hal_AddressableLEDJNI
* Method: setColorOrder
* Signature: (II)V
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_AddressableLEDJNI_setColorOrder
(JNIEnv* env, jclass, jint handle, jint colorOrder)
{
int32_t status = 0;
HAL_SetAddressableLEDColorOrder(
static_cast<HAL_AddressableLEDHandle>(handle),
static_cast<HAL_AddressableLEDColorOrder>(colorOrder), &status);
CheckStatus(env, status);
}

/*
* Class: edu_wpi_first_hal_AddressableLEDJNI
* Method: setLength
Expand Down
57 changes: 56 additions & 1 deletion wpilibj/src/main/java/edu/wpi/first/wpilibj/AddressableLED.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,58 @@
/**
* A class for driving addressable LEDs, such as WS2812Bs and NeoPixels.
*
* <p>By default, the timing supports WS2812B LEDs, but is configurable using setBitTiming()
* <p>By default, the timing supports WS2812B LEDs, but is configurable using {@link
* #setBitTiming(int, int, int, int)}
*
* <p>Some LEDs use a different color order than the default RGB. The color order is configurable
* using {@link #setColorOrder(ColorOrder)}.
*
* <p>Only 1 LED driver is currently supported by the roboRIO. However, multiple LED strips can be
* connected in series and controlled from the single driver.
*/
public class AddressableLED implements AutoCloseable {

/** Order that color data is sent over the wire. */
enum ColorOrder {
/** RBG order. */
kRBG(AddressableLEDJNI.COLOR_ORDER_RBG),
/** BGR order. */
kBGR(AddressableLEDJNI.COLOR_ORDER_BGR),
/** BRG order. */
kBRG(AddressableLEDJNI.COLOR_ORDER_BRG),
/** GRB order. */
kGRB(AddressableLEDJNI.COLOR_ORDER_GRB),
/** GBR order. */
kGBR(AddressableLEDJNI.COLOR_ORDER_GBR),
/** RGB order. */
kRGB(AddressableLEDJNI.COLOR_ORDER_RGB);

/** The native value for this ColorOrder */
public final int value;

ColorOrder(int value) {
this.value = value;
}

/**
* Gets a color order from an int value.
*
* @param value int value
* @return color order
*/
public ColorOrder fromValue(int value) {
return switch (value) {
case AddressableLEDJNI.COLOR_ORDER_RBG -> kRBG;
case AddressableLEDJNI.COLOR_ORDER_BGR -> kBGR;
case AddressableLEDJNI.COLOR_ORDER_BRG -> kBRG;
case AddressableLEDJNI.COLOR_ORDER_GRB -> kGRB;
case AddressableLEDJNI.COLOR_ORDER_GBR -> kGBR;
case AddressableLEDJNI.COLOR_ORDER_RGB -> kRGB;
default -> kRGB;
};
}
}

private final int m_pwmHandle;
private final int m_handle;

Expand All @@ -42,6 +88,15 @@ public void close() {
}
}

/**
* Sets the color order for this AddressableLED. The default order is RGB.
*
* @param order the color order
*/
public void setColorOrder(ColorOrder order) {
AddressableLEDJNI.setColorOrder(m_handle, order.value);
}

/**
* Sets the length of the LED strip.
*
Expand Down

0 comments on commit b3dfcf3

Please sign in to comment.