aboutsummaryrefslogtreecommitdiff
path: root/PunchingBag/src/Arduino.java
diff options
context:
space:
mode:
Diffstat (limited to 'PunchingBag/src/Arduino.java')
-rw-r--r--PunchingBag/src/Arduino.java58
1 files changed, 53 insertions, 5 deletions
diff --git a/PunchingBag/src/Arduino.java b/PunchingBag/src/Arduino.java
index 12df25f..4fa7cfa 100644
--- a/PunchingBag/src/Arduino.java
+++ b/PunchingBag/src/Arduino.java
@@ -9,12 +9,25 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import javax.swing.event.EventListenerList;
+
import gnu.io.*;
public class Arduino implements Runnable {
InputStream in;
OutputStream out;
+
+ private EventListenerList readListenerList = new EventListenerList();
+ private EventListenerList writeListenerList = new EventListenerList();
+
+ public void addSerialWriteListener(SerialWriteListener l) {
+ writeListenerList.add(SerialWriteListener.class, l);
+ }
+
+ public void addSerialReadListener(SerialReadListener l) {
+ readListenerList.add(SerialReadListener.class, l);
+ }
static void listPorts()
{
@@ -46,6 +59,25 @@ public class Arduino implements Runnable {
}
}
+ public String getID() throws IOException {
+ byte[] idBytes = {0x49, 0x44};
+ this.write(idBytes);
+ try {
+ Thread.sleep(100);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ int id;
+ while ((id = this.read()) == -1) {}
+ if (id == 66) {
+ return "B";
+ } else if (id == 76) {
+ return "L";
+ } else {
+ return "ERROR " + id;
+ }
+ }
+
/**
* @return A HashSet containing the CommPortIdentifier for all serial ports that are not currently being used.
*/
@@ -60,7 +92,7 @@ public class Arduino implements Runnable {
CommPort thePort = com.open("CommUtil", 50);
thePort.close();
h.add(com);
- System.out.println("Found a port: " + com.getPortType());
+ //System.out.println("Found a port: " + com.getPortType());
} catch (PortInUseException e) {
System.out.println("Port, " + com.getName() + ", is in use.");
} catch (Exception e) {
@@ -86,12 +118,12 @@ public class Arduino implements Runnable {
if ( commPort instanceof SerialPort )
{
SerialPort serialPort = (SerialPort) commPort;
- serialPort.setSerialPortParams(57600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
+ serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
in = serialPort.getInputStream();
out = serialPort.getOutputStream();
- (new Thread(this)).start();
+ //(new Thread(this)).start();
}
else
{
@@ -168,7 +200,7 @@ public class Arduino implements Runnable {
while ( ( len = this.in.read(buffer)) > -1 )
{
System.out.print(new String(buffer,0,len));
- input(new String(buffer,0,len));
+ //input(new String(buffer,0,len));
}
}
catch ( IOException e )
@@ -181,14 +213,30 @@ public class Arduino implements Runnable {
public void write(byte[] bytes) throws IOException {
out.write(bytes);
+ SerialWriteListener[] sListeners = writeListenerList
+ .getListeners(SerialWriteListener.class);
+ for (int i = 0; i < writeListenerList.getListenerCount(); i++) {
+ sListeners[i].serialWriteEvent(bytes);
+ }
}
public void write(byte b) throws IOException {
out.write(b);
+ SerialWriteListener[] sListeners = writeListenerList
+ .getListeners(SerialWriteListener.class);
+ for (int i = 0; i < writeListenerList.getListenerCount(); i++) {
+ sListeners[i].serialWriteEvent(b);
+ }
}
public int read() throws IOException {
- return in.read();
+ int read = in.read();
+ SerialReadListener[] sListeners = readListenerList
+ .getListeners(SerialReadListener.class);
+ for (int i = 0; i < readListenerList.getListenerCount(); i++) {
+ sListeners[i].serialReadEvent(read);
+ }
+ return read;
}
}