Skip to content

Commit f5927dc

Browse files
author
Jonathan Feinberg
committed
Permit implementing serialEvet and serialAvailable without having to link to Serial at buildtime.
This is useful for Python mode, or any other Java-compatible Processing that doesn't allow reflection on the sketch.
1 parent d41c597 commit f5927dc

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

java/libraries/serial/src/processing/serial/Serial.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,21 @@ public Serial(PApplet parent, String portName, int baudRate, char parity, int da
112112
throw new RuntimeException("Error opening serial port " + e.getPortName() + ": " + e.getExceptionType());
113113
}
114114

115+
serialEventMethod = findCallback("serialEvent");
116+
serialAvailableMethod = findCallback("serialAvailable");
117+
}
118+
119+
private Method findCallback(final String name) {
115120
try {
116-
serialEventMethod = parent.getClass().getMethod("serialEvent", new Class[] { this.getClass() });
121+
return parent.getClass().getMethod(name, this.getClass());
117122
} catch (Exception e) {
118123
}
119-
124+
// Permit callback(Object) as alternative to callback(Serial).
120125
try {
121-
serialAvailableMethod = parent.getClass().getMethod("serialAvailable", new Class[] { this.getClass() });
126+
return parent.getClass().getMethod(name, Object.class);
122127
} catch (Exception e) {
123128
}
129+
return null;
124130
}
125131

126132

@@ -133,7 +139,7 @@ public void pre() {
133139
if (serialAvailableMethod != null && invokeSerialAvailable) {
134140
invokeSerialAvailable = false;
135141
try {
136-
serialAvailableMethod.invoke(parent, new Object[] { this });
142+
serialAvailableMethod.invoke(parent, this);
137143
} catch (Exception e) {
138144
System.err.println("Error, disabling serialAvailable() for "+port.getPortName());
139145
System.err.println(e.getLocalizedMessage());
@@ -392,7 +398,7 @@ public void serialEvent(SerialPortEvent event) {
392398
// available() and read() inside draw - but this function has no
393399
// thread-safety issues since it's being invoked during pre in the context
394400
// of the Processing applet
395-
serialEventMethod.invoke(parent, new Object[] { this });
401+
serialEventMethod.invoke(parent, this);
396402
} catch (Exception e) {
397403
System.err.println("Error, disabling serialEvent() for "+port.getPortName());
398404
System.err.println(e.getLocalizedMessage());

0 commit comments

Comments
 (0)