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