Initial utec map support.

git-svn-id: http://svn.3splooges.com/romraider-arch/trunk@512 d2e2e1cd-ba16-0410-be16-b7c4453c7c2d
This commit is contained in:
Tgui 2007-02-13 08:27:43 +00:00
parent 55438103ce
commit f6a9c33a54
7 changed files with 506 additions and 28 deletions

View File

@ -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.
/*

View File

@ -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;
}
}

View File

@ -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();
}

View File

@ -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");
}
}

View File

@ -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();
}
}
}

View File

@ -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<charArray.length ; i++){
int charValue = charArray[i];
mapChecksumValue += charValue;
}
System.out.println("Map name Checksum:"+mapChecksumValue);
int mapCommentChecksumValue = 0;
charArray = this.mapComment.toCharArray();
for(int i=0 ; i<charArray.length ; i++){
int charValue = charArray[i];
mapCommentChecksumValue += charValue;
}
System.out.println("Map comment Checksum:"+mapCommentChecksumValue);
int fuelChecksum = 0;
for(int i=0 ; i<40 ; i++){
for(int j=0 ; j<11 ; j++){
fuelChecksum += (this.fuelMap[j][i]*10 + 1000 + 1);
}
}
System.out.println("Fuel Checksum:"+fuelChecksum);
int timingChecksum = 0;
for(int i=0 ; i<40 ; i++){
for(int j=0 ; j<11 ; j++){
timingChecksum += (this.timingMap[j][i]*10 + 1000 + 1);
}
}
System.out.println("Timing Checksum:"+timingChecksum);
int boostChecksum = 0;
for(int i=0 ; i<40 ; i++){
for(int j=0 ; j<11 ; j++){
boostChecksum += (this.boostMap[j][i]*100 + 1);
}
}
System.out.println("Boost Checksum:"+boostChecksum);
int totalChecksum = mapChecksumValue+mapCommentChecksumValue+fuelChecksum+timingChecksum+boostChecksum;
System.out.println("Total decimal checksum:"+totalChecksum);
String hexString = Integer.toHexString(totalChecksum);
System.out.println("Total hex checksum:"+hexString);
hexString = hexString.substring(1);
hexString = 0 + hexString;
totalChecksum = Integer.parseInt(hexString, 16);
System.out.println("Total hex checksum:"+Integer.toHexString(totalChecksum));
System.out.println("Final expected value:"+Integer.parseInt("0B05E", 16));
}
public void populateBoostMapData(){
int fuelStart = rawMapData.indexOf("Boost Map\r")+9;
int fuelEnd = rawMapData.indexOf("\r[END]");
String singleRow = rawMapData.substring(fuelStart, fuelEnd);
String numericalValue = "";
String[] split = singleRow.split(" *");
int counter = 0;
boolean makeDouble = false;
for(int i=0; i<split.length; i++){
String tempString = split[i];
if(tempString.equals("[")){
makeDouble = true;
numericalValue = "";
}
else if(tempString.equals("]")){
makeDouble = false;
double parsedDouble = Double.parseDouble(numericalValue);
this.tempStorage[counter] = parsedDouble;
counter++;
}
else{
if(makeDouble == true){
numericalValue+=tempString;
}
}
}
// Move temp stored data into appropriate double array
counter = 0;
for(int i=0 ; i<40 ; i++){
for(int j=0 ; j<11 ; j++){
this.boostMap[j][i] = this.tempStorage[counter];
counter++;
}
}
// Test print of data
for(int i=0 ; i<40 ; i++){
for(int j=0 ; j<11 ; j++){
//System.out.println("Boost Output: "+ this.boostMap[j][i]);
}
}
}
public void populateTimingMapData(){
int fuelStart = rawMapData.indexOf("Timing Map\r")+9;
int fuelEnd = rawMapData.indexOf("\r\rBoost Map");
String singleRow = rawMapData.substring(fuelStart, fuelEnd);
String numericalValue = "";
String[] split = singleRow.split(" *");
int counter = 0;
boolean makeDouble = false;
for(int i=0; i<split.length; i++){
String tempString = split[i];
if(tempString.equals("[")){
makeDouble = true;
numericalValue = "";
}
else if(tempString.equals("]")){
makeDouble = false;
double parsedDouble = Double.parseDouble(numericalValue);
this.tempStorage[counter] = parsedDouble;
counter++;
}
else{
if(makeDouble == true){
numericalValue+=tempString;
}
}
}
// Move temp stored data into appropriate double array
counter = 0;
for(int i=0 ; i<40 ; i++){
for(int j=0 ; j<11 ; j++){
this.timingMap[j][i] = this.tempStorage[counter];
counter++;
}
}
// Test print of data
for(int i=0 ; i<40 ; i++){
for(int j=0 ; j<11 ; j++){
//System.out.println("Timing Output: "+ this.timingMap[j][i]);
}
}
}
public void populateFuelMapData(){
int fuelStart = rawMapData.indexOf("Fuel Map\r")+9;
int fuelEnd = rawMapData.indexOf("\r\rTiming Map");
String singleRow = rawMapData.substring(fuelStart, fuelEnd);
String numericalValue = "";
String[] split = singleRow.split(" *");
int counter = 0;
boolean makeDouble = false;
for(int i=0; i<split.length; i++){
String tempString = split[i];
if(tempString.equals("[")){
makeDouble = true;
numericalValue = "";
}
else if(tempString.equals("]")){
makeDouble = false;
double parsedDouble = Double.parseDouble(numericalValue);
this.tempStorage[counter] = parsedDouble;
counter++;
}
else{
if(makeDouble == true){
numericalValue+=tempString;
}
}
}
// Move temp stored data into appropriate double array
counter = 0;
for(int i=0 ; i<40 ; i++){
for(int j=0 ; j<11 ; j++){
this.fuelMap[j][i] = this.tempStorage[counter];
counter++;
}
}
// Test print of data
for(int i=0 ; i<40 ; i++){
for(int j=0 ; j<11 ; j++){
//System.out.println("Fuel Output: "+ this.fuelMap[j][i]);
}
}
}
public void populateMapComment(){
int start = rawMapData.indexOf("Map Comments:-[")+15;
int stop = rawMapData.indexOf("]\rFuel Map");
this.mapComment = rawMapData.substring(start, stop);
//System.out.println("Map comment:"+mapComment+":");
}
public void populateMapName(){
int start = rawMapData.indexOf("Map Name:-[")+11;
int stop = rawMapData.indexOf("]\rMap Comments");
//System.out.println("Start:"+start);
//System.out.println("Stop:"+stop);
this.mapName = rawMapData.substring(start, stop);
//System.out.println("Map name:"+mapName+":");
}
public void cleanUpMapData() {
int start = rawMapData.indexOf("[START]");
int stop = rawMapData.indexOf("[EOF]") + 5;
int size = rawMapData.length();
//System.out.println("Start:" + start);
//System.out.println("Stop:" + stop);
//System.out.println("Length:" + size);
if (stop != size) {
rawMapData.delete(0, start);
rawMapData.delete(stop, rawMapData.length());
//System.out.println("***************" + rawMapData);
} else {
//System.out.println("Nothing to update.");
//System.out.println(rawMapData);
}
}
// Test method
public void testInputFile(String fileLocation) {
rawMapData = new StringBuffer();
BufferedReader br = null;
try {
FileReader fr = new FileReader(fileLocation);
br = new BufferedReader(fr);
String record = new String();
try {
while ((record = br.readLine()) != null) {
this.rawMapData.append(record+"\r");
// System.out.println(record);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
populateMapData();
}
}

View File

@ -0,0 +1,16 @@
package enginuity.logger.utec.test;
import enginuity.logger.utec.mapData.UtecMapData;
public class TestUtecMap {
public static void main(String[] args){
UtecMapData umd = new UtecMapData();
//umd.testInputFile("/Users/emorgan/test1.txt");
//umd.testInputFile("/Users/emorgan/test2.txt");
//umd.testInputFile("/Users/emorgan/test3.txt");
//umd.testInputFile("/Users/emorgan/test4.txt");
//umd.testInputFile("/Users/emorgan/testout.txt");
umd.testInputFile("/Users/emorgan/stage2utec.TXT");
}
}