6 * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND
9 * ///\\\ ///\\\ ///\\\ ///\\\
10 * \\\/// \\\/// \\\/// \\\///
13 * EXPERTS ONLY!! EXPERTS ONLY!!
15 * This class implements the output function to the Panda Boards. It
16 * will be moved into GLucose once stabilized.
18 public static class PandaDriver {
21 public void onToggle(boolean enabled);
24 private Listener listener = null;
27 public final String ip;
29 public PandaMapping pm;
31 private final static int PORT = 779;
33 private final DatagramSocket socket;
36 private final NetAddress address;
38 // Whether board output is enabled
39 private boolean enabled = false;
41 // Frame count for Grizzlies
42 private int frameNum = 1;
45 private final OscMessage message;
47 // List of point indices that get sent to this board
48 private final int[] points;
51 private final byte[] packet = new byte[4*280]; // magic number, our UDP packet size
53 private static final int NO_POINT = -1;
57 ////////////////////////////////////////////////////////////////
59 // READ THIS RIGHT NOW BEFORE YOU MODIFY THE BELOW!!!!!!!!!!!!!
60 // READ THIS RIGHT NOW BEFORE YOU MODIFY THE BELOW!!!!!!!!!!!!!
61 // READ THIS RIGHT NOW BEFORE YOU MODIFY THE BELOW!!!!!!!!!!!!!
63 // The mappings below indicate the physical order of strips
64 // connected to a pandaboard channel. The strip numbers are a
65 // reflection of how the model is built.
67 // For ANYTHING in the model which is a rectangular prism,
68 // which means Cubes, the BassBox, and each Speaker, the
69 // strips are numbered incrementally by face. The first
70 // face is always the FRONT, which you are looking at.
71 // The next face is the RIGHT, then the BACK, then the LEFT.
73 // For every face, the strips are ordered numerically moving
74 // clockwise from the the TOP LEFT.
78 // Strip 0: front face, top strip, left to right
79 // Strip 1: front face, right strip, top to bottom
80 // Strip 2: front face, bottom strip, right to left
81 // Strip 3: front face, left strip, bottom to top
83 // Strip 4: right face, top strip, left to right
85 // Strip 14: left face, bottom strip, right to left
86 // Strip 15: left face, left strip, bottom to top
88 ////////////////////////////////////////////////////////////////
90 private final static int FORWARD = -1;
91 private final static int BACKWARD = -2;
94 * These constant arrays indicate the order in which the strips of a cube
95 * are wired. There are four different options, depending on which bottom
96 * corner of the cube the data wire comes in.
98 private final static int[][] CUBE_STRIP_ORDERINGS = new int[][] {
99 // { 2, 1, 0, 3, 13, 12, 15, 14, 4, 7, 6, 5, 11, 10, 9, 8 }, // FRONT_LEFT
100 // { 6, 5, 4, 7, 1, 0, 3, 2, 8, 11, 10, 9, 15, 14, 13, 12 }, // FRONT_RIGHT
101 // { 14, 13, 12, 15, 9, 8, 11, 10, 0, 3, 2, 1, 7, 6, 5, 4 }, // REAR_LEFT
102 // { 10, 9, 8, 11, 5, 4, 7, 6, 12, 15, 14, 13, 3, 2, 1, 0 }, // REAR_RIGHT
105 { 2, 1, 0, 3, 13, 12, 15, 14, 4, 7, 6, 5, 11, 10, 9, 8 }, // FRONT_LEFT
106 { 6, 5, 4, 7, 1, 0, 3, 2, 8, 11, 10, 9, 15, 14, 13, 12 }, // FRONT_RIGHT
107 { 14, 13, 12, 15, 9, 8, 11, 10, 0, 3, 2, 1, 7, 6, 5, 4 }, // REAR_LEFT
108 { 9, 8, 11, 5, 4, 7, 6, 10, 14, 2, 1, 0, 3, 13, 12, 15 }, // REAR_RIGHT
112 private final static int[][] BASS_STRIP_ORDERING = {
113 // front face, counterclockwise from bottom front left
114 {2, BACKWARD /* if this strip has extra pixels, you can add them here */ /*, 4 */ },
115 {1, BACKWARD /* if this strip is short some pixels, substract them here */ /*, -3 */ },
119 // left face, counterclockwise from bottom front left
125 // back face, counterclockwise from bottom rear left
131 // right face, counterclockwise from bottom rear right
138 private final static int[][] STRUT_STRIP_ORDERING = {
149 private final static int[][] FLOOR_STRIP_ORDERING = {
156 // The speakers are currently configured to be wired the same
157 // as cubes with Wiring.FRONT_LEFT. If this needs to be changed,
158 // remove this null assignment and change the below to have mappings
159 // for the LEFT and RIGHT speaker
160 private final static int[][][] SPEAKER_STRIP_ORDERING = {
163 // Front face, counter-clockwise from bottom left
171 // Front face, counter-clockwise from bottom left
179 public PandaDriver(String ip) {
182 // Initialize our OSC output stuff
183 address = new NetAddress(ip, 779);
184 message = new OscMessage("/shady/pointbuffer");
187 socket = new DatagramSocket();
188 } catch (Exception x) {
189 throw new RuntimeException(x);
192 // Build the array of points, initialize all to nothing
193 points = new int[PandaMapping.PIXELS_PER_BOARD];
194 for (int i = 0; i < points.length; ++i) {
195 points[i] = NO_POINT;
199 public PandaDriver(String ip, Model model, PandaMapping _pm) {
203 // Ok, we are initialized, time to build the array if points in order to
204 // send out. We start at the head of our point buffer, and work our way
205 // down. This is the order in which points will be sent down the wire.
208 // Iterate through all our channelq s
209 for (ChannelMapping channel : pm.channelList) {
211 int pi = ci * ChannelMapping.PIXELS_PER_CHANNEL;
213 switch (channel.mode) {
215 case ChannelMapping.MODE_CUBES:
216 // We have a list of cubes per channel
217 for (int rawCubeIndex : channel.objectIndices) {
218 if (rawCubeIndex < 0) {
219 // No cube here, skip ahead in the buffer
220 pi += Cube.POINTS_PER_CUBE;
222 // The cube exists, check which way it is wired to
223 // figure out the order of strips.
224 Cube cube = model.getCubeByRawIndex(rawCubeIndex);
225 int stripOrderIndex = 0;
226 switch (cube.wiring) {
227 case FRONT_LEFT: stripOrderIndex = 0; break;
228 case FRONT_RIGHT: stripOrderIndex = 1; break;
229 case REAR_LEFT: stripOrderIndex = 2; break;
230 case REAR_RIGHT: stripOrderIndex = 3; break;
233 // TODO(mcslee): clean up, ordering always consistent now
236 // Iterate through all the strips on the cube and add the points
237 for (int stripIndex : CUBE_STRIP_ORDERINGS[stripOrderIndex]) {
238 // We go backwards here... in the model strips go clockwise, but
239 // the physical wires are run counter-clockwise
240 pi = mapStrip(cube.strips.get(stripIndex), BACKWARD, points, pi);
246 case ChannelMapping.MODE_BASS:
247 for (int[] config : BASS_STRIP_ORDERING) {
248 pi = mapStrip(model.bassBox.strips.get(config[0]), config[1], points, pi);
249 if (config.length >= 3) pi += config[2];
253 case ChannelMapping.MODE_STRUTS_AND_FLOOR:
254 for (int[] config : STRUT_STRIP_ORDERING) {
255 pi = mapStrip(model.bassBox.struts.get(config[0]), config[1], points, pi);
256 if (config.length >= 3) pi += config[2];
258 for (int[] config : FLOOR_STRIP_ORDERING) {
259 pi = mapStrip(model.boothFloor.strips.get(config[0]), config[1], points, pi);
260 if (config.length >= 3) pi += config[2];
264 case ChannelMapping.MODE_SPEAKER:
265 int [][] speakerStripOrdering;
266 if (SPEAKER_STRIP_ORDERING == null) {
267 // Copy the cube strip ordering
268 int[] frontLeftCubeWiring = CUBE_STRIP_ORDERINGS[0];
269 speakerStripOrdering = new int[frontLeftCubeWiring.length][];
270 for (int i = 0; i < frontLeftCubeWiring.length; ++i) {
271 speakerStripOrdering[i] = new int[] { frontLeftCubeWiring[0], BACKWARD };
274 speakerStripOrdering = SPEAKER_STRIP_ORDERING[channel.objectIndices[0]];
276 for (int[] config : speakerStripOrdering) {
277 Speaker speaker = model.speakers.get(channel.objectIndices[0]);
278 pi = mapStrip(speaker.strips.get(config[0]), config[1], points, pi);
279 if (config.length >= 3) pi += config[2];
283 case ChannelMapping.MODE_NULL:
284 // No problem, nothing on this channel!
288 throw new RuntimeException("Invalid/unhandled channel mapping mode: " + channel.mode);
294 private int mapStrip(Strip s, int direction, int[] points, int pi) {
295 return mapStrip(s, direction, points, pi, s.points.size());
298 private int mapStrip(Strip s, int direction, int[] points, int pi, int len) {
299 if (direction == FORWARD) {
301 for (LXPoint p : s.points) {
302 points[pi++] = p.index;
307 } else if (direction == BACKWARD) {
308 for (int i = len-1; i >= 0; --i) {
309 points[pi++] = s.points.get(i).index;
312 throw new RuntimeException("Unidentified strip mapping direction: " + direction);
317 public PandaDriver setListener(Listener listener) {
318 this.listener = listener;
322 public void setEnabled(boolean enabled) {
323 if (this.enabled != enabled) {
324 this.enabled = enabled;
325 println("PandaBoard/" + ip + ": " + (enabled ? "ON" : "OFF"));
326 if (listener != null) {
327 listener.onToggle(enabled);
332 public boolean isEnabled() {
336 public void disable() {
340 public void enable() {
344 public void toggle() {
345 setEnabled(!enabled);
348 private final int[] GRIZZLY_STRIP_ORDERING = new int[] { 9, 8, 11, 5, 4, 7, 6, 10, 14, 2, 1, 0, 3, 13, 12, 15 };
350 public final void send(int[] colors) {
357 for (ChannelMapping channel : pm.channelList) {
358 for (int rawCubeIndex : channel.objectIndices) {
359 if (rawCubeIndex > 0) {
360 Cube cube = _model.getCubeByRawIndex(rawCubeIndex);
362 // TODO(mcslee): clean this up, precompute paths
363 for (int stripIndex : GRIZZLY_STRIP_ORDERING) {
364 Strip strip = cube.strips.get(stripIndex);
365 int stripLen = ((stripIndex == 9) || (stripIndex == 16)) ? 15 : 16;
366 for (int i = stripLen-1; i >= 0; --i) {
367 int c = colors[strip.points.get(i).index];
368 byte r = (byte) ((c >> 16) & 0xFF);
369 byte g = (byte) ((c >> 8) & 0xFF);
370 byte b = (byte) ((c) & 0xFF);
371 packet[len++] = (byte) 0; // alpha channel, unused but makes for 4-byte alignment
372 packet[len++] = (byte) r;
373 packet[len++] = (byte) g;
374 packet[len++] = (byte) b;
378 // for (LXPoint p : cube.points) {
379 // int c = (p.index < 0) ? 0 : colors[p.index];
380 // byte r = (byte) ((c >> 16) & 0xFF);
381 // byte g = (byte) ((c >> 8) & 0xFF);
382 // byte b = (byte) ((c) & 0xFF);
383 // packet[len++] = (byte) 0; // alpha channel, unused but makes for 4-byte alignment
384 // packet[len++] = (byte) r;
385 // packet[len++] = (byte) g;
386 // packet[len++] = (byte) b;
390 // println("Packet number: " + packetNum);
391 sendPacket(frameNum, packetNum++);
394 // for (int index : points) {
395 // int c = (index < 0) ? 0 : colors[index];
396 // byte r = (byte) ((c >> 16) & 0xFF);
397 // byte g = (byte) ((c >> 8) & 0xFF);
398 // byte b = (byte) ((c) & 0xFF);
399 // packet[len++] = 0; // alpha channel, unused but makes for 4-byte alignment
400 // packet[len++] = r;
401 // packet[len++] = g;
402 // packet[len++] = b;
404 // // Flush once packet is full buffer size
405 // if (len >= packet.length) {
406 // sendPacket(packetNum++);
411 // // Flush any remaining data
413 // sendPacket(packetNum++);
418 private void sendPacket(int frameNum, int packetNum) {
419 // println("Sending frame #" + frameNum + ", channel # " + packetNum);
420 message.clearArguments();
421 message.add(frameNum);
422 message.add(0xDEADBEEF);
423 message.add(packetNum);
424 message.add(0xFEEDBEEF);
425 message.add(packet.length);
427 message.add(0xBEFFFFEB);
430 // OscP5.flush(message, address); // new DatagramSocket every time, no thanks
431 byte[] bytes = message.getBytes();
432 DatagramPacket packet = new DatagramPacket(bytes, bytes.length, address.inetaddress(), PORT);
434 } catch (Exception x) {