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 {
19 public final String ip;
22 private final NetAddress address;
24 // Whether board output is enabled
25 private boolean enabled = false;
28 private final OscMessage message;
30 // List of point indices that get sent to this board
31 private final int[] points;
34 private final byte[] packet = new byte[4*352]; // magic number, our UDP packet size
36 private static final int NO_POINT = -1;
38 public PandaDriver(String ip) {
41 // Initialize our OSC output stuff
42 address = new NetAddress(ip, 9001);
43 message = new OscMessage("/shady/pointbuffer");
45 // Build the array of points, initialize all to nothing
46 points = new int[PandaMapping.PIXELS_PER_BOARD];
47 for (int i = 0; i < points.length; ++i) {
52 private final static int FORWARD = -1;
53 private final static int BACKWARD = -2;
55 ////////////////////////////////////////////////////////////////
57 // READ THIS RIGHT NOW BEFORE YOU MODIFY THE BELOW!!!!!!!!!!!!!
58 // READ THIS RIGHT NOW BEFORE YOU MODIFY THE BELOW!!!!!!!!!!!!!
59 // READ THIS RIGHT NOW BEFORE YOU MODIFY THE BELOW!!!!!!!!!!!!!
61 // The mappings below indicate the physical order of strips
62 // connected to a pandaboard channel. The strip numbers are a
63 // reflection of how the model is built.
65 // For ANYTHING in the model which is a rectangular prism,
66 // which means Cubes, the BassBox, and each Speaker, the
67 // strips are numbered incrementally by face. The first
68 // face is always the FRONT, which you are looking at.
69 // The next face is the RIGHT, then the BACK, then the LEFT.
71 // For every face, the strips are ordered numerically moving
72 // clockwise from the the TOP LEFT.
76 // Strip 0: front face, top strip, left to right
77 // Strip 1: front face, right strip, top to bottom
78 // Strip 2: front face, bottom strip, right to left
79 // Strip 3: front face, left strip, bottom to top
81 // Strip 4: right face, top strip, left to right
83 // Strip 14: left face, bottom strip, right to left
84 // Strip 15: left face, left strip, bottom to top
86 ////////////////////////////////////////////////////////////////
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 private final static int[][] BASS_STRIP_ORDERING = {
102 // front face, counterclockwise from bottom front left
103 {2, BACKWARD /* if this strip has extra pixels, you can add them here */ /*, 4 */ },
104 {1, BACKWARD /* if this strip is short some pixels, substract them here */ /*, -3 */ },
108 // left face, counterclockwise from bottom front left
114 // back face, counterclockwise from bottom rear left
120 // right face, counterclockwise from bottom rear right
127 private final static int[][] STRUT_STRIP_ORDERING = {
138 private final static int[][] FLOOR_STRIP_ORDERING = {
145 // The speakers are currently configured to be wired the same
146 // as cubes with Wiring.FRONT_LEFT. If this needs to be changed,
147 // remove this null assignment and change the below to have mappings
148 // for the LEFT and RIGHT speaker
149 private final static int[][][] SPEAKER_STRIP_ORDERING = {
152 // Front face, counter-clockwise from bottom left
160 // Front face, counter-clockwise from bottom left
168 public PandaDriver(String ip, Model model, PandaMapping pm) {
171 // Ok, we are initialized, time to build the array if points in order to
172 // send out. We start at the head of our point buffer, and work our way
173 // down. This is the order in which points will be sent down the wire.
176 // Iterate through all our channels
177 for (ChannelMapping channel : pm.channelList) {
179 int pi = ci * ChannelMapping.PIXELS_PER_CHANNEL;
181 switch (channel.mode) {
183 case ChannelMapping.MODE_CUBES:
184 // We have a list of cubes per channel
185 for (int rawCubeIndex : channel.objectIndices) {
186 if (rawCubeIndex < 0) {
187 // No cube here, skip ahead in the buffer
188 pi += Cube.POINTS_PER_CUBE;
190 // The cube exists, check which way it is wired to
191 // figure out the order of strips.
192 Cube cube = model.getCubeByRawIndex(rawCubeIndex);
193 int stripOrderIndex = 0;
194 switch (cube.wiring) {
195 case FRONT_LEFT: stripOrderIndex = 0; break;
196 case FRONT_RIGHT: stripOrderIndex = 1; break;
197 case REAR_LEFT: stripOrderIndex = 2; break;
198 case REAR_RIGHT: stripOrderIndex = 3; break;
201 // Iterate through all the strips on the cube and add the points
202 for (int stripIndex : CUBE_STRIP_ORDERINGS[stripOrderIndex]) {
203 // We go backwards here... in the model strips go clockwise, but
204 // the physical wires are run counter-clockwise
205 pi = mapStrip(cube.strips.get(stripIndex), BACKWARD, points, pi);
211 case ChannelMapping.MODE_BASS:
212 for (int[] config : BASS_STRIP_ORDERING) {
213 pi = mapStrip(model.bassBox.strips.get(config[0]), config[1], points, pi);
214 if (config.length >= 3) pi += config[2];
218 case ChannelMapping.MODE_STRUTS_AND_FLOOR:
219 for (int[] config : STRUT_STRIP_ORDERING) {
220 pi = mapStrip(model.bassBox.struts.get(config[0]), config[1], points, pi);
221 if (config.length >= 3) pi += config[2];
223 for (int[] config : FLOOR_STRIP_ORDERING) {
224 pi = mapStrip(model.boothFloor.strips.get(config[0]), config[1], points, pi);
225 if (config.length >= 3) pi += config[2];
229 case ChannelMapping.MODE_SPEAKER:
230 int [][] speakerStripOrdering;
231 if (SPEAKER_STRIP_ORDERING == null) {
232 // Copy the cube strip ordering
233 int[] frontLeftCubeWiring = CUBE_STRIP_ORDERINGS[0];
234 speakerStripOrdering = new int[frontLeftCubeWiring.length][];
235 for (int i = 0; i < frontLeftCubeWiring.length; ++i) {
236 speakerStripOrdering[i] = new int[] { frontLeftCubeWiring[0], BACKWARD };
239 speakerStripOrdering = SPEAKER_STRIP_ORDERING[channel.objectIndices[0]];
241 for (int[] config : speakerStripOrdering) {
242 Speaker speaker = model.speakers.get(channel.objectIndices[0]);
243 pi = mapStrip(speaker.strips.get(config[0]), config[1], points, pi);
244 if (config.length >= 3) pi += config[2];
248 case ChannelMapping.MODE_NULL:
249 // No problem, nothing on this channel!
253 throw new RuntimeException("Invalid/unhandled channel mapping mode: " + channel.mode);
259 private int mapStrip(Strip s, int direction, int[] points, int pi) {
260 if (direction == FORWARD) {
261 for (Point p : s.points) {
262 points[pi++] = p.index;
264 } else if (direction == BACKWARD) {
265 for (int i = s.points.size()-1; i >= 0; --i) {
266 points[pi++] = s.points.get(i).index;
269 throw new RuntimeException("Unidentified strip mapping direction: " + direction);
274 public void disable() {
277 println("PandaBoard/" + ip + ": OFF");
281 public void enable() {
284 println("PandaBoard/" + ip + ": ON");
288 public void toggle() {
290 println("PandaBoard/" + ip + ": " + (enabled ? "ON" : "OFF"));
293 public final void send(int[] colors) {
299 for (int index : points) {
300 int c = (index < 0) ? 0 : colors[index];
301 byte r = (byte) ((c >> 16) & 0xFF);
302 byte g = (byte) ((c >> 8) & 0xFF);
303 byte b = (byte) ((c) & 0xFF);
304 packet[len++] = 0; // alpha channel, unused but makes for 4-byte alignment
309 // Flush once packet is full buffer size
310 if (len >= packet.length) {
311 sendPacket(packetNum++);
316 // Flush any remaining data
318 sendPacket(packetNum++);
323 private void sendPacket(int packetNum) {
324 message.clearArguments();
325 message.add(packetNum);
326 message.add(packet.length);
329 OscP5.flush(message, address);
330 } catch (Exception x) {