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