Merge branch 'panda-refactor' of https://github.com/sugarcubes/SugarCubes
[SugarCubes.git] / _Internals.pde
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
16 import glucose.*;
17 import glucose.control.*;
18 import glucose.effect.*;
19 import glucose.model.*;
20 import glucose.pattern.*;
21 import glucose.transform.*;
22 import glucose.transition.*;
23 import heronarts.lx.*;
24 import heronarts.lx.control.*;
25 import heronarts.lx.effect.*;
26 import heronarts.lx.modulator.*;
27 import heronarts.lx.pattern.*;
28 import heronarts.lx.transition.*;
29 import ddf.minim.*;
30 import ddf.minim.analysis.*;
31 import processing.opengl.*;
32 import rwmidi.*;
33
34 final int VIEWPORT_WIDTH = 900;
35 final int VIEWPORT_HEIGHT = 700;
36 final int TARGET_FRAMERATE = 45;
37
38 int startMillis, lastMillis;
39 GLucose glucose;
40 HeronLX lx;
41 MappingTool mappingTool;
42 LXPattern[] patterns;
43 LXTransition[] transitions;
44 LXEffect[] effects;
45 OverlayUI ui;
46 ControlUI controlUI;
47 MappingUI mappingUI;
48 PandaDriver pandaFront;
49 PandaDriver pandaRear;
50 boolean mappingMode = false;
51
52 boolean pandaBoardsEnabled = false;
53
54 boolean debugMode = false;
55
56 void 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
105 void controllerChangeReceived(rwmidi.Controller cc) {
106 if (debugMode) {
107 println("CC: " + cc.toString());
108 }
109 }
110
111 void noteOnReceived(Note note) {
112 if (debugMode) {
113 println("Note On: " + note.toString());
114 }
115 }
116
117 void noteOffReceived(Note note) {
118 if (debugMode) {
119 println("Note Off: " + note.toString());
120 }
121 }
122
123 void logTime(String evt) {
124 int now = millis();
125 println(evt + ": " + (now - lastMillis) + "ms");
126 lastMillis = now;
127 }
128
129 void 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
141 void drawUI() {
142 if (uiOn) {
143 ui.draw();
144 } else {
145 ui.drawHelpTip();
146 }
147 ui.drawFPS();
148 }
149
150 boolean uiOn = true;
151 int restoreToIndex = -1;
152
153 void 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