From e878be52603188b628366ab476297c755307c0dc Mon Sep 17 00:00:00 2001 From: Josh Stewart Date: Mon, 24 Jul 2017 14:19:29 +1000 Subject: [PATCH] MISRA work in tables.ino --- speeduino/table.ino | 166 +++++++++++++++++++++++++++----------------- 1 file changed, 101 insertions(+), 65 deletions(-) diff --git a/speeduino/table.ino b/speeduino/table.ino index 6c9efe05..01c5d080 100644 --- a/speeduino/table.ino +++ b/speeduino/table.ino @@ -376,25 +376,29 @@ int get3DTableValue(struct table3D *fromTable, int Y_in, int X_in) //Initial check incase the values were hit straight on - /* + long p = (long)X - xMinValue; if (xMaxValue == xMinValue) { p = (p << 8); } //This only occurs if the requested X value was equal to one of the X axis bins else { p = ( (p << 8) / (xMaxValue - xMinValue) ); } //This is the standard case - */ - long p; - if (xMaxValue == xMinValue) { p = ((long)(X - xMinValue) << 8); } //This only occurs if the requested X value was equal to one of the X axis bins - else { p = ((long)(X - xMinValue) << 8) / (xMaxValue - xMinValue); } //This is the standard case - - /* - long q = (long)Y - yMinValue; - if (yMaxValue == yMinValue) { q = (q << 8); } - else { q = 256 - ( (q << 8) / (yMinValue - yMaxValue) ); } - */ + long q; + if (yMaxValue == yMinValue) + { + q = (long)Y - yMinValue; + q = (q << 8); + } + //Standard case + else + { + q = long(Y) - yMaxValue; + q = 256 - ( (q << 8) / (yMinValue - yMaxValue) ); + } +/* long q; if (yMaxValue == yMinValue) { q = ((long)(Y - yMinValue) << 8); } else { q = 256 - (((long)(Y - yMaxValue) << 8) / (yMinValue - yMaxValue)); } + */ int m = ((256-p) * (256-q)) >> 8; int n = (p * (256-q)) >> 8; @@ -480,76 +484,108 @@ float get3DTableValueF(struct table3D *fromTable, int Y, int X) return ( (A * m) + (B * n) + (C * o) + (D * r) ); } - -//This function pulls a value from a 3D table given a target for X and Y coordinates. -//It performs a 2D linear interpolation as descibred in: http://www.megamanual.com/v22manual/ve_tuner.pdf +*/ int get3DTableValueS(struct table3D *fromTable, int Y, int X) { byte xMin, xMax; byte yMin, yMax; - long p, q; - int yMaxValue, yMinValue; - int xMaxValue, xMinValue; + int tableResult = 0; - if(fromTable->lastXMin==0) {fromTable->lastXMin=fromTable->xSize-1;} - else {xMin = fromTable->lastXMin;} - if(fromTable->lastYMin==0) {fromTable->lastYMin=fromTable->ySize-1;} - else {yMin = fromTable->lastYMin;} - - if(xMin>fromTable->xSize-1) - { - fromTable->lastXMin = fromTable->xSize-1; - xMin = fromTable->xSize-1; - } - if(yMin>fromTable->ySize-1) - { - fromTable->lastYMin = fromTable->ySize-1; - yMin = fromTable->ySize-1; - } + xMin = fromTable->lastXMin; + yMin = fromTable->lastYMin; + if(xMin == 0) { xMin = fromTable->xSize-1; } + if(yMin == 0) { yMin = fromTable->ySize-1; } do //RPM axis { - if(X>=fromTable->axisX[xMin]) {break;} + if(X > fromTable->axisX[xMin]) + { + if (xMin == (fromTable->xSize-1) ) { xMax = xMin; } //Overflow protection + else { xMax = xMin + 1; } + break; + } + else if(X == fromTable->axisX[xMin]) + { + xMax = xMin; + break; + } xMin--; - }while(1); - fromTable->lastXMin = xMin + 1; + }while(TRUE); + do //MAP axis { - if(Y<=fromTable->axisY[yMin]) {break;} + if(Y < fromTable->axisY[yMin]) + { + if (yMin == (fromTable->ySize-1) ) { yMax = yMin; } //Overflow protection + else { yMax = yMin + 1; } + break; + } + else if(Y == fromTable->axisY[yMin]) + { + yMax = yMin; + break; + } yMin--; - }while(1); - fromTable->lastYMin = yMin + 1; + }while(TRUE); - xMax = xMin + 1; - yMax = yMin + 1; - if (xMax>fromTable->xSize-1) //Overflow protection - { - xMax = fromTable->xSize-1; - xMin = xMax - 1; - } - if (yMax>fromTable->ySize-1) //Overflow protection - { - yMax = fromTable->ySize-1; - yMin = yMax - 1; - } + fromTable->lastXMin = xMax; + fromTable->lastYMin = yMax; - yMaxValue = fromTable->axisY[yMax]; - yMinValue = fromTable->axisY[yMin]; - xMaxValue = fromTable->axisX[xMax]; - xMinValue = fromTable->axisX[xMin]; + //Serial.print("xMin, xMax ");Serial.print(xMin);Serial.print(",");Serial.println(xMax); + //Serial.print("yMin, yMax ");Serial.print(yMin);Serial.print(",");Serial.println(yMax); int A = fromTable->values[yMin][xMin]; - int B = fromTable->values[yMin][xMax]; - int C = fromTable->values[yMax][xMin]; - int D = fromTable->values[yMax][xMax]; + if((xMin == xMax) && (yMin == yMax)) { tableResult = A; } + else if(xMin == xMax) // Simple 2D + { + int D = fromTable->values[yMax][xMax]; + int yMaxValue = fromTable->axisY[yMax]; + int yMinValue = fromTable->axisY[yMin]; + long q = (long)Y - yMinValue; + q = ((q << 6) * (D - A)) / (yMaxValue - yMinValue); + tableResult = A + (q >> 6); + } + else if(yMin == yMax) // Simple 2D + { + int D = fromTable->values[yMax][xMax]; + int xMaxValue = fromTable->axisX[xMax]; + int xMinValue = fromTable->axisX[xMin]; + long q = (long)X - xMinValue; + q = ((q << 6) * (D - A)) / (xMaxValue - xMinValue); + tableResult = A + (q >> 6); + } + else + { + int B = fromTable->values[yMin][xMax]; + int C = fromTable->values[yMax][xMin]; + int D = fromTable->values[yMax][xMax]; - p = ((long)(X - xMinValue) << 8) / (xMaxValue - xMinValue); //(RPM - RPM[1])/(RPM[2]- RPM[1]) - q = 256 - (((long)(Y - yMaxValue) << 8) / (yMinValue - yMaxValue)); //(MAP - MAP[2])/(MAP[2]- MAP[1]) + int yMaxValue = fromTable->axisY[yMax]; + int yMinValue = fromTable->axisY[yMin]; + int xMaxValue = fromTable->axisX[xMax]; + int xMinValue = fromTable->axisX[xMin]; - int m = ((256-p) * (256-q)) >> 8; - int n = (p * (256-q)) >> 8; - int o = ((256-p) * q) >> 8; - int r = (p * q) >> 8; - return ( (A * m) + (B * n) + (C * o) + (D * r) ) >> 8; + long p = (long)X - xMinValue; + if (xMaxValue == xMinValue) { p = (p << 8); } //This only occurs if the requested X value was equal to one of the X axis bins + else { p = ( (p << 8) / (xMaxValue - xMinValue) ); } //This is the standard case + + long q; + if (yMaxValue == yMinValue) + { + q = (long)Y - yMinValue; + q = (q << 8); + } + else + { + q = long(Y) - yMaxValue; + q = 256 - ( (q << 8) / (yMinValue - yMaxValue) ); + } + + int m = ((256-p) * (256-q)) >> 8; + int n = (p * (256-q)) >> 8; + int o = ((256-p) * q) >> 8; + int r = (p * q) >> 8; + tableResult = ( (A * m) + (B * n) + (C * o) + (D * r) ) >> 8; + } + return tableResult; } -*/