Mapping for Asana hackathon
[SugarCubes.git] / _PandaDriver.pde
1 import netP5.*;
2 import oscP5.*;
3
4 /**
5 * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND
6 *
7 * //\\ //\\ //\\ //\\
8 * ///\\\ ///\\\ ///\\\ ///\\\
9 * \\\/// \\\/// \\\/// \\\///
10 * \\// \\// \\// \\//
11 *
12 * EXPERTS ONLY!! EXPERTS ONLY!!
13 *
14 * This class implements the output function to the Panda Boards. It
15 * will be moved into GLucose once stabilized.
16 */
17 public static class PandaDriver {
18
19 interface Listener {
20 public void onToggle(boolean enabled);
21 }
22
23 private Listener listener = null;
24
25 // IP address
26 public final String ip;
27
28 // Address to send to
29 private final NetAddress address;
30
31 // Whether board output is enabled
32 private boolean enabled = false;
33
34 // OSC message
35 private final OscMessage message;
36
37 // List of point indices that get sent to this board
38 private final int[] points;
39
40 // Packet data
41 private final byte[] packet = new byte[4*352]; // magic number, our UDP packet size
42
43 private static final int NO_POINT = -1;
44
45 ////////////////////////////////////////////////////////////////
46 //
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!!!!!!!!!!!!!
50 //
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.
54 //
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.
60 //
61 // For every face, the strips are ordered numerically moving
62 // clockwise from the the TOP LEFT.
63 //
64 // So, for a cube:
65 //
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
70 //
71 // Strip 4: right face, top strip, left to right
72 // ... and so on
73 // Strip 14: left face, bottom strip, right to left
74 // Strip 15: left face, left strip, bottom to top
75 //
76 ////////////////////////////////////////////////////////////////
77
78 private final static int FORWARD = -1;
79 private final static int BACKWARD = -2;
80
81 /**
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.
85 */
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
91
92
93 { 2, 1, 0, 3, 13, 12, 15, 14, 4, 7, 6, 5, 11, 10, 9, 8 }, // FRONT_LEFT
94 { 6, 5, 4, 7, 1, 0, 3, 2, 8, 11, 10, 9, 15, 14, 13, 12 }, // FRONT_RIGHT
95 { 14, 13, 12, 15, 9, 8, 11, 10, 0, 3, 2, 1, 7, 6, 5, 4 }, // REAR_LEFT
96 { 9, 8, 11, 5, 4, 7, 6, 10, 14, 2, 1, 0, 3, 13, 12, 15 }, // REAR_RIGHT
97
98 };
99
100 private final static int[][] BASS_STRIP_ORDERING = {
101 // front face, counterclockwise from bottom front left
102 {2, BACKWARD /* if this strip has extra pixels, you can add them here */ /*, 4 */ },
103 {1, BACKWARD /* if this strip is short some pixels, substract them here */ /*, -3 */ },
104 {0, BACKWARD },
105 {3, BACKWARD },
106
107 // left face, counterclockwise from bottom front left
108 {13, BACKWARD },
109 {12, BACKWARD },
110 {15, BACKWARD },
111 {14, BACKWARD },
112
113 // back face, counterclockwise from bottom rear left
114 {9, BACKWARD },
115 {8, BACKWARD },
116 {11, BACKWARD },
117 {10, BACKWARD },
118
119 // right face, counterclockwise from bottom rear right
120 {5, BACKWARD },
121 {4, BACKWARD },
122 {7, BACKWARD },
123 {6, BACKWARD },
124 };
125
126 private final static int[][] STRUT_STRIP_ORDERING = {
127 {6, BACKWARD},
128 {5, FORWARD},
129 {4, BACKWARD},
130 {3, FORWARD},
131 {2, BACKWARD},
132 {1, FORWARD},
133 {0, BACKWARD},
134 {7, FORWARD},
135 };
136
137 private final static int[][] FLOOR_STRIP_ORDERING = {
138 {0, FORWARD},
139 {1, FORWARD},
140 {2, FORWARD},
141 {3, BACKWARD},
142 };
143
144 // The speakers are currently configured to be wired the same
145 // as cubes with Wiring.FRONT_LEFT. If this needs to be changed,
146 // remove this null assignment and change the below to have mappings
147 // for the LEFT and RIGHT speaker
148 private final static int[][][] SPEAKER_STRIP_ORDERING = {
149 // Left speaker
150 {
151 // Front face, counter-clockwise from bottom left
152 {2, BACKWARD },
153 {1, BACKWARD },
154 {0, BACKWARD },
155 {3, BACKWARD },
156 },
157 // Right speaker
158 {
159 // Front face, counter-clockwise from bottom left
160 {2, BACKWARD },
161 {1, BACKWARD },
162 {0, BACKWARD },
163 {3, BACKWARD },
164 }
165 };
166
167 public PandaDriver(String ip) {
168 this.ip = ip;
169
170 // Initialize our OSC output stuff
171 address = new NetAddress(ip, 9001);
172 message = new OscMessage("/shady/pointbuffer");
173
174 // Build the array of points, initialize all to nothing
175 points = new int[PandaMapping.PIXELS_PER_BOARD];
176 for (int i = 0; i < points.length; ++i) {
177 points[i] = NO_POINT;
178 }
179 }
180
181 public PandaDriver(String ip, Model model, PandaMapping pm) {
182 this(ip);
183
184 // Ok, we are initialized, time to build the array if points in order to
185 // send out. We start at the head of our point buffer, and work our way
186 // down. This is the order in which points will be sent down the wire.
187 int ci = -1;
188
189 // Iterate through all our channels
190 for (ChannelMapping channel : pm.channelList) {
191 ++ci;
192 int pi = ci * ChannelMapping.PIXELS_PER_CHANNEL;
193
194 switch (channel.mode) {
195
196 case ChannelMapping.MODE_CUBES:
197 // We have a list of cubes per channel
198 for (int rawCubeIndex : channel.objectIndices) {
199 if (rawCubeIndex < 0) {
200 // No cube here, skip ahead in the buffer
201 pi += Cube.POINTS_PER_CUBE;
202 } else {
203 // The cube exists, check which way it is wired to
204 // figure out the order of strips.
205 Cube cube = model.getCubeByRawIndex(rawCubeIndex);
206 int stripOrderIndex = 0;
207 switch (cube.wiring) {
208 case FRONT_LEFT: stripOrderIndex = 0; break;
209 case FRONT_RIGHT: stripOrderIndex = 1; break;
210 case REAR_LEFT: stripOrderIndex = 2; break;
211 case REAR_RIGHT: stripOrderIndex = 3; break;
212 }
213
214 // Iterate through all the strips on the cube and add the points
215 for (int stripIndex : CUBE_STRIP_ORDERINGS[stripOrderIndex]) {
216 // We go backwards here... in the model strips go clockwise, but
217 // the physical wires are run counter-clockwise
218 pi = mapStrip(cube.strips.get(stripIndex), BACKWARD, points, pi);
219 }
220 }
221 }
222 break;
223
224 case ChannelMapping.MODE_BASS:
225 for (int[] config : BASS_STRIP_ORDERING) {
226 pi = mapStrip(model.bassBox.strips.get(config[0]), config[1], points, pi);
227 if (config.length >= 3) pi += config[2];
228 }
229 break;
230
231 case ChannelMapping.MODE_STRUTS_AND_FLOOR:
232 for (int[] config : STRUT_STRIP_ORDERING) {
233 pi = mapStrip(model.bassBox.struts.get(config[0]), config[1], points, pi);
234 if (config.length >= 3) pi += config[2];
235 }
236 for (int[] config : FLOOR_STRIP_ORDERING) {
237 pi = mapStrip(model.boothFloor.strips.get(config[0]), config[1], points, pi);
238 if (config.length >= 3) pi += config[2];
239 }
240 break;
241
242 case ChannelMapping.MODE_SPEAKER:
243 int [][] speakerStripOrdering;
244 if (SPEAKER_STRIP_ORDERING == null) {
245 // Copy the cube strip ordering
246 int[] frontLeftCubeWiring = CUBE_STRIP_ORDERINGS[0];
247 speakerStripOrdering = new int[frontLeftCubeWiring.length][];
248 for (int i = 0; i < frontLeftCubeWiring.length; ++i) {
249 speakerStripOrdering[i] = new int[] { frontLeftCubeWiring[0], BACKWARD };
250 }
251 } else {
252 speakerStripOrdering = SPEAKER_STRIP_ORDERING[channel.objectIndices[0]];
253 }
254 for (int[] config : speakerStripOrdering) {
255 Speaker speaker = model.speakers.get(channel.objectIndices[0]);
256 pi = mapStrip(speaker.strips.get(config[0]), config[1], points, pi);
257 if (config.length >= 3) pi += config[2];
258 }
259 break;
260
261 case ChannelMapping.MODE_NULL:
262 // No problem, nothing on this channel!
263 break;
264
265 default:
266 throw new RuntimeException("Invalid/unhandled channel mapping mode: " + channel.mode);
267 }
268
269 }
270 }
271
272 private int mapStrip(Strip s, int direction, int[] points, int pi) {
273 if (direction == FORWARD) {
274 for (LXPoint p : s.points) {
275 points[pi++] = p.index;
276 }
277 } else if (direction == BACKWARD) {
278 for (int i = s.points.size()-1; i >= 0; --i) {
279 points[pi++] = s.points.get(i).index;
280 }
281 } else {
282 throw new RuntimeException("Unidentified strip mapping direction: " + direction);
283 }
284 return pi;
285 }
286
287 public PandaDriver setListener(Listener listener) {
288 this.listener = listener;
289 return this;
290 }
291
292 public void setEnabled(boolean enabled) {
293 if (this.enabled != enabled) {
294 this.enabled = enabled;
295 println("PandaBoard/" + ip + ": " + (enabled ? "ON" : "OFF"));
296 if (listener != null) {
297 listener.onToggle(enabled);
298 }
299 }
300 }
301
302 public boolean isEnabled() {
303 return this.enabled;
304 }
305
306 public void disable() {
307 setEnabled(false);
308 }
309
310 public void enable() {
311 setEnabled(true);
312 }
313
314 public void toggle() {
315 setEnabled(!enabled);
316 }
317
318 public final void send(int[] colors) {
319 if (!enabled) {
320 return;
321 }
322 int len = 0;
323 int packetNum = 0;
324 for (int index : points) {
325 int c = (index < 0) ? 0 : colors[index];
326 byte r = (byte) ((c >> 16) & 0xFF);
327 byte g = (byte) ((c >> 8) & 0xFF);
328 byte b = (byte) ((c) & 0xFF);
329 packet[len++] = 0; // alpha channel, unused but makes for 4-byte alignment
330 packet[len++] = r;
331 packet[len++] = g;
332 packet[len++] = b;
333
334 // Flush once packet is full buffer size
335 if (len >= packet.length) {
336 sendPacket(packetNum++);
337 len = 0;
338 }
339 }
340
341 // Flush any remaining data
342 if (len > 0) {
343 sendPacket(packetNum++);
344 }
345 }
346
347
348 private void sendPacket(int packetNum) {
349 message.clearArguments();
350 message.add(packetNum);
351 message.add(packet.length);
352 message.add(packet);
353 try {
354 OscP5.flush(message, address);
355 } catch (Exception x) {
356 x.printStackTrace();
357 }
358 }
359 }