/programy/Java/AVRcamVIEW/src/avr/connection/AbstractConnection.java |
---|
0,0 → 1,160 |
/* |
AVRcamVIEW: A PC application to test out the functionallity of the |
AVRcam real-time image processing engine. |
Copyright (C) 2004 Brent A. Taylor |
This program is free software; you can redistribute it and/or |
modify it under the terms of the GNU General Public |
License as published by the Free Software Foundation; either |
version 2 of the License, or (at your option) any later version. |
This program is distributed in the hope that it will be useful, |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
General Public License for more details. |
You should have received a copy of the GNU General Public |
License along with this program; if not, write to the Free Software |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
For more information on the AVRcamVIEW, please contact: |
taylorba@comcast.net |
or go to www.jrobot.net for more details regarding the system. |
*/ |
package avr.connection; |
import javax.swing.event.*; |
import avr.connection.event.*; |
/*********************************************************************** |
* Defines a Connection that fires events describing if the connection |
* was opened or closed. |
*/ |
public abstract class AbstractConnection implements Connection { |
/** |
* The list of registered event listeners. |
*/ |
private EventListenerList listenerList; |
/** |
* Boolean to test if the connection is connected (true) or |
* disconnected (false). |
*/ |
private boolean connected; |
/** |
* Initialize this object and set connected to false. |
*/ |
protected AbstractConnection() { |
listenerList = new EventListenerList(); |
connected = false; |
} |
/** |
* Set connected to true and fire a connected event. |
*/ |
protected void setConnected() { |
setConnected(true); |
fireConnectedEvent(this); |
} |
/** |
* Set connected to false and fire a disconnected event. |
*/ |
protected void setDisconnected() { |
setConnected(false); |
fireDisconnectedEvent(this); |
} |
/** |
* Set this connected variable to the given value. |
* @param connected true if this connection is connected, false otherwise. |
*/ |
private void setConnected(boolean connected) { |
this.connected = connected; |
} |
/** |
* Tells whether or not this connection is connected. |
* @return true if, and only if, this connection is connected. |
*/ |
public boolean isConnected() { |
return connected; |
} |
/** |
* Adds a listener to the list that's notified each time a connect or |
* disconnect occurs. |
* @param cl the ConnectionListener |
*/ |
public void addConnectionListener(ConnectionListener cl) { |
listenerList.add(ConnectionListener.class, cl); |
} |
/** |
* Removes a listener to the list that's notified each time a connect or |
* disconnect occurs. |
* @param cl the ConnectionListener |
*/ |
public void removeConnectionListener(ConnectionListener cl) { |
listenerList.remove(ConnectionListener.class, cl); |
} |
public void removeAllConnectionListeners() { |
ConnectionListener[] listeners = getConnectionListeners(); |
for(int i = 0; i < listeners.length; i++) { |
removeConnectionListener(listeners[i]); |
} |
} |
/** |
* Returns an array of all the connection listeners registered on |
* this connection. |
* @return all of this connection's ConnectionListeners or |
* an empty array if no connection listeners are currently registered |
*/ |
public ConnectionListener[] getConnectionListeners() { |
return (ConnectionListener[]) |
listenerList.getListeners(ConnectionListener.class); |
} |
/** |
* Notifies all listeners that a connection has been established. |
* @param source This connection. |
*/ |
protected void fireConnectedEvent(Object source) { |
ConnectionListener[] listeners = getConnectionListeners(); |
ConnectionEvent event = null; |
for(int i = 0; i < listeners.length; i++) { |
if(event == null) { |
event = new ConnectionEvent(this); |
} |
listeners[i].connected(event); |
} |
} |
/** |
* Notifies all listeners that a connection has been destroyed. |
* @param source This connection. |
*/ |
protected void fireDisconnectedEvent(Object source) { |
ConnectionListener[] listeners = getConnectionListeners(); |
ConnectionEvent event = null; |
for(int i = 0; i < listeners.length; i++) { |
if(event == null) { |
event = new ConnectionEvent(this); |
} |
listeners[i].disconnected(event); |
} |
} |
} |
/programy/Java/AVRcamVIEW/src/avr/connection/Connection.java |
---|
0,0 → 1,81 |
/* |
AVRcamVIEW: A PC application to test out the functionallity of the |
AVRcam real-time image processing engine. |
Copyright (C) 2004 Brent A. Taylor |
This program is free software; you can redistribute it and/or |
modify it under the terms of the GNU General Public |
License as published by the Free Software Foundation; either |
version 2 of the License, or (at your option) any later version. |
This program is distributed in the hope that it will be useful, |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
General Public License for more details. |
You should have received a copy of the GNU General Public |
License along with this program; if not, write to the Free Software |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
For more information on the AVRcamVIEW, please contact: |
taylorba@comcast.net |
or go to www.jrobot.net for more details regarding the system. |
*/ |
package avr.connection; |
import java.io.*; |
import java.nio.channels.*; |
/*********************************************************************** |
* Defines methods for some type of I/O Connection. |
*/ |
public interface Connection { |
/** |
* Connects this connection. |
* @throws IOException If the connection could not be connected. |
*/ |
public void connect() throws Exception; |
/** |
* Disconnects this connection. |
* @throws IOException If the connection could not be disconnected. |
*/ |
public void disconnect() throws IOException; |
/** |
* Returns the Object to which this connection is wrapped around. |
* @return The Object to which this connection is wrapped around. |
*/ |
public Object getConnectionObject(); |
/** |
* Tells whether or not this connection is connected. |
* @return true if, and only if, this connection is connected. |
*/ |
public boolean isConnected(); |
/** |
* Returns the InputStream associated with this connection. |
* @return The InputStream associated with this connection. |
* @throws IOException If the InputStream could not be returned. |
*/ |
public InputStream getInputStream() throws IOException; |
/** |
* Returns the OutputStream associated with this connection. |
* @return The OutputStream associated with this connection. |
* @throws IOException If the OutputStream could not be returned. |
*/ |
public OutputStream getOutputStream() throws IOException; |
/** |
* Returns the ByteChannel associated with this connection. |
* @return The ByteChannel associated with this connection. |
*/ |
public ByteChannel getChannel(); |
} |
/programy/Java/AVRcamVIEW/src/avr/connection/SerialConnection.java |
---|
0,0 → 1,128 |
/* |
AVRcamVIEW: A PC application to test out the functionallity of the |
AVRcam real-time image processing engine. |
Copyright (C) 2004 Brent A. Taylor |
This program is free software; you can redistribute it and/or |
modify it under the terms of the GNU General Public |
License as published by the Free Software Foundation; either |
version 2 of the License, or (at your option) any later version. |
This program is distributed in the hope that it will be useful, |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
General Public License for more details. |
You should have received a copy of the GNU General Public |
License along with this program; if not, write to the Free Software |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
For more information on the AVRcamVIEW, please contact: |
taylorba@comcast.net |
or go to www.jrobot.net for more details regarding the system. |
*/ |
package avr.connection; |
import java.io.*; |
import java.net.*; |
import java.nio.*; |
import java.nio.channels.*; |
import java.util.*; |
import javax.comm.*; |
/*********************************************************************** |
* This class represents a Connection to a Serial Port. |
*/ |
public class SerialConnection extends AbstractConnection { |
private SerialPort serialPort; |
private SerialParams params; |
private String comPort; |
public static String[] getSerialPorts() { |
ArrayList ports = new ArrayList(); |
Enumeration allPorts = CommPortIdentifier.getPortIdentifiers(); |
CommPortIdentifier identifier; |
while(allPorts.hasMoreElements()) { |
identifier = (CommPortIdentifier)allPorts.nextElement(); |
if(identifier.getPortType() == CommPortIdentifier.PORT_SERIAL) { |
ports.add(identifier.getName()); |
} |
} |
Collections.reverse(ports); |
return (String[])ports.toArray(new String[ports.size()]); |
} |
public SerialConnection(String comPort) { |
this(comPort, new SerialParams()); |
} |
public SerialConnection(String comPort, SerialParams params) { |
this.comPort = comPort; |
this.params = params; |
} |
public Object getConnectionObject() { |
return serialPort; |
} |
public synchronized void connect() throws Exception { |
CommPortIdentifier identifier = CommPortIdentifier.getPortIdentifier(comPort); |
serialPort = (SerialPort)identifier.open("AVRcamView", 2000); |
serialPort.setFlowControlMode(params.getFlowControl()); |
serialPort.setSerialPortParams(params.getBaudRate(), |
params.getDataBits(), |
params.getStopBits(), |
params.getParity()); |
setConnected(); |
} |
public synchronized void disconnect() throws IOException { |
serialPort.close(); |
setDisconnected(); |
} |
public InputStream getInputStream() throws IOException { |
return serialPort.getInputStream(); |
} |
public OutputStream getOutputStream() throws IOException { |
return serialPort.getOutputStream(); |
} |
public ByteChannel getChannel() { |
return null; |
} |
public String toString() { |
return serialPort.toString(); |
} |
public void setSerialParams(SerialParams params) throws UnsupportedCommOperationException { |
serialPort.setFlowControlMode(params.getFlowControl()); |
serialPort.setSerialPortParams(params.getBaudRate(), |
params.getDataBits(), |
params.getStopBits(), |
params.getParity()); |
this.params = params; |
} |
public String getComPort() { |
return comPort; |
} |
public SerialParams getSerialParams() { |
return params; |
} |
} |
/programy/Java/AVRcamVIEW/src/avr/connection/SerialParams.java |
---|
0,0 → 1,127 |
/* |
AVRcamVIEW: A PC application to test out the functionallity of the |
AVRcam real-time image processing engine. |
Copyright (C) 2004 Brent A. Taylor |
This program is free software; you can redistribute it and/or |
modify it under the terms of the GNU General Public |
License as published by the Free Software Foundation; either |
version 2 of the License, or (at your option) any later version. |
This program is distributed in the hope that it will be useful, |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
General Public License for more details. |
You should have received a copy of the GNU General Public |
License along with this program; if not, write to the Free Software |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
For more information on the AVRcamVIEW, please contact: |
taylorba@comcast.net |
or go to www.jrobot.net for more details regarding the system. |
*/ |
package avr.connection; |
import java.io.*; |
import java.util.*; |
import javax.comm.*; |
public class SerialParams implements Serializable { |
public static void main(String[] args) { |
System.out.println("Flow Control None: " + SerialPort.FLOWCONTROL_NONE); |
System.out.println("Flow Control RTSCTS IN: " + SerialPort.FLOWCONTROL_RTSCTS_IN); |
System.out.println("Flow Control RTSCTS OUT: " + SerialPort.FLOWCONTROL_RTSCTS_OUT); |
System.out.println("Flow Control XON/XOFF IN: " + SerialPort.FLOWCONTROL_XONXOFF_IN); |
System.out.println("Flow Control XON/XOFF OUT: " + SerialPort.FLOWCONTROL_XONXOFF_OUT); |
System.out.println("Data Bits 5: " + SerialPort.DATABITS_5); |
System.out.println("Data Bits 6: " + SerialPort.DATABITS_6); |
System.out.println("Data Bits 7: " + SerialPort.DATABITS_7); |
System.out.println("Data Bits 8: " + SerialPort.DATABITS_8); |
System.out.println("Parity Even: " + SerialPort.PARITY_EVEN); |
System.out.println("Parity Odd: " + SerialPort.PARITY_ODD); |
System.out.println("Parity Mark: " + SerialPort.PARITY_MARK); |
System.out.println("Parity Space: " + SerialPort.PARITY_SPACE); |
System.out.println("Parity None: " + SerialPort.PARITY_NONE); |
System.out.println("Stop Bits 1: " + SerialPort.STOPBITS_1); |
System.out.println("Stop Bits 1.5: " + SerialPort.STOPBITS_1_5); |
System.out.println("Stop Bits 2: " + SerialPort.STOPBITS_2); |
} |
private int baudRate; |
private int dataBits; |
private int stopBits; |
private int parity; |
private int flowControl; |
public SerialParams() { |
this(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE, SerialPort.FLOWCONTROL_NONE); |
} |
public SerialParams(int baudRate, int dataBits, |
int stopBits, int parity, int flowControl) { |
this.baudRate = baudRate; |
this.dataBits = dataBits; |
this.stopBits = stopBits; |
this.parity = parity; |
this.flowControl = flowControl; |
} |
public int getBaudRate() { |
return baudRate; |
} |
public int getFlowControl() { |
return flowControl; |
} |
public int getDataBits() { |
return dataBits; |
} |
public int getParity() { |
return parity; |
} |
public int getStopBits() { |
return stopBits; |
} |
public void setStopBits(int stopBits) { |
this.stopBits = stopBits; |
} |
public void setParity(int parity) { |
this.parity = parity; |
} |
public void setFlowControl(int flowControl) { |
this.flowControl = flowControl; |
} |
public void setDataBits(int dataBits) { |
this.dataBits = dataBits; |
} |
public void setBaudRate(int baudRate) { |
this.baudRate = baudRate; |
} |
public String toString() { |
StringBuffer builder = new StringBuffer("Serial Parameters["); |
builder.append("baudrate=").append(baudRate) |
.append("databits=").append(dataBits) |
.append("stopbits=").append(stopBits) |
.append("parity=").append(parity) |
.append("flowcontrol=").append(flowControl); |
return builder.toString(); |
} |
} |
/programy/Java/AVRcamVIEW/src/avr/connection/event/ConnectionEvent.java |
---|
0,0 → 1,47 |
/* |
AVRcamVIEW: A PC application to test out the functionallity of the |
AVRcam real-time image processing engine. |
Copyright (C) 2004 Brent A. Taylor |
This program is free software; you can redistribute it and/or |
modify it under the terms of the GNU General Public |
License as published by the Free Software Foundation; either |
version 2 of the License, or (at your option) any later version. |
This program is distributed in the hope that it will be useful, |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
General Public License for more details. |
You should have received a copy of the GNU General Public |
License along with this program; if not, write to the Free Software |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
For more information on the AVRcamVIEW, please contact: |
taylorba@comcast.net |
or go to www.jrobot.net for more details regarding the system. |
*/ |
package avr.connection.event; |
import java.util.*; |
import avr.connection.*; |
import avr.device.*; |
/*********************************************************************** |
* Defines and event to be fired when a Connection is opened or closed. |
*/ |
public class ConnectionEvent extends EventObject { |
/** |
* Constructs a Connection Event created by the given Connection object. |
* @param source The object firing this event. |
*/ |
public ConnectionEvent(Object source) { |
super(source); |
} |
} |
/programy/Java/AVRcamVIEW/src/avr/connection/event/ConnectionListener.java |
---|
0,0 → 1,49 |
/* |
AVRcamVIEW: A PC application to test out the functionallity of the |
AVRcam real-time image processing engine. |
Copyright (C) 2004 Brent A. Taylor |
This program is free software; you can redistribute it and/or |
modify it under the terms of the GNU General Public |
License as published by the Free Software Foundation; either |
version 2 of the License, or (at your option) any later version. |
This program is distributed in the hope that it will be useful, |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
General Public License for more details. |
You should have received a copy of the GNU General Public |
License along with this program; if not, write to the Free Software |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
For more information on the AVRcamVIEW, please contact: |
taylorba@comcast.net |
or go to www.jrobot.net for more details regarding the system. |
*/ |
package avr.connection.event; |
import java.util.*; |
/*********************************************************************** |
* This class defines methods that are invoked when a Connection is |
* either opened or closed. |
*/ |
public interface ConnectionListener extends EventListener { |
/** |
* Invoked when a Connection is opened. |
* @param ce A ConnectionEvent encapsulating the event information. |
*/ |
public void connected(ConnectionEvent ce); |
/** |
* Invoked when a Connection is closed. |
* @param ce A ConnectionEvent encapsulating the event information. |
*/ |
public void disconnected(ConnectionEvent ce); |
} |