Merge branch 'panda-refactor' of https://github.com/sugarcubes/SugarCubes
[SugarCubes.git] / _Internals.pde
... / ...
CommitLineData
1/**
2 * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND
3 *
4 * //\\ //\\ //\\ //\\
5 * ///\\\ ///\\\ ///\\\ ///\\\
6 * \\\/// \\\/// \\\/// \\\///
7 * \\// \\// \\// \\//
8 *
9 * EXPERTS ONLY!! EXPERTS ONLY!!
10 *
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.*;
18import glucose.effect.*;
19import glucose.model.*;
20import glucose.pattern.*;
21import glucose.transform.*;
22import glucose.transition.*;
23import heronarts.lx.*;
24import heronarts.lx.control.*;
25import heronarts.lx.effect.*;
26import heronarts.lx.modulator.*;
27import heronarts.lx.pattern.*;
28import heronarts.lx.transition.*;
29import ddf.minim.*;
30import ddf.minim.analysis.*;
31import processing.opengl.*;
32import rwmidi.*;
33
34final int VIEWPORT_WIDTH = 900;
35final int VIEWPORT_HEIGHT = 700;
36final int TARGET_FRAMERATE = 45;
37
38int startMillis, lastMillis;
39GLucose glucose;
40HeronLX lx;
41MappingTool mappingTool;
42LXPattern[] patterns;
43LXTransition[] transitions;
44LXEffect[] effects;
45OverlayUI ui;
46ControlUI controlUI;
47MappingUI mappingUI;
48PandaDriver pandaFront;
49PandaDriver pandaRear;
50boolean mappingMode = false;
51
52boolean pandaBoardsEnabled = false;
53
54boolean debugMode = false;
55
56void setup() {
57 startMillis = lastMillis = millis();
58
59 // Initialize the Processing graphics environment
60 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
61 frameRate(TARGET_FRAMERATE);
62 noSmooth();
63 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
64 logTime("Created viewport");
65
66 // Create the GLucose engine to run the cubes
67 glucose = new GLucose(this, new SCMapping());
68 lx = glucose.lx;
69 lx.enableKeyboardTempo();
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");
77 glucose.setTransitions(transitions = transitions(glucose));
78 logTime("Built transitions");
79
80 // Build output driver
81 int[][] frontChannels = glucose.mapping.buildFrontChannelList();
82 int[][] rearChannels = glucose.mapping.buildRearChannelList();
83 int[][] flippedRGB = glucose.mapping.buildFlippedRGBList();
84 mappingTool = new MappingTool(glucose, frontChannels, rearChannels);
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");
88
89 // Build overlay UI
90 ui = controlUI = new ControlUI();
91 mappingUI = new MappingUI(mappingTool);
92 logTime("Built overlay UI");
93
94 // MIDI devices
95 for (MidiInputDevice d : RWMidi.getInputDevices()) {
96 d.createInput(this);
97 }
98 SCMidiDevices.initializeStandardDevices(glucose);
99 logTime("Setup MIDI devices");
100
101 println("Total setup: " + (millis() - startMillis) + "ms");
102 println("Hit the 'p' key to toggle Panda Board output");
103}
104
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
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.
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 }
139}
140
141void drawUI() {
142 if (uiOn) {
143 ui.draw();
144 } else {
145 ui.drawHelpTip();
146 }
147 ui.drawFPS();
148}
149
150boolean uiOn = true;
151int restoreToIndex = -1;
152
153void keyPressed() {
154 if (mappingMode) {
155 mappingTool.keyPressed();
156 }
157 switch (key) {
158 case 'd':
159 debugMode = !debugMode;
160 println("Debug output: " + (debugMode ? "ON" : "OFF"));
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;
179 case 'p':
180 pandaBoardsEnabled = !pandaBoardsEnabled;
181 println("PandaBoard Output: " + (pandaBoardsEnabled ? "ON" : "OFF"));
182 break;
183 case 'u':
184 uiOn = !uiOn;
185 break;
186 }
187}
188
189