diff --git a/speeduino.ino b/speeduino.ino index 59360747..004bec90 100644 --- a/speeduino.ino +++ b/speeduino.ino @@ -54,7 +54,7 @@ volatile unsigned long toothOneMinusOneTime = 0; //The 2nd to last time (micros( struct table3D fuelTable; //8x8 fuel map struct table3D ignitionTable; //8x8 ignition map -struct table2Dx4 taeTable; //4 bin TPS Acceleration Enrichment map (2D) +struct table2D taeTable; //4 bin TPS Acceleration Enrichment map (2D) struct table2Dx10 WUETable; //10 bin Warm Up Enrichment map (2D) unsigned long counter; diff --git a/table.h b/table.h index e3d01e02..62a6515b 100644 --- a/table.h +++ b/table.h @@ -3,13 +3,15 @@ This file is used for everything related to maps/tables including their definiti */ #include -struct table2Dx4 { - const static byte xSize = 4; +struct table2D { + static byte xSize; - byte values[xSize]; - int axisX[xSize]; + byte *values; + int *axisX; }; +void table2D_setSize(struct table2D targetTable, byte newSize); + struct table2Dx10 { const static byte xSize = 10; @@ -42,4 +44,4 @@ Eg: 2x2 table */ int get3DTableValue(struct table3D, int, int); -int get2DTableValue(struct table2D, int); +int table2D_getValue(struct table2D, int); diff --git a/table.ino b/table.ino index df64db26..07257e12 100644 --- a/table.ino +++ b/table.ino @@ -1,5 +1,33 @@ -//This function simply pulls a 1D linear interpolated (ie averaged) value from a 2D table -int get2DTableValue(struct table2Dx4 fromTable, int X) +/* +Because the size of the table is dynamic, this functino is required to reallocate the array sizes +Note that this will clear all the existing values of the table +*/ +void table2D_setSize(struct table2D targetTable, byte newSize) +{ + //We are able to check whether this is the first time the table has been initialised by checking the xSize. By default it will be 0 + if(targetTable.xSize == 0) + { + //Initialise the arrays + targetTable.values = (byte *)malloc(newSize * sizeof(byte)); + targetTable.axisX = (int *)malloc(newSize * sizeof(int)); + } + else + { + //Free the existing memory and then initialise to new size + free(targetTable.values); + free(targetTable.axisX); + targetTable.values = (byte *)malloc(newSize * sizeof(byte)); + targetTable.axisX = (int *)malloc(newSize * sizeof(int)); + } + targetTable.xSize = newSize; + +} + +/* +This function simply pulls a 1D linear interpolated (ie averaged) value from a 2D table +ie: Given a value on the X axis, it returns a Y value that coresponds to the point on the curve between the nearest two defined X values +*/ +int table2D_getValue(struct table2D fromTable, int X) { int xMinValue = fromTable.axisX[0]; int xMaxValue = fromTable.axisX[fromTable.xSize-1]; @@ -38,7 +66,7 @@ int get2DTableValue(struct table2Dx4 fromTable, int X) //Non-Float version int yVal; - yVal = ((m << 6) / n) * (abs(fromTable.values[xMax] - fromTable.values[xMin])); + yVal = ((long)(m << 6) / n) * (abs(fromTable.values[xMax] - fromTable.values[xMin])); yVal = (yVal >> 6); if (fromTable.values[xMax] > fromTable.values[xMin]) { yVal = fromTable.values[xMin] + yVal; }