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.hal; 006 007import java.nio.ByteBuffer; 008 009/** 010 * Addressable LED HAL JNI Methods. 011 * 012 * @see "hal/AddressableLED.h" 013 */ 014public class AddressableLEDJNI extends JNIWrapper { 015 public static final int COLOR_ORDER_RGB = 0; 016 public static final int COLOR_ORDER_RBG = 1; 017 public static final int COLOR_ORDER_BGR = 2; 018 public static final int COLOR_ORDER_BRG = 3; 019 public static final int COLOR_ORDER_GBR = 4; 020 public static final int COLOR_ORDER_GRB = 5; 021 022 /** 023 * Create a new instance of an Addressable LED port. 024 * 025 * @param channel the smartio channel 026 * @return Addressable LED handle 027 * @see "HAL_InitializeAddressableLED" 028 */ 029 public static native int initialize(int channel); 030 031 /** 032 * Free the Addressable LED Handle. 033 * 034 * @param handle the Addressable LED handle to free 035 * @see "HAL_FreeAddressableLED" 036 */ 037 public static native void free(int handle); 038 039 /** 040 * Sets the start buffer location used for the LED strip. 041 * 042 * <p>All addressable LEDs use a single backing buffer 1024 LEDs in size. The max length for a 043 * single output is 1024 LEDs (with an offset of zero). 044 * 045 * @param handle the Addressable LED handle 046 * @param start the strip start, in LEDs 047 */ 048 public static native void setStart(int handle, int start); 049 050 /** 051 * Sets the length of the LED strip. 052 * 053 * <p>All addressable LEDs use a single backing buffer 1024 LEDs in size. The max length for a 054 * single output is 1024 LEDs (with an offset of zero). 055 * 056 * @param handle the Addressable LED handle 057 * @param length the strip length, in LEDs 058 */ 059 public static native void setLength(int handle, int length); 060 061 /** 062 * Sets the led output data. 063 * 064 * <p>All addressable LEDs use a single backing buffer 1024 LEDs in size. This function may be 065 * used to set part of or all of the buffer. 066 * 067 * @param outStart the strip start in the backing buffer, in LEDs 068 * @param colorOrder the color order 069 * @param data the buffer to write 070 * @see "HAL_WriteAddressableLEDData" 071 */ 072 public static void setData(int outStart, int colorOrder, byte[] data) { 073 setData(outStart, colorOrder, data, 0, data.length); 074 } 075 076 /** 077 * Sets the led output data. 078 * 079 * <p>All addressable LEDs use a single backing buffer 1024 LEDs in size. This function may be 080 * used to set part of or all of the buffer. 081 * 082 * @param outStart the strip start in the backing buffer, in LEDs 083 * @param colorOrder the color order 084 * @param data the buffer to write 085 * @param start offset into data, in bytes 086 * @param len Length of data, in bytes 087 */ 088 public static native void setData(int outStart, int colorOrder, byte[] data, int start, int len); 089 090 /** 091 * Sets the led output data. 092 * 093 * <p>All addressable LEDs use a single backing buffer 1024 LEDs in size. This function may be 094 * used to set part of or all of the buffer. 095 * 096 * @param outStart the strip start in the backing buffer, in LEDs 097 * @param colorOrder the color order 098 * @param data the buffer to write 099 */ 100 public static void setData(int outStart, int colorOrder, ByteBuffer data) { 101 int pos = data.position(); 102 setData(outStart, colorOrder, data, pos, data.capacity() - pos); 103 } 104 105 /** 106 * Sets the led output data. 107 * 108 * <p>All addressable LEDs use a single backing buffer 1024 LEDs in size. This function may be 109 * used to set part of or all of the buffer. 110 * 111 * @param outStart the strip start in the backing buffer, in LEDs 112 * @param colorOrder the color order 113 * @param data the buffer to write 114 * @param start offset into data, in bytes 115 * @param len Length of data, in bytes 116 */ 117 public static void setData(int outStart, int colorOrder, ByteBuffer data, int start, int len) { 118 if (data.isDirect()) { 119 if (start < 0) { 120 throw new IndexOutOfBoundsException("start must be >= 0"); 121 } 122 if (len < 0) { 123 throw new IndexOutOfBoundsException("len must be >= 0"); 124 } 125 if ((start + len) > data.capacity()) { 126 throw new IndexOutOfBoundsException("start + len must be smaller than buffer capacity"); 127 } 128 setDataFromBuffer(outStart, colorOrder, data, start, len); 129 } else if (data.hasArray()) { 130 setData(outStart, colorOrder, data.array(), data.arrayOffset() + start, len); 131 } else { 132 throw new UnsupportedOperationException("ByteBuffer must be direct or have a backing array"); 133 } 134 } 135 136 /** 137 * Sets the led output data. 138 * 139 * <p>All addressable LEDs use a single backing buffer 1024 LEDs in size. This function may be 140 * used to set part of or all of the buffer. 141 * 142 * @param outStart the strip start in the backing buffer, in LEDs 143 * @param colorOrder the color order 144 * @param data the buffer to write 145 * @param start offset into data, in bytes 146 * @param len Length of data, in bytes 147 */ 148 private static native void setDataFromBuffer( 149 int outStart, int colorOrder, ByteBuffer data, int start, int len); 150 151 /** Utility class. */ 152 private AddressableLEDJNI() {} 153}