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