Add a mapping tool for Trip to deal with hardware issues
[SugarCubes.git] / _Internals.pde
CommitLineData
49815cc0 1/**
1ecdb44a
MS
2 * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND
3 *
4 * //\\ //\\ //\\ //\\
5 * ///\\\ ///\\\ ///\\\ ///\\\
6 * \\\/// \\\/// \\\/// \\\///
7 * \\// \\// \\// \\//
8 *
9 * EXPERTS ONLY!! EXPERTS ONLY!!
10 *
49815cc0
MS
11 * If you are an artist, you may ignore this file! It just sets
12 * up the framework to run the patterns. Should not need modification
13 * for general animation work.
14 */
15
16import glucose.*;
17import glucose.control.*;
3f8be614 18import glucose.effect.*;
f3f5a876 19import glucose.model.*;
49815cc0 20import glucose.pattern.*;
f3f5a876 21import glucose.transform.*;
49815cc0 22import glucose.transition.*;
49815cc0 23import heronarts.lx.*;
3f8be614 24import heronarts.lx.control.*;
49815cc0 25import heronarts.lx.effect.*;
49815cc0 26import heronarts.lx.modulator.*;
f3f5a876 27import heronarts.lx.pattern.*;
49815cc0
MS
28import heronarts.lx.transition.*;
29import ddf.minim.*;
30import ddf.minim.analysis.*;
31import processing.opengl.*;
5d70e4d7 32import rwmidi.*;
49815cc0
MS
33
34final int VIEWPORT_WIDTH = 900;
35final int VIEWPORT_HEIGHT = 700;
36final int TARGET_FRAMERATE = 45;
37
38int startMillis, lastMillis;
39GLucose glucose;
40HeronLX lx;
bf551144 41MappingTool mappingTool;
49815cc0
MS
42LXPattern[] patterns;
43LXTransition[] transitions;
44LXEffect[] effects;
45OverlayUI ui;
bf551144
MS
46ControlUI controlUI;
47MappingUI mappingUI;
e73ef85d
MS
48PandaDriver pandaFront;
49PandaDriver pandaRear;
bf551144 50boolean mappingMode = false;
e73ef85d
MS
51
52boolean pandaBoardsEnabled = false;
49815cc0
MS
53
54void setup() {
55 startMillis = lastMillis = millis();
56
57 // Initialize the Processing graphics environment
58 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
59 frameRate(TARGET_FRAMERATE);
3f8be614 60 noSmooth();
49815cc0
MS
61 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
62 logTime("Created viewport");
63
64 // Create the GLucose engine to run the cubes
1ecdb44a 65 glucose = new GLucose(this, new SCMapping());
49815cc0
MS
66 lx = glucose.lx;
67 logTime("Built GLucose engine");
68
69 // Set the patterns
70 glucose.lx.setPatterns(patterns = patterns(glucose));
bf551144 71 mappingTool = new MappingTool(glucose);
49815cc0
MS
72 logTime("Built patterns");
73 glucose.lx.addEffects(effects = effects(glucose));
74 logTime("Built effects");
75 transitions = transitions(glucose);
76 logTime("Built transitions");
e73ef85d
MS
77
78 // Build output driver
79 int[][] frontChannels = glucose.mapping.buildFrontChannelList();
80 int[][] rearChannels = glucose.mapping.buildRearChannelList();
81 int[][] flippedRGB = glucose.mapping.buildFlippedRGBList();
82 pandaFront = new PandaDriver(new NetAddress("192.168.1.28", 9001), glucose.model, frontChannels, flippedRGB);
83 pandaRear = new PandaDriver(new NetAddress("192.168.1.29", 9001), glucose.model, rearChannels, flippedRGB);
84 logTime("Build PandaDriver");
49815cc0
MS
85
86 // Build overlay UI
bf551144
MS
87 ui = controlUI = new ControlUI();
88 mappingUI = new MappingUI(mappingTool);
49815cc0 89 logTime("Built overlay UI");
1ecdb44a 90
49815cc0 91 // MIDI devices
bf551144 92 SCMidiDevices.initializeStandardDevices(glucose, controlUI.patternKnobs, controlUI.transitionKnobs, controlUI.effectKnobs);
3f8be614 93 logTime("Setup MIDI devices");
49815cc0
MS
94
95 println("Total setup: " + (millis() - startMillis) + "ms");
e73ef85d 96 println("Hit the 'p' key to toggle Panda Board output");
49815cc0
MS
97}
98
99void logTime(String evt) {
100 int now = millis();
101 println(evt + ": " + (now - lastMillis) + "ms");
102 lastMillis = now;
103}
104
105void draw() {
106 // The glucose engine deals with the core simulation here, we don't need
107 // to do anything specific. This method just needs to exist.
e73ef85d
MS
108
109 // TODO(mcslee): move into GLucose engine
110 if (pandaBoardsEnabled) {
111 color[] colors = glucose.getColors();
112 pandaFront.send(colors);
113 pandaRear.send(colors);
114 }
49815cc0
MS
115}
116
117void drawUI() {
118 if (uiOn) {
119 ui.draw();
120 } else {
121 ui.drawHelpTip();
122 }
123 ui.drawFPS();
124}
125
126boolean uiOn = true;
bf551144
MS
127int restoreToIndex = -1;
128
49815cc0 129void keyPressed() {
bf551144
MS
130 if (mappingMode) {
131 mappingTool.keyPressed();
132 }
3f8be614 133 switch (key) {
bf551144
MS
134 case 'm':
135 mappingMode = !mappingMode;
136 if (mappingMode) {
137 LXPattern pattern = lx.getPattern();
138 for (int i = 0; i < patterns.length; ++i) {
139 if (pattern == patterns[i]) {
140 restoreToIndex = i;
141 break;
142 }
143 }
144 ui = mappingUI;
145 lx.setPatterns(new LXPattern[] { mappingTool });
146 } else {
147 ui = controlUI;
148 lx.setPatterns(patterns);
149 lx.goIndex(restoreToIndex);
150 }
151 break;
e73ef85d
MS
152 case 'p':
153 pandaBoardsEnabled = !pandaBoardsEnabled;
154 println("PandaBoard Output: " + (pandaBoardsEnabled ? "ON" : "OFF"));
155 break;
3f8be614
MS
156 case 'u':
157 uiOn = !uiOn;
158 break;
49815cc0
MS
159 }
160}
161
162