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