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 final String ip;
23 private final NetAddress address;
25 // Whether board output is enabled
26 private boolean enabled = false;
29 private final OscMessage message;
31 // List of point indices that get sent to this board
32 private final int[] points;
35 private final byte[] packet = new byte[4*352]; // magic number, our UDP packet size
37 private static final int NO_POINT = -1;
39 public PandaDriver(String ip) {
42 // Initialize our OSC output stuff
43 address = new NetAddress(ip, 9001);
44 message = new OscMessage("/shady/pointbuffer");
46 // Build the array of points, initialize all to nothing
47 points = new int[PandaMapping.PIXELS_PER_BOARD];
48 for (int i = 0; i < points.length; ++i) {
53 private final static int FORWARD = -1;
54 private final static int BACKWARD = -2;
56 ////////////////////////////////////////////////////////////////
58 // READ THIS RIGHT NOW BEFORE YOU MODIFY THE BELOW!!!!!!!!!!!!!
59 // READ THIS RIGHT NOW BEFORE YOU MODIFY THE BELOW!!!!!!!!!!!!!
60 // READ THIS RIGHT NOW BEFORE YOU MODIFY THE BELOW!!!!!!!!!!!!!
62 // The mappings below indicate the physical order of strips
63 // connected to a pandaboard channel. The strip numbers are a
64 // reflection of how the model is built.
66 // For ANYTHING in the model which is a rectangular prism,
67 // which means Cubes, the BassBox, and each Speaker, the
68 // strips are numbered incrementally by face. The first
69 // face is always the FRONT, which you are looking at.
70 // The next face is the RIGHT, then the BACK, then the LEFT.
72 // For every face, the strips are ordered numerically moving
73 // clockwise from the the TOP LEFT.
77 // Strip 0: front face, top strip, left to right
78 // Strip 1: front face, right strip, top to bottom
79 // Strip 2: front face, bottom strip, right to left
80 // Strip 3: front face, left strip, bottom to top
82 // Strip 4: right face, top strip, left to right
84 // Strip 14: left face, bottom strip, right to left
85 // Strip 15: left face, left strip, bottom to top
87 ////////////////////////////////////////////////////////////////
91 * These constant arrays indicate the order in which the strips of a cube
92 * are wired. There are four different options, depending on which bottom
93 * corner of the cube the data wire comes in.
95 private final static int[][] CUBE_STRIP_ORDERINGS = new int[][] {
96 { 2, 1, 0, 3, 13, 12, 15, 14, 4, 7, 6, 5, 11, 10, 9, 8 }, // FRONT_LEFT
97 { 6, 5, 4, 7, 1, 0, 3, 2, 8, 11, 10, 9, 15, 14, 13, 12 }, // FRONT_RIGHT
98 { 14, 13, 12, 15, 9, 8, 11, 10, 0, 3, 2, 1, 7, 6, 5, 4 }, // REAR_LEFT
99 { 10, 9, 8, 11, 5, 4, 7, 6, 12, 15, 14, 13, 3, 2, 1, 0 }, // REAR_RIGHT
102 private final static int[][] BASS_STRIP_ORDERING = {
103 // front face, counterclockwise from bottom front left
104 {2, BACKWARD /* if this strip has extra pixels, you can add them here */ /*, 4 */ },
105 {1, BACKWARD /* if this strip is short some pixels, substract them here */ /*, -3 */ },
109 // left face, counterclockwise from bottom front left
115 // back face, counterclockwise from bottom rear left
121 // right face, counterclockwise from bottom rear right
128 private final static int[][] STRUT_STRIP_ORDERING = {
139 private final static int[][] FLOOR_STRIP_ORDERING = {
146 // The speakers are currently configured to be wired the same
147 // as cubes with Wiring.FRONT_LEFT. If this needs to be changed,
148 // remove this null assignment and change the below to have mappings
149 // for the LEFT and RIGHT speaker
150 private final static int[][][] SPEAKER_STRIP_ORDERING = null; /* {
153 // Front face, counter-clockwise from bottom left
161 // Front face, counter-clockwise from bottom left
169 private final static int[][] LEFT_SPEAKER_STRIP_ORDERING = {
172 public PandaDriver(String ip, Model model, PandaMapping pm) {
175 // Ok, we are initialized, time to build the array if points in order to
176 // send out. We start at the head of our point buffer, and work our way
177 // down. This is the order in which points will be sent down the wire.
180 // Iterate through all our channels
181 for (ChannelMapping channel : pm.channelList) {
183 int pi = ci * ChannelMapping.PIXELS_PER_CHANNEL;
185 switch (channel.mode) {
187 case ChannelMapping.MODE_CUBES:
188 // We have a list of cubes per channel
189 for (int rawCubeIndex : channel.objectIndices) {
190 if (rawCubeIndex < 0) {
191 // No cube here, skip ahead in the buffer
192 pi += Cube.POINTS_PER_CUBE;
194 // The cube exists, check which way it is wired to
195 // figure out the order of strips.
196 Cube cube = model.getCubeByRawIndex(rawCubeIndex);
197 int stripOrderIndex = 0;
198 switch (cube.wiring) {
199 case FRONT_LEFT: stripOrderIndex = 0; break;
200 case FRONT_RIGHT: stripOrderIndex = 1; break;
201 case REAR_LEFT: stripOrderIndex = 2; break;
202 case REAR_RIGHT: stripOrderIndex = 3; break;
205 // Iterate through all the strips on the cube and add the points
206 for (int stripIndex : CUBE_STRIP_ORDERINGS[stripOrderIndex]) {
207 // We go backwards here... in the model strips go clockwise, but
208 // the physical wires are run counter-clockwise
209 pi = mapStrip(cube.strips.get(stripIndex), BACKWARD, points, pi);
215 case ChannelMapping.MODE_BASS:
216 for (int[] config : BASS_STRIP_ORDERING) {
217 pi = mapStrip(model.bassBox.strips.get(config[0]), config[1], points, pi);
218 if (config.length >= 3) pi += config[2];
222 case ChannelMapping.MODE_STRUTS_AND_FLOOR:
223 for (int[] config : STRUT_STRIP_ORDERING) {
224 pi = mapStrip(model.bassBox.struts.get(config[0]), config[1], points, pi);
225 if (config.length >= 3) pi += config[2];
227 for (int[] config : FLOOR_STRIP_ORDERING) {
228 pi = mapStrip(model.boothFloor.strips.get(config[0]), config[1], points, pi);
229 if (config.length >= 3) pi += config[2];
233 case ChannelMapping.MODE_SPEAKER:
234 int [][] speakerStripOrdering;
235 if (SPEAKER_STRIP_ORDERING == null) {
236 // Copy the cube strip ordering
237 int[] frontLeftCubeWiring = CUBE_STRIP_ORDERINGS[0];
238 speakerStripOrdering = new int[frontLeftCubeWiring.length][];
239 for (int i = 0; i < frontLeftCubeWiring.length; ++i) {
240 speakerStripOrdering[i] = new int[] { frontLeftCubeWiring[0], BACKWARD };
243 speakerStripOrdering = SPEAKER_STRIP_ORDERING[channel.objectIndices[0]];
245 for (int[] config : speakerStripOrdering) {
246 Speaker speaker = model.speakers.get(channel.objectIndices[0]);
247 pi = mapStrip(speaker.strips.get(config[0]), config[1], points, pi);
248 if (config.length >= 3) pi += config[2];
252 case ChannelMapping.MODE_NULL:
253 // No problem, nothing on this channel!
257 throw new RuntimeException("Invalid/unhandled channel mapping mode: " + channel.mode);
263 private int mapStrip(Strip s, int direction, int[] points, int pi) {
264 if (direction == FORWARD) {
265 for (Point p : s.points) {
266 points[pi++] = p.index;
268 } else if (direction == BACKWARD) {
269 for (int i = s.points.size()-1; i >= 0; --i) {
270 points[pi++] = s.points.get(i).index;
273 throw new RuntimeException("Unidentified strip mapping direction: " + direction);
278 public void disable() {
281 println("PandaBoard/" + ip + ": OFF");
285 public void enable() {
288 println("PandaBoard/" + ip + ": ON");
292 public void toggle() {
294 println("PandaBoard/" + ip + ": " + (enabled ? "ON" : "OFF"));
297 public final void send(int[] colors) {
303 for (int index : points) {
304 int c = (index < 0) ? 0 : colors[index];
305 byte r = (byte) ((c >> 16) & 0xFF);
306 byte g = (byte) ((c >> 8) & 0xFF);
307 byte b = (byte) ((c) & 0xFF);
308 packet[len++] = 0; // alpha channel, unused but makes for 4-byte alignment
313 // Flush once packet is full buffer size
314 if (len >= packet.length) {
315 sendPacket(packetNum++);
320 // Flush any remaining data
322 sendPacket(packetNum++);
326 private void sendPacket(int packetNum) {
327 message.clearArguments();
328 message.add(packetNum);
329 message.add(packet.length);
332 OscP5.flush(message, address);
333 } catch (Exception x) {