From b14146b440542fdd6eb7d1146b98d3f8eb9ecb98 Mon Sep 17 00:00:00 2001 From: Zachary Browning Date: Tue, 18 Aug 2015 14:09:27 -0400 Subject: [PATCH 1/7] Spacing made consistent. --- src/blinkstick/BlinkStick.java | 1696 ++++++++++++++++---------------- 1 file changed, 848 insertions(+), 848 deletions(-) diff --git a/src/blinkstick/BlinkStick.java b/src/blinkstick/BlinkStick.java index 4f0cea4..5be917e 100644 --- a/src/blinkstick/BlinkStick.java +++ b/src/blinkstick/BlinkStick.java @@ -1,848 +1,848 @@ -/** - * ##library.name## - * ##library.sentence## - * ##library.url## - * - * Copyright ##copyright## ##author## - * All Rights Reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO - * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT - * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING - * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY - * OF SUCH DAMAGE. - * - * @author ##author## - * @modified ##date## - * @version ##library.prettyVersion## (##library.version##) - */ - -package blinkstick; - -import com.codeminders.hidapi.HIDDeviceInfo; -import com.codeminders.hidapi.HIDManager; -import com.codeminders.hidapi.HIDDevice; -import java.util.Hashtable; -import java.util.Random; -import java.util.List; -import java.util.ArrayList; - -/** - * This is a template class and can be used to start a new processing library or - * tool. Make sure you rename this class as well as the name of the example - * package 'template' to your own library or tool naming convention. - * - * @example Hello - * - * (the tag @example followed by the name of an example included in - * folder 'examples' will automatically include the example in the - * javadoc.) - * - */ - -/** - * @author Arvydas - * - */ -public class BlinkStick { - - public final static String VERSION = "##library.prettyVersion##"; - - private static Boolean Initialized = false; - - private static final Hashtable COLORS = new Hashtable() { - /** - * - */ - private static final long serialVersionUID = 1L; - - { - put("aqua", "#00ffff"); - put("aliceblue", "#f0f8ff"); - put("antiquewhite", "#faebd7"); - put("black", "#000000"); - put("blue", "#0000ff"); - put("cyan", "#00ffff"); - put("darkblue", "#00008b"); - put("darkcyan", "#008b8b"); - put("darkgreen", "#006400"); - put("darkturquoise", "#00ced1"); - put("deepskyblue", "#00bfff"); - put("green", "#008000"); - put("lime", "#00ff00"); - put("mediumblue", "#0000cd"); - put("mediumspringgreen", "#00fa9a"); - put("navy", "#000080"); - put("springgreen", "#00ff7f"); - put("teal", "#008080"); - put("midnightblue", "#191970"); - put("dodgerblue", "#1e90ff"); - put("lightseagreen", "#20b2aa"); - put("forestgreen", "#228b22"); - put("seagreen", "#2e8b57"); - put("darkslategray", "#2f4f4f"); - put("darkslategrey", "#2f4f4f"); - put("limegreen", "#32cd32"); - put("mediumseagreen", "#3cb371"); - put("turquoise", "#40e0d0"); - put("royalblue", "#4169e1"); - put("steelblue", "#4682b4"); - put("darkslateblue", "#483d8b"); - put("mediumturquoise", "#48d1cc"); - put("indigo", "#4b0082"); - put("darkolivegreen", "#556b2f"); - put("cadetblue", "#5f9ea0"); - put("cornflowerblue", "#6495ed"); - put("mediumaquamarine", "#66cdaa"); - put("dimgray", "#696969"); - put("dimgrey", "#696969"); - put("slateblue", "#6a5acd"); - put("olivedrab", "#6b8e23"); - put("slategray", "#708090"); - put("slategrey", "#708090"); - put("lightslategray", "#778899"); - put("lightslategrey", "#778899"); - put("mediumslateblue", "#7b68ee"); - put("lawngreen", "#7cfc00"); - put("aquamarine", "#7fffd4"); - put("chartreuse", "#7fff00"); - put("gray", "#808080"); - put("grey", "#808080"); - put("maroon", "#800000"); - put("olive", "#808000"); - put("purple", "#800080"); - put("lightskyblue", "#87cefa"); - put("skyblue", "#87ceeb"); - put("blueviolet", "#8a2be2"); - put("darkmagenta", "#8b008b"); - put("darkred", "#8b0000"); - put("saddlebrown", "#8b4513"); - put("darkseagreen", "#8fbc8f"); - put("lightgreen", "#90ee90"); - put("mediumpurple", "#9370db"); - put("darkviolet", "#9400d3"); - put("palegreen", "#98fb98"); - put("darkorchid", "#9932cc"); - put("yellowgreen", "#9acd32"); - put("sienna", "#a0522d"); - put("brown", "#a52a2a"); - put("darkgray", "#a9a9a9"); - put("darkgrey", "#a9a9a9"); - put("greenyellow", "#adff2f"); - put("lightblue", "#add8e6"); - put("paleturquoise", "#afeeee"); - put("lightsteelblue", "#b0c4de"); - put("powderblue", "#b0e0e6"); - put("firebrick", "#b22222"); - put("darkgoldenrod", "#b8860b"); - put("mediumorchid", "#ba55d3"); - put("rosybrown", "#bc8f8f"); - put("darkkhaki", "#bdb76b"); - put("silver", "#c0c0c0"); - put("mediumvioletred", "#c71585"); - put("indianred", "#cd5c5c"); - put("peru", "#cd853f"); - put("chocolate", "#d2691e"); - put("tan", "#d2b48c"); - put("lightgray", "#d3d3d3"); - put("lightgrey", "#d3d3d3"); - put("thistle", "#d8bfd8"); - put("goldenrod", "#daa520"); - put("orchid", "#da70d6"); - put("palevioletred", "#db7093"); - put("crimson", "#dc143c"); - put("gainsboro", "#dcdcdc"); - put("plum", "#dda0dd"); - put("burlywood", "#deb887"); - put("lightcyan", "#e0ffff"); - put("lavender", "#e6e6fa"); - put("darksalmon", "#e9967a"); - put("palegoldenrod", "#eee8aa"); - put("violet", "#ee82ee"); - put("azure", "#f0ffff"); - put("honeydew", "#f0fff0"); - put("khaki", "#f0e68c"); - put("lightcoral", "#f08080"); - put("sandybrown", "#f4a460"); - put("beige", "#f5f5dc"); - put("mintcream", "#f5fffa"); - put("wheat", "#f5deb3"); - put("whitesmoke", "#f5f5f5"); - put("ghostwhite", "#f8f8ff"); - put("lightgoldenrodyellow", "#fafad2"); - put("linen", "#faf0e6"); - put("salmon", "#fa8072"); - put("oldlace", "#fdf5e6"); - put("bisque", "#ffe4c4"); - put("blanchedalmond", "#ffebcd"); - put("coral", "#ff7f50"); - put("cornsilk", "#fff8dc"); - put("darkorange", "#ff8c00"); - put("deeppink", "#ff1493"); - put("floralwhite", "#fffaf0"); - put("fuchsia", "#ff00ff"); - put("gold", "#ffd700"); - put("hotpink", "#ff69b4"); - put("ivory", "#fffff0"); - put("lavenderblush", "#fff0f5"); - put("lemonchiffon", "#fffacd"); - put("lightpink", "#ffb6c1"); - put("lightsalmon", "#ffa07a"); - put("lightyellow", "#ffffe0"); - put("magenta", "#ff00ff"); - put("mistyrose", "#ffe4e1"); - put("moccasin", "#ffe4b5"); - put("navajowhite", "#ffdead"); - put("orange", "#ffa500"); - put("orangered", "#ff4500"); - put("papayawhip", "#ffefd5"); - put("peachpuff", "#ffdab9"); - put("pink", "#ffc0cb"); - put("red", "#ff0000"); - put("seashell", "#fff5ee"); - put("snow", "#fffafa"); - put("tomato", "#ff6347"); - put("white", "#ffffff"); - put("yellow", "#ffff00"); - } - }; - - /** - * BlinkStick vendor ID - */ - public final static int VENDOR_ID = 0x20a0; - - /** - * BlinkStick product ID - */ - public final static int PRODUCT_ID = 0x41e5; - - /** - * HID device object to communicate directly with BlinkStick - */ - private HIDDevice device = null; - - /** - * return the version of the library. - * - * @return String - */ - public static String version() { - return VERSION; - } - - /** - * Assign HIDDevice - * - * @param device HID device object to communicate directly with BlinkStick - */ - private void setDevice(HIDDevice device) { - this.device = device; - } - - /** - * Load hidapi library based on the OS. This function is automatically called whenever - * a search for BlinkStick is performed for the first time. - */ - public static void Initialize() { - if (!Initialized) { - Initialized = true; - - com.codeminders.hidapi.ClassPathLibraryLoader - .loadNativeHIDLibrary(); - } - } - - /** - * Find first BlinkStick connected to the computer - * - * @return BlinkStick object or null if no BlinkSticks are connected - */ - public static BlinkStick findFirst() { - Initialize(); - - HIDDeviceInfo[] infos = findAllDescriptors(); - - if (infos.length > 0) { - BlinkStick result = new BlinkStick(); - try { - result.setDevice(infos[0].open()); - return result; - } catch (Exception e) { - } - } - return null; - } - - /** Find BlinkStick by serial number - * - * @param serial The serial number to search - * - * @return BlinkStick object or null if device was not found - */ - public static BlinkStick findBySerial(String serial) { - Initialize(); - - HIDDeviceInfo[] infos = findAllDescriptors(); - - for (HIDDeviceInfo info : infos) { - if (info.getSerial_number().equals(serial)) { - BlinkStick result = new BlinkStick(); - try { - result.setDevice(infos[0].open()); - return result; - } catch (Exception e) { - } - } - } - - return null; - } - - /** - * Find all BlinkStick HIDDeviceInfo descriptions connected to the computer - * - * @return an array of HIDDeviceInfo objects with VID and PID matching BlinkStick - */ - private static HIDDeviceInfo[] findAllDescriptors() { - Initialize(); - - List blinkstickList = new ArrayList(); - - try { - HIDManager hidManager = HIDManager.getInstance(); - - HIDDeviceInfo[] infos = hidManager.listDevices(); - - for (HIDDeviceInfo info : infos) { - if (info.getVendor_id() == VENDOR_ID - && info.getProduct_id() == PRODUCT_ID) { - blinkstickList.add(info); - } - } - } catch (Exception e) { - } - - return blinkstickList.toArray(new HIDDeviceInfo[blinkstickList.size()]); - } - - /** - * Find all BlinkSticks connected to the computer - * - * @return an array of BlinkStick objects - */ - public static BlinkStick[] findAll() { - List blinkstickList = new ArrayList(); - - HIDDeviceInfo[] infos = findAllDescriptors(); - - for (HIDDeviceInfo info : infos) { - BlinkStick blinkstick = new BlinkStick(); - try { - blinkstick.setDevice(info.open()); - blinkstickList.add(blinkstick); - } catch (Exception e) { - } - } - - return blinkstickList.toArray(new BlinkStick[blinkstickList.size()]); - } - - /** - * Set the color of the device with separate r, g and b int values. - * The values are automatically converted to byte values - * - * @param r red int color value 0..255 - * @param g gree int color value 0..255 - * @param b blue int color value 0..255 - */ - public void setColor(int r, int g, int b) { - this.setColor((byte) r, (byte) g, (byte) b); - } - - /** - * Set the color of the device with separate r, g and b byte values - * - * @param r red byte color value 0..255 - * @param g gree byte color value 0..255 - * @param b blue byte color value 0..255 - */ - public void setColor(byte r, byte g, byte b) { - try { - device.sendFeatureReport(new byte[] {1, r, g, b}); - } catch (Exception e) { - e.printStackTrace(); - } - } - - /** - * Set indexed color of the device with separate r, g and b byte values for channel and LED index - * - * @param channel Channel (0 - R, 1 - G, 2 - B) - * @param index Index of the LED - * @param r red int color value 0..255 - * @param g gree int color value 0..255 - * @param b blue int color value 0..255 - */ - public void setIndexedColor(int channel, int index, int r, int g, int b) { - this.setIndexedColor((byte)channel, (byte)index, (byte)r, (byte)g, (byte)b); - } - - /** - * Set indexed color of the device with separate r, g and b byte values for channel and LED index - * - * @param channel Channel (0 - R, 1 - G, 2 - B) - * @param index Index of the LED - * @param r red byte color value 0..255 - * @param g gree byte color value 0..255 - * @param b blue byte color value 0..255 - */ - public void setIndexedColor(byte channel, byte index, byte r, byte g, byte b) { - try { - device.sendFeatureReport(new byte[] {5, channel, index, r, g, b}); - } catch (Exception e) { - e.printStackTrace(); - } - } - - /** - * Set the indexed color of BlinkStick Pro with Processing color value - * - * @param channel Channel (0 - R, 1 - G, 2 - B) - * @param index Index of the LED - * @param value color as int - */ - public void setIndexedColor(int channel, int index, int value) { - int r = (value >> 16) & 0xFF; - int g = (value >> 8) & 0xFF; - int b = value & 0xFF; - - this.setIndexedColor(channel, index, r, g, b); - } - - /** - * Set the indexed color of BlinkStick Pro with Processing color value for channel 0 - * - * @param index Index of the LED - * @param value color as int - */ - public void setIndexedColor(int index, int value) { - int r = (value >> 16) & 0xFF; - int g = (value >> 8) & 0xFF; - int b = value & 0xFF; - - this.setIndexedColor(0, index, r, g, b); - } - - /** - * Set the color of the device with Processing color value - * - * @param value color as int - */ - public void setColor(int value) { - int r = (value >> 16) & 0xFF; - int g = (value >> 8) & 0xFF; - int b = value & 0xFF; - - this.setColor(r, g, b); - } - - /** - * Set the color of the device with string value - * - * @param value this can either be a named color "red", "green", "blue" and etc. - * or a hex color in #rrggbb format - */ - public void setColor(String value) { - if (COLORS.containsKey(value)) { - this.setColor(hex2Rgb(COLORS.get(value))); - } else { - this.setColor(hex2Rgb(value)); - } - } - - /** - * Set random color - */ - public void setRandomColor() { - Random random = new Random(); - this.setColor( - random.nextInt(256), - random.nextInt(256), - random.nextInt(256)); - } - - /** - * Turn the device off - */ - public void turnOff() { - this.setColor(0, 0, 0); - } - - - /** - * Convert hex string to color object - * - * @param colorStr Color value as hex string #rrggbb - * - * @return color object - */ - private int hex2Rgb(String colorStr) { - int red = Integer.valueOf(colorStr.substring(1, 3), 16)+ 0; - int green = Integer.valueOf(colorStr.substring(3, 5), 16) + 0; - int blue = Integer.valueOf(colorStr.substring(5, 7), 16) + 0; - - return (255 << 24) | (red << 16) | (green << 8) | blue; - } - - /** - * Get the current color of the device as int - * - * @return The current color of the device as int - */ - public int getColor() { - byte[] data = new byte[33]; - data[0] = 1;// First byte is ReportID - - try { - int read = device.getFeatureReport(data); - if (read > 0) { - return (255 << 24) | (data[1] << 16) | (data[2] << 8) | data[3]; - } - } catch (Exception e) { - } - - return 0; - } - - /** - * Get the current color of the device in #rrggbb format - * - * @return Returns the current color of the device as #rrggbb formated string - */ - public String getColorString() { - int c = getColor(); - - int red = (c >> 16) & 0xFF; - int green = (c >> 8) & 0xFF; - int blue = c & 0xFF; - - return "#" + String.format("%02X", red) - + String.format("%02X", green) - + String.format("%02X", blue); - } - - /** - * Get value of InfoBlocks - * - * @param id InfoBlock id, should be 1 or 2 as only supported info blocks - */ - private String getInfoBlock(int id) { - byte[] data = new byte[33]; - data[0] = (byte) (id + 1); - - String result = ""; - try { - int read = device.getFeatureReport(data); - if (read > 0) { - for (int i = 1; i < data.length; i++) { - if (i == 0) { - break; - } - - result += (char) data[i]; - } - } - } catch (Exception e) { - } - - return result; - } - - /** - * Get value of InfoBlock1 - * - * @return The value of info block 1 - */ - public String getInfoBlock1() { - return getInfoBlock(1); - } - - /** - * Get value of InfoBlock2 - * - * @return The value of info block 2 - */ - public String getInfoBlock2() { - return getInfoBlock(2); - } - - - /** - * Set value for InfoBlocks - * - * @param id InfoBlock id, should be 1 or 2 as only supported info blocks - * @param value The value to be written to the info block - */ - private void setInfoBlock(int id, String value) { - char[] charArray = value.toCharArray(); - byte[] data = new byte[33]; - data[0] = (byte) (id + 1); - - for (int i = 0; i < charArray.length; i++) { - if (i > 32) { - break; - } - - data[i + 1] = (byte) charArray[i]; - } - - try { - device.sendFeatureReport(data); - } catch (Exception e) { - e.printStackTrace(); - - } - } - - /** - * Set value for InfoBlock1 - * - * @param value The value to be written to the info block 1 - */ - public void setInfoBlock1(String value) { - setInfoBlock(1, value); - } - - /** - * Set value for InfoBlock2 - * - * @param value The value to be written to the info block 2 - */ - public void setInfoBlock2(String value) { - setInfoBlock(2, value); - } - - /** - * Get the manufacturer of the device - * - * @return Returns the manufacturer name of the device - */ - public String getManufacturer() { - try { - return device.getManufacturerString(); - } catch (Exception e) { - return ""; - } - } - - /** - * Get the product description of the device - * - * @return Returns the product name of the device. - */ - public String getProduct() { - try { - return device.getProductString(); - } catch (Exception e) { - return ""; - } - } - - - /** - * Get the serial number of the device - * - * @return Returns the serial number of device. - */ - public String getSerial() { - try { - return device.getSerialNumberString(); - } catch (Exception e) { - return ""; - } - } - - - /** - * Determine report id for the amount of data to be sent - * - * @return Returns the report id - */ - private byte determineReportId(int length) { - byte reportId = 9; - //Automatically determine the correct report id to send the data to - if (length <= 8 * 3) - { - reportId = 6; - } - else if (length <= 16 * 3) - { - reportId = 7; - } - else if (length <= 32 * 3) - { - reportId = 8; - } - else if (length <= 64 * 3) - { - reportId = 9; - } - else if (length <= 128 * 3) - { - reportId = 10; - } - - return reportId; - } - - /** - * Determine the adjusted maximum amount of LED for the report - * - * @return Returns the adjusted amount of LED data - */ - private byte determineMaxLeds(int length) { - byte maxLeds = 64; - //Automatically determine the correct report id to send the data to - if (length <= 8 * 3) - { - maxLeds = 8; - } - else if (length <= 16 * 3) - { - maxLeds = 16; - } - else if (length <= 32 * 3) - { - maxLeds = 32; - } - else if (length <= 64 * 3) - { - maxLeds = 64; - } - else if (length <= 128 * 3) - { - maxLeds = 64; - } - - return maxLeds; - } - - /** - * Send a packet of data to LEDs on channel 0 (R) - * - * @param colorData Report data must be a byte array in the following format: [g0, r0, b0, g1, r1, b1, g2, r2, b2 ...] - */ - public void setColors(byte[] colorData) - { - this.setColors((byte)0, colorData); - } - - /** - * Send a packet of data to LEDs - * - * @param channel Channel (0 - R, 1 - G, 2 - B) - * @param colorData Report data must be a byte array in the following format: [g0, r0, b0, g1, r1, b1, g2, r2, b2 ...] - */ - public void setColors(int channel, byte[] colorData) - { - this.setColors((byte)channel, colorData); - } - - /** - * Send a packet of data to LEDs - * - * @param channel Channel (0 - R, 1 - G, 2 - B) - * @param colorData Report data must be a byte array in the following format: [g0, r0, b0, g1, r1, b1, g2, r2, b2 ...] - */ - public void setColors(byte channel, byte[] colorData) - { - byte leds = this.determineMaxLeds(colorData.length); - byte[] data = new byte[leds * 3 + 2]; - - data[0] = this.determineReportId(colorData.length); - data[1] = channel; - - - for (int i = 0; i < Math.min(colorData.length, data.length - 2); i++) - { - data[i + 2] = colorData[i]; - } - - for (int i = colorData.length + 2; i < data.length; i++) - { - data[i] = 0; - } - - try { - device.sendFeatureReport(data); - } catch (Exception e) { - e.printStackTrace(); - } - } - - /** - * Set the mode of BlinkStick Pro as int - * - * @param mode 0 - Normal, 1 - Inverse, 2 - WS2812, 3 - WS2812 mirror - */ - public void setMode(int mode) - { - this.setMode((byte)mode); - } - - /** - * Set the mode of BlinkStick Pro as byte - * - * @param mode 0 - Normal, 1 - Inverse, 2 - WS2812, 3 - WS2812 mirror - */ - public void setMode(byte mode) - { - try { - device.sendFeatureReport(new byte[] {4, mode}); - } catch (Exception e) { - e.printStackTrace(); - } - } - - /** - * Get the mode of BlinkStick Pro - * - * @return 0 - Normal, 1 - Inverse, 2 - WS2812, 3 - WS2812 mirror - */ - public byte getMode() - { - byte[] data = new byte[2]; - data[0] = 4;// First byte is ReportID - - try { - int read = device.getFeatureReport(data); - if (read > 0) { - return data[1]; - } - } catch (Exception e) { - } - - return -1; - } - -} +/** + * ##library.name## + * ##library.sentence## + * ##library.url## + * + * Copyright ##copyright## ##author## + * All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * @author ##author## + * @modified ##date## + * @version ##library.prettyVersion## (##library.version##) + */ + +package blinkstick; + +import com.codeminders.hidapi.HIDDeviceInfo; +import com.codeminders.hidapi.HIDManager; +import com.codeminders.hidapi.HIDDevice; +import java.util.Hashtable; +import java.util.Random; +import java.util.List; +import java.util.ArrayList; + +/** + * This is a template class and can be used to start a new processing library or + * tool. Make sure you rename this class as well as the name of the example + * package 'template' to your own library or tool naming convention. + * + * @example Hello + * + * (the tag @example followed by the name of an example included in + * folder 'examples' will automatically include the example in the + * javadoc.) + * + */ + +/** + * @author Arvydas + * + */ +public class BlinkStick { + + public final static String VERSION = "##library.prettyVersion##"; + + private static Boolean Initialized = false; + + private static final Hashtable COLORS = new Hashtable() { + /** + * + */ + private static final long serialVersionUID = 1L; + + { + put("aqua", "#00ffff"); + put("aliceblue", "#f0f8ff"); + put("antiquewhite", "#faebd7"); + put("black", "#000000"); + put("blue", "#0000ff"); + put("cyan", "#00ffff"); + put("darkblue", "#00008b"); + put("darkcyan", "#008b8b"); + put("darkgreen", "#006400"); + put("darkturquoise", "#00ced1"); + put("deepskyblue", "#00bfff"); + put("green", "#008000"); + put("lime", "#00ff00"); + put("mediumblue", "#0000cd"); + put("mediumspringgreen", "#00fa9a"); + put("navy", "#000080"); + put("springgreen", "#00ff7f"); + put("teal", "#008080"); + put("midnightblue", "#191970"); + put("dodgerblue", "#1e90ff"); + put("lightseagreen", "#20b2aa"); + put("forestgreen", "#228b22"); + put("seagreen", "#2e8b57"); + put("darkslategray", "#2f4f4f"); + put("darkslategrey", "#2f4f4f"); + put("limegreen", "#32cd32"); + put("mediumseagreen", "#3cb371"); + put("turquoise", "#40e0d0"); + put("royalblue", "#4169e1"); + put("steelblue", "#4682b4"); + put("darkslateblue", "#483d8b"); + put("mediumturquoise", "#48d1cc"); + put("indigo", "#4b0082"); + put("darkolivegreen", "#556b2f"); + put("cadetblue", "#5f9ea0"); + put("cornflowerblue", "#6495ed"); + put("mediumaquamarine", "#66cdaa"); + put("dimgray", "#696969"); + put("dimgrey", "#696969"); + put("slateblue", "#6a5acd"); + put("olivedrab", "#6b8e23"); + put("slategray", "#708090"); + put("slategrey", "#708090"); + put("lightslategray", "#778899"); + put("lightslategrey", "#778899"); + put("mediumslateblue", "#7b68ee"); + put("lawngreen", "#7cfc00"); + put("aquamarine", "#7fffd4"); + put("chartreuse", "#7fff00"); + put("gray", "#808080"); + put("grey", "#808080"); + put("maroon", "#800000"); + put("olive", "#808000"); + put("purple", "#800080"); + put("lightskyblue", "#87cefa"); + put("skyblue", "#87ceeb"); + put("blueviolet", "#8a2be2"); + put("darkmagenta", "#8b008b"); + put("darkred", "#8b0000"); + put("saddlebrown", "#8b4513"); + put("darkseagreen", "#8fbc8f"); + put("lightgreen", "#90ee90"); + put("mediumpurple", "#9370db"); + put("darkviolet", "#9400d3"); + put("palegreen", "#98fb98"); + put("darkorchid", "#9932cc"); + put("yellowgreen", "#9acd32"); + put("sienna", "#a0522d"); + put("brown", "#a52a2a"); + put("darkgray", "#a9a9a9"); + put("darkgrey", "#a9a9a9"); + put("greenyellow", "#adff2f"); + put("lightblue", "#add8e6"); + put("paleturquoise", "#afeeee"); + put("lightsteelblue", "#b0c4de"); + put("powderblue", "#b0e0e6"); + put("firebrick", "#b22222"); + put("darkgoldenrod", "#b8860b"); + put("mediumorchid", "#ba55d3"); + put("rosybrown", "#bc8f8f"); + put("darkkhaki", "#bdb76b"); + put("silver", "#c0c0c0"); + put("mediumvioletred", "#c71585"); + put("indianred", "#cd5c5c"); + put("peru", "#cd853f"); + put("chocolate", "#d2691e"); + put("tan", "#d2b48c"); + put("lightgray", "#d3d3d3"); + put("lightgrey", "#d3d3d3"); + put("thistle", "#d8bfd8"); + put("goldenrod", "#daa520"); + put("orchid", "#da70d6"); + put("palevioletred", "#db7093"); + put("crimson", "#dc143c"); + put("gainsboro", "#dcdcdc"); + put("plum", "#dda0dd"); + put("burlywood", "#deb887"); + put("lightcyan", "#e0ffff"); + put("lavender", "#e6e6fa"); + put("darksalmon", "#e9967a"); + put("palegoldenrod", "#eee8aa"); + put("violet", "#ee82ee"); + put("azure", "#f0ffff"); + put("honeydew", "#f0fff0"); + put("khaki", "#f0e68c"); + put("lightcoral", "#f08080"); + put("sandybrown", "#f4a460"); + put("beige", "#f5f5dc"); + put("mintcream", "#f5fffa"); + put("wheat", "#f5deb3"); + put("whitesmoke", "#f5f5f5"); + put("ghostwhite", "#f8f8ff"); + put("lightgoldenrodyellow", "#fafad2"); + put("linen", "#faf0e6"); + put("salmon", "#fa8072"); + put("oldlace", "#fdf5e6"); + put("bisque", "#ffe4c4"); + put("blanchedalmond", "#ffebcd"); + put("coral", "#ff7f50"); + put("cornsilk", "#fff8dc"); + put("darkorange", "#ff8c00"); + put("deeppink", "#ff1493"); + put("floralwhite", "#fffaf0"); + put("fuchsia", "#ff00ff"); + put("gold", "#ffd700"); + put("hotpink", "#ff69b4"); + put("ivory", "#fffff0"); + put("lavenderblush", "#fff0f5"); + put("lemonchiffon", "#fffacd"); + put("lightpink", "#ffb6c1"); + put("lightsalmon", "#ffa07a"); + put("lightyellow", "#ffffe0"); + put("magenta", "#ff00ff"); + put("mistyrose", "#ffe4e1"); + put("moccasin", "#ffe4b5"); + put("navajowhite", "#ffdead"); + put("orange", "#ffa500"); + put("orangered", "#ff4500"); + put("papayawhip", "#ffefd5"); + put("peachpuff", "#ffdab9"); + put("pink", "#ffc0cb"); + put("red", "#ff0000"); + put("seashell", "#fff5ee"); + put("snow", "#fffafa"); + put("tomato", "#ff6347"); + put("white", "#ffffff"); + put("yellow", "#ffff00"); + } + }; + + /** + * BlinkStick vendor ID + */ + public final static int VENDOR_ID = 0x20a0; + + /** + * BlinkStick product ID + */ + public final static int PRODUCT_ID = 0x41e5; + + /** + * HID device object to communicate directly with BlinkStick + */ + private HIDDevice device = null; + + /** + * return the version of the library. + * + * @return String + */ + public static String version() { + return VERSION; + } + + /** + * Assign HIDDevice + * + * @param device HID device object to communicate directly with BlinkStick + */ + private void setDevice(HIDDevice device) { + this.device = device; + } + + /** + * Load hidapi library based on the OS. This function is automatically called whenever + * a search for BlinkStick is performed for the first time. + */ + public static void Initialize() { + if (!Initialized) { + Initialized = true; + + com.codeminders.hidapi.ClassPathLibraryLoader + .loadNativeHIDLibrary(); + } + } + + /** + * Find first BlinkStick connected to the computer + * + * @return BlinkStick object or null if no BlinkSticks are connected + */ + public static BlinkStick findFirst() { + Initialize(); + + HIDDeviceInfo[] infos = findAllDescriptors(); + + if (infos.length > 0) { + BlinkStick result = new BlinkStick(); + try { + result.setDevice(infos[0].open()); + return result; + } catch (Exception e) { + } + } + return null; + } + + /** Find BlinkStick by serial number + * + * @param serial The serial number to search + * + * @return BlinkStick object or null if device was not found + */ + public static BlinkStick findBySerial(String serial) { + Initialize(); + + HIDDeviceInfo[] infos = findAllDescriptors(); + + for (HIDDeviceInfo info : infos) { + if (info.getSerial_number().equals(serial)) { + BlinkStick result = new BlinkStick(); + try { + result.setDevice(infos[0].open()); + return result; + } catch (Exception e) { + } + } + } + + return null; + } + + /** + * Find all BlinkStick HIDDeviceInfo descriptions connected to the computer + * + * @return an array of HIDDeviceInfo objects with VID and PID matching BlinkStick + */ + private static HIDDeviceInfo[] findAllDescriptors() { + Initialize(); + + List blinkstickList = new ArrayList(); + + try { + HIDManager hidManager = HIDManager.getInstance(); + + HIDDeviceInfo[] infos = hidManager.listDevices(); + + for (HIDDeviceInfo info : infos) { + if (info.getVendor_id() == VENDOR_ID + && info.getProduct_id() == PRODUCT_ID) { + blinkstickList.add(info); + } + } + } catch (Exception e) { + } + + return blinkstickList.toArray(new HIDDeviceInfo[blinkstickList.size()]); + } + + /** + * Find all BlinkSticks connected to the computer + * + * @return an array of BlinkStick objects + */ + public static BlinkStick[] findAll() { + List blinkstickList = new ArrayList(); + + HIDDeviceInfo[] infos = findAllDescriptors(); + + for (HIDDeviceInfo info : infos) { + BlinkStick blinkstick = new BlinkStick(); + try { + blinkstick.setDevice(info.open()); + blinkstickList.add(blinkstick); + } catch (Exception e) { + } + } + + return blinkstickList.toArray(new BlinkStick[blinkstickList.size()]); + } + + /** + * Set the color of the device with separate r, g and b int values. + * The values are automatically converted to byte values + * + * @param r red int color value 0..255 + * @param g gree int color value 0..255 + * @param b blue int color value 0..255 + */ + public void setColor(int r, int g, int b) { + this.setColor((byte) r, (byte) g, (byte) b); + } + + /** + * Set the color of the device with separate r, g and b byte values + * + * @param r red byte color value 0..255 + * @param g gree byte color value 0..255 + * @param b blue byte color value 0..255 + */ + public void setColor(byte r, byte g, byte b) { + try { + device.sendFeatureReport(new byte[] {1, r, g, b}); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * Set indexed color of the device with separate r, g and b byte values for channel and LED index + * + * @param channel Channel (0 - R, 1 - G, 2 - B) + * @param index Index of the LED + * @param r red int color value 0..255 + * @param g gree int color value 0..255 + * @param b blue int color value 0..255 + */ + public void setIndexedColor(int channel, int index, int r, int g, int b) { + this.setIndexedColor((byte)channel, (byte)index, (byte)r, (byte)g, (byte)b); + } + + /** + * Set indexed color of the device with separate r, g and b byte values for channel and LED index + * + * @param channel Channel (0 - R, 1 - G, 2 - B) + * @param index Index of the LED + * @param r red byte color value 0..255 + * @param g gree byte color value 0..255 + * @param b blue byte color value 0..255 + */ + public void setIndexedColor(byte channel, byte index, byte r, byte g, byte b) { + try { + device.sendFeatureReport(new byte[] {5, channel, index, r, g, b}); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * Set the indexed color of BlinkStick Pro with Processing color value + * + * @param channel Channel (0 - R, 1 - G, 2 - B) + * @param index Index of the LED + * @param value color as int + */ + public void setIndexedColor(int channel, int index, int value) { + int r = (value >> 16) & 0xFF; + int g = (value >> 8) & 0xFF; + int b = value & 0xFF; + + this.setIndexedColor(channel, index, r, g, b); + } + + /** + * Set the indexed color of BlinkStick Pro with Processing color value for channel 0 + * + * @param index Index of the LED + * @param value color as int + */ + public void setIndexedColor(int index, int value) { + int r = (value >> 16) & 0xFF; + int g = (value >> 8) & 0xFF; + int b = value & 0xFF; + + this.setIndexedColor(0, index, r, g, b); + } + + /** + * Set the color of the device with Processing color value + * + * @param value color as int + */ + public void setColor(int value) { + int r = (value >> 16) & 0xFF; + int g = (value >> 8) & 0xFF; + int b = value & 0xFF; + + this.setColor(r, g, b); + } + + /** + * Set the color of the device with string value + * + * @param value this can either be a named color "red", "green", "blue" and etc. + * or a hex color in #rrggbb format + */ + public void setColor(String value) { + if (COLORS.containsKey(value)) { + this.setColor(hex2Rgb(COLORS.get(value))); + } else { + this.setColor(hex2Rgb(value)); + } + } + + /** + * Set random color + */ + public void setRandomColor() { + Random random = new Random(); + this.setColor( + random.nextInt(256), + random.nextInt(256), + random.nextInt(256)); + } + + /** + * Turn the device off + */ + public void turnOff() { + this.setColor(0, 0, 0); + } + + + /** + * Convert hex string to color object + * + * @param colorStr Color value as hex string #rrggbb + * + * @return color object + */ + private int hex2Rgb(String colorStr) { + int red = Integer.valueOf(colorStr.substring(1, 3), 16)+ 0; + int green = Integer.valueOf(colorStr.substring(3, 5), 16) + 0; + int blue = Integer.valueOf(colorStr.substring(5, 7), 16) + 0; + + return (255 << 24) | (red << 16) | (green << 8) | blue; + } + + /** + * Get the current color of the device as int + * + * @return The current color of the device as int + */ + public int getColor() { + byte[] data = new byte[33]; + data[0] = 1;// First byte is ReportID + + try { + int read = device.getFeatureReport(data); + if (read > 0) { + return (255 << 24) | (data[1] << 16) | (data[2] << 8) | data[3]; + } + } catch (Exception e) { + } + + return 0; + } + + /** + * Get the current color of the device in #rrggbb format + * + * @return Returns the current color of the device as #rrggbb formated string + */ + public String getColorString() { + int c = getColor(); + + int red = (c >> 16) & 0xFF; + int green = (c >> 8) & 0xFF; + int blue = c & 0xFF; + + return "#" + String.format("%02X", red) + + String.format("%02X", green) + + String.format("%02X", blue); + } + + /** + * Get value of InfoBlocks + * + * @param id InfoBlock id, should be 1 or 2 as only supported info blocks + */ + private String getInfoBlock(int id) { + byte[] data = new byte[33]; + data[0] = (byte) (id + 1); + + String result = ""; + try { + int read = device.getFeatureReport(data); + if (read > 0) { + for (int i = 1; i < data.length; i++) { + if (i == 0) { + break; + } + + result += (char) data[i]; + } + } + } catch (Exception e) { + } + + return result; + } + + /** + * Get value of InfoBlock1 + * + * @return The value of info block 1 + */ + public String getInfoBlock1() { + return getInfoBlock(1); + } + + /** + * Get value of InfoBlock2 + * + * @return The value of info block 2 + */ + public String getInfoBlock2() { + return getInfoBlock(2); + } + + + /** + * Set value for InfoBlocks + * + * @param id InfoBlock id, should be 1 or 2 as only supported info blocks + * @param value The value to be written to the info block + */ + private void setInfoBlock(int id, String value) { + char[] charArray = value.toCharArray(); + byte[] data = new byte[33]; + data[0] = (byte) (id + 1); + + for (int i = 0; i < charArray.length; i++) { + if (i > 32) { + break; + } + + data[i + 1] = (byte) charArray[i]; + } + + try { + device.sendFeatureReport(data); + } catch (Exception e) { + e.printStackTrace(); + + } + } + + /** + * Set value for InfoBlock1 + * + * @param value The value to be written to the info block 1 + */ + public void setInfoBlock1(String value) { + setInfoBlock(1, value); + } + + /** + * Set value for InfoBlock2 + * + * @param value The value to be written to the info block 2 + */ + public void setInfoBlock2(String value) { + setInfoBlock(2, value); + } + + /** + * Get the manufacturer of the device + * + * @return Returns the manufacturer name of the device + */ + public String getManufacturer() { + try { + return device.getManufacturerString(); + } catch (Exception e) { + return ""; + } + } + + /** + * Get the product description of the device + * + * @return Returns the product name of the device. + */ + public String getProduct() { + try { + return device.getProductString(); + } catch (Exception e) { + return ""; + } + } + + + /** + * Get the serial number of the device + * + * @return Returns the serial number of device. + */ + public String getSerial() { + try { + return device.getSerialNumberString(); + } catch (Exception e) { + return ""; + } + } + + + /** + * Determine report id for the amount of data to be sent + * + * @return Returns the report id + */ + private byte determineReportId(int length) { + byte reportId = 9; + //Automatically determine the correct report id to send the data to + if (length <= 8 * 3) + { + reportId = 6; + } + else if (length <= 16 * 3) + { + reportId = 7; + } + else if (length <= 32 * 3) + { + reportId = 8; + } + else if (length <= 64 * 3) + { + reportId = 9; + } + else if (length <= 128 * 3) + { + reportId = 10; + } + + return reportId; + } + + /** + * Determine the adjusted maximum amount of LED for the report + * + * @return Returns the adjusted amount of LED data + */ + private byte determineMaxLeds(int length) { + byte maxLeds = 64; + //Automatically determine the correct report id to send the data to + if (length <= 8 * 3) + { + maxLeds = 8; + } + else if (length <= 16 * 3) + { + maxLeds = 16; + } + else if (length <= 32 * 3) + { + maxLeds = 32; + } + else if (length <= 64 * 3) + { + maxLeds = 64; + } + else if (length <= 128 * 3) + { + maxLeds = 64; + } + + return maxLeds; + } + + /** + * Send a packet of data to LEDs on channel 0 (R) + * + * @param colorData Report data must be a byte array in the following format: [g0, r0, b0, g1, r1, b1, g2, r2, b2 ...] + */ + public void setColors(byte[] colorData) + { + this.setColors((byte)0, colorData); + } + + /** + * Send a packet of data to LEDs + * + * @param channel Channel (0 - R, 1 - G, 2 - B) + * @param colorData Report data must be a byte array in the following format: [g0, r0, b0, g1, r1, b1, g2, r2, b2 ...] + */ + public void setColors(int channel, byte[] colorData) + { + this.setColors((byte)channel, colorData); + } + + /** + * Send a packet of data to LEDs + * + * @param channel Channel (0 - R, 1 - G, 2 - B) + * @param colorData Report data must be a byte array in the following format: [g0, r0, b0, g1, r1, b1, g2, r2, b2 ...] + */ + public void setColors(byte channel, byte[] colorData) + { + byte leds = this.determineMaxLeds(colorData.length); + byte[] data = new byte[leds * 3 + 2]; + + data[0] = this.determineReportId(colorData.length); + data[1] = channel; + + + for (int i = 0; i < Math.min(colorData.length, data.length - 2); i++) + { + data[i + 2] = colorData[i]; + } + + for (int i = colorData.length + 2; i < data.length; i++) + { + data[i] = 0; + } + + try { + device.sendFeatureReport(data); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * Set the mode of BlinkStick Pro as int + * + * @param mode 0 - Normal, 1 - Inverse, 2 - WS2812, 3 - WS2812 mirror + */ + public void setMode(int mode) + { + this.setMode((byte)mode); + } + + /** + * Set the mode of BlinkStick Pro as byte + * + * @param mode 0 - Normal, 1 - Inverse, 2 - WS2812, 3 - WS2812 mirror + */ + public void setMode(byte mode) + { + try { + device.sendFeatureReport(new byte[] {4, mode}); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * Get the mode of BlinkStick Pro + * + * @return 0 - Normal, 1 - Inverse, 2 - WS2812, 3 - WS2812 mirror + */ + public byte getMode() + { + byte[] data = new byte[2]; + data[0] = 4;// First byte is ReportID + + try { + int read = device.getFeatureReport(data); + if (read > 0) { + return data[1]; + } + } catch (Exception e) { + } + + return -1; + } + +} From 8bf4aec1dd3d7a53cfa77eae4f889fc9f6ada647 Mon Sep 17 00:00:00 2001 From: Zachary Browning Date: Tue, 18 Aug 2015 15:04:59 -0400 Subject: [PATCH 2/7] Exceptions should be thrown. --- src/blinkstick/BlinkStick.java | 227 +++++++++++++++------------------ 1 file changed, 104 insertions(+), 123 deletions(-) diff --git a/src/blinkstick/BlinkStick.java b/src/blinkstick/BlinkStick.java index 5be917e..594545c 100644 --- a/src/blinkstick/BlinkStick.java +++ b/src/blinkstick/BlinkStick.java @@ -41,6 +41,8 @@ import com.codeminders.hidapi.HIDDeviceInfo; import com.codeminders.hidapi.HIDManager; import com.codeminders.hidapi.HIDDevice; + +import java.io.IOException; import java.util.Hashtable; import java.util.Random; import java.util.List; @@ -276,20 +278,20 @@ public static void Initialize() { * Find first BlinkStick connected to the computer * * @return BlinkStick object or null if no BlinkSticks are connected + * @throws IOException */ - public static BlinkStick findFirst() { + public static BlinkStick findFirst() throws IOException { Initialize(); HIDDeviceInfo[] infos = findAllDescriptors(); if (infos.length > 0) { BlinkStick result = new BlinkStick(); - try { - result.setDevice(infos[0].open()); - return result; - } catch (Exception e) { - } + + result.setDevice(infos[0].open()); + return result; } + return null; } @@ -298,8 +300,9 @@ public static BlinkStick findFirst() { * @param serial The serial number to search * * @return BlinkStick object or null if device was not found + * @throws IOException */ - public static BlinkStick findBySerial(String serial) { + public static BlinkStick findBySerial(String serial) throws IOException { Initialize(); HIDDeviceInfo[] infos = findAllDescriptors(); @@ -307,11 +310,9 @@ public static BlinkStick findBySerial(String serial) { for (HIDDeviceInfo info : infos) { if (info.getSerial_number().equals(serial)) { BlinkStick result = new BlinkStick(); - try { - result.setDevice(infos[0].open()); - return result; - } catch (Exception e) { - } + + result.setDevice(infos[0].open()); + return result; } } @@ -322,24 +323,22 @@ public static BlinkStick findBySerial(String serial) { * Find all BlinkStick HIDDeviceInfo descriptions connected to the computer * * @return an array of HIDDeviceInfo objects with VID and PID matching BlinkStick + * @throws IOException */ - private static HIDDeviceInfo[] findAllDescriptors() { + private static HIDDeviceInfo[] findAllDescriptors() throws IOException { Initialize(); List blinkstickList = new ArrayList(); - try { - HIDManager hidManager = HIDManager.getInstance(); + HIDManager hidManager = HIDManager.getInstance(); - HIDDeviceInfo[] infos = hidManager.listDevices(); + HIDDeviceInfo[] infos = hidManager.listDevices(); - for (HIDDeviceInfo info : infos) { - if (info.getVendor_id() == VENDOR_ID - && info.getProduct_id() == PRODUCT_ID) { - blinkstickList.add(info); - } + for (HIDDeviceInfo info : infos) { + if (info.getVendor_id() == VENDOR_ID + && info.getProduct_id() == PRODUCT_ID) { + blinkstickList.add(info); } - } catch (Exception e) { } return blinkstickList.toArray(new HIDDeviceInfo[blinkstickList.size()]); @@ -349,19 +348,18 @@ private static HIDDeviceInfo[] findAllDescriptors() { * Find all BlinkSticks connected to the computer * * @return an array of BlinkStick objects + * @throws IOException */ - public static BlinkStick[] findAll() { + public static BlinkStick[] findAll() throws IOException { List blinkstickList = new ArrayList(); HIDDeviceInfo[] infos = findAllDescriptors(); for (HIDDeviceInfo info : infos) { BlinkStick blinkstick = new BlinkStick(); - try { - blinkstick.setDevice(info.open()); - blinkstickList.add(blinkstick); - } catch (Exception e) { - } + + blinkstick.setDevice(info.open()); + blinkstickList.add(blinkstick); } return blinkstickList.toArray(new BlinkStick[blinkstickList.size()]); @@ -374,8 +372,9 @@ public static BlinkStick[] findAll() { * @param r red int color value 0..255 * @param g gree int color value 0..255 * @param b blue int color value 0..255 + * @throws IOException */ - public void setColor(int r, int g, int b) { + public void setColor(int r, int g, int b) throws IOException { this.setColor((byte) r, (byte) g, (byte) b); } @@ -385,13 +384,10 @@ public void setColor(int r, int g, int b) { * @param r red byte color value 0..255 * @param g gree byte color value 0..255 * @param b blue byte color value 0..255 + * @throws IOException */ - public void setColor(byte r, byte g, byte b) { - try { - device.sendFeatureReport(new byte[] {1, r, g, b}); - } catch (Exception e) { - e.printStackTrace(); - } + public void setColor(byte r, byte g, byte b) throws IOException { + device.sendFeatureReport(new byte[] {1, r, g, b}); } /** @@ -402,8 +398,9 @@ public void setColor(byte r, byte g, byte b) { * @param r red int color value 0..255 * @param g gree int color value 0..255 * @param b blue int color value 0..255 + * @throws IOException */ - public void setIndexedColor(int channel, int index, int r, int g, int b) { + public void setIndexedColor(int channel, int index, int r, int g, int b) throws IOException { this.setIndexedColor((byte)channel, (byte)index, (byte)r, (byte)g, (byte)b); } @@ -415,13 +412,11 @@ public void setIndexedColor(int channel, int index, int r, int g, int b) { * @param r red byte color value 0..255 * @param g gree byte color value 0..255 * @param b blue byte color value 0..255 + * @throws IOException */ - public void setIndexedColor(byte channel, byte index, byte r, byte g, byte b) { - try { - device.sendFeatureReport(new byte[] {5, channel, index, r, g, b}); - } catch (Exception e) { - e.printStackTrace(); - } + public void setIndexedColor(byte channel, byte index, byte r, byte g, byte b) throws IOException { + device.sendFeatureReport(new byte[] {5, channel, index, r, g, b}); + } /** @@ -430,8 +425,9 @@ public void setIndexedColor(byte channel, byte index, byte r, byte g, byte b) { * @param channel Channel (0 - R, 1 - G, 2 - B) * @param index Index of the LED * @param value color as int + * @throws IOException */ - public void setIndexedColor(int channel, int index, int value) { + public void setIndexedColor(int channel, int index, int value) throws IOException { int r = (value >> 16) & 0xFF; int g = (value >> 8) & 0xFF; int b = value & 0xFF; @@ -444,8 +440,9 @@ public void setIndexedColor(int channel, int index, int value) { * * @param index Index of the LED * @param value color as int + * @throws IOException */ - public void setIndexedColor(int index, int value) { + public void setIndexedColor(int index, int value) throws IOException { int r = (value >> 16) & 0xFF; int g = (value >> 8) & 0xFF; int b = value & 0xFF; @@ -457,8 +454,9 @@ public void setIndexedColor(int index, int value) { * Set the color of the device with Processing color value * * @param value color as int + * @throws IOException */ - public void setColor(int value) { + public void setColor(int value) throws IOException { int r = (value >> 16) & 0xFF; int g = (value >> 8) & 0xFF; int b = value & 0xFF; @@ -471,8 +469,9 @@ public void setColor(int value) { * * @param value this can either be a named color "red", "green", "blue" and etc. * or a hex color in #rrggbb format + * @throws IOException */ - public void setColor(String value) { + public void setColor(String value) throws IOException { if (COLORS.containsKey(value)) { this.setColor(hex2Rgb(COLORS.get(value))); } else { @@ -482,8 +481,9 @@ public void setColor(String value) { /** * Set random color + * @throws IOException */ - public void setRandomColor() { + public void setRandomColor() throws IOException { Random random = new Random(); this.setColor( random.nextInt(256), @@ -493,12 +493,12 @@ public void setRandomColor() { /** * Turn the device off + * @throws IOException */ - public void turnOff() { + public void turnOff() throws IOException { this.setColor(0, 0, 0); } - /** * Convert hex string to color object * @@ -518,17 +518,15 @@ private int hex2Rgb(String colorStr) { * Get the current color of the device as int * * @return The current color of the device as int + * @throws IOException */ - public int getColor() { + public int getColor() throws IOException { byte[] data = new byte[33]; data[0] = 1;// First byte is ReportID - try { - int read = device.getFeatureReport(data); - if (read > 0) { - return (255 << 24) | (data[1] << 16) | (data[2] << 8) | data[3]; - } - } catch (Exception e) { + int read = device.getFeatureReport(data); + if (read > 0) { + return (255 << 24) | (data[1] << 16) | (data[2] << 8) | data[3]; } return 0; @@ -538,8 +536,9 @@ public int getColor() { * Get the current color of the device in #rrggbb format * * @return Returns the current color of the device as #rrggbb formated string + * @throws IOException */ - public String getColorString() { + public String getColorString() throws IOException { int c = getColor(); int red = (c >> 16) & 0xFF; @@ -555,24 +554,23 @@ public String getColorString() { * Get value of InfoBlocks * * @param id InfoBlock id, should be 1 or 2 as only supported info blocks + * @throws IOException */ - private String getInfoBlock(int id) { + private String getInfoBlock(int id) throws IOException { byte[] data = new byte[33]; data[0] = (byte) (id + 1); String result = ""; - try { - int read = device.getFeatureReport(data); - if (read > 0) { - for (int i = 1; i < data.length; i++) { - if (i == 0) { - break; - } - - result += (char) data[i]; + + int read = device.getFeatureReport(data); + if (read > 0) { + for (int i = 1; i < data.length; i++) { + if (i == 0) { + break; } + + result += (char) data[i]; } - } catch (Exception e) { } return result; @@ -582,8 +580,9 @@ private String getInfoBlock(int id) { * Get value of InfoBlock1 * * @return The value of info block 1 + * @throws IOException */ - public String getInfoBlock1() { + public String getInfoBlock1() throws IOException { return getInfoBlock(1); } @@ -591,19 +590,20 @@ public String getInfoBlock1() { * Get value of InfoBlock2 * * @return The value of info block 2 + * @throws IOException */ - public String getInfoBlock2() { + public String getInfoBlock2() throws IOException { return getInfoBlock(2); } - /** * Set value for InfoBlocks * * @param id InfoBlock id, should be 1 or 2 as only supported info blocks * @param value The value to be written to the info block + * @throws IOException */ - private void setInfoBlock(int id, String value) { + private void setInfoBlock(int id, String value) throws IOException { char[] charArray = value.toCharArray(); byte[] data = new byte[33]; data[0] = (byte) (id + 1); @@ -616,20 +616,16 @@ private void setInfoBlock(int id, String value) { data[i + 1] = (byte) charArray[i]; } - try { - device.sendFeatureReport(data); - } catch (Exception e) { - e.printStackTrace(); - - } + device.sendFeatureReport(data); } /** * Set value for InfoBlock1 * * @param value The value to be written to the info block 1 + * @throws IOException */ - public void setInfoBlock1(String value) { + public void setInfoBlock1(String value) throws IOException { setInfoBlock(1, value); } @@ -637,8 +633,9 @@ public void setInfoBlock1(String value) { * Set value for InfoBlock2 * * @param value The value to be written to the info block 2 + * @throws IOException */ - public void setInfoBlock2(String value) { + public void setInfoBlock2(String value) throws IOException { setInfoBlock(2, value); } @@ -646,43 +643,32 @@ public void setInfoBlock2(String value) { * Get the manufacturer of the device * * @return Returns the manufacturer name of the device + * @throws IOException */ - public String getManufacturer() { - try { - return device.getManufacturerString(); - } catch (Exception e) { - return ""; - } + public String getManufacturer() throws IOException { + return device.getManufacturerString(); } /** * Get the product description of the device * * @return Returns the product name of the device. + * @throws IOException */ - public String getProduct() { - try { - return device.getProductString(); - } catch (Exception e) { - return ""; - } + public String getProduct() throws IOException { + return device.getProductString(); } - /** * Get the serial number of the device * * @return Returns the serial number of device. + * @throws IOException */ - public String getSerial() { - try { - return device.getSerialNumberString(); - } catch (Exception e) { - return ""; - } + public String getSerial() throws IOException { + return device.getSerialNumberString(); } - /** * Determine report id for the amount of data to be sent * @@ -751,8 +737,9 @@ else if (length <= 128 * 3) * Send a packet of data to LEDs on channel 0 (R) * * @param colorData Report data must be a byte array in the following format: [g0, r0, b0, g1, r1, b1, g2, r2, b2 ...] + * @throws IOException */ - public void setColors(byte[] colorData) + public void setColors(byte[] colorData) throws IOException { this.setColors((byte)0, colorData); } @@ -762,8 +749,9 @@ public void setColors(byte[] colorData) * * @param channel Channel (0 - R, 1 - G, 2 - B) * @param colorData Report data must be a byte array in the following format: [g0, r0, b0, g1, r1, b1, g2, r2, b2 ...] + * @throws IOException */ - public void setColors(int channel, byte[] colorData) + public void setColors(int channel, byte[] colorData) throws IOException { this.setColors((byte)channel, colorData); } @@ -773,8 +761,9 @@ public void setColors(int channel, byte[] colorData) * * @param channel Channel (0 - R, 1 - G, 2 - B) * @param colorData Report data must be a byte array in the following format: [g0, r0, b0, g1, r1, b1, g2, r2, b2 ...] + * @throws IOException */ - public void setColors(byte channel, byte[] colorData) + public void setColors(byte channel, byte[] colorData) throws IOException { byte leds = this.determineMaxLeds(colorData.length); byte[] data = new byte[leds * 3 + 2]; @@ -793,19 +782,16 @@ public void setColors(byte channel, byte[] colorData) data[i] = 0; } - try { - device.sendFeatureReport(data); - } catch (Exception e) { - e.printStackTrace(); - } + device.sendFeatureReport(data); } /** * Set the mode of BlinkStick Pro as int * * @param mode 0 - Normal, 1 - Inverse, 2 - WS2812, 3 - WS2812 mirror + * @throws IOException */ - public void setMode(int mode) + public void setMode(int mode) throws IOException { this.setMode((byte)mode); } @@ -814,35 +800,30 @@ public void setMode(int mode) * Set the mode of BlinkStick Pro as byte * * @param mode 0 - Normal, 1 - Inverse, 2 - WS2812, 3 - WS2812 mirror + * @throws IOException */ - public void setMode(byte mode) + public void setMode(byte mode) throws IOException { - try { - device.sendFeatureReport(new byte[] {4, mode}); - } catch (Exception e) { - e.printStackTrace(); - } + device.sendFeatureReport(new byte[] {4, mode}); + } /** * Get the mode of BlinkStick Pro * * @return 0 - Normal, 1 - Inverse, 2 - WS2812, 3 - WS2812 mirror + * @throws IOException */ - public byte getMode() + public byte getMode() throws IOException { byte[] data = new byte[2]; data[0] = 4;// First byte is ReportID - try { - int read = device.getFeatureReport(data); - if (read > 0) { - return data[1]; - } - } catch (Exception e) { + int read = device.getFeatureReport(data); + if (read > 0) { + return data[1]; } return -1; } - } From 8889272e7a93a61f12769e11057a13106e01c9b6 Mon Sep 17 00:00:00 2001 From: Zachary Browning Date: Tue, 18 Aug 2015 15:06:11 -0400 Subject: [PATCH 3/7] Consistent method braces. --- src/blinkstick/BlinkStick.java | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/blinkstick/BlinkStick.java b/src/blinkstick/BlinkStick.java index 594545c..b06980f 100644 --- a/src/blinkstick/BlinkStick.java +++ b/src/blinkstick/BlinkStick.java @@ -739,8 +739,7 @@ else if (length <= 128 * 3) * @param colorData Report data must be a byte array in the following format: [g0, r0, b0, g1, r1, b1, g2, r2, b2 ...] * @throws IOException */ - public void setColors(byte[] colorData) throws IOException - { + public void setColors(byte[] colorData) throws IOException { this.setColors((byte)0, colorData); } @@ -751,8 +750,7 @@ public void setColors(byte[] colorData) throws IOException * @param colorData Report data must be a byte array in the following format: [g0, r0, b0, g1, r1, b1, g2, r2, b2 ...] * @throws IOException */ - public void setColors(int channel, byte[] colorData) throws IOException - { + public void setColors(int channel, byte[] colorData) throws IOException { this.setColors((byte)channel, colorData); } @@ -763,8 +761,7 @@ public void setColors(int channel, byte[] colorData) throws IOException * @param colorData Report data must be a byte array in the following format: [g0, r0, b0, g1, r1, b1, g2, r2, b2 ...] * @throws IOException */ - public void setColors(byte channel, byte[] colorData) throws IOException - { + public void setColors(byte channel, byte[] colorData) throws IOException { byte leds = this.determineMaxLeds(colorData.length); byte[] data = new byte[leds * 3 + 2]; @@ -791,8 +788,7 @@ public void setColors(byte channel, byte[] colorData) throws IOException * @param mode 0 - Normal, 1 - Inverse, 2 - WS2812, 3 - WS2812 mirror * @throws IOException */ - public void setMode(int mode) throws IOException - { + public void setMode(int mode) throws IOException { this.setMode((byte)mode); } @@ -802,8 +798,7 @@ public void setMode(int mode) throws IOException * @param mode 0 - Normal, 1 - Inverse, 2 - WS2812, 3 - WS2812 mirror * @throws IOException */ - public void setMode(byte mode) throws IOException - { + public void setMode(byte mode) throws IOException { device.sendFeatureReport(new byte[] {4, mode}); } @@ -814,8 +809,7 @@ public void setMode(byte mode) throws IOException * @return 0 - Normal, 1 - Inverse, 2 - WS2812, 3 - WS2812 mirror * @throws IOException */ - public byte getMode() throws IOException - { + public byte getMode() throws IOException { byte[] data = new byte[2]; data[0] = 4;// First byte is ReportID From 833ef03b807cbb33b9e48077785b9c99ed1b3232 Mon Sep 17 00:00:00 2001 From: Zachary Browning Date: Tue, 18 Aug 2015 15:06:50 -0400 Subject: [PATCH 4/7] Methods marked as static when appropriate. --- src/blinkstick/BlinkStick.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/blinkstick/BlinkStick.java b/src/blinkstick/BlinkStick.java index b06980f..0bbf572 100644 --- a/src/blinkstick/BlinkStick.java +++ b/src/blinkstick/BlinkStick.java @@ -506,7 +506,7 @@ public void turnOff() throws IOException { * * @return color object */ - private int hex2Rgb(String colorStr) { + private static int hex2Rgb(String colorStr) { int red = Integer.valueOf(colorStr.substring(1, 3), 16)+ 0; int green = Integer.valueOf(colorStr.substring(3, 5), 16) + 0; int blue = Integer.valueOf(colorStr.substring(5, 7), 16) + 0; @@ -674,7 +674,7 @@ public String getSerial() throws IOException { * * @return Returns the report id */ - private byte determineReportId(int length) { + private static byte determineReportId(int length) { byte reportId = 9; //Automatically determine the correct report id to send the data to if (length <= 8 * 3) @@ -706,7 +706,7 @@ else if (length <= 128 * 3) * * @return Returns the adjusted amount of LED data */ - private byte determineMaxLeds(int length) { + private static byte determineMaxLeds(int length) { byte maxLeds = 64; //Automatically determine the correct report id to send the data to if (length <= 8 * 3) @@ -762,10 +762,10 @@ public void setColors(int channel, byte[] colorData) throws IOException { * @throws IOException */ public void setColors(byte channel, byte[] colorData) throws IOException { - byte leds = this.determineMaxLeds(colorData.length); + byte leds = BlinkStick.determineMaxLeds(colorData.length); byte[] data = new byte[leds * 3 + 2]; - data[0] = this.determineReportId(colorData.length); + data[0] = BlinkStick.determineReportId(colorData.length); data[1] = channel; From 0ccdbec758676fa42f06765857d1f3fcbd17069b Mon Sep 17 00:00:00 2001 From: Zachary Browning Date: Tue, 18 Aug 2015 15:08:48 -0400 Subject: [PATCH 5/7] Alphabetized methods. --- src/blinkstick/BlinkStick.java | 664 ++++++++++++++++----------------- 1 file changed, 332 insertions(+), 332 deletions(-) diff --git a/src/blinkstick/BlinkStick.java b/src/blinkstick/BlinkStick.java index 0bbf572..ccacdda 100644 --- a/src/blinkstick/BlinkStick.java +++ b/src/blinkstick/BlinkStick.java @@ -239,60 +239,113 @@ public class BlinkStick { public final static int PRODUCT_ID = 0x41e5; /** - * HID device object to communicate directly with BlinkStick - */ - private HIDDevice device = null; - - /** - * return the version of the library. + * Determine the adjusted maximum amount of LED for the report * - * @return String + * @return Returns the adjusted amount of LED data */ - public static String version() { - return VERSION; + private static byte determineMaxLeds(int length) { + byte maxLeds = 64; + //Automatically determine the correct report id to send the data to + if (length <= 8 * 3) + { + maxLeds = 8; + } + else if (length <= 16 * 3) + { + maxLeds = 16; + } + else if (length <= 32 * 3) + { + maxLeds = 32; + } + else if (length <= 64 * 3) + { + maxLeds = 64; + } + else if (length <= 128 * 3) + { + maxLeds = 64; + } + + return maxLeds; } /** - * Assign HIDDevice + * Determine report id for the amount of data to be sent * - * @param device HID device object to communicate directly with BlinkStick + * @return Returns the report id */ - private void setDevice(HIDDevice device) { - this.device = device; + private static byte determineReportId(int length) { + byte reportId = 9; + //Automatically determine the correct report id to send the data to + if (length <= 8 * 3) + { + reportId = 6; + } + else if (length <= 16 * 3) + { + reportId = 7; + } + else if (length <= 32 * 3) + { + reportId = 8; + } + else if (length <= 64 * 3) + { + reportId = 9; + } + else if (length <= 128 * 3) + { + reportId = 10; + } + + return reportId; } /** - * Load hidapi library based on the OS. This function is automatically called whenever - * a search for BlinkStick is performed for the first time. + * Find all BlinkSticks connected to the computer + * + * @return an array of BlinkStick objects + * @throws IOException */ - public static void Initialize() { - if (!Initialized) { - Initialized = true; + public static BlinkStick[] findAll() throws IOException { + List blinkstickList = new ArrayList(); - com.codeminders.hidapi.ClassPathLibraryLoader - .loadNativeHIDLibrary(); + HIDDeviceInfo[] infos = findAllDescriptors(); + + for (HIDDeviceInfo info : infos) { + BlinkStick blinkstick = new BlinkStick(); + + blinkstick.setDevice(info.open()); + blinkstickList.add(blinkstick); } + + return blinkstickList.toArray(new BlinkStick[blinkstickList.size()]); } /** - * Find first BlinkStick connected to the computer + * Find all BlinkStick HIDDeviceInfo descriptions connected to the computer * - * @return BlinkStick object or null if no BlinkSticks are connected + * @return an array of HIDDeviceInfo objects with VID and PID matching BlinkStick * @throws IOException */ - public static BlinkStick findFirst() throws IOException { + private static HIDDeviceInfo[] findAllDescriptors() throws IOException { Initialize(); - HIDDeviceInfo[] infos = findAllDescriptors(); + List blinkstickList = new ArrayList(); - if (infos.length > 0) { - BlinkStick result = new BlinkStick(); + HIDManager hidManager = HIDManager.getInstance(); - result.setDevice(infos[0].open()); - return result; + HIDDeviceInfo[] infos = hidManager.listDevices(); + + for (HIDDeviceInfo info : infos) { + if (info.getVendor_id() == VENDOR_ID + && info.getProduct_id() == PRODUCT_ID) { + blinkstickList.add(info); + } } - return null; + return blinkstickList.toArray(new HIDDeviceInfo[blinkstickList.size()]); } /** Find BlinkStick by serial number @@ -320,247 +373,115 @@ public static BlinkStick findBySerial(String serial) throws IOException { } /** - * Find all BlinkStick HIDDeviceInfo descriptions connected to the computer + * Find first BlinkStick connected to the computer * - * @return an array of HIDDeviceInfo objects with VID and PID matching BlinkStick + * @return BlinkStick object or null if no BlinkSticks are connected * @throws IOException */ - private static HIDDeviceInfo[] findAllDescriptors() throws IOException { + public static BlinkStick findFirst() throws IOException { Initialize(); - List blinkstickList = new ArrayList(); - - HIDManager hidManager = HIDManager.getInstance(); + HIDDeviceInfo[] infos = findAllDescriptors(); - HIDDeviceInfo[] infos = hidManager.listDevices(); + if (infos.length > 0) { + BlinkStick result = new BlinkStick(); - for (HIDDeviceInfo info : infos) { - if (info.getVendor_id() == VENDOR_ID - && info.getProduct_id() == PRODUCT_ID) { - blinkstickList.add(info); - } + result.setDevice(infos[0].open()); + return result; } - return blinkstickList.toArray(new HIDDeviceInfo[blinkstickList.size()]); + return null; } /** - * Find all BlinkSticks connected to the computer + * Convert hex string to color object * - * @return an array of BlinkStick objects - * @throws IOException + * @param colorStr Color value as hex string #rrggbb + * + * @return color object */ - public static BlinkStick[] findAll() throws IOException { - List blinkstickList = new ArrayList(); - - HIDDeviceInfo[] infos = findAllDescriptors(); - - for (HIDDeviceInfo info : infos) { - BlinkStick blinkstick = new BlinkStick(); - - blinkstick.setDevice(info.open()); - blinkstickList.add(blinkstick); - } + private static int hex2Rgb(String colorStr) { + int red = Integer.valueOf(colorStr.substring(1, 3), 16)+ 0; + int green = Integer.valueOf(colorStr.substring(3, 5), 16) + 0; + int blue = Integer.valueOf(colorStr.substring(5, 7), 16) + 0; - return blinkstickList.toArray(new BlinkStick[blinkstickList.size()]); + return (255 << 24) | (red << 16) | (green << 8) | blue; } /** - * Set the color of the device with separate r, g and b int values. - * The values are automatically converted to byte values - * - * @param r red int color value 0..255 - * @param g gree int color value 0..255 - * @param b blue int color value 0..255 - * @throws IOException + * Load hidapi library based on the OS. This function is automatically called whenever + * a search for BlinkStick is performed for the first time. */ - public void setColor(int r, int g, int b) throws IOException { - this.setColor((byte) r, (byte) g, (byte) b); + public static void Initialize() { + if (!Initialized) { + Initialized = true; + + com.codeminders.hidapi.ClassPathLibraryLoader + .loadNativeHIDLibrary(); + } } - /** - * Set the color of the device with separate r, g and b byte values + /** + * return the version of the library. * - * @param r red byte color value 0..255 - * @param g gree byte color value 0..255 - * @param b blue byte color value 0..255 - * @throws IOException + * @return String */ - public void setColor(byte r, byte g, byte b) throws IOException { - device.sendFeatureReport(new byte[] {1, r, g, b}); + public static String version() { + return VERSION; } /** - * Set indexed color of the device with separate r, g and b byte values for channel and LED index - * - * @param channel Channel (0 - R, 1 - G, 2 - B) - * @param index Index of the LED - * @param r red int color value 0..255 - * @param g gree int color value 0..255 - * @param b blue int color value 0..255 - * @throws IOException + * HID device object to communicate directly with BlinkStick */ - public void setIndexedColor(int channel, int index, int r, int g, int b) throws IOException { - this.setIndexedColor((byte)channel, (byte)index, (byte)r, (byte)g, (byte)b); - } + private HIDDevice device = null; /** - * Set indexed color of the device with separate r, g and b byte values for channel and LED index + * Get the current color of the device as int * - * @param channel Channel (0 - R, 1 - G, 2 - B) - * @param index Index of the LED - * @param r red byte color value 0..255 - * @param g gree byte color value 0..255 - * @param b blue byte color value 0..255 + * @return The current color of the device as int * @throws IOException */ - public void setIndexedColor(byte channel, byte index, byte r, byte g, byte b) throws IOException { - device.sendFeatureReport(new byte[] {5, channel, index, r, g, b}); + public int getColor() throws IOException { + byte[] data = new byte[33]; + data[0] = 1;// First byte is ReportID + + int read = device.getFeatureReport(data); + if (read > 0) { + return (255 << 24) | (data[1] << 16) | (data[2] << 8) | data[3]; + } + return 0; } /** - * Set the indexed color of BlinkStick Pro with Processing color value + * Get the current color of the device in #rrggbb format * - * @param channel Channel (0 - R, 1 - G, 2 - B) - * @param index Index of the LED - * @param value color as int + * @return Returns the current color of the device as #rrggbb formated string * @throws IOException */ - public void setIndexedColor(int channel, int index, int value) throws IOException { - int r = (value >> 16) & 0xFF; - int g = (value >> 8) & 0xFF; - int b = value & 0xFF; + public String getColorString() throws IOException { + int c = getColor(); - this.setIndexedColor(channel, index, r, g, b); + int red = (c >> 16) & 0xFF; + int green = (c >> 8) & 0xFF; + int blue = c & 0xFF; + + return "#" + String.format("%02X", red) + + String.format("%02X", green) + + String.format("%02X", blue); } /** - * Set the indexed color of BlinkStick Pro with Processing color value for channel 0 + * Get value of InfoBlocks * - * @param index Index of the LED - * @param value color as int + * @param id InfoBlock id, should be 1 or 2 as only supported info blocks * @throws IOException */ - public void setIndexedColor(int index, int value) throws IOException { - int r = (value >> 16) & 0xFF; - int g = (value >> 8) & 0xFF; - int b = value & 0xFF; + private String getInfoBlock(int id) throws IOException { + byte[] data = new byte[33]; + data[0] = (byte) (id + 1); - this.setIndexedColor(0, index, r, g, b); - } - - /** - * Set the color of the device with Processing color value - * - * @param value color as int - * @throws IOException - */ - public void setColor(int value) throws IOException { - int r = (value >> 16) & 0xFF; - int g = (value >> 8) & 0xFF; - int b = value & 0xFF; - - this.setColor(r, g, b); - } - - /** - * Set the color of the device with string value - * - * @param value this can either be a named color "red", "green", "blue" and etc. - * or a hex color in #rrggbb format - * @throws IOException - */ - public void setColor(String value) throws IOException { - if (COLORS.containsKey(value)) { - this.setColor(hex2Rgb(COLORS.get(value))); - } else { - this.setColor(hex2Rgb(value)); - } - } - - /** - * Set random color - * @throws IOException - */ - public void setRandomColor() throws IOException { - Random random = new Random(); - this.setColor( - random.nextInt(256), - random.nextInt(256), - random.nextInt(256)); - } - - /** - * Turn the device off - * @throws IOException - */ - public void turnOff() throws IOException { - this.setColor(0, 0, 0); - } - - /** - * Convert hex string to color object - * - * @param colorStr Color value as hex string #rrggbb - * - * @return color object - */ - private static int hex2Rgb(String colorStr) { - int red = Integer.valueOf(colorStr.substring(1, 3), 16)+ 0; - int green = Integer.valueOf(colorStr.substring(3, 5), 16) + 0; - int blue = Integer.valueOf(colorStr.substring(5, 7), 16) + 0; - - return (255 << 24) | (red << 16) | (green << 8) | blue; - } - - /** - * Get the current color of the device as int - * - * @return The current color of the device as int - * @throws IOException - */ - public int getColor() throws IOException { - byte[] data = new byte[33]; - data[0] = 1;// First byte is ReportID - - int read = device.getFeatureReport(data); - if (read > 0) { - return (255 << 24) | (data[1] << 16) | (data[2] << 8) | data[3]; - } - - return 0; - } - - /** - * Get the current color of the device in #rrggbb format - * - * @return Returns the current color of the device as #rrggbb formated string - * @throws IOException - */ - public String getColorString() throws IOException { - int c = getColor(); - - int red = (c >> 16) & 0xFF; - int green = (c >> 8) & 0xFF; - int blue = c & 0xFF; - - return "#" + String.format("%02X", red) - + String.format("%02X", green) - + String.format("%02X", blue); - } - - /** - * Get value of InfoBlocks - * - * @param id InfoBlock id, should be 1 or 2 as only supported info blocks - * @throws IOException - */ - private String getInfoBlock(int id) throws IOException { - byte[] data = new byte[33]; - data[0] = (byte) (id + 1); - - String result = ""; + String result = ""; int read = device.getFeatureReport(data); if (read > 0) { @@ -597,140 +518,133 @@ public String getInfoBlock2() throws IOException { } /** - * Set value for InfoBlocks + * Get the manufacturer of the device * - * @param id InfoBlock id, should be 1 or 2 as only supported info blocks - * @param value The value to be written to the info block + * @return Returns the manufacturer name of the device * @throws IOException */ - private void setInfoBlock(int id, String value) throws IOException { - char[] charArray = value.toCharArray(); - byte[] data = new byte[33]; - data[0] = (byte) (id + 1); + public String getManufacturer() throws IOException { + return device.getManufacturerString(); + } - for (int i = 0; i < charArray.length; i++) { - if (i > 32) { - break; - } + /** + * Get the mode of BlinkStick Pro + * + * @return 0 - Normal, 1 - Inverse, 2 - WS2812, 3 - WS2812 mirror + * @throws IOException + */ + public byte getMode() throws IOException { + byte[] data = new byte[2]; + data[0] = 4;// First byte is ReportID - data[i + 1] = (byte) charArray[i]; + int read = device.getFeatureReport(data); + if (read > 0) { + return data[1]; } - device.sendFeatureReport(data); + return -1; } /** - * Set value for InfoBlock1 + * Get the product description of the device * - * @param value The value to be written to the info block 1 + * @return Returns the product name of the device. * @throws IOException */ - public void setInfoBlock1(String value) throws IOException { - setInfoBlock(1, value); + public String getProduct() throws IOException { + return device.getProductString(); } /** - * Set value for InfoBlock2 + * Get the serial number of the device * - * @param value The value to be written to the info block 2 + * @return Returns the serial number of device. * @throws IOException */ - public void setInfoBlock2(String value) throws IOException { - setInfoBlock(2, value); + public String getSerial() throws IOException { + return device.getSerialNumberString(); } /** - * Get the manufacturer of the device + * Set the color of the device with separate r, g and b byte values * - * @return Returns the manufacturer name of the device + * @param r red byte color value 0..255 + * @param g gree byte color value 0..255 + * @param b blue byte color value 0..255 * @throws IOException */ - public String getManufacturer() throws IOException { - return device.getManufacturerString(); + public void setColor(byte r, byte g, byte b) throws IOException { + device.sendFeatureReport(new byte[] {1, r, g, b}); } /** - * Get the product description of the device + * Set the color of the device with Processing color value * - * @return Returns the product name of the device. + * @param value color as int * @throws IOException */ - public String getProduct() throws IOException { - return device.getProductString(); + public void setColor(int value) throws IOException { + int r = (value >> 16) & 0xFF; + int g = (value >> 8) & 0xFF; + int b = value & 0xFF; + + this.setColor(r, g, b); } /** - * Get the serial number of the device + * Set the color of the device with separate r, g and b int values. + * The values are automatically converted to byte values * - * @return Returns the serial number of device. + * @param r red int color value 0..255 + * @param g gree int color value 0..255 + * @param b blue int color value 0..255 * @throws IOException */ - public String getSerial() throws IOException { - return device.getSerialNumberString(); + public void setColor(int r, int g, int b) throws IOException { + this.setColor((byte) r, (byte) g, (byte) b); } /** - * Determine report id for the amount of data to be sent + * Set the color of the device with string value * - * @return Returns the report id + * @param value this can either be a named color "red", "green", "blue" and etc. + * or a hex color in #rrggbb format + * @throws IOException */ - private static byte determineReportId(int length) { - byte reportId = 9; - //Automatically determine the correct report id to send the data to - if (length <= 8 * 3) - { - reportId = 6; - } - else if (length <= 16 * 3) - { - reportId = 7; - } - else if (length <= 32 * 3) - { - reportId = 8; - } - else if (length <= 64 * 3) - { - reportId = 9; - } - else if (length <= 128 * 3) - { - reportId = 10; + public void setColor(String value) throws IOException { + if (COLORS.containsKey(value)) { + this.setColor(hex2Rgb(COLORS.get(value))); + } else { + this.setColor(hex2Rgb(value)); } - - return reportId; } /** - * Determine the adjusted maximum amount of LED for the report + * Send a packet of data to LEDs * - * @return Returns the adjusted amount of LED data + * @param channel Channel (0 - R, 1 - G, 2 - B) + * @param colorData Report data must be a byte array in the following format: [g0, r0, b0, g1, r1, b1, g2, r2, b2 ...] + * @throws IOException */ - private static byte determineMaxLeds(int length) { - byte maxLeds = 64; - //Automatically determine the correct report id to send the data to - if (length <= 8 * 3) - { - maxLeds = 8; - } - else if (length <= 16 * 3) - { - maxLeds = 16; - } - else if (length <= 32 * 3) - { - maxLeds = 32; - } - else if (length <= 64 * 3) + public void setColors(byte channel, byte[] colorData) throws IOException { + byte leds = BlinkStick.determineMaxLeds(colorData.length); + byte[] data = new byte[leds * 3 + 2]; + + data[0] = BlinkStick.determineReportId(colorData.length); + data[1] = channel; + + + for (int i = 0; i < Math.min(colorData.length, data.length - 2); i++) { - maxLeds = 64; + data[i + 2] = colorData[i]; } - else if (length <= 128 * 3) + + for (int i = colorData.length + 2; i < data.length; i++) { - maxLeds = 64; + data[i] = 0; } - return maxLeds; + device.sendFeatureReport(data); } /** @@ -755,41 +669,115 @@ public void setColors(int channel, byte[] colorData) throws IOException { } /** - * Send a packet of data to LEDs + * Assign HIDDevice + * + * @param device HID device object to communicate directly with BlinkStick + */ + private void setDevice(HIDDevice device) { + this.device = device; + } + + /** + * Set indexed color of the device with separate r, g and b byte values for channel and LED index * * @param channel Channel (0 - R, 1 - G, 2 - B) - * @param colorData Report data must be a byte array in the following format: [g0, r0, b0, g1, r1, b1, g2, r2, b2 ...] + * @param index Index of the LED + * @param r red byte color value 0..255 + * @param g gree byte color value 0..255 + * @param b blue byte color value 0..255 * @throws IOException */ - public void setColors(byte channel, byte[] colorData) throws IOException { - byte leds = BlinkStick.determineMaxLeds(colorData.length); - byte[] data = new byte[leds * 3 + 2]; + public void setIndexedColor(byte channel, byte index, byte r, byte g, byte b) throws IOException { + device.sendFeatureReport(new byte[] {5, channel, index, r, g, b}); - data[0] = BlinkStick.determineReportId(colorData.length); - data[1] = channel; + } + /** + * Set the indexed color of BlinkStick Pro with Processing color value for channel 0 + * + * @param index Index of the LED + * @param value color as int + * @throws IOException + */ + public void setIndexedColor(int index, int value) throws IOException { + int r = (value >> 16) & 0xFF; + int g = (value >> 8) & 0xFF; + int b = value & 0xFF; - for (int i = 0; i < Math.min(colorData.length, data.length - 2); i++) - { - data[i + 2] = colorData[i]; - } + this.setIndexedColor(0, index, r, g, b); + } - for (int i = colorData.length + 2; i < data.length; i++) - { - data[i] = 0; + /** + * Set the indexed color of BlinkStick Pro with Processing color value + * + * @param channel Channel (0 - R, 1 - G, 2 - B) + * @param index Index of the LED + * @param value color as int + * @throws IOException + */ + public void setIndexedColor(int channel, int index, int value) throws IOException { + int r = (value >> 16) & 0xFF; + int g = (value >> 8) & 0xFF; + int b = value & 0xFF; + + this.setIndexedColor(channel, index, r, g, b); + } + + /** + * Set indexed color of the device with separate r, g and b byte values for channel and LED index + * + * @param channel Channel (0 - R, 1 - G, 2 - B) + * @param index Index of the LED + * @param r red int color value 0..255 + * @param g gree int color value 0..255 + * @param b blue int color value 0..255 + * @throws IOException + */ + public void setIndexedColor(int channel, int index, int r, int g, int b) throws IOException { + this.setIndexedColor((byte)channel, (byte)index, (byte)r, (byte)g, (byte)b); + } + + /** + * Set value for InfoBlocks + * + * @param id InfoBlock id, should be 1 or 2 as only supported info blocks + * @param value The value to be written to the info block + * @throws IOException + */ + private void setInfoBlock(int id, String value) throws IOException { + char[] charArray = value.toCharArray(); + byte[] data = new byte[33]; + data[0] = (byte) (id + 1); + + for (int i = 0; i < charArray.length; i++) { + if (i > 32) { + break; + } + + data[i + 1] = (byte) charArray[i]; } device.sendFeatureReport(data); } /** - * Set the mode of BlinkStick Pro as int + * Set value for InfoBlock1 * - * @param mode 0 - Normal, 1 - Inverse, 2 - WS2812, 3 - WS2812 mirror + * @param value The value to be written to the info block 1 * @throws IOException */ - public void setMode(int mode) throws IOException { - this.setMode((byte)mode); + public void setInfoBlock1(String value) throws IOException { + setInfoBlock(1, value); + } + + /** + * Set value for InfoBlock2 + * + * @param value The value to be written to the info block 2 + * @throws IOException + */ + public void setInfoBlock2(String value) throws IOException { + setInfoBlock(2, value); } /** @@ -804,20 +792,32 @@ public void setMode(byte mode) throws IOException { } /** - * Get the mode of BlinkStick Pro + * Set the mode of BlinkStick Pro as int * - * @return 0 - Normal, 1 - Inverse, 2 - WS2812, 3 - WS2812 mirror + * @param mode 0 - Normal, 1 - Inverse, 2 - WS2812, 3 - WS2812 mirror * @throws IOException */ - public byte getMode() throws IOException { - byte[] data = new byte[2]; - data[0] = 4;// First byte is ReportID + public void setMode(int mode) throws IOException { + this.setMode((byte)mode); + } - int read = device.getFeatureReport(data); - if (read > 0) { - return data[1]; - } + /** + * Set random color + * @throws IOException + */ + public void setRandomColor() throws IOException { + Random random = new Random(); + this.setColor( + random.nextInt(256), + random.nextInt(256), + random.nextInt(256)); + } - return -1; + /** + * Turn the device off + * @throws IOException + */ + public void turnOff() throws IOException { + this.setColor(0, 0, 0); } } From 331c436ba4b1c4d35c1989c517057ded03c8e38c Mon Sep 17 00:00:00 2001 From: Zachary Browning Date: Tue, 18 Aug 2015 15:31:32 -0400 Subject: [PATCH 6/7] Added in brightness color adjustment utility method from blinkstick-android. --- src/blinkstick/BlinkStick.java | 61 +++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/src/blinkstick/BlinkStick.java b/src/blinkstick/BlinkStick.java index ccacdda..9855d51 100644 --- a/src/blinkstick/BlinkStick.java +++ b/src/blinkstick/BlinkStick.java @@ -453,6 +453,36 @@ public int getColor() throws IOException { return 0; } + /** + * Adjust RGB values to a specified brightness. + * + * @param r + * @param g + * @param b + * @param maxBrightness Value 0-255 + * @return + */ + public static byte[] getColor(int r, int g, int b, int maxBrightness) { + return getColor((byte) r, (byte) g, (byte) b, maxBrightness); + } + + /** + * Adjust RGB values to a specified brightness. + * + * @param r + * @param g + * @param b + * @param brightness Value 0-255. + * @return + */ + public static byte[] getColor(byte r, byte g, byte b, int brightness) { + final byte newR = remapColor(r, brightness); + final byte newG = remapColor(g, brightness); + final byte newB = remapColor(b, brightness); + + return new byte[] {newR,newG,newB}; + } + /** * Get the current color of the device in #rrggbb format * @@ -565,6 +595,36 @@ public String getSerial() throws IOException { return device.getSerialNumberString(); } + private static byte remap(byte value, float leftMin, float leftMax, float rightMin, float rightMax) + { + //Figure out how 'wide' each range is + float leftSpan = leftMax - leftMin; + float rightSpan = rightMax - rightMin; + + //Java does not have unsigned bytes, so we have to do some byte to int conversion + int valueInt = value; + if (valueInt < 0) { + valueInt = valueInt + 0xff; + } + + //Convert the left range into a 0-1 range (float) + float valueScaled = (valueInt - leftMin) / leftSpan; + + //Convert the 0-1 range into a value in the right range. + valueInt = (int)(rightMin + (valueScaled * rightSpan)); + + //Convert back to correct signed value before conversion to byte + if (valueInt > 127) { + valueInt = valueInt - 0xff; + } + + return (byte)valueInt; + } + + private static byte remapColor(byte value, float max_value) { + return remap(value, 0, 255, 0, max_value); + } + /** * Set the color of the device with separate r, g and b byte values * @@ -689,7 +749,6 @@ private void setDevice(HIDDevice device) { */ public void setIndexedColor(byte channel, byte index, byte r, byte g, byte b) throws IOException { device.sendFeatureReport(new byte[] {5, channel, index, r, g, b}); - } /** From 5aa5065f64009eb1bd076bf6da584d90d3c18706 Mon Sep 17 00:00:00 2001 From: Zachary Browning Date: Tue, 18 Aug 2015 15:47:02 -0400 Subject: [PATCH 7/7] Added final keyword where appropriate. --- src/blinkstick/BlinkStick.java | 170 ++++++++++++++++----------------- 1 file changed, 83 insertions(+), 87 deletions(-) diff --git a/src/blinkstick/BlinkStick.java b/src/blinkstick/BlinkStick.java index 9855d51..d21cc7b 100644 --- a/src/blinkstick/BlinkStick.java +++ b/src/blinkstick/BlinkStick.java @@ -44,6 +44,7 @@ import java.io.IOException; import java.util.Hashtable; +import java.util.Map; import java.util.Random; import java.util.List; import java.util.ArrayList; @@ -71,7 +72,7 @@ public class BlinkStick { private static Boolean Initialized = false; - private static final Hashtable COLORS = new Hashtable() { + private static final Map COLORS = new Hashtable() { /** * */ @@ -228,6 +229,11 @@ public class BlinkStick { } }; + /** + * HID device object to communicate directly with BlinkStick + */ + private HIDDevice device = null; + /** * BlinkStick vendor ID */ @@ -243,7 +249,7 @@ public class BlinkStick { * * @return Returns the adjusted amount of LED data */ - private static byte determineMaxLeds(int length) { + private static byte determineMaxLeds(final int length) { byte maxLeds = 64; //Automatically determine the correct report id to send the data to if (length <= 8 * 3) @@ -275,7 +281,7 @@ else if (length <= 128 * 3) * * @return Returns the report id */ - private static byte determineReportId(int length) { + private static byte determineReportId(final int length) { byte reportId = 9; //Automatically determine the correct report id to send the data to if (length <= 8 * 3) @@ -309,12 +315,12 @@ else if (length <= 128 * 3) * @throws IOException */ public static BlinkStick[] findAll() throws IOException { - List blinkstickList = new ArrayList(); + final List blinkstickList = new ArrayList(); - HIDDeviceInfo[] infos = findAllDescriptors(); + final HIDDeviceInfo[] infos = findAllDescriptors(); - for (HIDDeviceInfo info : infos) { - BlinkStick blinkstick = new BlinkStick(); + for (final HIDDeviceInfo info : infos) { + final BlinkStick blinkstick = new BlinkStick(); blinkstick.setDevice(info.open()); blinkstickList.add(blinkstick); @@ -332,13 +338,11 @@ public static BlinkStick[] findAll() throws IOException { private static HIDDeviceInfo[] findAllDescriptors() throws IOException { Initialize(); - List blinkstickList = new ArrayList(); - - HIDManager hidManager = HIDManager.getInstance(); + final List blinkstickList = new ArrayList(); + final HIDManager hidManager = HIDManager.getInstance(); + final HIDDeviceInfo[] infos = hidManager.listDevices(); - HIDDeviceInfo[] infos = hidManager.listDevices(); - - for (HIDDeviceInfo info : infos) { + for (final HIDDeviceInfo info : infos) { if (info.getVendor_id() == VENDOR_ID && info.getProduct_id() == PRODUCT_ID) { blinkstickList.add(info); @@ -355,15 +359,14 @@ private static HIDDeviceInfo[] findAllDescriptors() throws IOException { * @return BlinkStick object or null if device was not found * @throws IOException */ - public static BlinkStick findBySerial(String serial) throws IOException { + public static BlinkStick findBySerial(final String serial) throws IOException { Initialize(); - HIDDeviceInfo[] infos = findAllDescriptors(); + final HIDDeviceInfo[] infos = findAllDescriptors(); - for (HIDDeviceInfo info : infos) { + for (final HIDDeviceInfo info : infos) { if (info.getSerial_number().equals(serial)) { - BlinkStick result = new BlinkStick(); - + final BlinkStick result = new BlinkStick(); result.setDevice(infos[0].open()); return result; } @@ -381,11 +384,10 @@ public static BlinkStick findBySerial(String serial) throws IOException { public static BlinkStick findFirst() throws IOException { Initialize(); - HIDDeviceInfo[] infos = findAllDescriptors(); + final HIDDeviceInfo[] infos = findAllDescriptors(); if (infos.length > 0) { - BlinkStick result = new BlinkStick(); - + final BlinkStick result = new BlinkStick(); result.setDevice(infos[0].open()); return result; } @@ -400,10 +402,10 @@ public static BlinkStick findFirst() throws IOException { * * @return color object */ - private static int hex2Rgb(String colorStr) { - int red = Integer.valueOf(colorStr.substring(1, 3), 16)+ 0; - int green = Integer.valueOf(colorStr.substring(3, 5), 16) + 0; - int blue = Integer.valueOf(colorStr.substring(5, 7), 16) + 0; + private static int hex2Rgb(final String colorStr) { + final int red = Integer.valueOf(colorStr.substring(1, 3), 16)+ 0; + final int green = Integer.valueOf(colorStr.substring(3, 5), 16) + 0; + final int blue = Integer.valueOf(colorStr.substring(5, 7), 16) + 0; return (255 << 24) | (red << 16) | (green << 8) | blue; } @@ -430,11 +432,6 @@ public static String version() { return VERSION; } - /** - * HID device object to communicate directly with BlinkStick - */ - private HIDDevice device = null; - /** * Get the current color of the device as int * @@ -442,10 +439,10 @@ public static String version() { * @throws IOException */ public int getColor() throws IOException { - byte[] data = new byte[33]; + final byte[] data = new byte[33]; data[0] = 1;// First byte is ReportID - int read = device.getFeatureReport(data); + final int read = device.getFeatureReport(data); if (read > 0) { return (255 << 24) | (data[1] << 16) | (data[2] << 8) | data[3]; } @@ -462,10 +459,10 @@ public int getColor() throws IOException { * @param maxBrightness Value 0-255 * @return */ - public static byte[] getColor(int r, int g, int b, int maxBrightness) { + public static byte[] getColor(final int r, final int g, final int b, final int maxBrightness) { return getColor((byte) r, (byte) g, (byte) b, maxBrightness); } - + /** * Adjust RGB values to a specified brightness. * @@ -475,14 +472,14 @@ public static byte[] getColor(int r, int g, int b, int maxBrightness) { * @param brightness Value 0-255. * @return */ - public static byte[] getColor(byte r, byte g, byte b, int brightness) { + public static byte[] getColor(final byte r, final byte g, final byte b, final int brightness) { final byte newR = remapColor(r, brightness); final byte newG = remapColor(g, brightness); final byte newB = remapColor(b, brightness); return new byte[] {newR,newG,newB}; } - + /** * Get the current color of the device in #rrggbb format * @@ -490,11 +487,11 @@ public static byte[] getColor(byte r, byte g, byte b, int brightness) { * @throws IOException */ public String getColorString() throws IOException { - int c = getColor(); + final int c = getColor(); - int red = (c >> 16) & 0xFF; - int green = (c >> 8) & 0xFF; - int blue = c & 0xFF; + final int red = (c >> 16) & 0xFF; + final int green = (c >> 8) & 0xFF; + final int blue = c & 0xFF; return "#" + String.format("%02X", red) + String.format("%02X", green) @@ -507,13 +504,13 @@ public String getColorString() throws IOException { * @param id InfoBlock id, should be 1 or 2 as only supported info blocks * @throws IOException */ - private String getInfoBlock(int id) throws IOException { - byte[] data = new byte[33]; + private String getInfoBlock(final int id) throws IOException { + final byte[] data = new byte[33]; data[0] = (byte) (id + 1); String result = ""; - int read = device.getFeatureReport(data); + final int read = device.getFeatureReport(data); if (read > 0) { for (int i = 1; i < data.length; i++) { if (i == 0) { @@ -564,10 +561,10 @@ public String getManufacturer() throws IOException { * @throws IOException */ public byte getMode() throws IOException { - byte[] data = new byte[2]; + final byte[] data = new byte[2]; data[0] = 4;// First byte is ReportID - int read = device.getFeatureReport(data); + final int read = device.getFeatureReport(data); if (read > 0) { return data[1]; } @@ -595,33 +592,32 @@ public String getSerial() throws IOException { return device.getSerialNumberString(); } - private static byte remap(byte value, float leftMin, float leftMax, float rightMin, float rightMax) - { + private static byte remap(final byte value, final float leftMin, final float leftMax, final float rightMin, final float rightMax) { //Figure out how 'wide' each range is - float leftSpan = leftMax - leftMin; - float rightSpan = rightMax - rightMin; - + final float leftSpan = leftMax - leftMin; + final float rightSpan = rightMax - rightMin; + //Java does not have unsigned bytes, so we have to do some byte to int conversion int valueInt = value; if (valueInt < 0) { valueInt = valueInt + 0xff; } - + //Convert the left range into a 0-1 range (float) - float valueScaled = (valueInt - leftMin) / leftSpan; - + final float valueScaled = (valueInt - leftMin) / leftSpan; + //Convert the 0-1 range into a value in the right range. valueInt = (int)(rightMin + (valueScaled * rightSpan)); - + //Convert back to correct signed value before conversion to byte if (valueInt > 127) { valueInt = valueInt - 0xff; } - + return (byte)valueInt; } - private static byte remapColor(byte value, float max_value) { + private static byte remapColor(final byte value, final float max_value) { return remap(value, 0, 255, 0, max_value); } @@ -633,7 +629,7 @@ private static byte remapColor(byte value, float max_value) { * @param b blue byte color value 0..255 * @throws IOException */ - public void setColor(byte r, byte g, byte b) throws IOException { + public void setColor(final byte r, final byte g, final byte b) throws IOException { device.sendFeatureReport(new byte[] {1, r, g, b}); } @@ -643,10 +639,10 @@ public void setColor(byte r, byte g, byte b) throws IOException { * @param value color as int * @throws IOException */ - public void setColor(int value) throws IOException { - int r = (value >> 16) & 0xFF; - int g = (value >> 8) & 0xFF; - int b = value & 0xFF; + public void setColor(final int value) throws IOException { + final int r = (value >> 16) & 0xFF; + final int g = (value >> 8) & 0xFF; + final int b = value & 0xFF; this.setColor(r, g, b); } @@ -660,7 +656,7 @@ public void setColor(int value) throws IOException { * @param b blue int color value 0..255 * @throws IOException */ - public void setColor(int r, int g, int b) throws IOException { + public void setColor(final int r, final int g, final int b) throws IOException { this.setColor((byte) r, (byte) g, (byte) b); } @@ -671,7 +667,7 @@ public void setColor(int r, int g, int b) throws IOException { * or a hex color in #rrggbb format * @throws IOException */ - public void setColor(String value) throws IOException { + public void setColor(final String value) throws IOException { if (COLORS.containsKey(value)) { this.setColor(hex2Rgb(COLORS.get(value))); } else { @@ -686,9 +682,9 @@ public void setColor(String value) throws IOException { * @param colorData Report data must be a byte array in the following format: [g0, r0, b0, g1, r1, b1, g2, r2, b2 ...] * @throws IOException */ - public void setColors(byte channel, byte[] colorData) throws IOException { - byte leds = BlinkStick.determineMaxLeds(colorData.length); - byte[] data = new byte[leds * 3 + 2]; + public void setColors(final byte channel, final byte[] colorData) throws IOException { + final byte leds = BlinkStick.determineMaxLeds(colorData.length); + final byte[] data = new byte[leds * 3 + 2]; data[0] = BlinkStick.determineReportId(colorData.length); data[1] = channel; @@ -713,7 +709,7 @@ public void setColors(byte channel, byte[] colorData) throws IOException { * @param colorData Report data must be a byte array in the following format: [g0, r0, b0, g1, r1, b1, g2, r2, b2 ...] * @throws IOException */ - public void setColors(byte[] colorData) throws IOException { + public void setColors(final byte[] colorData) throws IOException { this.setColors((byte)0, colorData); } @@ -724,7 +720,7 @@ public void setColors(byte[] colorData) throws IOException { * @param colorData Report data must be a byte array in the following format: [g0, r0, b0, g1, r1, b1, g2, r2, b2 ...] * @throws IOException */ - public void setColors(int channel, byte[] colorData) throws IOException { + public void setColors(final int channel, final byte[] colorData) throws IOException { this.setColors((byte)channel, colorData); } @@ -733,7 +729,7 @@ public void setColors(int channel, byte[] colorData) throws IOException { * * @param device HID device object to communicate directly with BlinkStick */ - private void setDevice(HIDDevice device) { + private void setDevice(final HIDDevice device) { this.device = device; } @@ -747,7 +743,7 @@ private void setDevice(HIDDevice device) { * @param b blue byte color value 0..255 * @throws IOException */ - public void setIndexedColor(byte channel, byte index, byte r, byte g, byte b) throws IOException { + public void setIndexedColor(final byte channel, final byte index, final byte r, final byte g, final byte b) throws IOException { device.sendFeatureReport(new byte[] {5, channel, index, r, g, b}); } @@ -758,10 +754,10 @@ public void setIndexedColor(byte channel, byte index, byte r, byte g, byte b) th * @param value color as int * @throws IOException */ - public void setIndexedColor(int index, int value) throws IOException { - int r = (value >> 16) & 0xFF; - int g = (value >> 8) & 0xFF; - int b = value & 0xFF; + public void setIndexedColor(final int index, final int value) throws IOException { + final int r = (value >> 16) & 0xFF; + final int g = (value >> 8) & 0xFF; + final int b = value & 0xFF; this.setIndexedColor(0, index, r, g, b); } @@ -774,10 +770,10 @@ public void setIndexedColor(int index, int value) throws IOException { * @param value color as int * @throws IOException */ - public void setIndexedColor(int channel, int index, int value) throws IOException { - int r = (value >> 16) & 0xFF; - int g = (value >> 8) & 0xFF; - int b = value & 0xFF; + public void setIndexedColor(final int channel, final int index, final int value) throws IOException { + final int r = (value >> 16) & 0xFF; + final int g = (value >> 8) & 0xFF; + final int b = value & 0xFF; this.setIndexedColor(channel, index, r, g, b); } @@ -792,7 +788,7 @@ public void setIndexedColor(int channel, int index, int value) throws IOExceptio * @param b blue int color value 0..255 * @throws IOException */ - public void setIndexedColor(int channel, int index, int r, int g, int b) throws IOException { + public void setIndexedColor(final int channel, final int index, final int r, final int g, final int b) throws IOException { this.setIndexedColor((byte)channel, (byte)index, (byte)r, (byte)g, (byte)b); } @@ -803,9 +799,9 @@ public void setIndexedColor(int channel, int index, int r, int g, int b) throws * @param value The value to be written to the info block * @throws IOException */ - private void setInfoBlock(int id, String value) throws IOException { - char[] charArray = value.toCharArray(); - byte[] data = new byte[33]; + private void setInfoBlock(final int id, final String value) throws IOException { + final char[] charArray = value.toCharArray(); + final byte[] data = new byte[33]; data[0] = (byte) (id + 1); for (int i = 0; i < charArray.length; i++) { @@ -825,7 +821,7 @@ private void setInfoBlock(int id, String value) throws IOException { * @param value The value to be written to the info block 1 * @throws IOException */ - public void setInfoBlock1(String value) throws IOException { + public void setInfoBlock1(final String value) throws IOException { setInfoBlock(1, value); } @@ -835,7 +831,7 @@ public void setInfoBlock1(String value) throws IOException { * @param value The value to be written to the info block 2 * @throws IOException */ - public void setInfoBlock2(String value) throws IOException { + public void setInfoBlock2(final String value) throws IOException { setInfoBlock(2, value); } @@ -845,7 +841,7 @@ public void setInfoBlock2(String value) throws IOException { * @param mode 0 - Normal, 1 - Inverse, 2 - WS2812, 3 - WS2812 mirror * @throws IOException */ - public void setMode(byte mode) throws IOException { + public void setMode(final byte mode) throws IOException { device.sendFeatureReport(new byte[] {4, mode}); } @@ -856,7 +852,7 @@ public void setMode(byte mode) throws IOException { * @param mode 0 - Normal, 1 - Inverse, 2 - WS2812, 3 - WS2812 mirror * @throws IOException */ - public void setMode(int mode) throws IOException { + public void setMode(final int mode) throws IOException { this.setMode((byte)mode); } @@ -865,7 +861,7 @@ public void setMode(int mode) throws IOException { * @throws IOException */ public void setRandomColor() throws IOException { - Random random = new Random(); + final Random random = new Random(); this.setColor( random.nextInt(256), random.nextInt(256),