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;
57 * These constant arrays indicate the order in which the strips of a cube
58 * are wired. There are four different options, depending on which bottom
59 * corner of the cube the data wire comes in.
61 private final static int[][] CUBE_STRIP_ORDERINGS = new int[][] {
62 { 2, 1, 0, 3, 13, 12, 15, 14, 4, 7, 6, 5, 11, 10, 9, 8 }, // FRONT_LEFT
63 { 6, 5, 4, 7, 1, 0, 3, 2, 8, 11, 10, 9, 15, 14, 13, 12 }, // FRONT_RIGHT
64 { 14, 13, 12, 15, 9, 8, 11, 10, 0, 3, 2, 1, 7, 6, 5, 4 }, // REAR_LEFT
65 { 10, 9, 8, 11, 5, 4, 7, 6, 12, 15, 14, 13, 3, 2, 1, 0 }, // REAR_RIGHT
68 private final static int[][] BASS_STRIP_ORDERING = {
69 // front face, counterclockwise from bottom front left
75 // left face, counterclockwise from bottom front left
81 // back face, counterclockwise from bottom rear left
87 // right face, counterclockwise from bottom rear right
94 private final static int[][] STRUT_STRIP_ORDERING = {
101 {0, BACKWARD, /* dummy pixels at the end of string, remove when strip is fixed */ 4},
105 private final static int[][] FLOOR_STRIP_ORDERING = {
112 // The speakers are currently configured to be wired the same
113 // as cubes with Wiring.FRONT_LEFT. If this needs to be changed,
114 // remove this null assignment and change the below to have mappings
115 // for the LEFT and RIGHT speaker
116 private final static int[][][] SPEAKER_STRIP_ORDERING = null; /* {
119 // Front face, counter-clockwise from bottom left
127 // Front face, counter-clockwise from bottom left
135 private final static int[][] LEFT_SPEAKER_STRIP_ORDERING = {
138 public PandaDriver(String ip, Model model, PandaMapping pm) {
141 // Ok, we are initialized, time to build the array if points in order to
142 // send out. We start at the head of our point buffer, and work our way
143 // down. This is the order in which points will be sent down the wire.
146 // Iterate through all our channels
147 for (ChannelMapping channel : pm.channelList) {
149 int pi = ci * ChannelMapping.PIXELS_PER_CHANNEL;
151 switch (channel.mode) {
153 case ChannelMapping.MODE_CUBES:
154 // We have a list of cubes per channel
155 for (int rawCubeIndex : channel.objectIndices) {
156 if (rawCubeIndex < 0) {
157 // No cube here, skip ahead in the buffer
158 pi += Cube.POINTS_PER_CUBE;
160 // The cube exists, check which way it is wired to
161 // figure out the order of strips.
162 Cube cube = model.getCubeByRawIndex(rawCubeIndex);
163 int stripOrderIndex = 0;
164 switch (cube.wiring) {
165 case FRONT_LEFT: stripOrderIndex = 0; break;
166 case FRONT_RIGHT: stripOrderIndex = 1; break;
167 case REAR_LEFT: stripOrderIndex = 2; break;
168 case REAR_RIGHT: stripOrderIndex = 3; break;
171 // Iterate through all the strips on the cube and add the points
172 for (int stripIndex : CUBE_STRIP_ORDERINGS[stripOrderIndex]) {
173 // We go backwards here... in the model strips go clockwise, but
174 // the physical wires are run counter-clockwise
175 pi = mapStrip(cube.strips.get(stripIndex), BACKWARD, points, pi);
181 case ChannelMapping.MODE_BASS:
182 for (int[] config : BASS_STRIP_ORDERING) {
183 pi = mapStrip(model.bassBox.strips.get(config[0]), config[1], points, pi);
184 if (config.length >= 3) pi += config[2];
188 case ChannelMapping.MODE_STRUTS_AND_FLOOR:
189 for (int[] config : STRUT_STRIP_ORDERING) {
190 pi = mapStrip(model.bassBox.struts.get(config[0]), config[1], points, pi);
191 if (config.length >= 3) pi += config[2];
193 for (int[] config : FLOOR_STRIP_ORDERING) {
194 pi = mapStrip(model.boothFloor.strips.get(config[0]), config[1], points, pi);
195 if (config.length >= 3) pi += config[2];
199 case ChannelMapping.MODE_SPEAKER:
200 int [][] speakerStripOrdering;
201 if (SPEAKER_STRIP_ORDERING == null) {
202 // Copy the cube strip ordering
203 int[] frontLeftCubeWiring = CUBE_STRIP_ORDERINGS[0];
204 speakerStripOrdering = new int[frontLeftCubeWiring.length][];
205 for (int i = 0; i < frontLeftCubeWiring.length; ++i) {
206 speakerStripOrdering[i] = new int[] { frontLeftCubeWiring[0], BACKWARD };
209 speakerStripOrdering = SPEAKER_STRIP_ORDERING[channel.objectIndices[0]];
211 for (int[] config : speakerStripOrdering) {
212 Speaker speaker = model.speakers.get(channel.objectIndices[0]);
213 pi = mapStrip(speaker.strips.get(config[0]), config[1], points, pi);
214 if (config.length >= 3) pi += config[2];
218 case ChannelMapping.MODE_NULL:
219 // No problem, nothing on this channel!
223 throw new RuntimeException("Invalid/unhandled channel mapping mode: " + channel.mode);
229 private int mapStrip(Strip s, int direction, int[] points, int pi) {
230 if (direction == FORWARD) {
231 for (Point p : s.points) {
232 points[pi++] = p.index;
234 } else if (direction == BACKWARD) {
235 for (int i = s.points.size()-1; i >= 0; --i) {
236 points[pi++] = s.points.get(i).index;
239 throw new RuntimeException("Unidentified strip mapping direction: " + direction);
244 public void disable() {
247 println("PandaBoard/" + ip + ": OFF");
251 public void enable() {
254 println("PandaBoard/" + ip + ": ON");
258 public void toggle() {
260 println("PandaBoard/" + ip + ": " + (enabled ? "ON" : "OFF"));
263 public final void send(int[] colors) {
269 for (int index : points) {
270 int c = (index < 0) ? 0 : colors[index];
271 byte r = (byte) ((c >> 16) & 0xFF);
272 byte g = (byte) ((c >> 8) & 0xFF);
273 byte b = (byte) ((c) & 0xFF);
274 packet[len++] = 0; // alpha channel, unused but makes for 4-byte alignment
279 // Flush once packet is full buffer size
280 if (len >= packet.length) {
281 sendPacket(packetNum++);
286 // Flush any remaining data
288 sendPacket(packetNum++);
292 private void sendPacket(int packetNum) {
293 message.clearArguments();
294 message.add(packetNum);
295 message.add(packet.length);
298 OscP5.flush(message, address);
299 } catch (Exception x) {