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