Mapping the cube DJ booth
[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 */
17public class PandaDriver {
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
a922e963
MS
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 };
44b8de9c
MS
64
65 public PandaDriver(String ip, Model model, PandaMapping pm) {
66 this(ip);
e73ef85d 67
a922e963
MS
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
84086fa3 74 for (ChannelMapping channel : pm.channelList) {
a922e963
MS
75 ++ci;
76 int pi = ci * ChannelMapping.PIXELS_PER_CHANNEL;
77
84086fa3 78 switch (channel.mode) {
a922e963 79
84086fa3 80 case ChannelMapping.MODE_CUBES:
a922e963 81 // We have a list of cubes per channel
84086fa3
MS
82 for (int rawCubeIndex : channel.objectIndices) {
83 if (rawCubeIndex < 0) {
a922e963
MS
84 // No cube here, skip ahead in the buffer
85 pi += Cube.POINTS_PER_CUBE;
84086fa3 86 } else {
a922e963
MS
87 // The cube exists, check which way it is wired to
88 // figure out the order of strips.
84086fa3
MS
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 }
a922e963
MS
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
84086fa3
MS
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 }
e73ef85d 108 }
84086fa3
MS
109 break;
110
111 case ChannelMapping.MODE_BASS:
a922e963 112 // TODO(mapping): figure out how we end up connecting the bass cabinet
84086fa3
MS
113 break;
114
115 case ChannelMapping.MODE_FLOOR:
116 // TODO(mapping): figure out how these strips are wired
84086fa3
MS
117 break;
118
119 case ChannelMapping.MODE_SPEAKER:
120 // TODO(mapping): figure out how these strips are wired
84086fa3
MS
121 break;
122
123 case ChannelMapping.MODE_NULL:
a922e963 124 // No problem, nothing on this channel!
84086fa3
MS
125 break;
126
127 default:
128 throw new RuntimeException("Invalid/unhandled channel mapping mode: " + channel.mode);
e73ef85d 129 }
a922e963 130
e73ef85d 131 }
e73ef85d
MS
132 }
133
a922e963
MS
134 public void toggle() {
135 enabled = !enabled;
136 println("PandaBoard/" + ip + ": " + (enabled ? "ON" : "OFF"));
137 }
138
e73ef85d 139 public final void send(int[] colors) {
79ae8245
MS
140 if (!enabled) {
141 return;
142 }
e73ef85d
MS
143 int len = 0;
144 int packetNum = 0;
145 for (int index : points) {
84086fa3 146 int c = (index < 0) ? 0 : colors[index];
e73ef85d
MS
147 byte r = (byte) ((c >> 16) & 0xFF);
148 byte g = (byte) ((c >> 8) & 0xFF);
149 byte b = (byte) ((c) & 0xFF);
a922e963 150 packet[len++] = 0; // alpha channel, unused but makes for 4-byte alignment
e73ef85d
MS
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) {
f584b5eb 157 sendPacket(packetNum++);
e73ef85d
MS
158 len = 0;
159 }
160 }
161
162 // Flush any remaining data
163 if (len > 0) {
f584b5eb 164 sendPacket(packetNum++);
e73ef85d
MS
165 }
166 }
167
f584b5eb 168 private void sendPacket(int packetNum) {
e73ef85d
MS
169 message.clearArguments();
170 message.add(packetNum);
f584b5eb 171 message.add(packet.length);
e73ef85d
MS
172 message.add(packet);
173 try {
bfff6bc2 174 OscP5.flush(message, address);
e73ef85d
MS
175 } catch (Exception x) {
176 x.printStackTrace();
177 }
178 }
179}
180