Mapping the cube DJ booth
[SugarCubes.git] / _Mappings.pde
1 /**
2 * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND
3 *
4 * //\\ //\\ //\\ //\\
5 * ///\\\ ///\\\ ///\\\ ///\\\
6 * \\\/// \\\/// \\\/// \\\///
7 * \\// \\// \\// \\//
8 *
9 * EXPERTS ONLY!! EXPERTS ONLY!!
10 *
11 * This file implements the mapping functions needed to lay out the physical
12 * cubes and the output ports on the panda board. It should only be modified
13 * when physical changes or tuning is being done to the structure.
14 */
15
16 public Model buildModel() {
17
18
19 // Shorthand helpers for specifying wiring more quickly
20 final Cube.Wiring WFL = Cube.Wiring.FRONT_LEFT;
21 final Cube.Wiring WFR = Cube.Wiring.FRONT_RIGHT;
22 final Cube.Wiring WRL = Cube.Wiring.REAR_LEFT;
23 final Cube.Wiring WRR = Cube.Wiring.REAR_RIGHT;
24
25 final float CH = Cube.EDGE_HEIGHT;
26
27 final float BBY = BassBox.EDGE_HEIGHT + BoothFloor.PLEXI_WIDTH;
28 final float BBX = 56;
29 final float BBZ = 2;
30
31
32 // The model is represented as an array of towers. The cubes in the tower
33 // are represenented relatively. Each tower has an x, y, z reference position,
34 // which is typically the base cube's bottom left corner.
35 //
36 // Following that is an array of floats. A 2-d array contains an x-offset
37 // and a z-offset from the previous reference position. Typically the first cube
38 // will just be {0, 0}. Each successive cube uses the position of the previous
39 // cube as its reference.
40 //
41 // A 3-d array contains an x-offset, a z-offset, and a rotation about the
42 // y-axis.
43 //
44 // The cubes automatically increment their y-position by Cube.EDGE_HEIGHT.
45 TowerMapping[] towerCubes = new TowerMapping[] {
46
47 // DJ booth, from left to right
48 new TowerMapping(BBX, BBY, BBZ, new CubeMapping[] {
49 new CubeMapping(-7.25, 7.5, -25, WFR),
50 new CubeMapping(7.5, -15.75, 12, WRL),
51 }),
52 new TowerMapping(BBX, BBY, BBZ, new CubeMapping[] {
53 new CubeMapping(19.625, 5.375, -22, WFR),
54 new CubeMapping(8, -14.5, 10, WRR),
55 }),
56 new TowerMapping(BBX, BBY, BBZ, new CubeMapping[] {
57 new CubeMapping(48, 4.75, -35, WRL),
58 new CubeMapping(8, -15, 10, WRR),
59 }),
60 new TowerMapping(BBX, BBY, BBZ, new CubeMapping[] {
61 new CubeMapping(78.75, 3.75, -28, WRR),
62 new CubeMapping(8, -15, 10, WRR),
63 }),
64 new TowerMapping(BBX, BBY, BBZ, new CubeMapping[] {
65 new CubeMapping(104.75, 0, -27, WRL),
66 new CubeMapping(8, -15, 10, WFL),
67 }),
68
69 };
70
71 // Single cubes can be constructed directly here if you need them
72 Cube[] singleCubes = new Cube[] {
73 // new Cube(x, y, z, rx, ry, rz, wiring),
74 };
75
76 // The bass box!
77 BassBox bassBox = new BassBox(BBX, 0, BBZ);
78
79 // The speakers!
80 List<Speaker> speakers = Arrays.asList(new Speaker[] {
81 new Speaker(-12, 6, 0, 15),
82 new Speaker(TRAILER_WIDTH - Speaker.EDGE_WIDTH + 8, 6, 3, -15)
83 });
84
85 // These guts just convert the shorthand mappings into usable objects
86 ArrayList<Tower> towerList = new ArrayList<Tower>();
87 ArrayList<Cube> tower;
88 Cube[] cubes = new Cube[80];
89 int cubeIndex = 1;
90 float px, pz, ny;
91 for (TowerMapping tm : towerCubes) {
92 px = tm.x;
93 ny = tm.y;
94 pz = tm.z;
95 tower = new ArrayList<Cube>();
96 for (CubeMapping cm : tm.cubeMappings) {
97 tower.add(cubes[cubeIndex++] = new Cube(px = px + cm.dx, ny, pz = pz + cm.dz, 0, cm.ry, 0, cm.wiring));
98 ny += Cube.EDGE_HEIGHT;
99 }
100 towerList.add(new Tower(tower));
101 }
102 for (Cube cube : singleCubes) {
103 cubes[cubeIndex++] = cube;
104 }
105
106 return new Model(towerList, cubes, bassBox, speakers);
107 }
108
109 public PandaMapping[] buildPandaList() {
110 return new PandaMapping[] {
111 new PandaMapping(
112 "10.200.1.28", new ChannelMapping[] {
113 new ChannelMapping(ChannelMapping.MODE_BASS),
114 new ChannelMapping(ChannelMapping.MODE_FLOOR),
115 new ChannelMapping(ChannelMapping.MODE_SPEAKER, 0),
116 new ChannelMapping(ChannelMapping.MODE_SPEAKER, 1),
117 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 1 }),
118 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }),
119 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }),
120 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }),
121 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }),
122 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }),
123 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }),
124 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }),
125 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }),
126 }),
127
128 new PandaMapping(
129 "10.200.1.29", new ChannelMapping[] {
130 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }),
131 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }),
132 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }),
133 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }),
134 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }),
135 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }),
136 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }),
137 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }),
138 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }),
139 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }),
140 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }),
141 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }),
142 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }),
143 }),
144 };
145 }
146
147 class TowerMapping {
148 public final float x, y, z;
149 public final CubeMapping[] cubeMappings;
150
151 TowerMapping(float x, float y, float z, CubeMapping[] cubeMappings) {
152 this.x = x;
153 this.y = y;
154 this.z = z;
155 this.cubeMappings = cubeMappings;
156 }
157 }
158
159 class CubeMapping {
160 public final float dx, dz, ry;
161 public final Cube.Wiring wiring;
162
163 CubeMapping(float dx, float dz, Cube.Wiring wiring) {
164 this(dx, dz, 0, wiring);
165 }
166
167 CubeMapping(float dx, float dz, float ry) {
168 this(dz, dz, ry, Cube.Wiring.FRONT_LEFT);
169 }
170
171 CubeMapping(float dx, float dz, float ry, Cube.Wiring wiring) {
172 this.dx = dx;
173 this.dz = dz;
174 this.ry = ry;
175 this.wiring = wiring;
176 }
177 }
178
179 /**
180 * Each panda board has an IP address and a fixed number of channels. The channels
181 * each have a fixed number of pixels on them. Whether or not that many physical
182 * pixels are connected to the channel, we still send it that much data.
183 */
184 class PandaMapping {
185
186 // How many channels are on the panda board
187 public final static int CHANNELS_PER_BOARD = 13;
188
189 // How many total pixels on the whole board
190 public final static int PIXELS_PER_BOARD = ChannelMapping.PIXELS_PER_CHANNEL * CHANNELS_PER_BOARD;
191
192 final String ip;
193 final ChannelMapping[] channelList = new ChannelMapping[CHANNELS_PER_BOARD];
194
195 PandaMapping(String ip, ChannelMapping[] rawChannelList) {
196 this.ip = ip;
197
198 // Ensure our array is the right length and has all valid items in it
199 for (int i = 0; i < channelList.length; ++i) {
200 channelList[i] = (i < rawChannelList.length) ? rawChannelList[i] : new ChannelMapping();
201 if (channelList[i] == null) {
202 channelList[i] = new ChannelMapping();
203 }
204 }
205 }
206 }
207
208 /**
209 * Each channel on a pandaboard can be mapped in a number of modes. The typial is
210 * to a series of connected cubes, but we also have special mappings for the bass box,
211 * the speaker enclosures, and the DJ booth floor.
212 *
213 * This class is just the mapping meta-data. It sanitizes the input to make sure
214 * that the cubes and objects being referenced actually exist in the model.
215 *
216 * The logic for how to encode the pixels is contained in the PandaDriver.
217 */
218 class ChannelMapping {
219
220 // How many cubes per channel xc_PB is configured for
221 public final static int CUBES_PER_CHANNEL = 4;
222
223 // How many total pixels on each channel
224 public final static int PIXELS_PER_CHANNEL = Cube.POINTS_PER_CUBE * CUBES_PER_CHANNEL;
225
226 public static final int MODE_NULL = 0;
227 public static final int MODE_CUBES = 1;
228 public static final int MODE_BASS = 2;
229 public static final int MODE_SPEAKER = 3;
230 public static final int MODE_FLOOR = 4;
231 public static final int MODE_INVALID = 5;
232
233 public static final int NO_OBJECT = -1;
234
235 final int mode;
236 final int[] objectIndices = new int[CUBES_PER_CHANNEL];
237
238 ChannelMapping() {
239 this(MODE_NULL);
240 }
241
242 ChannelMapping(int mode) {
243 this(mode, new int[]{});
244 }
245
246 ChannelMapping(int mode, int rawObjectIndex) {
247 this(mode, new int[]{ rawObjectIndex });
248 }
249
250 ChannelMapping(int mode, int[] rawObjectIndices) {
251 if (mode < 0 || mode >= MODE_INVALID) {
252 throw new RuntimeException("Invalid channel mapping mode: " + mode);
253 }
254 if (mode == MODE_SPEAKER) {
255 if (rawObjectIndices.length != 1) {
256 throw new RuntimeException("Speaker channel mapping mode must specify one speaker index");
257 }
258 int speakerIndex = rawObjectIndices[0];
259 if (speakerIndex < 0 || speakerIndex >= glucose.model.speakers.size()) {
260 throw new RuntimeException("Invalid speaker channel mapping: " + speakerIndex);
261 }
262 } else if ((mode == MODE_FLOOR) || (mode == MODE_BASS) || (mode == MODE_NULL)) {
263 if (rawObjectIndices.length > 0) {
264 throw new RuntimeException("Bass/floor/null mappings cannot specify object indices");
265 }
266 } else if (mode == MODE_CUBES) {
267 for (int rawCubeIndex : rawObjectIndices) {
268 if (glucose.model.getCubeByRawIndex(rawCubeIndex) == null) {
269 throw new RuntimeException("Non-existing cube specified in cube mapping: " + rawCubeIndex);
270 }
271 }
272 }
273
274 this.mode = mode;
275 for (int i = 0; i < objectIndices.length; ++i) {
276 objectIndices[i] = (i < rawObjectIndices.length) ? rawObjectIndices[i] : NO_OBJECT;
277 }
278 }
279 }
280