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 private final static int[][] BASS_STRIP_ORDERING = {
94 // front face, counterclockwise from bottom front left
95 {2, BACKWARD /* if this strip has extra pixels, you can add them here */ /*, 4 */ },
96 {1, BACKWARD /* if this strip is short some pixels, substract them here */ /*, -3 */ },
100 // left face, counterclockwise from bottom front left
106 // back face, counterclockwise from bottom rear left
112 // right face, counterclockwise from bottom rear right
119 private final static int[][] STRUT_STRIP_ORDERING = {
130 private final static int[][] FLOOR_STRIP_ORDERING = {
137 // The speakers are currently configured to be wired the same
138 // as cubes with Wiring.FRONT_LEFT. If this needs to be changed,
139 // remove this null assignment and change the below to have mappings
140 // for the LEFT and RIGHT speaker
141 private final static int[][][] SPEAKER_STRIP_ORDERING = {
144 // Front face, counter-clockwise from bottom left
152 // Front face, counter-clockwise from bottom left
160 public PandaDriver(String ip) {
163 // Initialize our OSC output stuff
164 address = new NetAddress(ip, 9001);
165 message = new OscMessage("/shady/pointbuffer");
167 // Build the array of points, initialize all to nothing
168 points = new int[PandaMapping.PIXELS_PER_BOARD];
169 for (int i = 0; i < points.length; ++i) {
170 points[i] = NO_POINT;
174 public PandaDriver(String ip, Model model, PandaMapping pm) {
177 // Ok, we are initialized, time to build the array if points in order to
178 // send out. We start at the head of our point buffer, and work our way
179 // down. This is the order in which points will be sent down the wire.
182 // Iterate through all our channels
183 for (ChannelMapping channel : pm.channelList) {
185 int pi = ci * ChannelMapping.PIXELS_PER_CHANNEL;
187 switch (channel.mode) {
189 case ChannelMapping.MODE_CUBES:
190 // We have a list of cubes per channel
191 for (int rawCubeIndex : channel.objectIndices) {
192 if (rawCubeIndex < 0) {
193 // No cube here, skip ahead in the buffer
194 pi += Cube.POINTS_PER_CUBE;
196 // The cube exists, check which way it is wired to
197 // figure out the order of strips.
198 Cube cube = model.getCubeByRawIndex(rawCubeIndex);
199 int stripOrderIndex = 0;
200 switch (cube.wiring) {
201 case FRONT_LEFT: stripOrderIndex = 0; break;
202 case FRONT_RIGHT: stripOrderIndex = 1; break;
203 case REAR_LEFT: stripOrderIndex = 2; break;
204 case REAR_RIGHT: stripOrderIndex = 3; break;
207 // Iterate through all the strips on the cube and add the points
208 for (int stripIndex : CUBE_STRIP_ORDERINGS[stripOrderIndex]) {
209 // We go backwards here... in the model strips go clockwise, but
210 // the physical wires are run counter-clockwise
211 pi = mapStrip(cube.strips.get(stripIndex), BACKWARD, points, pi);
217 case ChannelMapping.MODE_BASS:
218 for (int[] config : BASS_STRIP_ORDERING) {
219 pi = mapStrip(model.bassBox.strips.get(config[0]), config[1], points, pi);
220 if (config.length >= 3) pi += config[2];
224 case ChannelMapping.MODE_STRUTS_AND_FLOOR:
225 for (int[] config : STRUT_STRIP_ORDERING) {
226 pi = mapStrip(model.bassBox.struts.get(config[0]), config[1], points, pi);
227 if (config.length >= 3) pi += config[2];
229 for (int[] config : FLOOR_STRIP_ORDERING) {
230 pi = mapStrip(model.boothFloor.strips.get(config[0]), config[1], points, pi);
231 if (config.length >= 3) pi += config[2];
235 case ChannelMapping.MODE_SPEAKER:
236 int [][] speakerStripOrdering;
237 if (SPEAKER_STRIP_ORDERING == null) {
238 // Copy the cube strip ordering
239 int[] frontLeftCubeWiring = CUBE_STRIP_ORDERINGS[0];
240 speakerStripOrdering = new int[frontLeftCubeWiring.length][];
241 for (int i = 0; i < frontLeftCubeWiring.length; ++i) {
242 speakerStripOrdering[i] = new int[] { frontLeftCubeWiring[0], BACKWARD };
245 speakerStripOrdering = SPEAKER_STRIP_ORDERING[channel.objectIndices[0]];
247 for (int[] config : speakerStripOrdering) {
248 Speaker speaker = model.speakers.get(channel.objectIndices[0]);
249 pi = mapStrip(speaker.strips.get(config[0]), config[1], points, pi);
250 if (config.length >= 3) pi += config[2];
254 case ChannelMapping.MODE_NULL:
255 // No problem, nothing on this channel!
259 throw new RuntimeException("Invalid/unhandled channel mapping mode: " + channel.mode);
265 private int mapStrip(Strip s, int direction, int[] points, int pi) {
266 if (direction == FORWARD) {
267 for (Point p : s.points) {
268 points[pi++] = p.index;
270 } else if (direction == BACKWARD) {
271 for (int i = s.points.size()-1; i >= 0; --i) {
272 points[pi++] = s.points.get(i).index;
275 throw new RuntimeException("Unidentified strip mapping direction: " + direction);
280 public PandaDriver setListener(Listener listener) {
281 this.listener = listener;
285 public void setEnabled(boolean enabled) {
286 if (this.enabled != enabled) {
287 this.enabled = enabled;
288 println("PandaBoard/" + ip + ": " + (enabled ? "ON" : "OFF"));
289 if (listener != null) {
290 listener.onToggle(enabled);
295 public boolean isEnabled() {
299 public void disable() {
303 public void enable() {
307 public void toggle() {
308 setEnabled(!enabled);
311 public final void send(int[] colors) {
317 for (int index : points) {
318 int c = (index < 0) ? 0 : colors[index];
319 byte r = (byte) ((c >> 16) & 0xFF);
320 byte g = (byte) ((c >> 8) & 0xFF);
321 byte b = (byte) ((c) & 0xFF);
322 packet[len++] = 0; // alpha channel, unused but makes for 4-byte alignment
327 // Flush once packet is full buffer size
328 if (len >= packet.length) {
329 sendPacket(packetNum++);
334 // Flush any remaining data
336 sendPacket(packetNum++);
341 private void sendPacket(int packetNum) {
342 message.clearArguments();
343 message.add(packetNum);
344 message.add(packet.length);
347 OscP5.flush(message, address);
348 } catch (Exception x) {