Skip to content

Commit 0b53157

Browse files
committed
Made PluginHandler more robust to make room for SystemTrayHandler
PluginHandler accepts different parameters now, and PluginHandler.load(String plugin_name) gets called separately now.
1 parent db2d35e commit 0b53157

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

Processing/PluginHandler.java

+28-20
Original file line numberDiff line numberDiff line change
@@ -11,52 +11,60 @@ public class PluginHandler
1111
{
1212
public byte[][] leds;
1313
private Invocable invocable_engine;
14+
private BeatDetect beat;
15+
private FFT fft;
16+
private String plugins_directory;
1417

15-
public PluginHandler(String sketch_location, String plugin_name, BeatDetect beat, FFT fft)
18+
public PluginHandler(String plugins_directory, BeatDetect beat, FFT fft)
1619
{
1720
final int NUM_LEDS = 17; // TODO User definable
1821

19-
loadPlugin(sketch_location, plugin_name, beat, fft, NUM_LEDS);
22+
this.plugins_directory = plugins_directory;
23+
this.beat = beat;
24+
this.fft = fft;
25+
26+
// Initialize the leds array
27+
leds = new byte[NUM_LEDS][3];
28+
29+
// Zero-out the array in case the plugin dev does something stupid
30+
for(int i = 0; i < NUM_LEDS; i++)
31+
{
32+
leds[i][0] = 0;
33+
leds[i][1] = 0;
34+
leds[i][2] = 0;
35+
}
2036
}
2137

2238
public void update()
2339
{
24-
try
40+
if ( invocable_engine != null )
2541
{
26-
// invoke the global function named "update"
27-
invocable_engine.invokeFunction("update");
42+
try
43+
{
44+
// invoke the global function named "update"
45+
invocable_engine.invokeFunction("update");
46+
}
47+
catch (ScriptException e) {e.printStackTrace();}
48+
catch (NoSuchMethodException e) {e.printStackTrace();}
2849
}
29-
catch (ScriptException e) {e.printStackTrace();}
30-
catch (NoSuchMethodException e) {e.printStackTrace();}
3150
}
3251

33-
private void loadPlugin(String sketch_location, String plugin_name, BeatDetect beat, FFT fft, int num_leds)
52+
public void load(String plugin_name)
3453
{
3554
// create a script engine manager
3655
ScriptEngineManager factory = new ScriptEngineManager();
3756

3857
// create a script engine
3958
ScriptEngine engine = factory.getEngineByName("JavaScript");
4059

41-
// Initialize our array
42-
leds = new byte[num_leds][3];
43-
44-
// zero-out the array in case the plugin dev does something stupid
45-
for(int i = 0; i < num_leds; i++)
46-
{
47-
leds[i][0] = 0;
48-
leds[i][1] = 0;
49-
leds[i][2] = 0;
50-
}
51-
5260
// expose leds array, beat, and fft as variables to script to be used
5361
engine.put("leds", leds);
5462
engine.put("FFT", fft);
5563
engine.put("BeatDetect", beat);
5664

5765
try
5866
{
59-
String file_location = sketch_location + plugin_name + ".js";
67+
String file_location = plugins_directory + plugin_name + ".js";
6068
// evaluate JavaScript code from given file
6169
engine.eval(new FileReader(file_location));
6270
}

Processing/Processing.pde

+4-3
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,13 @@ void setup()
5252
// in Processing this does not work
5353
// String working_directory = MyClassName.class.getResource("").getPath();
5454
// So we have to use this instead:
55-
String sketch_location = sketchPath("")+"Plugins/";
55+
String sketch_location = sketchPath("")+"Plugins/"; // TODO - This folder isn't here when you export the program.
5656

57-
String pluginName = "Rainbow";
57+
String plugin_name = "Rainbow";
5858

5959
// Import a custom pattern plugin
60-
plugin = new PluginHandler(sketch_location, pluginName, beat, fft);
60+
plugin = new PluginHandler(sketch_location, beat, fft);
61+
plugin.load(plugin_name);
6162
}
6263

6364
// Trys to find and set the Soundflower (2ch) input

0 commit comments

Comments
 (0)