4 //import hypermedia.net.*;
10 * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND
13 * ///\\\ ///\\\ ///\\\ ///\\\
14 * \\\/// \\\/// \\\/// \\\///
17 * EXPERTS ONLY!! EXPERTS ONLY!!
19 * This class implements the output function to the Panda Boards. It
20 * will be moved into GLucose once stabilized.
22 public static class PandaDriver extends Thread{
24 void start(){super.start();}
28 for(int i=0; i<queue.size(); i++){
29 this.sendNow(queue.get(queue.size()-1));
33 try{sleep(1);} catch(Exception e){}
38 //UDP udp; // define the UDP object
41 public final String ip;
44 private final NetAddress address;
46 // Whether board output is enabled
47 private boolean enabled = false;
50 private final OscMessage message;
52 // List of point indices that get sent to this board
53 private final int[] points;
56 private final byte[] packet = new byte[4*352]; // magic number, our UDP packet size
58 private static final int NO_POINT = -1;
60 public PandaDriver(String ip) {
63 //udp = new UDP(this, 10000);
65 // Initialize our OSC output stuff
66 address = new NetAddress(ip, 9001);
67 message = new OscMessage("/shady/pointbuffer");
69 // Build the array of points, initialize all to nothing
70 points = new int[PandaMapping.PIXELS_PER_BOARD];
71 for (int i = 0; i < points.length; ++i) {
77 private final static int FORWARD = -1;
78 private final static int BACKWARD = -2;
80 ////////////////////////////////////////////////////////////////
82 // READ THIS RIGHT NOW BEFORE YOU MODIFY THE BELOW!!!!!!!!!!!!!
83 // READ THIS RIGHT NOW BEFORE YOU MODIFY THE BELOW!!!!!!!!!!!!!
84 // READ THIS RIGHT NOW BEFORE YOU MODIFY THE BELOW!!!!!!!!!!!!!
86 // The mappings below indicate the physical order of strips
87 // connected to a pandaboard channel. The strip numbers are a
88 // reflection of how the model is built.
90 // For ANYTHING in the model which is a rectangular prism,
91 // which means Cubes, the BassBox, and each Speaker, the
92 // strips are numbered incrementally by face. The first
93 // face is always the FRONT, which you are looking at.
94 // The next face is the RIGHT, then the BACK, then the LEFT.
96 // For every face, the strips are ordered numerically moving
97 // clockwise from the the TOP LEFT.
101 // Strip 0: front face, top strip, left to right
102 // Strip 1: front face, right strip, top to bottom
103 // Strip 2: front face, bottom strip, right to left
104 // Strip 3: front face, left strip, bottom to top
106 // Strip 4: right face, top strip, left to right
108 // Strip 14: left face, bottom strip, right to left
109 // Strip 15: left face, left strip, bottom to top
111 ////////////////////////////////////////////////////////////////
115 * These constant arrays indicate the order in which the strips of a cube
116 * are wired. There are four different options, depending on which bottom
117 * corner of the cube the data wire comes in.
119 private final static int[][] CUBE_STRIP_ORDERINGS = new int[][] {
120 { 2, 1, 0, 3, 13, 12, 15, 14, 4, 7, 6, 5, 11, 10, 9, 8 }, // FRONT_LEFT
121 { 6, 5, 4, 7, 1, 0, 3, 2, 8, 11, 10, 9, 15, 14, 13, 12 }, // FRONT_RIGHT
122 { 14, 13, 12, 15, 9, 8, 11, 10, 0, 3, 2, 1, 7, 6, 5, 4 }, // REAR_LEFT
123 { 10, 9, 8, 11, 5, 4, 7, 6, 12, 15, 14, 13, 3, 2, 1, 0 }, // REAR_RIGHT
126 private final static int[][] BASS_STRIP_ORDERING = {
127 // front face, counterclockwise from bottom front left
128 {2, BACKWARD /* if this strip has extra pixels, you can add them here */ /*, 4 */ },
129 {1, BACKWARD /* if this strip is short some pixels, substract them here */ /*, -3 */ },
133 // left face, counterclockwise from bottom front left
139 // back face, counterclockwise from bottom rear left
145 // right face, counterclockwise from bottom rear right
152 private final static int[][] STRUT_STRIP_ORDERING = {
163 private final static int[][] FLOOR_STRIP_ORDERING = {
170 // The speakers are currently configured to be wired the same
171 // as cubes with Wiring.FRONT_LEFT. If this needs to be changed,
172 // remove this null assignment and change the below to have mappings
173 // for the LEFT and RIGHT speaker
174 private final static int[][][] SPEAKER_STRIP_ORDERING = null; /* {
177 // Front face, counter-clockwise from bottom left
185 // Front face, counter-clockwise from bottom left
193 private final static int[][] LEFT_SPEAKER_STRIP_ORDERING = {
196 ArrayList<int[]> queue;
198 public PandaDriver(String ip, Model model, PandaMapping pm) {
201 queue = new ArrayList<int[]>();
203 // Ok, we are initialized, time to build the array if points in order to
204 // send out. We start at the head of our point buffer, and work our way
205 // down. This is the order in which points will be sent down the wire.
208 // Iterate through all our channels
209 for (ChannelMapping channel : pm.channelList) {
211 int pi = ci * ChannelMapping.PIXELS_PER_CHANNEL;
213 switch (channel.mode) {
215 case ChannelMapping.MODE_CUBES:
216 // We have a list of cubes per channel
217 for (int rawCubeIndex : channel.objectIndices) {
218 if (rawCubeIndex < 0) {
219 // No cube here, skip ahead in the buffer
220 pi += Cube.POINTS_PER_CUBE;
222 // The cube exists, check which way it is wired to
223 // figure out the order of strips.
224 Cube cube = model.getCubeByRawIndex(rawCubeIndex);
225 int stripOrderIndex = 0;
226 switch (cube.wiring) {
227 case FRONT_LEFT: stripOrderIndex = 0; break;
228 case FRONT_RIGHT: stripOrderIndex = 1; break;
229 case REAR_LEFT: stripOrderIndex = 2; break;
230 case REAR_RIGHT: stripOrderIndex = 3; break;
233 // Iterate through all the strips on the cube and add the points
234 for (int stripIndex : CUBE_STRIP_ORDERINGS[stripOrderIndex]) {
235 // We go backwards here... in the model strips go clockwise, but
236 // the physical wires are run counter-clockwise
237 pi = mapStrip(cube.strips.get(stripIndex), BACKWARD, points, pi);
243 case ChannelMapping.MODE_BASS:
244 for (int[] config : BASS_STRIP_ORDERING) {
245 pi = mapStrip(model.bassBox.strips.get(config[0]), config[1], points, pi);
246 if (config.length >= 3) pi += config[2];
250 case ChannelMapping.MODE_STRUTS_AND_FLOOR:
251 for (int[] config : STRUT_STRIP_ORDERING) {
252 pi = mapStrip(model.bassBox.struts.get(config[0]), config[1], points, pi);
253 if (config.length >= 3) pi += config[2];
255 for (int[] config : FLOOR_STRIP_ORDERING) {
256 pi = mapStrip(model.boothFloor.strips.get(config[0]), config[1], points, pi);
257 if (config.length >= 3) pi += config[2];
261 case ChannelMapping.MODE_SPEAKER:
262 int [][] speakerStripOrdering;
263 if (SPEAKER_STRIP_ORDERING == null) {
264 // Copy the cube strip ordering
265 int[] frontLeftCubeWiring = CUBE_STRIP_ORDERINGS[0];
266 speakerStripOrdering = new int[frontLeftCubeWiring.length][];
267 for (int i = 0; i < frontLeftCubeWiring.length; ++i) {
268 speakerStripOrdering[i] = new int[] { frontLeftCubeWiring[0], BACKWARD };
271 speakerStripOrdering = SPEAKER_STRIP_ORDERING[channel.objectIndices[0]];
273 for (int[] config : speakerStripOrdering) {
274 Speaker speaker = model.speakers.get(channel.objectIndices[0]);
275 pi = mapStrip(speaker.strips.get(config[0]), config[1], points, pi);
276 if (config.length >= 3) pi += config[2];
280 case ChannelMapping.MODE_NULL:
281 // No problem, nothing on this channel!
285 throw new RuntimeException("Invalid/unhandled channel mapping mode: " + channel.mode);
291 private int mapStrip(Strip s, int direction, int[] points, int pi) {
292 if (direction == FORWARD) {
293 for (Point p : s.points) {
294 points[pi++] = p.index;
296 } else if (direction == BACKWARD) {
297 for (int i = s.points.size()-1; i >= 0; --i) {
298 points[pi++] = s.points.get(i).index;
301 throw new RuntimeException("Unidentified strip mapping direction: " + direction);
306 public void disable() {
309 println("PandaBoard/" + ip + ": OFF");
313 public void enable() {
316 println("PandaBoard/" + ip + ": ON");
320 public void toggle() {
322 println("PandaBoard/" + ip + ": " + (enabled ? "ON" : "OFF"));
325 public final void send(int[] colors) {
328 public final void sendNow(int[] colors) {
329 if (!enabled || colors==null) {
334 if(points==null) { return; }
335 for (int index: points) {
337 if(index>0) { c= colors[index]; }
338 byte r = (byte) ((c >> 16) & 0xFF);
339 byte g = (byte) ((c >> 8) & 0xFF);
340 byte b = (byte) ((c) & 0xFF);
341 packet[len++] = 0; // alpha channel, unused but makes for 4-byte alignment
346 // Flush once packet is full buffer size
347 if (len >= packet.length) {
348 sendPacket(packetNum++);
353 // Flush any remaining data
355 sendPacket(packetNum++);
360 private void sendPacket(int packetNum) {
361 message.clearArguments();
362 message.add(packetNum);
363 message.add(packet.length);
366 //udp.send(packet, "10.200.1.29", 9001);
367 OscP5.flush(message, address);
368 } catch (Exception x) {