diff --git a/src/enginuity/logger/utec/comm/UtecControl.java b/src/enginuity/logger/utec/comm/UtecSerialConnection.java similarity index 96% rename from src/enginuity/logger/utec/comm/UtecControl.java rename to src/enginuity/logger/utec/comm/UtecSerialConnection.java index a2df10ce..562e954b 100755 --- a/src/enginuity/logger/utec/comm/UtecControl.java +++ b/src/enginuity/logger/utec/comm/UtecSerialConnection.java @@ -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(); diff --git a/src/enginuity/logger/utec/commInterface/UtecInterface.java b/src/enginuity/logger/utec/commInterface/UtecInterface.java index 56921115..5191cab2 100755 --- a/src/enginuity/logger/utec/commInterface/UtecInterface.java +++ b/src/enginuity/logger/utec/commInterface/UtecInterface.java @@ -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 * diff --git a/src/enginuity/logger/utec/gui/JutecGUI.java b/src/enginuity/logger/utec/gui/JutecGUI.java index 7fad5052..fbafba5c 100755 --- a/src/enginuity/logger/utec/gui/JutecGUI.java +++ b/src/enginuity/logger/utec/gui/JutecGUI.java @@ -24,47 +24,61 @@ import enginuity.logger.utec.commInterface.UtecInterface; /** * @author botman */ -public class JutecGUI extends JFrame implements ActionListener, GetMapFromUtecListener{ - //Top level desktop pane +public class JutecGUI extends JFrame implements ActionListener, + GetMapFromUtecListener, KeyListener { + // Top level desktop pane public JLayeredPane desktop = null; - - //Top menu bar + + // Top menu bar private JMenuBar menuBar; - - //A known existing port + + // A known existing port private String defaultPort = null; - - //Currently selected port + + // Currently selected port private String currentPort = null; - + private BottomUtecControl bottomPanel = null; - + private JFileChooser fileChooser = null; + private int fileChosen; + private File selectedFile = null; - - - //FileMenu Items + + // 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(){ + public static JutecGUI getInstance() { return instance; } - + private JutecGUI(int setDefaultCloseOperation) { - + // Main frame // Grid layout with a top and bottom, ie two rows super("UTEC Loggers"); @@ -72,34 +86,33 @@ public class JutecGUI extends JFrame implements ActionListener, GetMapFromUtecLi this.setResizable(false); this.setDefaultCloseOperation(setDefaultCloseOperation); - //************************* - //Voice the welcome message - //************************* - - VoiceThread vc = new VoiceThread("Welcome to you teck logger! Use at your own risk."); + // ************************* + // Voice the welcome message + // ************************* + + VoiceThread vc = new VoiceThread( + "Welcome to you teck logger! Use at your own risk."); vc.start(); - - - //Actions to take when window is closing + + // Actions to take when window is closing addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.out.println("JUTEC Exiting"); - - //Use interface to close the connecetion to the Utec + + // Use interface to close the connecetion to the Utec UtecInterface.closeConnection(); - + } }); - //----------------------- - //Start Menu bar addtions - //----------------------- + // ----------------------- + // Start Menu bar addtions + // ----------------------- menuBar = new JMenuBar(); - - - //********************************************* - //Add a menu item for basic application actions - //********************************************* + + // ********************************************* + // Add a menu item for basic application actions + // ********************************************* // Define the menu system JMenu fileMenu = new JMenu("File"); saveItem.addActionListener(this); @@ -109,8 +122,7 @@ public class JutecGUI extends JFrame implements ActionListener, GetMapFromUtecLi fileMenu.add(saveMapItem); fileMenu.add(exitItem); menuBar.add(fileMenu); - - + JMenu getMapsMenu = new JMenu("Load Map"); loadMapOne.addActionListener(this); loadMapTwo.addActionListener(this); @@ -123,62 +135,68 @@ public class JutecGUI extends JFrame implements ActionListener, GetMapFromUtecLi getMapsMenu.add(loadMapFour); getMapsMenu.add(loadMapFive); menuBar.add(getMapsMenu); - - //---------------------------------- - //Add a menu item for comm port selection - //---------------------------------- - JMenu portsMenu =new JMenu("Ports"); - - //Gather list of ports from interface - Vector portsVector =UtecInterface.getPortsVector(); - + + // ---------------------------------- + // Add a menu item for comm port selection + // ---------------------------------- + JMenu portsMenu = new JMenu("Ports"); + + // Gather list of ports from interface + Vector portsVector = UtecInterface.getPortsVector(); + Iterator portsIterator = portsVector.iterator(); int counter = 0; - while(portsIterator.hasNext()){ + while (portsIterator.hasNext()) { counter++; Object o = portsIterator.next(); - String theName = (String)o; + String theName = (String) o; JMenuItem item = new JMenuItem(theName); item.setName(theName); item.addActionListener(this); portsMenu.add(item); - if(counter == 1){ + if (counter == 1) { defaultPort = theName; UtecInterface.setPortChoice(defaultPort); } } menuBar.add(portsMenu); - + // Add menu item to the JFrame this.setJMenuBar(menuBar); - - - //********************************************* - //Start Adding GUI Elements to the Window - //********************************************* - + + // ********************************************* + // 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(); - + JTabbedPane topTabbedPane = new JTabbedPane(); - topTabbedPane.add("Graph Data",new RealTimeData()); - + topTabbedPane.add("Graph Data", new RealTimeData()); + JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT); splitPane.setDividerLocation(435); - + splitPane.setTopComponent(topTabbedPane); splitPane.setBottomComponent(bottomPanel); - splitPane.setPreferredSize(new Dimension(800,600)); - - this.getContentPane().add(splitPane); - - - //*********************** - //Define the file chooser - //*********************** + splitPane.setPreferredSize(new Dimension(800, 600)); + + totalPanel.add(commandPanel, BorderLayout.NORTH); + totalPanel.add(splitPane, BorderLayout.CENTER); + + this.getContentPane().add(totalPanel); + + // *********************** + // Define the file chooser + // *********************** fileChooser = new JFileChooser(); - - - //Save singleton + + // Save singleton instance = this; } @@ -189,128 +207,143 @@ public class JutecGUI extends JFrame implements ActionListener, GetMapFromUtecLi * */ public void actionPerformed(ActionEvent e) { - String cmd = e.getActionCommand(); - - //Open Port - if (cmd.equals("New")) { - System.out.println("New action occuring"); - } - - //Close Port - else if (cmd.equals("Open")) { - System.out.println("Open action occuring"); - } - - //Start Capture - else if (cmd.equals("Save Log")) { - String saveFileName = null; - System.out.println("Save action occuring"); - fileChosen = fileChooser.showSaveDialog(this); - if(fileChosen == JFileChooser.APPROVE_OPTION) - { - saveFileName = fileChooser.getSelectedFile().getPath(); - //selectedFile = fileChooser.getSelectedFile(); - try { - File file = new File(saveFileName); - FileWriter out = new FileWriter(file); - out.write(bottomPanel.totalLog); - out.close(); - bottomPanel.totalLog = ""; - } - catch (IOException e2) { - System.out.println("Couldn't save file " + saveFileName); - e2.printStackTrace(); - } - } + // 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 if (cmd.equals("Save Map To File")) { - System.out.println("Saving map to file."); - if(this.currentMap != null){ + // Else this is either a port event or other + else { + String cmd = e.getActionCommand(); + // Open Port + if (cmd.equals("New")) { + System.out.println("New action occuring"); + } + + // Close Port + else if (cmd.equals("Open")) { + System.out.println("Open action occuring"); + } + + // Start Capture + else if (cmd.equals("Save Log")) { String saveFileName = null; - System.out.println("Save map now."); + System.out.println("Save action occuring"); fileChosen = fileChooser.showSaveDialog(this); - if(fileChosen == JFileChooser.APPROVE_OPTION) - { - saveFileName = fileChooser.getSelectedFile().getPath(); - this.currentMap.writeMapToFile(saveFileName); - - } - }else{ - System.out.println("Map is null."); + if (fileChosen == JFileChooser.APPROVE_OPTION) { + saveFileName = fileChooser.getSelectedFile().getPath(); + // selectedFile = fileChooser.getSelectedFile(); + try { + File file = new File(saveFileName); + FileWriter out = new FileWriter(file); + out.write(bottomPanel.totalLog); + out.close(); + bottomPanel.totalLog = ""; + } catch (IOException e2) { + System.out + .println("Couldn't save file " + saveFileName); + e2.printStackTrace(); + } + } + + } + + else if (cmd.equals("Save Map To File")) { + System.out.println("Saving map to file."); + if (this.currentMap != null) { + + String saveFileName = null; + System.out.println("Save map now."); + fileChosen = fileChooser.showSaveDialog(this); + if (fileChosen == JFileChooser.APPROVE_OPTION) { + saveFileName = fileChooser.getSelectedFile().getPath(); + this.currentMap.writeMapToFile(saveFileName); + + } + } else { + System.out.println("Map is null."); + } + } + + else if (cmd.equals("Load Map #1")) { + System.out.println("Starting to get map 1"); + UtecInterface.openConnection(); + UtecInterface.getMap(1, this); + } + + else if (cmd.equals("Load Map #2")) { + System.out.println("Starting to get map 2"); + UtecInterface.openConnection(); + UtecInterface.getMap(2, this); + } + + else if (cmd.equals("Load Map #3")) { + System.out.println("Starting to get map 3"); + UtecInterface.openConnection(); + UtecInterface.getMap(3, this); + } + + else if (cmd.equals("Load Map #4")) { + System.out.println("Starting to get map 4"); + UtecInterface.openConnection(); + UtecInterface.getMap(4, this); + } + + else if (cmd.equals("Load Map #5")) { + System.out.println("Starting to get map 5"); + UtecInterface.openConnection(); + UtecInterface.getMap(5, this); + } + + // Stop Capture + else if (cmd.equals("Exit")) { + // Use interface to finally close the connection to the Utec + UtecInterface.closeConnection(); + System.out.println("Exit action occuring"); + + // Close out the application + System.exit(0); + } + + // 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. + // Must pull the calling object and interrogate + else { + JMenuItem theItem = (JMenuItem) e.getSource(); + String portChoice = theItem.getName(); + System.out.println("Port chosen: " + portChoice); + currentPort = portChoice; + UtecInterface.setPortChoice(currentPort); + bottomPanel.setEnabled(true); + // Notify the infoPane of the current port choice + // infoPane.setPort(currentPort); } } - - else if (cmd.equals("Load Map #1")) { - System.out.println("Starting to get map 1"); - UtecInterface.openConnection(); - UtecInterface.getMap(1, this); - } - - else if (cmd.equals("Load Map #2")) { - System.out.println("Starting to get map 2"); - UtecInterface.openConnection(); - UtecInterface.getMap(2, this); - } - - else if (cmd.equals("Load Map #3")) { - System.out.println("Starting to get map 3"); - UtecInterface.openConnection(); - UtecInterface.getMap(3, this); - } - - else if (cmd.equals("Load Map #4")) { - System.out.println("Starting to get map 4"); - UtecInterface.openConnection(); - UtecInterface.getMap(4, this); - } - - else if (cmd.equals("Load Map #5")) { - System.out.println("Starting to get map 5"); - UtecInterface.openConnection(); - UtecInterface.getMap(5, this); - } - - //Stop Capture - else if (cmd.equals("Exit")) { - //Use interface to finally close the connection to the Utec - UtecInterface.closeConnection(); - System.out.println("Exit action occuring"); - - //Close out the application - System.exit(0); - } - - //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. - //Must pull the calling object and interrogate - else{ - JMenuItem theItem = (JMenuItem)e.getSource(); - String portChoice = theItem.getName(); - System.out.println("Port chosen: "+portChoice); - currentPort = portChoice; - UtecInterface.setPortChoice(currentPort); - bottomPanel.setEnabled(true); - //Notify the infoPane of the current port choice - //infoPane.setPort(currentPort); - } } - - - public static void startLogger(final int defaultCloseOperation, final Settings settings) { - SwingUtilities.invokeLater(new Runnable() { - public void run() { - JutecGUI application = new JutecGUI(defaultCloseOperation); - application.setVisible(true); - } - }); + + public static void startLogger(final int defaultCloseOperation, + final Settings settings) { + SwingUtilities.invokeLater(new Runnable() { + public void run() { + JutecGUI application = new JutecGUI(defaultCloseOperation); + application.setVisible(true); + } + }); } /** * Main method + * * @param args */ public static void main(String[] args) { @@ -320,7 +353,37 @@ public class JutecGUI extends JFrame implements ActionListener, GetMapFromUtecLi } public void mapRetrieved(UtecMapData theMap) { - System.out.println("Got a map from the utec:"+theMap.getMapName()); - this.currentMap = theMap; + 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 + } }