Hackathon anims
[SugarCubes.git] / _PandaDriver.pde
CommitLineData
e73ef85d
MS
1import netP5.*;
2import 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 */
b58e5a1d 17public static class PandaDriver {
d626bc9b
MS
18
19 interface Listener {
20 public void onToggle(boolean enabled);
21 }
22
23 private Listener listener = null;
24
79ae8245
MS
25 // IP address
26 public final String ip;
27
e73ef85d
MS
28 // Address to send to
29 private final NetAddress address;
30
79ae8245
MS
31 // Whether board output is enabled
32 private boolean enabled = false;
33
e73ef85d
MS
34 // OSC message
35 private final OscMessage message;
36
a922e963 37 // List of point indices that get sent to this board
e4d0d812 38 private final int[] points;
44b8de9c 39
e73ef85d 40 // Packet data
a922e963 41 private final byte[] packet = new byte[4*352]; // magic number, our UDP packet size
e73ef85d 42
84086fa3
MS
43 private static final int NO_POINT = -1;
44
a3ccf23a
MS
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
d626bc9b
MS
78 private final static int FORWARD = -1;
79 private final static int BACKWARD = -2;
a3ccf23a 80
a922e963
MS
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 */
29674806 86 private final static int[][] CUBE_STRIP_ORDERINGS = new int[][] {
270a8b44
MS
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
a922e963
MS
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
270a8b44
MS
96 { 9, 8, 11, 5, 4, 7, 6, 10, 14, 2, 1, 0, 3, 13, 12, 15 }, // REAR_RIGHT
97
a922e963 98 };
29674806
MS
99
100 private final static int[][] BASS_STRIP_ORDERING = {
1d75c8a9 101 // front face, counterclockwise from bottom front left
a3ccf23a
MS
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 */ },
1d75c8a9
MS
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},
a3ccf23a 133 {0, BACKWARD},
1d75c8a9 134 {7, FORWARD},
29674806
MS
135 };
136
137 private final static int[][] FLOOR_STRIP_ORDERING = {
138 {0, FORWARD},
139 {1, FORWARD},
140 {2, FORWARD},
1d75c8a9 141 {3, BACKWARD},
29674806
MS
142 };
143
1d75c8a9
MS
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
b58e5a1d 148 private final static int[][][] SPEAKER_STRIP_ORDERING = {
1d75c8a9
MS
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 }
29674806 165 };
d626bc9b
MS
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
44b8de9c
MS
181 public PandaDriver(String ip, Model model, PandaMapping pm) {
182 this(ip);
e28f168c 183
a922e963
MS
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
84086fa3 190 for (ChannelMapping channel : pm.channelList) {
a922e963
MS
191 ++ci;
192 int pi = ci * ChannelMapping.PIXELS_PER_CHANNEL;
193
84086fa3 194 switch (channel.mode) {
a922e963 195
84086fa3 196 case ChannelMapping.MODE_CUBES:
a922e963 197 // We have a list of cubes per channel
84086fa3
MS
198 for (int rawCubeIndex : channel.objectIndices) {
199 if (rawCubeIndex < 0) {
a922e963
MS
200 // No cube here, skip ahead in the buffer
201 pi += Cube.POINTS_PER_CUBE;
84086fa3 202 } else {
a922e963
MS
203 // The cube exists, check which way it is wired to
204 // figure out the order of strips.
84086fa3
MS
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 }
a922e963
MS
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
29674806 218 pi = mapStrip(cube.strips.get(stripIndex), BACKWARD, points, pi);
84086fa3
MS
219 }
220 }
e73ef85d 221 }
84086fa3
MS
222 break;
223
224 case ChannelMapping.MODE_BASS:
29674806
MS
225 for (int[] config : BASS_STRIP_ORDERING) {
226 pi = mapStrip(model.bassBox.strips.get(config[0]), config[1], points, pi);
1d75c8a9 227 if (config.length >= 3) pi += config[2];
29674806 228 }
84086fa3
MS
229 break;
230
1d75c8a9
MS
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 }
29674806
MS
236 for (int[] config : FLOOR_STRIP_ORDERING) {
237 pi = mapStrip(model.boothFloor.strips.get(config[0]), config[1], points, pi);
1d75c8a9 238 if (config.length >= 3) pi += config[2];
29674806 239 }
84086fa3
MS
240 break;
241
242 case ChannelMapping.MODE_SPEAKER:
1d75c8a9
MS
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) {
29674806
MS
255 Speaker speaker = model.speakers.get(channel.objectIndices[0]);
256 pi = mapStrip(speaker.strips.get(config[0]), config[1], points, pi);
1d75c8a9 257 if (config.length >= 3) pi += config[2];
29674806 258 }
84086fa3
MS
259 break;
260
261 case ChannelMapping.MODE_NULL:
a922e963 262 // No problem, nothing on this channel!
84086fa3
MS
263 break;
264
265 default:
266 throw new RuntimeException("Invalid/unhandled channel mapping mode: " + channel.mode);
e73ef85d 267 }
a922e963 268
e73ef85d 269 }
e73ef85d 270 }
29674806
MS
271
272 private int mapStrip(Strip s, int direction, int[] points, int pi) {
273 if (direction == FORWARD) {
2bb56822 274 for (LXPoint p : s.points) {
29674806
MS
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 }
e73ef85d 286
d626bc9b
MS
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 }
1d75c8a9
MS
299 }
300 }
1f42cce7
MS
301
302 public boolean isEnabled() {
303 return this.enabled;
304 }
d626bc9b
MS
305
306 public void disable() {
307 setEnabled(false);
308 }
1d75c8a9
MS
309
310 public void enable() {
d626bc9b 311 setEnabled(true);
1d75c8a9
MS
312 }
313
a922e963 314 public void toggle() {
d626bc9b 315 setEnabled(!enabled);
a922e963
MS
316 }
317
e73ef85d 318 public final void send(int[] colors) {
b58e5a1d 319 if (!enabled) {
79ae8245
MS
320 return;
321 }
e73ef85d
MS
322 int len = 0;
323 int packetNum = 0;
b58e5a1d
MS
324 for (int index : points) {
325 int c = (index < 0) ? 0 : colors[index];
e73ef85d
MS
326 byte r = (byte) ((c >> 16) & 0xFF);
327 byte g = (byte) ((c >> 8) & 0xFF);
328 byte b = (byte) ((c) & 0xFF);
a922e963 329 packet[len++] = 0; // alpha channel, unused but makes for 4-byte alignment
e73ef85d
MS
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) {
f584b5eb 336 sendPacket(packetNum++);
e73ef85d
MS
337 len = 0;
338 }
339 }
340
341 // Flush any remaining data
342 if (len > 0) {
f584b5eb 343 sendPacket(packetNum++);
e73ef85d
MS
344 }
345 }
346
e28f168c 347
f584b5eb 348 private void sendPacket(int packetNum) {
e73ef85d
MS
349 message.clearArguments();
350 message.add(packetNum);
f584b5eb 351 message.add(packet.length);
e73ef85d
MS
352 message.add(packet);
353 try {
bfff6bc2 354 OscP5.flush(message, address);
e73ef85d
MS
355 } catch (Exception x) {
356 x.printStackTrace();
357 }
358 }
359}