@@ -11,52 +11,60 @@ public class PluginHandler
11
11
{
12
12
public byte [][] leds ;
13
13
private Invocable invocable_engine ;
14
+ private BeatDetect beat ;
15
+ private FFT fft ;
16
+ private String plugins_directory ;
14
17
15
- public PluginHandler (String sketch_location , String plugin_name , BeatDetect beat , FFT fft )
18
+ public PluginHandler (String plugins_directory , BeatDetect beat , FFT fft )
16
19
{
17
20
final int NUM_LEDS = 17 ; // TODO User definable
18
21
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
+ }
20
36
}
21
37
22
38
public void update ()
23
39
{
24
- try
40
+ if ( invocable_engine != null )
25
41
{
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 ();}
28
49
}
29
- catch (ScriptException e ) {e .printStackTrace ();}
30
- catch (NoSuchMethodException e ) {e .printStackTrace ();}
31
50
}
32
51
33
- private void loadPlugin (String sketch_location , String plugin_name , BeatDetect beat , FFT fft , int num_leds )
52
+ public void load (String plugin_name )
34
53
{
35
54
// create a script engine manager
36
55
ScriptEngineManager factory = new ScriptEngineManager ();
37
56
38
57
// create a script engine
39
58
ScriptEngine engine = factory .getEngineByName ("JavaScript" );
40
59
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
-
52
60
// expose leds array, beat, and fft as variables to script to be used
53
61
engine .put ("leds" , leds );
54
62
engine .put ("FFT" , fft );
55
63
engine .put ("BeatDetect" , beat );
56
64
57
65
try
58
66
{
59
- String file_location = sketch_location + plugin_name + ".js" ;
67
+ String file_location = plugins_directory + plugin_name + ".js" ;
60
68
// evaluate JavaScript code from given file
61
69
engine .eval (new FileReader (file_location ));
62
70
}
0 commit comments