Update mapping constants and tool for trip
[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 void setup() {
55 startMillis = lastMillis = millis();
56
57 // Initialize the Processing graphics environment
58 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
59 frameRate(TARGET_FRAMERATE);
60 noSmooth();
61 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
62 logTime("Created viewport");
63
64 // Create the GLucose engine to run the cubes
65 glucose = new GLucose(this, new SCMapping());
66 lx = glucose.lx;
67 logTime("Built GLucose engine");
68
69 // Set the patterns
70 glucose.lx.setPatterns(patterns = patterns(glucose));
71 logTime("Built patterns");
72 glucose.lx.addEffects(effects = effects(glucose));
73 logTime("Built effects");
74 transitions = transitions(glucose);
75 logTime("Built transitions");
76
77 // Build output driver
78 int[][] frontChannels = glucose.mapping.buildFrontChannelList();
79 int[][] rearChannels = glucose.mapping.buildRearChannelList();
80 int[][] flippedRGB = glucose.mapping.buildFlippedRGBList();
81 mappingTool = new MappingTool(glucose, frontChannels, rearChannels);
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");
85
86 // Build overlay UI
87 ui = controlUI = new ControlUI();
88 mappingUI = new MappingUI(mappingTool);
89 logTime("Built overlay UI");
90
91 // MIDI devices
92 SCMidiDevices.initializeStandardDevices(glucose, controlUI.patternKnobs, controlUI.transitionKnobs, controlUI.effectKnobs);
93 logTime("Setup MIDI devices");
94
95 println("Total setup: " + (millis() - startMillis) + "ms");
96 println("Hit the 'p' key to toggle Panda Board output");
97 }
98
99 void logTime(String evt) {
100 int now = millis();
101 println(evt + ": " + (now - lastMillis) + "ms");
102 lastMillis = now;
103 }
104
105 void 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.
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 }
115 }
116
117 void drawUI() {
118 if (uiOn) {
119 ui.draw();
120 } else {
121 ui.drawHelpTip();
122 }
123 ui.drawFPS();
124 }
125
126 boolean uiOn = true;
127 int restoreToIndex = -1;
128
129 void keyPressed() {
130 if (mappingMode) {
131 mappingTool.keyPressed();
132 }
133 switch (key) {
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;
152 case 'p':
153 pandaBoardsEnabled = !pandaBoardsEnabled;
154 println("PandaBoard Output: " + (pandaBoardsEnabled ? "ON" : "OFF"));
155 break;
156 case 'u':
157 uiOn = !uiOn;
158 break;
159 }
160 }
161
162