Handle for boom effect and example of how to trigger
[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 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 */ },
97 {0, BACKWARD },
98 {3, BACKWARD },
99
100 // left face, counterclockwise from bottom front left
101 {13, BACKWARD },
102 {12, BACKWARD },
103 {15, BACKWARD },
104 {14, BACKWARD },
105
106 // back face, counterclockwise from bottom rear left
107 {9, BACKWARD },
108 {8, BACKWARD },
109 {11, BACKWARD },
110 {10, BACKWARD },
111
112 // right face, counterclockwise from bottom rear right
113 {5, BACKWARD },
114 {4, BACKWARD },
115 {7, BACKWARD },
116 {6, BACKWARD },
117 };
118
119 private final static int[][] STRUT_STRIP_ORDERING = {
120 {6, BACKWARD},
121 {5, FORWARD},
122 {4, BACKWARD},
123 {3, FORWARD},
124 {2, BACKWARD},
125 {1, FORWARD},
126 {0, BACKWARD},
127 {7, FORWARD},
128 };
129
130 private final static int[][] FLOOR_STRIP_ORDERING = {
131 {0, FORWARD},
132 {1, FORWARD},
133 {2, FORWARD},
134 {3, BACKWARD},
135 };
136
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 = {
142 // Left speaker
143 {
144 // Front face, counter-clockwise from bottom left
145 {2, BACKWARD },
146 {1, BACKWARD },
147 {0, BACKWARD },
148 {3, BACKWARD },
149 },
150 // Right speaker
151 {
152 // Front face, counter-clockwise from bottom left
153 {2, BACKWARD },
154 {1, BACKWARD },
155 {0, BACKWARD },
156 {3, BACKWARD },
157 }
158 };
159
160 public PandaDriver(String ip) {
161 this.ip = ip;
162
163 // Initialize our OSC output stuff
164 address = new NetAddress(ip, 9001);
165 message = new OscMessage("/shady/pointbuffer");
166
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;
171 }
172 }
173
174 public PandaDriver(String ip, Model model, PandaMapping pm) {
175 this(ip);
176
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.
180 int ci = -1;
181
182 // Iterate through all our channels
183 for (ChannelMapping channel : pm.channelList) {
184 ++ci;
185 int pi = ci * ChannelMapping.PIXELS_PER_CHANNEL;
186
187 switch (channel.mode) {
188
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;
195 } else {
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;
205 }
206
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);
212 }
213 }
214 }
215 break;
216
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];
221 }
222 break;
223
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];
228 }
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];
232 }
233 break;
234
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 };
243 }
244 } else {
245 speakerStripOrdering = SPEAKER_STRIP_ORDERING[channel.objectIndices[0]];
246 }
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];
251 }
252 break;
253
254 case ChannelMapping.MODE_NULL:
255 // No problem, nothing on this channel!
256 break;
257
258 default:
259 throw new RuntimeException("Invalid/unhandled channel mapping mode: " + channel.mode);
260 }
261
262 }
263 }
264
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;
269 }
270 } else if (direction == BACKWARD) {
271 for (int i = s.points.size()-1; i >= 0; --i) {
272 points[pi++] = s.points.get(i).index;
273 }
274 } else {
275 throw new RuntimeException("Unidentified strip mapping direction: " + direction);
276 }
277 return pi;
278 }
279
280 public PandaDriver setListener(Listener listener) {
281 this.listener = listener;
282 return this;
283 }
284
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);
291 }
292 }
293 }
294
295 public boolean isEnabled() {
296 return this.enabled;
297 }
298
299 public void disable() {
300 setEnabled(false);
301 }
302
303 public void enable() {
304 setEnabled(true);
305 }
306
307 public void toggle() {
308 setEnabled(!enabled);
309 }
310
311 public final void send(int[] colors) {
312 if (!enabled) {
313 return;
314 }
315 int len = 0;
316 int packetNum = 0;
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
323 packet[len++] = r;
324 packet[len++] = g;
325 packet[len++] = b;
326
327 // Flush once packet is full buffer size
328 if (len >= packet.length) {
329 sendPacket(packetNum++);
330 len = 0;
331 }
332 }
333
334 // Flush any remaining data
335 if (len > 0) {
336 sendPacket(packetNum++);
337 }
338 }
339
340
341 private void sendPacket(int packetNum) {
342 message.clearArguments();
343 message.add(packetNum);
344 message.add(packet.length);
345 message.add(packet);
346 try {
347 OscP5.flush(message, address);
348 } catch (Exception x) {
349 x.printStackTrace();
350 }
351 }
352 }