Fix indenation thing, OCD
[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 */
29674806 17public 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
a922e963
MS
56 /**
57 * These constant arrays indicate the order in which the strips of a cube
58 * are wired. There are four different options, depending on which bottom
59 * corner of the cube the data wire comes in.
60 */
29674806 61 private final static int[][] CUBE_STRIP_ORDERINGS = new int[][] {
a922e963
MS
62 { 2, 1, 0, 3, 13, 12, 15, 14, 4, 7, 6, 5, 11, 10, 9, 8 }, // FRONT_LEFT
63 { 6, 5, 4, 7, 1, 0, 3, 2, 8, 11, 10, 9, 15, 14, 13, 12 }, // FRONT_RIGHT
64 { 14, 13, 12, 15, 9, 8, 11, 10, 0, 3, 2, 1, 7, 6, 5, 4 }, // REAR_LEFT
65 { 10, 9, 8, 11, 5, 4, 7, 6, 12, 15, 14, 13, 3, 2, 1, 0 }, // REAR_RIGHT
66 };
29674806
MS
67
68 private final static int[][] BASS_STRIP_ORDERING = {
1d75c8a9
MS
69 // front face, counterclockwise from bottom front left
70 {2, BACKWARD },
71 {1, BACKWARD },
72 {0, BACKWARD },
73 {3, BACKWARD },
74
75 // left face, counterclockwise from bottom front left
76 {13, BACKWARD },
77 {12, BACKWARD },
78 {15, BACKWARD },
79 {14, BACKWARD },
80
81 // back face, counterclockwise from bottom rear left
82 {9, BACKWARD },
83 {8, BACKWARD },
84 {11, BACKWARD },
85 {10, BACKWARD },
86
87 // right face, counterclockwise from bottom rear right
88 {5, BACKWARD },
89 {4, BACKWARD },
90 {7, BACKWARD },
91 {6, BACKWARD },
92 };
93
94 private final static int[][] STRUT_STRIP_ORDERING = {
95 {6, BACKWARD},
96 {5, FORWARD},
97 {4, BACKWARD},
98 {3, FORWARD},
99 {2, BACKWARD},
100 {1, FORWARD},
101 {0, BACKWARD, /* dummy pixels at the end of string, remove when strip is fixed */ 4},
102 {7, FORWARD},
29674806
MS
103 };
104
105 private final static int[][] FLOOR_STRIP_ORDERING = {
106 {0, FORWARD},
107 {1, FORWARD},
108 {2, FORWARD},
1d75c8a9 109 {3, BACKWARD},
29674806
MS
110 };
111
1d75c8a9
MS
112 // The speakers are currently configured to be wired the same
113 // as cubes with Wiring.FRONT_LEFT. If this needs to be changed,
114 // remove this null assignment and change the below to have mappings
115 // for the LEFT and RIGHT speaker
116 private final static int[][][] SPEAKER_STRIP_ORDERING = null; /* {
117 // Left speaker
118 {
119 // Front face, counter-clockwise from bottom left
120 {2, BACKWARD },
121 {1, BACKWARD },
122 {0, BACKWARD },
123 {3, BACKWARD },
124 },
125 // Right speaker
126 {
127 // Front face, counter-clockwise from bottom left
128 {2, BACKWARD },
129 {1, BACKWARD },
130 {0, BACKWARD },
131 {3, BACKWARD },
132 }
133 };*/
134
135 private final static int[][] LEFT_SPEAKER_STRIP_ORDERING = {
29674806
MS
136 };
137
44b8de9c
MS
138 public PandaDriver(String ip, Model model, PandaMapping pm) {
139 this(ip);
e73ef85d 140
a922e963
MS
141 // Ok, we are initialized, time to build the array if points in order to
142 // send out. We start at the head of our point buffer, and work our way
143 // down. This is the order in which points will be sent down the wire.
144 int ci = -1;
145
146 // Iterate through all our channels
84086fa3 147 for (ChannelMapping channel : pm.channelList) {
a922e963
MS
148 ++ci;
149 int pi = ci * ChannelMapping.PIXELS_PER_CHANNEL;
150
84086fa3 151 switch (channel.mode) {
a922e963 152
84086fa3 153 case ChannelMapping.MODE_CUBES:
a922e963 154 // We have a list of cubes per channel
84086fa3
MS
155 for (int rawCubeIndex : channel.objectIndices) {
156 if (rawCubeIndex < 0) {
a922e963
MS
157 // No cube here, skip ahead in the buffer
158 pi += Cube.POINTS_PER_CUBE;
84086fa3 159 } else {
a922e963
MS
160 // The cube exists, check which way it is wired to
161 // figure out the order of strips.
84086fa3
MS
162 Cube cube = model.getCubeByRawIndex(rawCubeIndex);
163 int stripOrderIndex = 0;
164 switch (cube.wiring) {
165 case FRONT_LEFT: stripOrderIndex = 0; break;
166 case FRONT_RIGHT: stripOrderIndex = 1; break;
167 case REAR_LEFT: stripOrderIndex = 2; break;
168 case REAR_RIGHT: stripOrderIndex = 3; break;
169 }
a922e963
MS
170
171 // Iterate through all the strips on the cube and add the points
172 for (int stripIndex : CUBE_STRIP_ORDERINGS[stripOrderIndex]) {
173 // We go backwards here... in the model strips go clockwise, but
174 // the physical wires are run counter-clockwise
29674806 175 pi = mapStrip(cube.strips.get(stripIndex), BACKWARD, points, pi);
84086fa3
MS
176 }
177 }
e73ef85d 178 }
84086fa3
MS
179 break;
180
181 case ChannelMapping.MODE_BASS:
29674806
MS
182 for (int[] config : BASS_STRIP_ORDERING) {
183 pi = mapStrip(model.bassBox.strips.get(config[0]), config[1], points, pi);
1d75c8a9 184 if (config.length >= 3) pi += config[2];
29674806 185 }
84086fa3
MS
186 break;
187
1d75c8a9
MS
188 case ChannelMapping.MODE_STRUTS_AND_FLOOR:
189 for (int[] config : STRUT_STRIP_ORDERING) {
190 pi = mapStrip(model.bassBox.struts.get(config[0]), config[1], points, pi);
191 if (config.length >= 3) pi += config[2];
192 }
29674806
MS
193 for (int[] config : FLOOR_STRIP_ORDERING) {
194 pi = mapStrip(model.boothFloor.strips.get(config[0]), config[1], points, pi);
1d75c8a9 195 if (config.length >= 3) pi += config[2];
29674806 196 }
84086fa3
MS
197 break;
198
199 case ChannelMapping.MODE_SPEAKER:
1d75c8a9
MS
200 int [][] speakerStripOrdering;
201 if (SPEAKER_STRIP_ORDERING == null) {
202 // Copy the cube strip ordering
203 int[] frontLeftCubeWiring = CUBE_STRIP_ORDERINGS[0];
204 speakerStripOrdering = new int[frontLeftCubeWiring.length][];
205 for (int i = 0; i < frontLeftCubeWiring.length; ++i) {
206 speakerStripOrdering[i] = new int[] { frontLeftCubeWiring[0], BACKWARD };
207 }
208 } else {
209 speakerStripOrdering = SPEAKER_STRIP_ORDERING[channel.objectIndices[0]];
210 }
211 for (int[] config : speakerStripOrdering) {
29674806
MS
212 Speaker speaker = model.speakers.get(channel.objectIndices[0]);
213 pi = mapStrip(speaker.strips.get(config[0]), config[1], points, pi);
1d75c8a9 214 if (config.length >= 3) pi += config[2];
29674806 215 }
84086fa3
MS
216 break;
217
218 case ChannelMapping.MODE_NULL:
a922e963 219 // No problem, nothing on this channel!
84086fa3
MS
220 break;
221
222 default:
223 throw new RuntimeException("Invalid/unhandled channel mapping mode: " + channel.mode);
e73ef85d 224 }
a922e963 225
e73ef85d 226 }
e73ef85d 227 }
29674806
MS
228
229 private int mapStrip(Strip s, int direction, int[] points, int pi) {
230 if (direction == FORWARD) {
231 for (Point p : s.points) {
232 points[pi++] = p.index;
233 }
234 } else if (direction == BACKWARD) {
235 for (int i = s.points.size()-1; i >= 0; --i) {
236 points[pi++] = s.points.get(i).index;
237 }
238 } else {
239 throw new RuntimeException("Unidentified strip mapping direction: " + direction);
240 }
241 return pi;
242 }
e73ef85d 243
1d75c8a9
MS
244 public void disable() {
245 if (enabled) {
246 enabled = false;
247 println("PandaBoard/" + ip + ": OFF");
248 }
249 }
250
251 public void enable() {
252 if (!enabled) {
253 enabled = true;
254 println("PandaBoard/" + ip + ": ON");
255 }
256 }
257
a922e963
MS
258 public void toggle() {
259 enabled = !enabled;
260 println("PandaBoard/" + ip + ": " + (enabled ? "ON" : "OFF"));
261 }
262
e73ef85d 263 public final void send(int[] colors) {
79ae8245
MS
264 if (!enabled) {
265 return;
266 }
e73ef85d
MS
267 int len = 0;
268 int packetNum = 0;
269 for (int index : points) {
84086fa3 270 int c = (index < 0) ? 0 : colors[index];
e73ef85d
MS
271 byte r = (byte) ((c >> 16) & 0xFF);
272 byte g = (byte) ((c >> 8) & 0xFF);
273 byte b = (byte) ((c) & 0xFF);
a922e963 274 packet[len++] = 0; // alpha channel, unused but makes for 4-byte alignment
e73ef85d
MS
275 packet[len++] = r;
276 packet[len++] = g;
277 packet[len++] = b;
278
279 // Flush once packet is full buffer size
280 if (len >= packet.length) {
f584b5eb 281 sendPacket(packetNum++);
e73ef85d
MS
282 len = 0;
283 }
284 }
285
286 // Flush any remaining data
287 if (len > 0) {
f584b5eb 288 sendPacket(packetNum++);
e73ef85d
MS
289 }
290 }
291
f584b5eb 292 private void sendPacket(int packetNum) {
e73ef85d
MS
293 message.clearArguments();
294 message.add(packetNum);
f584b5eb 295 message.add(packet.length);
e73ef85d
MS
296 message.add(packet);
297 try {
bfff6bc2 298 OscP5.flush(message, address);
e73ef85d
MS
299 } catch (Exception x) {
300 x.printStackTrace();
301 }
302 }
303}
304