Add handling for the O2 calibrations (single bytes)

This commit is contained in:
Josh Stewart 2015-01-10 12:21:14 +11:00
parent 7b2b0212c2
commit 67a1c1399d
1 changed files with 21 additions and 12 deletions

View File

@ -62,7 +62,6 @@ void command()
receiveCalibration(tableID); //Receive new values and store in memory receiveCalibration(tableID); //Receive new values and store in memory
writeCalibration(); //Store received values in EEPROM writeCalibration(); //Store received values in EEPROM
analogWrite(13, 0 );
break; break;
@ -300,7 +299,7 @@ This function is used to store calibration data sent by Tuner Studio.
void receiveCalibration(byte tableID) void receiveCalibration(byte tableID)
{ {
byte* pnt_TargetTable; //Pointer that will be used to point to the required target table byte* pnt_TargetTable; //Pointer that will be used to point to the required target table
int OFFSET, DIVISION_FACTOR; int OFFSET, DIVISION_FACTOR, BYTES_PER_VALUE;
switch (tableID) switch (tableID)
{ {
@ -309,18 +308,22 @@ void receiveCalibration(byte tableID)
pnt_TargetTable = (byte *)&cltCalibrationTable; pnt_TargetTable = (byte *)&cltCalibrationTable;
OFFSET = CALIBRATION_TEMPERATURE_OFFSET; // OFFSET = CALIBRATION_TEMPERATURE_OFFSET; //
DIVISION_FACTOR = 10; DIVISION_FACTOR = 10;
BYTES_PER_VALUE = 2;
break; break;
case 1: case 1:
//Inlet air temp table //Inlet air temp table
pnt_TargetTable = (byte *)&iatCalibrationTable; pnt_TargetTable = (byte *)&iatCalibrationTable;
OFFSET = CALIBRATION_TEMPERATURE_OFFSET; OFFSET = CALIBRATION_TEMPERATURE_OFFSET;
DIVISION_FACTOR = 10; DIVISION_FACTOR = 10;
BYTES_PER_VALUE = 2;
break; break;
case 2: case 2:
//O2 table //O2 table
pnt_TargetTable = (byte *)&o2CalibrationTable; pnt_TargetTable = (byte *)&o2CalibrationTable;
OFFSET = 0; OFFSET = 0;
DIVISION_FACTOR = 1; DIVISION_FACTOR = 1;
o2CalibrationTable[10] = 123;
BYTES_PER_VALUE = 1;
break; break;
default: default:
@ -341,29 +344,35 @@ void receiveCalibration(byte tableID)
digitalWrite(13, LOW); digitalWrite(13, LOW);
for (x = 0; x < 1024; x++) for (x = 0; x < 1024; x++)
{ {
while ( Serial.available() < 2 ) {} //UNlike what is listed in the protocol documentation, the O2 sensor values are sent as bytes rather than ints
tempBuffer[0] = Serial.read(); if(BYTES_PER_VALUE == 1)
tempBuffer[1] = Serial.read(); {
while ( Serial.available() < 1 ) {}
tempValue = div(int(word(tempBuffer[0], tempBuffer[1])), DIVISION_FACTOR).quot; //Read 2 bytes, convert to word (an unsigned int), convert to signed int. These values come through * 10 from Tuner Studio tempValue = Serial.read();
//tempValue = div((tempBuffer[0] << 8 | tempBuffer[1]), 10).quot; }
else
{
while ( Serial.available() < 2 ) {}
tempBuffer[0] = Serial.read();
tempBuffer[1] = Serial.read();
tempValue = div(int(word(tempBuffer[0], tempBuffer[1])), DIVISION_FACTOR).quot; //Read 2 bytes, convert to word (an unsigned int), convert to signed int. These values come through * 10 from Tuner Studio
}
tempValue = tempValue + OFFSET; tempValue = tempValue + OFFSET;
//tempValue = word(tempBuffer[0], tempBuffer[1]);
if (every2nd) //Only use every 2nd value if (every2nd) //Only use every 2nd value
{ {
if (tempValue > 255) { tempValue = 255; } // Cap the maximum value to prevent overflow when converting to byte if (tempValue > 255) { tempValue = 255; } // Cap the maximum value to prevent overflow when converting to byte
if (tempValue < 0) { tempValue = 0; } if (tempValue < 0) { tempValue = 0; }
pnt_TargetTable[(x / 2)] = (byte)tempValue; pnt_TargetTable[(x / 2)] = (byte)tempValue;
//pnt_TargetTable[counter] = (byte)(counter);
int y = EEPROM_CALIBRATION_O2 + counter; int y = EEPROM_CALIBRATION_O2 + counter;
//EEPROM.write(y, (byte)counter);
every2nd = false; every2nd = false;
analogWrite(13, (counter % 50) ); analogWrite(13, (counter % 50) );
counter++; counter++;
} }
else { every2nd = true; }//digitalWrite(13, HIGH); } else { every2nd = true; }
} }