Commands to Utec supported.

git-svn-id: http://svn.3splooges.com/romraider-arch/trunk@518 d2e2e1cd-ba16-0410-be16-b7c4453c7c2d
This commit is contained in:
Tgui 2007-02-16 04:34:49 +00:00
parent 59360b5512
commit 9fbedabaab
3 changed files with 280 additions and 193 deletions

View File

@ -24,7 +24,7 @@ import enginuity.logger.utec.mapData.UtecMapData;
import gnu.io.*;
public class UtecControl implements SerialPortEventListener{
public class UtecSerialConnection implements SerialPortEventListener{
// Parent object organizing connections to and from UTEC
//private JPanel parent;
@ -76,7 +76,7 @@ public class UtecControl implements SerialPortEventListener{
*/
//public SerialConnection(JPanel parent, SerialParameters parameters,
// TextArea messageAreaOut, TextArea messageAreaIn) {
public UtecControl(SerialParameters parameters) {
public UtecSerialConnection(SerialParameters parameters) {
//this.parent = parent;
//this.messageAreaOut = messageAreaOut;
//this.messageAreaIn = messageAreaIn;
@ -101,8 +101,8 @@ public class UtecControl implements SerialPortEventListener{
*/
public void resetUtec(){
//OutPut 2 ctrl-x to UTEC
this.sendDataToUtec('\u0018');
this.sendDataToUtec('\u0018');
//this.sendDataToUtec('\u0018');
//this.sendDataToUtec('\u0018');
this.sendDataToUtec(33);
this.sendDataToUtec(33);
}
@ -187,7 +187,12 @@ public class UtecControl implements SerialPortEventListener{
*
* @param charValue
*/
private void sendDataToUtec(int charValue){
public void sendDataToUtec(int charValue){
if(this.sPort == null){
System.err.println("No Port Selected.");
return;
}
try{
outputToUtecStream.write(charValue);
}
@ -197,6 +202,8 @@ public class UtecControl implements SerialPortEventListener{
}
}
/**
* Opens a connection to the defined serial port If attempt fails,
* SerialConnectionException is thrown
@ -205,6 +212,11 @@ public class UtecControl implements SerialPortEventListener{
*/
public void openConnection() throws SerialConnectionException {
if(sPort == null){
System.err.println("No port selected.");
return;
}
// Obtain a CommPortIdentifier object for the port you want to open.
try {
//System.out.println("PORT: "+parameters.getPortName());
@ -223,7 +235,7 @@ public class UtecControl implements SerialPortEventListener{
sPort = (SerialPort) portId.open("SerialDemo", 30000);
} catch (PortInUseException e) {
System.err.println("Can't open serial port");
throw new SerialConnectionException(e.getMessage());
//throw new SerialConnectionException(e.getMessage());
}
// Set the parameters of the connection. If they won't set, close the
@ -289,6 +301,10 @@ public class UtecControl implements SerialPortEventListener{
*/
public void setConnectionParameters() throws SerialConnectionException {
if(sPort == null){
System.err.println("No port selected.");
return;
}
// Save state of parameters before trying a set.
int oldBaudRate = sPort.getBaudRate();
int oldDatabits = sPort.getDataBits();

View File

@ -34,7 +34,7 @@ public class UtecInterface{
private static SerialParameters parameters = new SerialParameters();
//Actual connection entity
private static UtecControl utecControl = new UtecControl(parameters);
private static UtecSerialConnection utecControl = new UtecSerialConnection(parameters);
public static boolean ISOPEN = utecControl.isOpen();
@ -51,6 +51,14 @@ public class UtecInterface{
utecControl.pullMapData(mapNumber, listener);
}
/**
* Pass a command to the UTEC
* @param charValue
*/
public static void sendDataToUtec(int charValue){
utecControl.sendDataToUtec(charValue);
}
/**
* Open a port as per names defined in get port names method
*

View File

@ -24,7 +24,8 @@ import enginuity.logger.utec.commInterface.UtecInterface;
/**
* @author botman
*/
public class JutecGUI extends JFrame implements ActionListener, GetMapFromUtecListener{
public class JutecGUI extends JFrame implements ActionListener,
GetMapFromUtecListener, KeyListener {
// Top level desktop pane
public JLayeredPane desktop = null;
@ -40,25 +41,38 @@ public class JutecGUI extends JFrame implements ActionListener, GetMapFromUtecLi
private BottomUtecControl bottomPanel = null;
private JFileChooser fileChooser = null;
private int fileChosen;
private File selectedFile = null;
private int fileChosen;
private File selectedFile = null;
// FileMenu Items
public JMenuItem saveItem = new JMenuItem("Save Log");
public JMenuItem saveMapItem = new JMenuItem("Save Map To File");
public JMenuItem exitItem = new JMenuItem("Exit");
public JMenuItem loadMapOne = new JMenuItem("Load Map #1");
public JMenuItem loadMapTwo = new JMenuItem("Load Map #2");
public JMenuItem loadMapThree = new JMenuItem("Load Map #3");
public JMenuItem loadMapFour = new JMenuItem("Load Map #4");
public JMenuItem loadMapFive = new JMenuItem("Load Map #5");
private static JutecGUI instance = null;
private UtecMapData currentMap = null;
// Text input field for sending commands directly to the UTEC
//private JTextArea textInput = new JTextArea();
private JTextField textInput = new JTextField();
private JLabel utecInputLabel = new JLabel(" Send Commands to UTEC: ");
public static JutecGUI getInstance() {
return instance;
}
@ -76,10 +90,10 @@ public class JutecGUI extends JFrame implements ActionListener, GetMapFromUtecLi
// Voice the welcome message
// *************************
VoiceThread vc = new VoiceThread("Welcome to you teck logger! Use at your own risk.");
VoiceThread vc = new VoiceThread(
"Welcome to you teck logger! Use at your own risk.");
vc.start();
// Actions to take when window is closing
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
@ -96,7 +110,6 @@ public class JutecGUI extends JFrame implements ActionListener, GetMapFromUtecLi
// -----------------------
menuBar = new JMenuBar();
// *********************************************
// Add a menu item for basic application actions
// *********************************************
@ -110,7 +123,6 @@ public class JutecGUI extends JFrame implements ActionListener, GetMapFromUtecLi
fileMenu.add(exitItem);
menuBar.add(fileMenu);
JMenu getMapsMenu = new JMenu("Load Map");
loadMapOne.addActionListener(this);
loadMapTwo.addActionListener(this);
@ -152,10 +164,15 @@ public class JutecGUI extends JFrame implements ActionListener, GetMapFromUtecLi
// Add menu item to the JFrame
this.setJMenuBar(menuBar);
// *********************************************
// Start Adding GUI Elements to the Window
// *********************************************
JPanel totalPanel = new JPanel(new BorderLayout());
JPanel commandPanel = new JPanel(new BorderLayout());
this.textInput.addKeyListener(this);
commandPanel.add(this.utecInputLabel, BorderLayout.WEST);
commandPanel.add(this.textInput, BorderLayout.CENTER);
bottomPanel = new BottomUtecControl();
@ -169,15 +186,16 @@ public class JutecGUI extends JFrame implements ActionListener, GetMapFromUtecLi
splitPane.setBottomComponent(bottomPanel);
splitPane.setPreferredSize(new Dimension(800, 600));
this.getContentPane().add(splitPane);
totalPanel.add(commandPanel, BorderLayout.NORTH);
totalPanel.add(splitPane, BorderLayout.CENTER);
this.getContentPane().add(totalPanel);
// ***********************
// Define the file chooser
// ***********************
fileChooser = new JFileChooser();
// Save singleton
instance = this;
}
@ -189,6 +207,20 @@ public class JutecGUI extends JFrame implements ActionListener, GetMapFromUtecLi
*
*/
public void actionPerformed(ActionEvent e) {
// Detect whether or not this is a command to be sent to the UTEC
if (e.getSource() == this.textInput) {
System.out.println("Text Input received:"+this.textInput.getText());
String tempString = this.textInput.getText();
tempString = tempString.substring(0, 1);
System.out.println("Reduce Input:"+tempString+":");
this.textInput.setText("");
}
// Else this is either a port event or other
else {
String cmd = e.getActionCommand();
// Open Port
@ -206,8 +238,7 @@ public class JutecGUI extends JFrame implements ActionListener, GetMapFromUtecLi
String saveFileName = null;
System.out.println("Save action occuring");
fileChosen = fileChooser.showSaveDialog(this);
if(fileChosen == JFileChooser.APPROVE_OPTION)
{
if (fileChosen == JFileChooser.APPROVE_OPTION) {
saveFileName = fileChooser.getSelectedFile().getPath();
// selectedFile = fileChooser.getSelectedFile();
try {
@ -216,9 +247,9 @@ public class JutecGUI extends JFrame implements ActionListener, GetMapFromUtecLi
out.write(bottomPanel.totalLog);
out.close();
bottomPanel.totalLog = "";
}
catch (IOException e2) {
System.out.println("Couldn't save file " + saveFileName);
} catch (IOException e2) {
System.out
.println("Couldn't save file " + saveFileName);
e2.printStackTrace();
}
}
@ -232,8 +263,7 @@ public class JutecGUI extends JFrame implements ActionListener, GetMapFromUtecLi
String saveFileName = null;
System.out.println("Save map now.");
fileChosen = fileChooser.showSaveDialog(this);
if(fileChosen == JFileChooser.APPROVE_OPTION)
{
if (fileChosen == JFileChooser.APPROVE_OPTION) {
saveFileName = fileChooser.getSelectedFile().getPath();
this.currentMap.writeMapToFile(saveFileName);
@ -285,7 +315,8 @@ public class JutecGUI extends JFrame implements ActionListener, GetMapFromUtecLi
// Only non explicitly defined actions are those generated by ports.
// Since an arbitrary machine could have any number of serial ports
//its impossible to hard code choices based on menu items generated on the fly.
// its impossible to hard code choices based on menu items generated
// on the fly.
// Must pull the calling object and interrogate
else {
JMenuItem theItem = (JMenuItem) e.getSource();
@ -298,9 +329,10 @@ public class JutecGUI extends JFrame implements ActionListener, GetMapFromUtecLi
// infoPane.setPort(currentPort);
}
}
}
public static void startLogger(final int defaultCloseOperation, final Settings settings) {
public static void startLogger(final int defaultCloseOperation,
final Settings settings) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JutecGUI application = new JutecGUI(defaultCloseOperation);
@ -311,6 +343,7 @@ public class JutecGUI extends JFrame implements ActionListener, GetMapFromUtecLi
/**
* Main method
*
* @param args
*/
public static void main(String[] args) {
@ -323,4 +356,34 @@ public class JutecGUI extends JFrame implements ActionListener, GetMapFromUtecLi
System.out.println("Got a map from the utec:" + theMap.getMapName());
this.currentMap = theMap;
}
public void keyPressed(KeyEvent arg0) {
//System.out.println("Key Pressed");
// System.out.println("Key Pressed:"+arg0.getKeyCode()+" :"+arg0.getKeyChar()+" :"+arg0.getModifiers()+" :"+(int)arg0.getKeyChar());
if((int)arg0.getKeyChar() != 65535){
int charCodeInt = (int)arg0.getKeyChar();
System.out.println("Key Code entered:"+charCodeInt);
//Pass along command to the UTEC
UtecInterface.openConnection();
UtecInterface.sendDataToUtec(charCodeInt);
}
// Empty out the text field
this.textInput.setText("");
}
public void keyReleased(KeyEvent arg0) {
//System.out.println("Key Released");
// TODO Auto-generated method stub
}
public void keyTyped(KeyEvent arg0) {
//System.out.println("Key Typed:"+arg0.getKeyCode()+" :"+arg0.getKeyChar()+" :"+arg0.getModifiers());
// TODO Auto-generated method stub
}
}