From f6a9c33a54a22ea29f03b259b92827f705d0711e Mon Sep 17 00:00:00 2001 From: Tgui Date: Tue, 13 Feb 2007 08:27:43 +0000 Subject: [PATCH] Initial utec map support. git-svn-id: http://svn.3splooges.com/romraider-arch/trunk@512 d2e2e1cd-ba16-0410-be16-b7c4453c7c2d --- ...SerialConnection.java => UtecControl.java} | 116 ++++++- .../logger/utec/commEvent/CommEvent.java | 76 ++++- .../utec/commInterface/CommInterface.java | 4 +- .../gui/bottomControl/BottomUtecControl.java | 12 +- .../utec/gui/realtimeData/RealTimeData.java | 8 +- .../logger/utec/mapData/UtecMapData.java | 302 ++++++++++++++++++ .../logger/utec/test/TestUtecMap.java | 16 + 7 files changed, 506 insertions(+), 28 deletions(-) rename src/enginuity/logger/utec/comm/{SerialConnection.java => UtecControl.java} (79%) create mode 100644 src/enginuity/logger/utec/mapData/UtecMapData.java create mode 100644 src/enginuity/logger/utec/test/TestUtecMap.java diff --git a/src/enginuity/logger/utec/comm/SerialConnection.java b/src/enginuity/logger/utec/comm/UtecControl.java similarity index 79% rename from src/enginuity/logger/utec/comm/SerialConnection.java rename to src/enginuity/logger/utec/comm/UtecControl.java index adab4f18..89ef3ca6 100755 --- a/src/enginuity/logger/utec/comm/SerialConnection.java +++ b/src/enginuity/logger/utec/comm/UtecControl.java @@ -6,6 +6,7 @@ import java.awt.event.*; import java.util.*; import javax.swing.*; import enginuity.logger.utec.commEvent.*; +import enginuity.logger.utec.mapData.UtecMapData; /** @@ -22,7 +23,7 @@ import enginuity.logger.utec.commEvent.*; import gnu.io.*; -public class SerialConnection implements SerialPortEventListener{ +public class UtecControl implements SerialPortEventListener{ // Parent object organizing connections to and from UTEC //private JPanel parent; @@ -56,6 +57,12 @@ public class SerialConnection implements SerialPortEventListener{ //Listeners private Vector portListeners = new Vector(); + + //Define whether or not we are recieving a map from the UTEC + private boolean isMapFromUtecPrep = false; + private boolean isMapFromUtec = false; + private UtecMapData currentMap = null; + /** * Public constructor * @@ -66,7 +73,7 @@ public class SerialConnection implements SerialPortEventListener{ */ //public SerialConnection(JPanel parent, SerialParameters parameters, // TextArea messageAreaOut, TextArea messageAreaIn) { - public SerialConnection(SerialParameters parameters) { + public UtecControl(SerialParameters parameters) { //this.parent = parent; //this.messageAreaOut = messageAreaOut; //this.messageAreaIn = messageAreaIn; @@ -74,10 +81,10 @@ public class SerialConnection implements SerialPortEventListener{ } /** - * Get UTEC to send data + * Get UTEC to send logger data data * */ - public void startDataFlow(){ + public void startLoggerDataFlow(){ System.out.println("Starting data flow from UTEC"); //OutPut a '1' to start basic data flow from UTEC @@ -95,16 +102,75 @@ public class SerialConnection implements SerialPortEventListener{ * */ public void resetUtec(){ - //OutPut and 'escape' to UTEC + //OutPut 2 ctrl-x to UTEC + this.sendDataToUtec('\u0018'); + this.sendDataToUtec('\u0018'); + } + + /** + * Get map data from map number passed in + * + * @param mapNumber + */ + public void pullMapData(int mapNumber){ + if(mapNumber < 1 || mapNumber > 5){ + return; + } + + // Setup map transfer prep state + this.isMapFromUtecPrep = true; + + // Reset the UTEC + this.resetUtec(); + + // Send an 'e' to enter map menu + this.sendDataToUtec('\u0065'); + + // Point UTEC menu to the appropriate map + if(mapNumber == 1){ + this.sendDataToUtec('\u0021'); + } + if(mapNumber == 2){ + this.sendDataToUtec('\u0040'); + } + if(mapNumber == 3){ + this.sendDataToUtec('\u0023'); + } + if(mapNumber == 4){ + this.sendDataToUtec('\u0024'); + } + if(mapNumber == 5){ + this.sendDataToUtec('\u0025'); + } + + // Write first ctrl-s to init save state + this.sendDataToUtec('\u0013'); + + // Make this class receptive to map transfer + this.isMapFromUtec = true; + + // No longer map prep + this.isMapFromUtecPrep = false; + + // Write second ctrl-s to start map data flow + this.sendDataToUtec('\u0013'); + } + + /** + * Helper method to write chars to the UTEC + * + * @param charValue + */ + private void sendDataToUtec(char charValue){ try{ - outputToUtecStream.write((int) '\u001B'); + outputToUtecStream.write((int) charValue); } catch(IOException e){ - System.err.println("Can't start flow of data from UTEC"); + System.err.println("Can't send char data to UTEC: "+charValue); e.getMessage(); } } - + /** * Opens a connection to the defined serial port If attempt fails, * SerialConnectionException is thrown @@ -325,15 +391,37 @@ public class SerialConnection implements SerialPortEventListener{ } //Build new event with buffer data - CommEvent commEvent = new CommEvent(new String(inputBuffer)); + CommEvent commEvent = null; + if(this.isMapFromUtec || this.isMapFromUtecPrep){ + // See if we are finally recieving a map from the UTEC + if(this.isMapFromUtec){ + + // If this is the start of map data flow, then create a new map data object + if(this.currentMap == null){ + currentMap = new UtecMapData(); + } + + // Append byte data from the UTEC + this.currentMap.addRawData(newData); + + } + //commEvent.setMapData(true); + }else{ + commEvent = new CommEvent(); + commEvent.setLoggerData(new String(inputBuffer)); + commEvent.setLoggerData(true); + } //Send received data to listeners - Iterator portIterator = portListeners.iterator(); - while(portIterator.hasNext()){ - CommListener theListener = (CommListener)portIterator.next(); - theListener.getCommEvent(commEvent); + if(commEvent != null){ + Iterator portIterator = portListeners.iterator(); + while(portIterator.hasNext()){ + CommListener theListener = (CommListener)portIterator.next(); + theListener.getCommEvent(commEvent); + } + break; } - break; + // If break event append BREAK RECEIVED message. /* diff --git a/src/enginuity/logger/utec/commEvent/CommEvent.java b/src/enginuity/logger/utec/commEvent/CommEvent.java index cb9ae426..6d7a3041 100755 --- a/src/enginuity/logger/utec/commEvent/CommEvent.java +++ b/src/enginuity/logger/utec/commEvent/CommEvent.java @@ -9,6 +9,9 @@ package enginuity.logger.utec.commEvent; import java.util.*; +import enginuity.logger.utec.mapData.UtecMapData; + + /** * @author emorgan * @@ -16,11 +19,16 @@ import java.util.*; * Window - Preferences - Java - Code Generation - Code and Comments */ public class CommEvent { - public String UtecBuffer = null; - public String[] data = new String[6]; - public double[] doubleData = new double[6]; + private String UtecBuffer = null; + private String[] data = new String[6]; + private double[] doubleData = new double[6]; - public CommEvent(String buffer){ + private boolean isLoggerData = false; + private boolean isMapData = false; + + private UtecMapData mapData = null; + + public void setLoggerData(String buffer){ UtecBuffer = buffer; StringTokenizer st = new StringTokenizer(UtecBuffer, ","); int counter = 0; @@ -73,4 +81,64 @@ public class CommEvent { } } + + + public boolean isLoggerData() { + return isLoggerData; + } + + + public void setLoggerData(boolean isLoggerData) { + this.isLoggerData = isLoggerData; + } + + + public boolean isMapData() { + return isMapData; + } + + + public void setMapData(boolean isMapData) { + this.isMapData = isMapData; + } + + + public UtecMapData getMapData() { + return mapData; + } + + + public void setMapData(UtecMapData mapData) { + this.mapData = mapData; + } + + + public String[] getData() { + return data; + } + + + public void setData(String[] data) { + this.data = data; + } + + + public double[] getDoubleData() { + return doubleData; + } + + + public void setDoubleData(double[] doubleData) { + this.doubleData = doubleData; + } + + + public String getUtecBuffer() { + return UtecBuffer; + } + + + public void setUtecBuffer(String utecBuffer) { + UtecBuffer = utecBuffer; + } } diff --git a/src/enginuity/logger/utec/commInterface/CommInterface.java b/src/enginuity/logger/utec/commInterface/CommInterface.java index df1a9e55..727df3f8 100755 --- a/src/enginuity/logger/utec/commInterface/CommInterface.java +++ b/src/enginuity/logger/utec/commInterface/CommInterface.java @@ -34,7 +34,7 @@ public class CommInterface{ private static SerialParameters parameters = new SerialParameters(); //Actual connection entity - private static SerialConnection connection = new SerialConnection(parameters); + private static UtecControl connection = new UtecControl(parameters); public static boolean ISOPEN = connection.isOpen(); @@ -101,7 +101,7 @@ public class CommInterface{ * */ public static void startDataLogFromUtec(){ - connection.startDataFlow(); + connection.startLoggerDataFlow(); } diff --git a/src/enginuity/logger/utec/gui/bottomControl/BottomUtecControl.java b/src/enginuity/logger/utec/gui/bottomControl/BottomUtecControl.java index 4bb87cdf..be20ed63 100755 --- a/src/enginuity/logger/utec/gui/bottomControl/BottomUtecControl.java +++ b/src/enginuity/logger/utec/gui/bottomControl/BottomUtecControl.java @@ -185,11 +185,13 @@ public class BottomUtecControl extends JPanel implements ActionListener, CommLis } public void getCommEvent(CommEvent e){ - String utecData = e.UtecBuffer; - totalLog+=utecData; - textFromUtec.append(utecData); - textFromUtec.setCaretPosition(textFromUtec.getDocument().getLength()); - + if(e.isLoggerData()){ + String utecData = e.getUtecBuffer(); + totalLog+=utecData; + textFromUtec.append(utecData); + textFromUtec.setCaretPosition(textFromUtec.getDocument().getLength()); + } + //System.out.println("Adding data to the text AREA"); } } diff --git a/src/enginuity/logger/utec/gui/realtimeData/RealTimeData.java b/src/enginuity/logger/utec/gui/realtimeData/RealTimeData.java index 843351f3..c220b84b 100755 --- a/src/enginuity/logger/utec/gui/realtimeData/RealTimeData.java +++ b/src/enginuity/logger/utec/gui/realtimeData/RealTimeData.java @@ -161,8 +161,10 @@ public class RealTimeData extends JComponent implements CommListener{ } public void getCommEvent(CommEvent e){ - stringData = e.data; - doubleData = e.doubleData; - this.repaint(); + if(e.isLoggerData()){ + stringData = e.getData(); + doubleData = e.getDoubleData(); + this.repaint(); + } } } diff --git a/src/enginuity/logger/utec/mapData/UtecMapData.java b/src/enginuity/logger/utec/mapData/UtecMapData.java new file mode 100644 index 00000000..9947a2f9 --- /dev/null +++ b/src/enginuity/logger/utec/mapData/UtecMapData.java @@ -0,0 +1,302 @@ +package enginuity.logger.utec.mapData; + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; + +public class UtecMapData { + private StringBuffer rawMapData = new StringBuffer(); + + private double[][] fuelMap = new double[11][40]; + private double[][] timingMap = new double[11][40]; + private double[][] boostMap = new double[11][40]; + + private String mapName = ""; + private String mapComment = ""; + + private double[] tempStorage = new double[440]; + + public void addRawData(int byteData) { + rawMapData.append(byteData); + } + + public void populateMapData() { + System.out.println("---------------"); + //System.out.println(rawMapData); + + // Functionality as the method names suggest + cleanUpMapData(); + populateMapName(); + populateMapComment(); + populateFuelMapData(); + populateTimingMapData(); + populateBoostMapData(); + calculateChecksum(); + } + + public void calculateChecksum(){ + + int mapChecksumValue = 0; + char[] charArray = this.mapName.toCharArray(); + for(int i=0 ; i