Update mappings for all objects, assuming wiring in Alex's brain
[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 final int LEFT_SPEAKER = 0;
111 final int RIGHT_SPEAKER = 1;
112
113 return new PandaMapping[] {
114 new PandaMapping(
115 "10.200.1.29", new ChannelMapping[] {
116 new ChannelMapping(),
117 new ChannelMapping(),
118 new ChannelMapping(),
119 new ChannelMapping(),
120 new ChannelMapping(),
121 new ChannelMapping(),
122 new ChannelMapping(),
123 new ChannelMapping(ChannelMapping.MODE_BASS),
124 new ChannelMapping(ChannelMapping.MODE_STRUTS_AND_FLOOR),
125 new ChannelMapping(ChannelMapping.MODE_SPEAKER, LEFT_SPEAKER),
126 new ChannelMapping(ChannelMapping.MODE_SPEAKER, RIGHT_SPEAKER),
127 }),
128
129 new PandaMapping(
130 "10.200.1.28", new ChannelMapping[] {
131 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 1, 2, 3, 4 }),
132 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 1, 2, 3, 4 }),
133 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 1, 2, 3, 4 }),
134 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 1, 2, 3, 4 }),
135 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 1, 2, 3, 4 }),
136 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 1, 2, 3, 4 }),
137 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 1, 2, 3, 4 }),
138 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 1, 2, 3, 4 }),
139 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 1, 2, 3, 4 }),
140 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 1, 2, 3, 4 }),
141 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 1, 2, 3, 4 }),
142 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 1, 2, 3, 4 }),
143 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 1, 2, 3, 4 }),
144 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 1, 2, 3, 4 }),
145 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 1, 2, 3, 4 }),
146 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 1, 2, 3, 4 }),
147 }),
148 };
149 }
150
151 class TowerMapping {
152 public final float x, y, z;
153 public final CubeMapping[] cubeMappings;
154
155 TowerMapping(float x, float y, float z, CubeMapping[] cubeMappings) {
156 this.x = x;
157 this.y = y;
158 this.z = z;
159 this.cubeMappings = cubeMappings;
160 }
161 }
162
163 class CubeMapping {
164 public final float dx, dz, ry;
165 public final Cube.Wiring wiring;
166
167 CubeMapping(float dx, float dz, Cube.Wiring wiring) {
168 this(dx, dz, 0, wiring);
169 }
170
171 CubeMapping(float dx, float dz, float ry) {
172 this(dz, dz, ry, Cube.Wiring.FRONT_LEFT);
173 }
174
175 CubeMapping(float dx, float dz, float ry, Cube.Wiring wiring) {
176 this.dx = dx;
177 this.dz = dz;
178 this.ry = ry;
179 this.wiring = wiring;
180 }
181 }
182
183 /**
184 * Each panda board has an IP address and a fixed number of channels. The channels
185 * each have a fixed number of pixels on them. Whether or not that many physical
186 * pixels are connected to the channel, we still send it that much data.
187 */
188 class PandaMapping {
189
190 // How many channels are on the panda board
191 public final static int CHANNELS_PER_BOARD = 16;
192
193 // How many total pixels on the whole board
194 public final static int PIXELS_PER_BOARD = ChannelMapping.PIXELS_PER_CHANNEL * CHANNELS_PER_BOARD;
195
196 final String ip;
197 final ChannelMapping[] channelList = new ChannelMapping[CHANNELS_PER_BOARD];
198
199 PandaMapping(String ip, ChannelMapping[] rawChannelList) {
200 this.ip = ip;
201
202 // Ensure our array is the right length and has all valid items in it
203 for (int i = 0; i < channelList.length; ++i) {
204 channelList[i] = (i < rawChannelList.length) ? rawChannelList[i] : new ChannelMapping();
205 if (channelList[i] == null) {
206 channelList[i] = new ChannelMapping();
207 }
208 }
209 }
210 }
211
212 /**
213 * Each channel on a pandaboard can be mapped in a number of modes. The typial is
214 * to a series of connected cubes, but we also have special mappings for the bass box,
215 * the speaker enclosures, and the DJ booth floor.
216 *
217 * This class is just the mapping meta-data. It sanitizes the input to make sure
218 * that the cubes and objects being referenced actually exist in the model.
219 *
220 * The logic for how to encode the pixels is contained in the PandaDriver.
221 */
222 class ChannelMapping {
223
224 // How many cubes per channel xc_PB is configured for
225 public final static int CUBES_PER_CHANNEL = 4;
226
227 // How many total pixels on each channel
228 public final static int PIXELS_PER_CHANNEL = Cube.POINTS_PER_CUBE * CUBES_PER_CHANNEL;
229
230 public static final int MODE_NULL = 0;
231 public static final int MODE_CUBES = 1;
232 public static final int MODE_BASS = 2;
233 public static final int MODE_SPEAKER = 3;
234 public static final int MODE_STRUTS_AND_FLOOR = 4;
235 public static final int MODE_INVALID = 5;
236
237 public static final int NO_OBJECT = -1;
238
239 final int mode;
240 final int[] objectIndices = new int[CUBES_PER_CHANNEL];
241
242 ChannelMapping() {
243 this(MODE_NULL);
244 }
245
246 ChannelMapping(int mode) {
247 this(mode, new int[]{});
248 }
249
250 ChannelMapping(int mode, int rawObjectIndex) {
251 this(mode, new int[]{ rawObjectIndex });
252 }
253
254 ChannelMapping(int mode, int[] rawObjectIndices) {
255 if (mode < 0 || mode >= MODE_INVALID) {
256 throw new RuntimeException("Invalid channel mapping mode: " + mode);
257 }
258 if (mode == MODE_SPEAKER) {
259 if (rawObjectIndices.length != 1) {
260 throw new RuntimeException("Speaker channel mapping mode must specify one speaker index");
261 }
262 int speakerIndex = rawObjectIndices[0];
263 if (speakerIndex < 0 || speakerIndex >= glucose.model.speakers.size()) {
264 throw new RuntimeException("Invalid speaker channel mapping: " + speakerIndex);
265 }
266 } else if ((mode == MODE_STRUTS_AND_FLOOR) || (mode == MODE_BASS) || (mode == MODE_NULL)) {
267 if (rawObjectIndices.length > 0) {
268 throw new RuntimeException("Bass/floor/null mappings cannot specify object indices");
269 }
270 } else if (mode == MODE_CUBES) {
271 for (int rawCubeIndex : rawObjectIndices) {
272 if (glucose.model.getCubeByRawIndex(rawCubeIndex) == null) {
273 throw new RuntimeException("Non-existing cube specified in cube mapping: " + rawCubeIndex);
274 }
275 }
276 }
277
278 this.mode = mode;
279 for (int i = 0; i < objectIndices.length; ++i) {
280 objectIndices[i] = (i < rawObjectIndices.length) ? rawObjectIndices[i] : NO_OBJECT;
281 }
282 }
283 }
284