2 * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND
5 * ///\\\ ///\\\ ///\\\ ///\\\
6 * \\\/// \\\/// \\\/// \\\///
9 * EXPERTS ONLY!! EXPERTS ONLY!!
11 * If you are an artist, you may ignore this file! It contains
12 * the code to drive grizzly board outputs.
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 };
17 static int[] cubePointIndices(Cube cube) {
18 int[] pointIndices = new int[Cube.POINTS_PER_CUBE - 2];
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;
30 abstract class IPOutput extends LXDatagramOutput {
31 public final String ipAddress;
33 protected IPOutput(LX lx, String ipAddress) throws SocketException {
35 this.ipAddress = ipAddress;
39 static abstract class OSCDatagram extends LXDatagram {
41 protected OSCDatagram(int bufferSize) {
45 protected final static int OSC_PORT = 779;
47 protected static byte[] oscString(String s) {
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) {
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);
68 * Grizzly Output, sends packets to one grizzly board with a fixed IP and a number
71 class GrizzlyOutput extends IPOutput {
73 private int frameNumber = 0;
75 public GrizzlyOutput(LX lx, String ipAddress, int ... cubeIndices) throws UnknownHostException, SocketException {
78 for (int rawCubeIndex : cubeIndices) {
79 if (rawCubeIndex > 0) {
80 Cube cube = model.getCubeByRawIndex(rawCubeIndex);
81 addDatagram(new GrizzlyDatagram(this, channelNum, cube).setAddress(ipAddress));
85 this.enabled.setValue(false);
88 protected void beforeSend(int[] colors) {
92 public int getFrameNumber() {
93 return this.frameNumber;
98 * Datagram to a Grizzlyboard. A simple fixed OSC packet.
100 static class GrizzlyDatagram extends OSCDatagram {
102 private final static byte[] OSC_ADDRESS = oscString("/shady/pointbuffer");
103 private final static byte[] OSC_TYPETAG = oscString(",iiiiibi");
105 private final static int HEADER_LENGTH = OSC_ADDRESS.length + OSC_TYPETAG.length + 24;
106 private final static int FOOTER_LENGTH = 4;
108 private GrizzlyOutput output;
110 private int[] pointIndices;
112 private final int frameNumberPos;
114 public GrizzlyDatagram(GrizzlyOutput output, int channelNum, Cube cube) {
115 this(output, channelNum, cubePointIndices(cube));
118 public GrizzlyDatagram(GrizzlyOutput output, int channelNum, int[] pointIndices) {
119 super(HEADER_LENGTH + 4*pointIndices.length + FOOTER_LENGTH);
122 this.output = output;
123 this.pointIndices = pointIndices;
124 int dataLength = 4*pointIndices.length;
129 System.arraycopy(OSC_ADDRESS, 0, this.buffer, pos, OSC_ADDRESS.length);
130 pos += OSC_ADDRESS.length;
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);
144 oscIntCopy(0xBEFFFFEB, this.buffer, this.buffer.length - 4);
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
162 * Grizzly Output, sends packets to one grizzly board with a fixed IP and a number
165 class PandaOutput extends IPOutput {
167 public PandaOutput(LX lx, String ipAddress, String[][] channelMappings) throws UnknownHostException, SocketException {
168 super(lx, ipAddress);
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;
175 int[] cubePointIndices;
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));
186 packetPointIndices = new int[PandaDatagram.PIXELS_PER_PACKET];
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));
197 packetPointIndices = new int[PandaDatagram.PIXELS_PER_PACKET];
203 addDatagram(new PandaDatagram(packetNum, packetPointIndices).setAddress(ipAddress));
205 this.enabled.setValue(false);
210 * Datagram to a Panda board. A simple fixed OSC packet.
212 static class PandaDatagram extends OSCDatagram {
214 private final static byte[] OSC_ADDRESS = oscString("/shady/pointbuffer");
215 private final static byte[] OSC_TYPETAG = oscString(",iib");
217 private final static int HEADER_LENGTH = OSC_ADDRESS.length + OSC_TYPETAG.length + 12;
219 private final static int PORT = 9001;
221 public final static int CUBES_PER_CHANNEL = 4;
223 public final static int PIXELS_PER_CHANNEL = Cube.POINTS_PER_CUBE * CUBES_PER_CHANNEL;
225 public final static int PIXELS_PER_PACKET = 352;
227 public final static int BYTES_PER_PIXEL = 4;
229 public final static int PACKET_SIZE = BYTES_PER_PIXEL*PIXELS_PER_PACKET;
231 private int[] pointIndices;
233 public PandaDatagram(int packetNum, int[] pointIndices) {
234 super(HEADER_LENGTH + PACKET_SIZE);
237 this.pointIndices = pointIndices;
242 System.arraycopy(OSC_ADDRESS, 0, this.buffer, pos, OSC_ADDRESS.length);
243 pos += OSC_ADDRESS.length;
246 System.arraycopy(OSC_TYPETAG, 0, this.buffer, pos, OSC_TYPETAG.length);
247 pos += OSC_TYPETAG.length;
250 pos += oscIntCopy(packetNum, this.buffer, pos);
253 pos += oscIntCopy(PACKET_SIZE, this.buffer, pos);
255 // Length of blob in osc-blob
256 pos += oscIntCopy(PACKET_SIZE, this.buffer, pos);
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