| 1 | /** |
| 2 | * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND |
| 3 | * |
| 4 | * //\\ //\\ //\\ //\\ |
| 5 | * ///\\\ ///\\\ ///\\\ ///\\\ |
| 6 | * \\\/// \\\/// \\\/// \\\/// |
| 7 | * \\// \\// \\// \\//H |
| 8 | * |
| 9 | * EXPERTS ONLY!! EXPERTS ONLY!! |
| 10 | * |
| 11 | * If you are an artist, you may ignore this file! It contains |
| 12 | * the code to drive grizzly board outputs. |
| 13 | */ |
| 14 | |
| 15 | private final static int[] STRIP_ORDERING = new int[] { 9, 8, 11, 5, 4, 7, 6, 10, 14, 2, 1, 0, 3, 13, 12, 15 }; |
| 16 | |
| 17 | static int[] cubePointIndices(Cube cube) { |
| 18 | int[] pointIndices = new int[Cube.POINTS_PER_CUBE - 2]; |
| 19 | int pi = 0; |
| 20 | for (int stripIndex : STRIP_ORDERING) { |
| 21 | Strip strip = cube.strips.get(stripIndex); |
| 22 | int stripLen = ((stripIndex == 9) || (stripIndex == 15)) ? 15 : 16; |
| 23 | for (int i = stripLen-1; i >= 0; --i) { |
| 24 | pointIndices[pi++] = strip.points.get(i).index; |
| 25 | } |
| 26 | } |
| 27 | return pointIndices; |
| 28 | } |
| 29 | |
| 30 | abstract class IPOutput extends LXDatagramOutput { |
| 31 | public final String ipAddress; |
| 32 | |
| 33 | protected IPOutput(LX lx, String ipAddress) throws SocketException { |
| 34 | super(lx); |
| 35 | this.ipAddress = ipAddress; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | static abstract class OSCDatagram extends LXDatagram { |
| 40 | |
| 41 | protected OSCDatagram(int bufferSize) { |
| 42 | super(bufferSize); |
| 43 | } |
| 44 | |
| 45 | protected final static int OSC_PORT = 779; |
| 46 | |
| 47 | protected static byte[] oscString(String s) { |
| 48 | int len = s.length(); |
| 49 | int padding = (4 - ((len + 1) % 4)) % 4; |
| 50 | byte[] bytes = new byte[len + 1 + padding]; |
| 51 | System.arraycopy(s.getBytes(), 0, bytes, 0, len); |
| 52 | for (int i = len; i < bytes.length; ++i) { |
| 53 | bytes[i] = 0; |
| 54 | } |
| 55 | return bytes; |
| 56 | } |
| 57 | |
| 58 | protected static int oscIntCopy(int i, byte[] buffer, int pos) { |
| 59 | buffer[pos] = (byte) ((i >> 24) & 0xff); |
| 60 | buffer[pos + 1] = (byte) ((i >> 16) & 0xff); |
| 61 | buffer[pos + 2] = (byte) ((i >> 8) & 0xff); |
| 62 | buffer[pos + 3] = (byte) (i & 0xff); |
| 63 | return 4; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Grizzly Output, sends packets to one grizzly board with a fixed IP and a number |
| 69 | * of channels. |
| 70 | */ |
| 71 | class GrizzlyOutput extends IPOutput { |
| 72 | |
| 73 | private int frameNumber = 0; |
| 74 | |
| 75 | public GrizzlyOutput(LX lx, String ipAddress, int ... cubeIndices) throws UnknownHostException, SocketException { |
| 76 | super(lx, ipAddress); |
| 77 | int channelNum = 0; |
| 78 | for (int rawCubeIndex : cubeIndices) { |
| 79 | if (rawCubeIndex > 0) { |
| 80 | Cube cube = model.getCubeByRawIndex(rawCubeIndex); |
| 81 | addDatagram(new GrizzlyDatagram(this, channelNum, cube).setAddress(ipAddress)); |
| 82 | } |
| 83 | ++channelNum; |
| 84 | } |
| 85 | this.enabled.setValue(false); |
| 86 | } |
| 87 | |
| 88 | protected void beforeSend(int[] colors) { |
| 89 | ++frameNumber; |
| 90 | } |
| 91 | |
| 92 | public int getFrameNumber() { |
| 93 | return this.frameNumber; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Datagram to a Grizzlyboard. A simple fixed OSC packet. |
| 99 | */ |
| 100 | static class GrizzlyDatagram extends OSCDatagram { |
| 101 | |
| 102 | private final static byte[] OSC_ADDRESS = oscString("/shady/pointbuffer"); |
| 103 | private final static byte[] OSC_TYPETAG = oscString(",iiiiibi"); |
| 104 | |
| 105 | private final static int HEADER_LENGTH = OSC_ADDRESS.length + OSC_TYPETAG.length + 24; |
| 106 | private final static int FOOTER_LENGTH = 4; |
| 107 | |
| 108 | private GrizzlyOutput output; |
| 109 | |
| 110 | private int[] pointIndices; |
| 111 | |
| 112 | private final int frameNumberPos; |
| 113 | |
| 114 | public GrizzlyDatagram(GrizzlyOutput output, int channelNum, Cube cube) { |
| 115 | this(output, channelNum, cubePointIndices(cube)); |
| 116 | } |
| 117 | |
| 118 | public GrizzlyDatagram(GrizzlyOutput output, int channelNum, int[] pointIndices) { |
| 119 | super(HEADER_LENGTH + 4*pointIndices.length + FOOTER_LENGTH); |
| 120 | setPort(OSC_PORT); |
| 121 | |
| 122 | this.output = output; |
| 123 | this.pointIndices = pointIndices; |
| 124 | int dataLength = 4*pointIndices.length; |
| 125 | |
| 126 | int pos = 0; |
| 127 | |
| 128 | // OSC address |
| 129 | System.arraycopy(OSC_ADDRESS, 0, this.buffer, pos, OSC_ADDRESS.length); |
| 130 | pos += OSC_ADDRESS.length; |
| 131 | |
| 132 | // OSC typetag |
| 133 | System.arraycopy(OSC_TYPETAG, 0, this.buffer, pos, OSC_TYPETAG.length); |
| 134 | pos += OSC_TYPETAG.length; |
| 135 | this.frameNumberPos = pos; |
| 136 | pos += oscIntCopy(0, this.buffer, pos); // placeholder for frame number |
| 137 | pos += oscIntCopy(0xDEADBEEF, this.buffer, pos); |
| 138 | pos += oscIntCopy(channelNum, this.buffer, pos); |
| 139 | pos += oscIntCopy(0xFEEDBEEF, this.buffer, pos); |
| 140 | pos += oscIntCopy(dataLength, this.buffer, pos); |
| 141 | pos += oscIntCopy(dataLength, this.buffer, pos); |
| 142 | |
| 143 | // end header |
| 144 | oscIntCopy(0xBEFFFFEB, this.buffer, this.buffer.length - 4); |
| 145 | } |
| 146 | |
| 147 | void onSend(int[] colors) { |
| 148 | oscIntCopy(this.output.getFrameNumber(), this.buffer, frameNumberPos); |
| 149 | int dataIndex = HEADER_LENGTH; |
| 150 | for (int index : this.pointIndices) { |
| 151 | color c = (index >= 0) ? colors[index] : 0; |
| 152 | this.buffer[dataIndex] = (byte) 0; // unused, alpha |
| 153 | this.buffer[dataIndex + 1] = (byte) ((c >> 16) & 0xff); // r |
| 154 | this.buffer[dataIndex + 2] = (byte) ((c >> 8) & 0xff); // g |
| 155 | this.buffer[dataIndex + 3] = (byte) (c & 0xff); // b |
| 156 | dataIndex += 4; |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Grizzly Output, sends packets to one grizzly board with a fixed IP and a number |
| 163 | * of channels. |
| 164 | */ |
| 165 | class PandaOutput extends IPOutput { |
| 166 | |
| 167 | public PandaOutput(LX lx, String ipAddress, String[][] channelMappings) throws UnknownHostException, SocketException { |
| 168 | super(lx, ipAddress); |
| 169 | int packetNum = 0; |
| 170 | int[] packetPointIndices = new int[PandaDatagram.PIXELS_PER_PACKET]; |
| 171 | int[] noCubePoints = new int[Cube.POINTS_PER_CUBE - 2]; // 15-strips |
| 172 | for (int i = 0; i < noCubePoints.length; ++i) { |
| 173 | noCubePoints[i] = -1; |
| 174 | } |
| 175 | int[] cubePointIndices; |
| 176 | int pi = 0; |
| 177 | for (String[] channel : channelMappings) { |
| 178 | for (int i = 0; i < PandaDatagram.CUBES_PER_CHANNEL; ++i) { |
| 179 | Cube cube = model.getCubeById((i < channel.length) ? channel[i] : null); |
| 180 | cubePointIndices = (cube == null) ? noCubePoints : cubePointIndices(cube); |
| 181 | for (int index : cubePointIndices) { |
| 182 | packetPointIndices[pi++] = index; |
| 183 | if (pi >= PandaDatagram.PIXELS_PER_PACKET) { |
| 184 | addDatagram(new PandaDatagram(packetNum, packetPointIndices).setAddress(ipAddress)); |
| 185 | pi = 0; |
| 186 | packetPointIndices = new int[PandaDatagram.PIXELS_PER_PACKET]; |
| 187 | ++packetNum; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | // 2 dummy pixels per cube at the end of each channel, due to 15-strips |
| 192 | for (int i = 0; i < PandaDatagram.CUBES_PER_CHANNEL * 2; ++i) { |
| 193 | packetPointIndices[pi++] = -1; |
| 194 | if (pi >= PandaDatagram.PIXELS_PER_PACKET) { |
| 195 | addDatagram(new PandaDatagram(packetNum, packetPointIndices).setAddress(ipAddress)); |
| 196 | pi = 0; |
| 197 | packetPointIndices = new int[PandaDatagram.PIXELS_PER_PACKET]; |
| 198 | ++packetNum; |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | if (pi > 0) { |
| 203 | addDatagram(new PandaDatagram(packetNum, packetPointIndices).setAddress(ipAddress)); |
| 204 | } |
| 205 | this.enabled.setValue(false); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Datagram to a Panda board. A simple fixed OSC packet. |
| 211 | */ |
| 212 | static class PandaDatagram extends OSCDatagram { |
| 213 | |
| 214 | private final static byte[] OSC_ADDRESS = oscString("/shady/pointbuffer"); |
| 215 | private final static byte[] OSC_TYPETAG = oscString(",iib"); |
| 216 | |
| 217 | private final static int HEADER_LENGTH = OSC_ADDRESS.length + OSC_TYPETAG.length + 12; |
| 218 | |
| 219 | private final static int PORT = 9001; |
| 220 | |
| 221 | public final static int CUBES_PER_CHANNEL = 4; |
| 222 | |
| 223 | public final static int PIXELS_PER_CHANNEL = Cube.POINTS_PER_CUBE * CUBES_PER_CHANNEL; |
| 224 | |
| 225 | public final static int PIXELS_PER_PACKET = 352; |
| 226 | |
| 227 | public final static int BYTES_PER_PIXEL = 4; |
| 228 | |
| 229 | public final static int PACKET_SIZE = BYTES_PER_PIXEL*PIXELS_PER_PACKET; |
| 230 | |
| 231 | private int[] pointIndices; |
| 232 | |
| 233 | public PandaDatagram(int packetNum, int[] pointIndices) { |
| 234 | super(HEADER_LENGTH + PACKET_SIZE); |
| 235 | setPort(PORT); |
| 236 | |
| 237 | this.pointIndices = pointIndices; |
| 238 | |
| 239 | int pos = 0; |
| 240 | |
| 241 | // OSC address |
| 242 | System.arraycopy(OSC_ADDRESS, 0, this.buffer, pos, OSC_ADDRESS.length); |
| 243 | pos += OSC_ADDRESS.length; |
| 244 | |
| 245 | // OSC typetag |
| 246 | System.arraycopy(OSC_TYPETAG, 0, this.buffer, pos, OSC_TYPETAG.length); |
| 247 | pos += OSC_TYPETAG.length; |
| 248 | |
| 249 | // Packet number |
| 250 | pos += oscIntCopy(packetNum, this.buffer, pos); |
| 251 | |
| 252 | // Length of blob |
| 253 | pos += oscIntCopy(PACKET_SIZE, this.buffer, pos); |
| 254 | |
| 255 | // Length of blob in osc-blob |
| 256 | pos += oscIntCopy(PACKET_SIZE, this.buffer, pos); |
| 257 | } |
| 258 | |
| 259 | void onSend(int[] colors) { |
| 260 | int dataIndex = HEADER_LENGTH; |
| 261 | for (int index : this.pointIndices) { |
| 262 | color c = (index >= 0) ? colors[index] : 0; |
| 263 | this.buffer[dataIndex] = (byte) 0; // unused, alpha |
| 264 | this.buffer[dataIndex + 1] = (byte) ((c >> 16) & 0xff); // r |
| 265 | this.buffer[dataIndex + 2] = (byte) ((c >> 8) & 0xff); // g |
| 266 | this.buffer[dataIndex + 3] = (byte) (c & 0xff); // b |
| 267 | dataIndex += 4; |
| 268 | } |
| 269 | } |
| 270 | } |