Merge pull request #1 from sugarcubes/master
[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 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 /**
54 * These constant arrays indicate the order in which the strips of a cube
55 * are wired. There are four different options, depending on which bottom
56 * corner of the cube the data wire comes in.
57 */
58 private final int[][] CUBE_STRIP_ORDERINGS = new int[][] {
59 { 2, 1, 0, 3, 13, 12, 15, 14, 4, 7, 6, 5, 11, 10, 9, 8 }, // FRONT_LEFT
60 { 6, 5, 4, 7, 1, 0, 3, 2, 8, 11, 10, 9, 15, 14, 13, 12 }, // FRONT_RIGHT
61 { 14, 13, 12, 15, 9, 8, 11, 10, 0, 3, 2, 1, 7, 6, 5, 4 }, // REAR_LEFT
62 { 10, 9, 8, 11, 5, 4, 7, 6, 12, 15, 14, 13, 3, 2, 1, 0 }, // REAR_RIGHT
63 };
64
65 public PandaDriver(String ip, Model model, PandaMapping pm) {
66 this(ip);
67
68 // Ok, we are initialized, time to build the array if points in order to
69 // send out. We start at the head of our point buffer, and work our way
70 // down. This is the order in which points will be sent down the wire.
71 int ci = -1;
72
73 // Iterate through all our channels
74 for (ChannelMapping channel : pm.channelList) {
75 ++ci;
76 int pi = ci * ChannelMapping.PIXELS_PER_CHANNEL;
77
78 switch (channel.mode) {
79
80 case ChannelMapping.MODE_CUBES:
81 // We have a list of cubes per channel
82 for (int rawCubeIndex : channel.objectIndices) {
83 if (rawCubeIndex < 0) {
84 // No cube here, skip ahead in the buffer
85 pi += Cube.POINTS_PER_CUBE;
86 } else {
87 // The cube exists, check which way it is wired to
88 // figure out the order of strips.
89 Cube cube = model.getCubeByRawIndex(rawCubeIndex);
90 int stripOrderIndex = 0;
91 switch (cube.wiring) {
92 case FRONT_LEFT: stripOrderIndex = 0; break;
93 case FRONT_RIGHT: stripOrderIndex = 1; break;
94 case REAR_LEFT: stripOrderIndex = 2; break;
95 case REAR_RIGHT: stripOrderIndex = 3; break;
96 }
97
98 // Iterate through all the strips on the cube and add the points
99 for (int stripIndex : CUBE_STRIP_ORDERINGS[stripOrderIndex]) {
100 // We go backwards here... in the model strips go clockwise, but
101 // the physical wires are run counter-clockwise
102 Strip s = cube.strips.get(stripIndex);
103 for (int j = s.points.size() - 1; j >= 0; --j) {
104 points[pi++] = s.points.get(j).index;
105 }
106 }
107 }
108 }
109 break;
110
111 case ChannelMapping.MODE_BASS:
112 // TODO(mapping): figure out how we end up connecting the bass cabinet
113 break;
114
115 case ChannelMapping.MODE_FLOOR:
116 // TODO(mapping): figure out how these strips are wired
117 break;
118
119 case ChannelMapping.MODE_SPEAKER:
120 // TODO(mapping): figure out how these strips are wired
121 break;
122
123 case ChannelMapping.MODE_NULL:
124 // No problem, nothing on this channel!
125 break;
126
127 default:
128 throw new RuntimeException("Invalid/unhandled channel mapping mode: " + channel.mode);
129 }
130
131 }
132 }
133
134 public void toggle() {
135 enabled = !enabled;
136 println("PandaBoard/" + ip + ": " + (enabled ? "ON" : "OFF"));
137 }
138
139 public final void send(int[] colors) {
140 if (!enabled) {
141 return;
142 }
143 int len = 0;
144 int packetNum = 0;
145 for (int index : points) {
146 int c = (index < 0) ? 0 : colors[index];
147 byte r = (byte) ((c >> 16) & 0xFF);
148 byte g = (byte) ((c >> 8) & 0xFF);
149 byte b = (byte) ((c) & 0xFF);
150 packet[len++] = 0; // alpha channel, unused but makes for 4-byte alignment
151 packet[len++] = r;
152 packet[len++] = g;
153 packet[len++] = b;
154
155 // Flush once packet is full buffer size
156 if (len >= packet.length) {
157 sendPacket(packetNum++);
158 len = 0;
159 }
160 }
161
162 // Flush any remaining data
163 if (len > 0) {
164 sendPacket(packetNum++);
165 }
166 }
167
168 private void sendPacket(int packetNum) {
169 message.clearArguments();
170 message.add(packetNum);
171 message.add(packet.length);
172 message.add(packet);
173 try {
174 OscP5.flush(message, address);
175 } catch (Exception x) {
176 x.printStackTrace();
177 }
178 }
179 }
180