| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent d41c597 commit f5927dc
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -112,15 +112,21 @@ public Serial(PApplet parent, String portName, int baudRate, char parity, int da | |||
| 112 | 112 | throw new RuntimeException("Error opening serial port " + e.getPortName() + ": " + e.getExceptionType()); | |
| 113 | 113 | } | |
| 114 | 114 | ||
| 115 | + serialEventMethod = findCallback("serialEvent"); | ||
| 116 | + serialAvailableMethod = findCallback("serialAvailable"); | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | + private Method findCallback(final String name) { | ||
| 115 | 120 | try { | |
| 116 | - serialEventMethod = parent.getClass().getMethod("serialEvent", new Class[] { this.getClass() }); | ||
| 121 | + return parent.getClass().getMethod(name, this.getClass()); | ||
| 117 | 122 | } catch (Exception e) { | |
| 118 | 123 | } | |
| 119 | - | ||
| 124 | + // Permit callback(Object) as alternative to callback(Serial). | ||
| 120 | 125 | try { | |
| 121 | - serialAvailableMethod = parent.getClass().getMethod("serialAvailable", new Class[] { this.getClass() }); | ||
| 126 | + return parent.getClass().getMethod(name, Object.class); | ||
| 122 | 127 | } catch (Exception e) { | |
| 123 | 128 | } | |
| 129 | + return null; | ||
| 124 | 130 | } | |
| 125 | 131 | ||
| 126 | 132 | ||
@@ -133,7 +139,7 @@ public void pre() { | |||
| 133 | 139 | if (serialAvailableMethod != null && invokeSerialAvailable) { | |
| 134 | 140 | invokeSerialAvailable = false; | |
| 135 | 141 | try { | |
| 136 | - serialAvailableMethod.invoke(parent, new Object[] { this }); | ||
| 142 | + serialAvailableMethod.invoke(parent, this); | ||
| 137 | 143 | } catch (Exception e) { | |
| 138 | 144 | System.err.println("Error, disabling serialAvailable() for "+port.getPortName()); | |
| 139 | 145 | System.err.println(e.getLocalizedMessage()); | |
@@ -392,7 +398,7 @@ public void serialEvent(SerialPortEvent event) { | |||
| 392 | 398 | // available() and read() inside draw - but this function has no | |
| 393 | 399 | // thread-safety issues since it's being invoked during pre in the context | |
| 394 | 400 | // of the Processing applet | |
| 395 | - serialEventMethod.invoke(parent, new Object[] { this }); | ||
| 401 | + serialEventMethod.invoke(parent, this); | ||
| 396 | 402 | } catch (Exception e) { | |
| 397 | 403 | System.err.println("Error, disabling serialEvent() for "+port.getPortName()); | |
| 398 | 404 | System.err.println(e.getLocalizedMessage()); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments