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;
32 private final NetAddress address;
34 // Whether board output is enabled
35 private boolean enabled = false;
37 // Frame count for Grizzlies
38 private int frameNum = 1;
41 private final OscMessage message;
43 // List of point indices that get sent to this board
44 private final int[] points;
47 private final byte[] packet = new byte[4*280]; // magic number, our UDP packet size
49 private static final int NO_POINT = -1;
53 ////////////////////////////////////////////////////////////////
55 // READ THIS RIGHT NOW BEFORE YOU MODIFY THE BELOW!!!!!!!!!!!!!
56 // READ THIS RIGHT NOW BEFORE YOU MODIFY THE BELOW!!!!!!!!!!!!!
57 // READ THIS RIGHT NOW BEFORE YOU MODIFY THE BELOW!!!!!!!!!!!!!
59 // The mappings below indicate the physical order of strips
60 // connected to a pandaboard channel. The strip numbers are a
61 // reflection of how the model is built.
63 // For ANYTHING in the model which is a rectangular prism,
64 // which means Cubes, the BassBox, and each Speaker, the
65 // strips are numbered incrementally by face. The first
66 // face is always the FRONT, which you are looking at.
67 // The next face is the RIGHT, then the BACK, then the LEFT.
69 // For every face, the strips are ordered numerically moving
70 // clockwise from the the TOP LEFT.
74 // Strip 0: front face, top strip, left to right
75 // Strip 1: front face, right strip, top to bottom
76 // Strip 2: front face, bottom strip, right to left
77 // Strip 3: front face, left strip, bottom to top
79 // Strip 4: right face, top strip, left to right
81 // Strip 14: left face, bottom strip, right to left
82 // Strip 15: left face, left strip, bottom to top
84 ////////////////////////////////////////////////////////////////
86 private final static int FORWARD = -1;
87 private final static int BACKWARD = -2;
90 * These constant arrays indicate the order in which the strips of a cube
91 * are wired. There are four different options, depending on which bottom
92 * corner of the cube the data wire comes in.
94 private final static int[][] CUBE_STRIP_ORDERINGS = new int[][] {
95 // { 2, 1, 0, 3, 13, 12, 15, 14, 4, 7, 6, 5, 11, 10, 9, 8 }, // FRONT_LEFT
96 // { 6, 5, 4, 7, 1, 0, 3, 2, 8, 11, 10, 9, 15, 14, 13, 12 }, // FRONT_RIGHT
97 // { 14, 13, 12, 15, 9, 8, 11, 10, 0, 3, 2, 1, 7, 6, 5, 4 }, // REAR_LEFT
98 // { 10, 9, 8, 11, 5, 4, 7, 6, 12, 15, 14, 13, 3, 2, 1, 0 }, // REAR_RIGHT
101 { 2, 1, 0, 3, 13, 12, 15, 14, 4, 7, 6, 5, 11, 10, 9, 8 }, // FRONT_LEFT
102 { 6, 5, 4, 7, 1, 0, 3, 2, 8, 11, 10, 9, 15, 14, 13, 12 }, // FRONT_RIGHT
103 { 14, 13, 12, 15, 9, 8, 11, 10, 0, 3, 2, 1, 7, 6, 5, 4 }, // REAR_LEFT
104 { 9, 8, 11, 5, 4, 7, 6, 10, 14, 2, 1, 0, 3, 13, 12, 15 }, // REAR_RIGHT
108 private final static int[][] BASS_STRIP_ORDERING = {
109 // front face, counterclockwise from bottom front left
110 {2, BACKWARD /* if this strip has extra pixels, you can add them here */ /*, 4 */ },
111 {1, BACKWARD /* if this strip is short some pixels, substract them here */ /*, -3 */ },
115 // left face, counterclockwise from bottom front left
121 // back face, counterclockwise from bottom rear left
127 // right face, counterclockwise from bottom rear right
134 private final static int[][] STRUT_STRIP_ORDERING = {
145 private final static int[][] FLOOR_STRIP_ORDERING = {
152 // The speakers are currently configured to be wired the same
153 // as cubes with Wiring.FRONT_LEFT. If this needs to be changed,
154 // remove this null assignment and change the below to have mappings
155 // for the LEFT and RIGHT speaker
156 private final static int[][][] SPEAKER_STRIP_ORDERING = {
159 // Front face, counter-clockwise from bottom left
167 // Front face, counter-clockwise from bottom left
175 public PandaDriver(String ip) {
178 // Initialize our OSC output stuff
179 address = new NetAddress(ip, 779);
180 message = new OscMessage("/shady/pointbuffer");
182 // Build the array of points, initialize all to nothing
183 points = new int[PandaMapping.PIXELS_PER_BOARD];
184 for (int i = 0; i < points.length; ++i) {
185 points[i] = NO_POINT;
189 public PandaDriver(String ip, Model model, PandaMapping _pm) {
193 // Ok, we are initialized, time to build the array if points in order to
194 // send out. We start at the head of our point buffer, and work our way
195 // down. This is the order in which points will be sent down the wire.
198 // Iterate through all our channelq s
199 for (ChannelMapping channel : pm.channelList) {
201 int pi = ci * ChannelMapping.PIXELS_PER_CHANNEL;
203 switch (channel.mode) {
205 case ChannelMapping.MODE_CUBES:
206 // We have a list of cubes per channel
207 for (int rawCubeIndex : channel.objectIndices) {
208 if (rawCubeIndex < 0) {
209 // No cube here, skip ahead in the buffer
210 pi += Cube.POINTS_PER_CUBE;
212 // The cube exists, check which way it is wired to
213 // figure out the order of strips.
214 Cube cube = model.getCubeByRawIndex(rawCubeIndex);
215 int stripOrderIndex = 0;
216 switch (cube.wiring) {
217 case FRONT_LEFT: stripOrderIndex = 0; break;
218 case FRONT_RIGHT: stripOrderIndex = 1; break;
219 case REAR_LEFT: stripOrderIndex = 2; break;
220 case REAR_RIGHT: stripOrderIndex = 3; break;
223 // Iterate through all the strips on the cube and add the points
224 for (int stripIndex : CUBE_STRIP_ORDERINGS[stripOrderIndex]) {
225 // We go backwards here... in the model strips go clockwise, but
226 // the physical wires are run counter-clockwise
227 pi = mapStrip(cube.strips.get(stripIndex), BACKWARD, points, pi);
233 case ChannelMapping.MODE_BASS:
234 for (int[] config : BASS_STRIP_ORDERING) {
235 pi = mapStrip(model.bassBox.strips.get(config[0]), config[1], points, pi);
236 if (config.length >= 3) pi += config[2];
240 case ChannelMapping.MODE_STRUTS_AND_FLOOR:
241 for (int[] config : STRUT_STRIP_ORDERING) {
242 pi = mapStrip(model.bassBox.struts.get(config[0]), config[1], points, pi);
243 if (config.length >= 3) pi += config[2];
245 for (int[] config : FLOOR_STRIP_ORDERING) {
246 pi = mapStrip(model.boothFloor.strips.get(config[0]), config[1], points, pi);
247 if (config.length >= 3) pi += config[2];
251 case ChannelMapping.MODE_SPEAKER:
252 int [][] speakerStripOrdering;
253 if (SPEAKER_STRIP_ORDERING == null) {
254 // Copy the cube strip ordering
255 int[] frontLeftCubeWiring = CUBE_STRIP_ORDERINGS[0];
256 speakerStripOrdering = new int[frontLeftCubeWiring.length][];
257 for (int i = 0; i < frontLeftCubeWiring.length; ++i) {
258 speakerStripOrdering[i] = new int[] { frontLeftCubeWiring[0], BACKWARD };
261 speakerStripOrdering = SPEAKER_STRIP_ORDERING[channel.objectIndices[0]];
263 for (int[] config : speakerStripOrdering) {
264 Speaker speaker = model.speakers.get(channel.objectIndices[0]);
265 pi = mapStrip(speaker.strips.get(config[0]), config[1], points, pi);
266 if (config.length >= 3) pi += config[2];
270 case ChannelMapping.MODE_NULL:
271 // No problem, nothing on this channel!
275 throw new RuntimeException("Invalid/unhandled channel mapping mode: " + channel.mode);
281 private int mapStrip(Strip s, int direction, int[] points, int pi) {
282 if (direction == FORWARD) {
283 for (LXPoint p : s.points) {
284 points[pi++] = p.index;
286 } else if (direction == BACKWARD) {
287 for (int i = s.points.size()-1; i >= 0; --i) {
288 points[pi++] = s.points.get(i).index;
291 throw new RuntimeException("Unidentified strip mapping direction: " + direction);
296 public PandaDriver setListener(Listener listener) {
297 this.listener = listener;
301 public void setEnabled(boolean enabled) {
302 if (this.enabled != enabled) {
303 this.enabled = enabled;
304 println("PandaBoard/" + ip + ": " + (enabled ? "ON" : "OFF"));
305 if (listener != null) {
306 listener.onToggle(enabled);
311 public boolean isEnabled() {
315 public void disable() {
319 public void enable() {
323 public void toggle() {
324 setEnabled(!enabled);
327 public final void send(int[] colors) {
334 for (ChannelMapping channel : pm.channelList) {
335 for (int j: channel.objectIndices) {
337 Cube cube = _model.getCubeByRawIndex(j);
338 for (LXPoint p : cube.points) {
339 int c = (p.index < 0) ? 0 : colors[p.index];
340 byte r = (byte) ((c >> 16) & 0xFF);
341 byte g = (byte) ((c >> 8) & 0xFF);
342 byte b = (byte) ((c) & 0xFF);
343 packet[len++] = (byte) 0; // alpha channel, unused but makes for 4-byte alignment
344 packet[len++] = (byte) r;
345 packet[len++] = (byte) g;
346 packet[len++] = (byte) b;
350 // println("Packet number: " + packetNum);
351 sendPacket(frameNum, packetNum++);
354 // for (int index : points) {
355 // int c = (index < 0) ? 0 : colors[index];
356 // byte r = (byte) ((c >> 16) & 0xFF);
357 // byte g = (byte) ((c >> 8) & 0xFF);
358 // byte b = (byte) ((c) & 0xFF);
359 // packet[len++] = 0; // alpha channel, unused but makes for 4-byte alignment
360 // packet[len++] = r;
361 // packet[len++] = g;
362 // packet[len++] = b;
364 // // Flush once packet is full buffer size
365 // if (len >= packet.length) {
366 // sendPacket(packetNum++);
371 // // Flush any remaining data
373 // sendPacket(packetNum++);
378 private void sendPacket(int frameNum, int packetNum) {
379 // println("Sending frame #" + frameNum + ", channel # " + packetNum);
380 message.clearArguments();
381 message.add(frameNum);
382 message.add(0xDEADBEEF);
383 message.add(packetNum);
384 message.add(0xFEEDBEEF);
385 message.add(packet.length);
387 message.add(0xBEFFFFEB);
390 OscP5.flush(message, address);
391 } catch (Exception x) {