Commit | Line | Data |
---|---|---|
e73ef85d MS |
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 | */ | |
29674806 | 17 | public static class PandaDriver { |
e73ef85d | 18 | |
79ae8245 MS |
19 | // IP address |
20 | public final String ip; | |
21 | ||
e73ef85d MS |
22 | // Address to send to |
23 | private final NetAddress address; | |
24 | ||
79ae8245 MS |
25 | // Whether board output is enabled |
26 | private boolean enabled = false; | |
27 | ||
e73ef85d MS |
28 | // OSC message |
29 | private final OscMessage message; | |
30 | ||
a922e963 | 31 | // List of point indices that get sent to this board |
e4d0d812 | 32 | private final int[] points; |
44b8de9c | 33 | |
e73ef85d | 34 | // Packet data |
a922e963 | 35 | private final byte[] packet = new byte[4*352]; // magic number, our UDP packet size |
e73ef85d | 36 | |
84086fa3 MS |
37 | private static final int NO_POINT = -1; |
38 | ||
44b8de9c | 39 | public PandaDriver(String ip) { |
79ae8245 | 40 | this.ip = ip; |
a922e963 MS |
41 | |
42 | // Initialize our OSC output stuff | |
43 | address = new NetAddress(ip, 9001); | |
e73ef85d | 44 | message = new OscMessage("/shady/pointbuffer"); |
a922e963 MS |
45 | |
46 | // Build the array of points, initialize all to nothing | |
44b8de9c MS |
47 | points = new int[PandaMapping.PIXELS_PER_BOARD]; |
48 | for (int i = 0; i < points.length; ++i) { | |
a922e963 | 49 | points[i] = NO_POINT; |
44b8de9c MS |
50 | } |
51 | } | |
52 | ||
29674806 MS |
53 | private final static int FORWARD = -1; |
54 | private final static int BACKWARD = -2; | |
55 | ||
a3ccf23a MS |
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 | ||
a922e963 MS |
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 | */ | |
29674806 | 95 | private final static int[][] CUBE_STRIP_ORDERINGS = new int[][] { |
a922e963 MS |
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 | }; | |
29674806 MS |
101 | |
102 | private final static int[][] BASS_STRIP_ORDERING = { | |
1d75c8a9 | 103 | // front face, counterclockwise from bottom front left |
a3ccf23a MS |
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 */ }, | |
1d75c8a9 MS |
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}, | |
a3ccf23a | 135 | {0, BACKWARD}, |
1d75c8a9 | 136 | {7, FORWARD}, |
29674806 MS |
137 | }; |
138 | ||
139 | private final static int[][] FLOOR_STRIP_ORDERING = { | |
140 | {0, FORWARD}, | |
141 | {1, FORWARD}, | |
142 | {2, FORWARD}, | |
1d75c8a9 | 143 | {3, BACKWARD}, |
29674806 MS |
144 | }; |
145 | ||
1d75c8a9 MS |
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 = { | |
29674806 MS |
170 | }; |
171 | ||
44b8de9c MS |
172 | public PandaDriver(String ip, Model model, PandaMapping pm) { |
173 | this(ip); | |
e73ef85d | 174 | |
a922e963 MS |
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 | |
84086fa3 | 181 | for (ChannelMapping channel : pm.channelList) { |
a922e963 MS |
182 | ++ci; |
183 | int pi = ci * ChannelMapping.PIXELS_PER_CHANNEL; | |
184 | ||
84086fa3 | 185 | switch (channel.mode) { |
a922e963 | 186 | |
84086fa3 | 187 | case ChannelMapping.MODE_CUBES: |
a922e963 | 188 | // We have a list of cubes per channel |
84086fa3 MS |
189 | for (int rawCubeIndex : channel.objectIndices) { |
190 | if (rawCubeIndex < 0) { | |
a922e963 MS |
191 | // No cube here, skip ahead in the buffer |
192 | pi += Cube.POINTS_PER_CUBE; | |
84086fa3 | 193 | } else { |
a922e963 MS |
194 | // The cube exists, check which way it is wired to |
195 | // figure out the order of strips. | |
84086fa3 MS |
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 | } | |
a922e963 MS |
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 | |
29674806 | 209 | pi = mapStrip(cube.strips.get(stripIndex), BACKWARD, points, pi); |
84086fa3 MS |
210 | } |
211 | } | |
e73ef85d | 212 | } |
84086fa3 MS |
213 | break; |
214 | ||
215 | case ChannelMapping.MODE_BASS: | |
29674806 MS |
216 | for (int[] config : BASS_STRIP_ORDERING) { |
217 | pi = mapStrip(model.bassBox.strips.get(config[0]), config[1], points, pi); | |
1d75c8a9 | 218 | if (config.length >= 3) pi += config[2]; |
29674806 | 219 | } |
84086fa3 MS |
220 | break; |
221 | ||
1d75c8a9 MS |
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 | } | |
29674806 MS |
227 | for (int[] config : FLOOR_STRIP_ORDERING) { |
228 | pi = mapStrip(model.boothFloor.strips.get(config[0]), config[1], points, pi); | |
1d75c8a9 | 229 | if (config.length >= 3) pi += config[2]; |
29674806 | 230 | } |
84086fa3 MS |
231 | break; |
232 | ||
233 | case ChannelMapping.MODE_SPEAKER: | |
1d75c8a9 MS |
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) { | |
29674806 MS |
246 | Speaker speaker = model.speakers.get(channel.objectIndices[0]); |
247 | pi = mapStrip(speaker.strips.get(config[0]), config[1], points, pi); | |
1d75c8a9 | 248 | if (config.length >= 3) pi += config[2]; |
29674806 | 249 | } |
84086fa3 MS |
250 | break; |
251 | ||
252 | case ChannelMapping.MODE_NULL: | |
a922e963 | 253 | // No problem, nothing on this channel! |
84086fa3 MS |
254 | break; |
255 | ||
256 | default: | |
257 | throw new RuntimeException("Invalid/unhandled channel mapping mode: " + channel.mode); | |
e73ef85d | 258 | } |
a922e963 | 259 | |
e73ef85d | 260 | } |
e73ef85d | 261 | } |
29674806 MS |
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 | } | |
e73ef85d | 277 | |
1d75c8a9 MS |
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 | ||
a922e963 MS |
292 | public void toggle() { |
293 | enabled = !enabled; | |
294 | println("PandaBoard/" + ip + ": " + (enabled ? "ON" : "OFF")); | |
295 | } | |
296 | ||
e73ef85d | 297 | public final void send(int[] colors) { |
79ae8245 MS |
298 | if (!enabled) { |
299 | return; | |
300 | } | |
e73ef85d MS |
301 | int len = 0; |
302 | int packetNum = 0; | |
303 | for (int index : points) { | |
84086fa3 | 304 | int c = (index < 0) ? 0 : colors[index]; |
e73ef85d MS |
305 | byte r = (byte) ((c >> 16) & 0xFF); |
306 | byte g = (byte) ((c >> 8) & 0xFF); | |
307 | byte b = (byte) ((c) & 0xFF); | |
a922e963 | 308 | packet[len++] = 0; // alpha channel, unused but makes for 4-byte alignment |
e73ef85d MS |
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) { | |
f584b5eb | 315 | sendPacket(packetNum++); |
e73ef85d MS |
316 | len = 0; |
317 | } | |
318 | } | |
319 | ||
320 | // Flush any remaining data | |
321 | if (len > 0) { | |
f584b5eb | 322 | sendPacket(packetNum++); |
e73ef85d MS |
323 | } |
324 | } | |
325 | ||
f584b5eb | 326 | private void sendPacket(int packetNum) { |
e73ef85d MS |
327 | message.clearArguments(); |
328 | message.add(packetNum); | |
f584b5eb | 329 | message.add(packet.length); |
e73ef85d MS |
330 | message.add(packet); |
331 | try { | |
bfff6bc2 | 332 | OscP5.flush(message, address); |
e73ef85d MS |
333 | } catch (Exception x) { |
334 | x.printStackTrace(); | |
335 | } | |
336 | } | |
337 | } | |
338 |