/** * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND * * //\\ //\\ //\\ //\\ * ///\\\ ///\\\ ///\\\ ///\\\ * \\\/// \\\/// \\\/// \\\/// * \\// \\// \\// \\//H * * EXPERTS ONLY!! EXPERTS ONLY!! * * If you are an artist, you may ignore this file! It just sets * up the framework to run the patterns. Should not need modification * for general animation work. */ GrizzlyOutput[] buildGrizzlies() throws SocketException, UnknownHostException { return new GrizzlyOutput[] { new GrizzlyOutput(lx, "192.168.88.100", 1, 2, 3, 4, 5, 6, 7, 8), new GrizzlyOutput(lx, "192.168.88.101", 9, 10, 11, 12, 13, 14, 15, 16), }; } class GrizzlyOutput extends LXDatagramOutput { private int frameNumber = 0; GrizzlyOutput(LX lx, String ipAddress, int ... cubeIndices) throws UnknownHostException, SocketException { super(lx); int channelNum = 1; for (int rawCubeIndex : cubeIndices) { if (rawCubeIndex > 0) { Cube cube = model.getCubeByRawIndex(rawCubeIndex); addDatagram(new GrizzlyDatagram(this, channelNum, cube).setAddress(ipAddress)); } ++channelNum; } } protected void beforeSend(int[] colors) { ++frameNumber; } public int getFrameNumber() { return this.frameNumber; } } static class GrizzlyDatagram extends LXDatagram { private static byte[] oscString(String s) { int len = s.length(); int padding = (4 - ((len + 1) % 4)) % 4; byte[] bytes = new byte[len + 1 + padding]; System.arraycopy(s.getBytes(), 0, bytes, 0, len); for (int i = len; i < bytes.length; ++i) { bytes[i] = 0; } return bytes; } private static int oscIntCopy(int i, byte[] buffer, int pos) { buffer[pos] = (byte) ((i >> 24) & 0xff); buffer[pos + 1] = (byte) ((i >> 16) & 0xff); buffer[pos + 2] = (byte) ((i >> 8) & 0xff); buffer[pos + 3] = (byte) (i & 0xff); return 4; } private final static int[] STRIP_ORDERING = new int[] { 9, 8, 11, 5, 4, 7, 6, 10, 14, 2, 1, 0, 3, 13, 12, 15 }; private static int[] cubePointIndices(Cube cube) { int[] pointIndices = new int[Cube.POINTS_PER_CUBE - 2]; int pi = 0; for (int stripIndex : STRIP_ORDERING) { Strip strip = cube.strips.get(stripIndex); int stripLen = ((stripIndex == 9) || (stripIndex == 15)) ? 15 : 16; for (int i = stripLen-1; i >= 0; --i) { pointIndices[pi++] = strip.points.get(i).index; } } return pointIndices; } private final static byte[] OSC_ADDRESS = oscString("/shady/pointbuffer"); private final static byte[] OSC_TYPETAG = oscString(",iiiiibi"); private final static int HEADER_LENGTH = OSC_ADDRESS.length + OSC_TYPETAG.length + 24; private final static int FOOTER_LENGTH = 4; private final static int OSC_PORT = 779; private GrizzlyOutput output; private int[] pointIndices; private final int frameNumberPos; private final int dataPos; public GrizzlyDatagram(GrizzlyOutput output, int channelNum, Cube cube) { this(output, channelNum, cubePointIndices(cube)); } public GrizzlyDatagram(GrizzlyOutput output, int channelNum, int[] pointIndices) { super(HEADER_LENGTH + 4*pointIndices.length + FOOTER_LENGTH); setPort(OSC_PORT); this.output = output; this.pointIndices = pointIndices; int dataLength = 4*pointIndices.length; int pos = 0; // OSC address System.arraycopy(OSC_ADDRESS, 0, this.buffer, pos, OSC_ADDRESS.length); pos += OSC_ADDRESS.length; // OSC typetag System.arraycopy(OSC_TYPETAG, 0, this.buffer, pos, OSC_TYPETAG.length); pos += OSC_TYPETAG.length; this.frameNumberPos = pos; pos += oscIntCopy(0, this.buffer, pos); // placeholder for frame number pos += oscIntCopy(0xDEADBEEF, this.buffer, pos); pos += oscIntCopy(channelNum, this.buffer, pos); pos += oscIntCopy(0xFEEDBEEF, this.buffer, pos); pos += oscIntCopy(dataLength, this.buffer, pos); pos += oscIntCopy(dataLength, this.buffer, pos); this.dataPos = pos; // end header oscIntCopy(0xBEFFFFEB, this.buffer, this.buffer.length - 4); } void onSend(int[] colors) { oscIntCopy(this.output.getFrameNumber(), this.buffer, frameNumberPos); int dataIndex = this.dataPos; for (int index : this.pointIndices) { color c = (index >= 0) ? colors[index] : 0; this.buffer[dataIndex] = (byte) 0; // unused, alpha this.buffer[dataIndex + 1] = (byte) ((c >> 16) & 0xff); // r this.buffer[dataIndex + 2] = (byte) ((c >> 8) & 0xff); // g this.buffer[dataIndex + 3] = (byte) (c & 0xff); // b dataIndex += 4; } } }