5 * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND
8 * ///\\\ ///\\\ ///\\\ ///\\\
9 * \\\/// \\\/// \\\/// \\\///
12 * EXPERTS ONLY!! EXPERTS ONLY!!
14 * This class implements the output function to the Panda Boards. It
15 * will be moved into GLucose once stabilized.
17 public static class PandaDriver {
20 public void onToggle(boolean enabled);
23 private Listener listener = null;
26 public final String ip;
29 private final NetAddress address;
31 // Whether board output is enabled
32 private boolean enabled = false;
35 private final OscMessage message;
37 // List of point indices that get sent to this board
38 private final int[] points;
41 private final byte[] packet = new byte[4*352]; // magic number, our UDP packet size
43 private static final int NO_POINT = -1;
45 ////////////////////////////////////////////////////////////////
47 // READ THIS RIGHT NOW BEFORE YOU MODIFY THE BELOW!!!!!!!!!!!!!
48 // READ THIS RIGHT NOW BEFORE YOU MODIFY THE BELOW!!!!!!!!!!!!!
49 // READ THIS RIGHT NOW BEFORE YOU MODIFY THE BELOW!!!!!!!!!!!!!
51 // The mappings below indicate the physical order of strips
52 // connected to a pandaboard channel. The strip numbers are a
53 // reflection of how the model is built.
55 // For ANYTHING in the model which is a rectangular prism,
56 // which means Cubes, the BassBox, and each Speaker, the
57 // strips are numbered incrementally by face. The first
58 // face is always the FRONT, which you are looking at.
59 // The next face is the RIGHT, then the BACK, then the LEFT.
61 // For every face, the strips are ordered numerically moving
62 // clockwise from the the TOP LEFT.
66 // Strip 0: front face, top strip, left to right
67 // Strip 1: front face, right strip, top to bottom
68 // Strip 2: front face, bottom strip, right to left
69 // Strip 3: front face, left strip, bottom to top
71 // Strip 4: right face, top strip, left to right
73 // Strip 14: left face, bottom strip, right to left
74 // Strip 15: left face, left strip, bottom to top
76 ////////////////////////////////////////////////////////////////
78 private final static int FORWARD = -1;
79 private final static int BACKWARD = -2;
82 * These constant arrays indicate the order in which the strips of a cube
83 * are wired. There are four different options, depending on which bottom
84 * corner of the cube the data wire comes in.
86 private final static int[][] CUBE_STRIP_ORDERINGS = new int[][] {
87 // { 2, 1, 0, 3, 13, 12, 15, 14, 4, 7, 6, 5, 11, 10, 9, 8 }, // FRONT_LEFT
88 // { 6, 5, 4, 7, 1, 0, 3, 2, 8, 11, 10, 9, 15, 14, 13, 12 }, // FRONT_RIGHT
89 // { 14, 13, 12, 15, 9, 8, 11, 10, 0, 3, 2, 1, 7, 6, 5, 4 }, // REAR_LEFT
90 // { 10, 9, 8, 11, 5, 4, 7, 6, 12, 15, 14, 13, 3, 2, 1, 0 }, // REAR_RIGHT
93 { 2, 1, 0, 3, 13, 12, 15, 14, 4, 7, 6, 5, 11, 10, 9, 8 }, // FRONT_LEFT
94 { 6, 5, 4, 7, 1, 0, 3, 2, 8, 11, 10, 9, 15, 14, 13, 12 }, // FRONT_RIGHT
95 { 14, 13, 12, 15, 9, 8, 11, 10, 0, 3, 2, 1, 7, 6, 5, 4 }, // REAR_LEFT
96 { 9, 8, 11, 5, 4, 7, 6, 10, 14, 2, 1, 0, 3, 13, 12, 15 }, // REAR_RIGHT
100 private final static int[][] BASS_STRIP_ORDERING = {
101 // front face, counterclockwise from bottom front left
102 {2, BACKWARD /* if this strip has extra pixels, you can add them here */ /*, 4 */ },
103 {1, BACKWARD /* if this strip is short some pixels, substract them here */ /*, -3 */ },
107 // left face, counterclockwise from bottom front left
113 // back face, counterclockwise from bottom rear left
119 // right face, counterclockwise from bottom rear right
126 private final static int[][] STRUT_STRIP_ORDERING = {
137 private final static int[][] FLOOR_STRIP_ORDERING = {
144 // The speakers are currently configured to be wired the same
145 // as cubes with Wiring.FRONT_LEFT. If this needs to be changed,
146 // remove this null assignment and change the below to have mappings
147 // for the LEFT and RIGHT speaker
148 private final static int[][][] SPEAKER_STRIP_ORDERING = {
151 // Front face, counter-clockwise from bottom left
159 // Front face, counter-clockwise from bottom left
167 public PandaDriver(String ip) {
170 // Initialize our OSC output stuff
171 address = new NetAddress(ip, 9001);
172 message = new OscMessage("/shady/pointbuffer");
174 // Build the array of points, initialize all to nothing
175 points = new int[PandaMapping.PIXELS_PER_BOARD];
176 for (int i = 0; i < points.length; ++i) {
177 points[i] = NO_POINT;
181 public PandaDriver(String ip, Model model, PandaMapping pm) {
184 // Ok, we are initialized, time to build the array if points in order to
185 // send out. We start at the head of our point buffer, and work our way
186 // down. This is the order in which points will be sent down the wire.
189 // Iterate through all our channels
190 for (ChannelMapping channel : pm.channelList) {
192 int pi = ci * ChannelMapping.PIXELS_PER_CHANNEL;
194 switch (channel.mode) {
196 case ChannelMapping.MODE_CUBES:
197 // We have a list of cubes per channel
198 for (int rawCubeIndex : channel.objectIndices) {
199 if (rawCubeIndex < 0) {
200 // No cube here, skip ahead in the buffer
201 pi += Cube.POINTS_PER_CUBE;
203 // The cube exists, check which way it is wired to
204 // figure out the order of strips.
205 Cube cube = model.getCubeByRawIndex(rawCubeIndex);
206 int stripOrderIndex = 0;
207 switch (cube.wiring) {
208 case FRONT_LEFT: stripOrderIndex = 0; break;
209 case FRONT_RIGHT: stripOrderIndex = 1; break;
210 case REAR_LEFT: stripOrderIndex = 2; break;
211 case REAR_RIGHT: stripOrderIndex = 3; break;
214 // Iterate through all the strips on the cube and add the points
215 for (int stripIndex : CUBE_STRIP_ORDERINGS[stripOrderIndex]) {
216 // We go backwards here... in the model strips go clockwise, but
217 // the physical wires are run counter-clockwise
218 pi = mapStrip(cube.strips.get(stripIndex), BACKWARD, points, pi);
224 case ChannelMapping.MODE_BASS:
225 for (int[] config : BASS_STRIP_ORDERING) {
226 pi = mapStrip(model.bassBox.strips.get(config[0]), config[1], points, pi);
227 if (config.length >= 3) pi += config[2];
231 case ChannelMapping.MODE_STRUTS_AND_FLOOR:
232 for (int[] config : STRUT_STRIP_ORDERING) {
233 pi = mapStrip(model.bassBox.struts.get(config[0]), config[1], points, pi);
234 if (config.length >= 3) pi += config[2];
236 for (int[] config : FLOOR_STRIP_ORDERING) {
237 pi = mapStrip(model.boothFloor.strips.get(config[0]), config[1], points, pi);
238 if (config.length >= 3) pi += config[2];
242 case ChannelMapping.MODE_SPEAKER:
243 int [][] speakerStripOrdering;
244 if (SPEAKER_STRIP_ORDERING == null) {
245 // Copy the cube strip ordering
246 int[] frontLeftCubeWiring = CUBE_STRIP_ORDERINGS[0];
247 speakerStripOrdering = new int[frontLeftCubeWiring.length][];
248 for (int i = 0; i < frontLeftCubeWiring.length; ++i) {
249 speakerStripOrdering[i] = new int[] { frontLeftCubeWiring[0], BACKWARD };
252 speakerStripOrdering = SPEAKER_STRIP_ORDERING[channel.objectIndices[0]];
254 for (int[] config : speakerStripOrdering) {
255 Speaker speaker = model.speakers.get(channel.objectIndices[0]);
256 pi = mapStrip(speaker.strips.get(config[0]), config[1], points, pi);
257 if (config.length >= 3) pi += config[2];
261 case ChannelMapping.MODE_NULL:
262 // No problem, nothing on this channel!
266 throw new RuntimeException("Invalid/unhandled channel mapping mode: " + channel.mode);
272 private int mapStrip(Strip s, int direction, int[] points, int pi) {
273 if (direction == FORWARD) {
274 for (LXPoint p : s.points) {
275 points[pi++] = p.index;
277 } else if (direction == BACKWARD) {
278 for (int i = s.points.size()-1; i >= 0; --i) {
279 points[pi++] = s.points.get(i).index;
282 throw new RuntimeException("Unidentified strip mapping direction: " + direction);
287 public PandaDriver setListener(Listener listener) {
288 this.listener = listener;
292 public void setEnabled(boolean enabled) {
293 if (this.enabled != enabled) {
294 this.enabled = enabled;
295 println("PandaBoard/" + ip + ": " + (enabled ? "ON" : "OFF"));
296 if (listener != null) {
297 listener.onToggle(enabled);
302 public boolean isEnabled() {
306 public void disable() {
310 public void enable() {
314 public void toggle() {
315 setEnabled(!enabled);
318 public final void send(int[] colors) {
324 for (int index : points) {
325 int c = (index < 0) ? 0 : colors[index];
326 byte r = (byte) ((c >> 16) & 0xFF);
327 byte g = (byte) ((c >> 8) & 0xFF);
328 byte b = (byte) ((c) & 0xFF);
329 packet[len++] = 0; // alpha channel, unused but makes for 4-byte alignment
334 // Flush once packet is full buffer size
335 if (len >= packet.length) {
336 sendPacket(packetNum++);
341 // Flush any remaining data
343 sendPacket(packetNum++);
348 private void sendPacket(int packetNum) {
349 message.clearArguments();
350 message.add(packetNum);
351 message.add(packet.length);
354 OscP5.flush(message, address);
355 } catch (Exception x) {