Merge branch 'panda-refactor' of https://github.com/sugarcubes/SugarCubes
[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 53
cc9fcf4b
MS
54boolean debugMode = false;
55
49815cc0
MS
56void setup() {
57 startMillis = lastMillis = millis();
58
59 // Initialize the Processing graphics environment
60 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
61 frameRate(TARGET_FRAMERATE);
3f8be614 62 noSmooth();
49815cc0
MS
63 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
64 logTime("Created viewport");
65
66 // Create the GLucose engine to run the cubes
1ecdb44a 67 glucose = new GLucose(this, new SCMapping());
49815cc0 68 lx = glucose.lx;
cc9fcf4b 69 lx.enableKeyboardTempo();
49815cc0
MS
70 logTime("Built GLucose engine");
71
72 // Set the patterns
73 glucose.lx.setPatterns(patterns = patterns(glucose));
74 logTime("Built patterns");
75 glucose.lx.addEffects(effects = effects(glucose));
76 logTime("Built effects");
cc9fcf4b 77 glucose.setTransitions(transitions = transitions(glucose));
49815cc0 78 logTime("Built transitions");
e73ef85d
MS
79
80 // Build output driver
81 int[][] frontChannels = glucose.mapping.buildFrontChannelList();
82 int[][] rearChannels = glucose.mapping.buildRearChannelList();
83 int[][] flippedRGB = glucose.mapping.buildFlippedRGBList();
2bae07c9 84 mappingTool = new MappingTool(glucose, frontChannels, rearChannels);
e73ef85d
MS
85 pandaFront = new PandaDriver(new NetAddress("192.168.1.28", 9001), glucose.model, frontChannels, flippedRGB);
86 pandaRear = new PandaDriver(new NetAddress("192.168.1.29", 9001), glucose.model, rearChannels, flippedRGB);
87 logTime("Build PandaDriver");
49815cc0
MS
88
89 // Build overlay UI
bf551144
MS
90 ui = controlUI = new ControlUI();
91 mappingUI = new MappingUI(mappingTool);
49815cc0 92 logTime("Built overlay UI");
1ecdb44a 93
49815cc0 94 // MIDI devices
cc9fcf4b
MS
95 for (MidiInputDevice d : RWMidi.getInputDevices()) {
96 d.createInput(this);
97 }
98 SCMidiDevices.initializeStandardDevices(glucose);
3f8be614 99 logTime("Setup MIDI devices");
49815cc0
MS
100
101 println("Total setup: " + (millis() - startMillis) + "ms");
e73ef85d 102 println("Hit the 'p' key to toggle Panda Board output");
49815cc0
MS
103}
104
cc9fcf4b
MS
105void controllerChangeReceived(rwmidi.Controller cc) {
106 if (debugMode) {
107 println("CC: " + cc.toString());
108 }
109}
110
111void noteOnReceived(Note note) {
112 if (debugMode) {
113 println("Note On: " + note.toString());
114 }
115}
116
117void noteOffReceived(Note note) {
118 if (debugMode) {
119 println("Note Off: " + note.toString());
120 }
121}
122
49815cc0
MS
123void logTime(String evt) {
124 int now = millis();
125 println(evt + ": " + (now - lastMillis) + "ms");
126 lastMillis = now;
127}
128
129void draw() {
130 // The glucose engine deals with the core simulation here, we don't need
131 // to do anything specific. This method just needs to exist.
e73ef85d
MS
132
133 // TODO(mcslee): move into GLucose engine
134 if (pandaBoardsEnabled) {
135 color[] colors = glucose.getColors();
136 pandaFront.send(colors);
137 pandaRear.send(colors);
138 }
49815cc0
MS
139}
140
141void drawUI() {
142 if (uiOn) {
143 ui.draw();
144 } else {
145 ui.drawHelpTip();
146 }
147 ui.drawFPS();
148}
149
150boolean uiOn = true;
bf551144
MS
151int restoreToIndex = -1;
152
49815cc0 153void keyPressed() {
bf551144
MS
154 if (mappingMode) {
155 mappingTool.keyPressed();
156 }
3f8be614 157 switch (key) {
cc9fcf4b
MS
158 case 'd':
159 debugMode = !debugMode;
160 println("Debug output: " + (debugMode ? "ON" : "OFF"));
bf551144
MS
161 case 'm':
162 mappingMode = !mappingMode;
163 if (mappingMode) {
164 LXPattern pattern = lx.getPattern();
165 for (int i = 0; i < patterns.length; ++i) {
166 if (pattern == patterns[i]) {
167 restoreToIndex = i;
168 break;
169 }
170 }
171 ui = mappingUI;
172 lx.setPatterns(new LXPattern[] { mappingTool });
173 } else {
174 ui = controlUI;
175 lx.setPatterns(patterns);
176 lx.goIndex(restoreToIndex);
177 }
178 break;
e73ef85d
MS
179 case 'p':
180 pandaBoardsEnabled = !pandaBoardsEnabled;
181 println("PandaBoard Output: " + (pandaBoardsEnabled ? "ON" : "OFF"));
cc9fcf4b 182 break;
3f8be614
MS
183 case 'u':
184 uiOn = !uiOn;
185 break;
49815cc0
MS
186 }
187}
188
189