Adjust 2D tables to support ints as well as bytes

This commit is contained in:
Josh Stewart 2014-05-12 21:27:22 +10:00
parent a3211d35b6
commit 1275901466
2 changed files with 91 additions and 24 deletions

View File

@ -3,11 +3,19 @@ This file is used for everything related to maps/tables including their definiti
*/
#include <Arduino.h>
/*
The 2D table can contain either 8-bit (byte) or 16-bit (int) values
The valueSize variable should be set to either 8 or 16 to indicate this BEFORE the table is used
*/
struct table2D {
byte valueSize;
byte xSize;
byte *values;
byte *axisX;
int *values16;
int *axisX16;
};
void table2D_setSize(struct table2D targetTable, byte newSize);

View File

@ -3,20 +3,45 @@ Because the size of the table is dynamic, this functino is required to reallocat
Note that this may clear some of the existing values of the table
*/
void table2D_setSize(struct table2D* targetTable, byte newSize)
{
//2D tables can contain either bytes or ints, depending on the value of the valueSize field
if(targetTable->valueSize == SIZE_BYTE)
{
targetTable->values = (byte *)realloc(targetTable->values, newSize * sizeof(byte));
targetTable->axisX = (byte *)realloc(targetTable->axisX, newSize * sizeof(byte));
targetTable->xSize = newSize;
}
else
{
targetTable->values16 = (int *)realloc(targetTable->values16, newSize * sizeof(int));
targetTable->axisX16 = (int *)realloc(targetTable->axisX16, 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
This function must take into account whether a table contains 8-bit or 16-bit values.
Unfortunately this means many of the lines are duplicated depending on this
*/
int table2D_getValue(struct table2D fromTable, int X)
{
int xMinValue = fromTable.axisX[0];
int xMaxValue = fromTable.axisX[fromTable.xSize-1];
int xMinValue, xMaxValue;
if (fromTable.valueSize == SIZE_BYTE)
{
//Byte version
xMinValue = fromTable.axisX[0];
xMaxValue = fromTable.axisX[fromTable.xSize-1];
}
else
{
//int version
xMinValue = fromTable.axisX16[0];
xMaxValue = fromTable.axisX16[fromTable.xSize-1];
}
int xMin = 0;
int xMax = 0;
@ -26,6 +51,9 @@ int table2D_getValue(struct table2D fromTable, int X)
for (int x = fromTable.xSize-1; x >= 0; x--)
{
if (fromTable.valueSize == SIZE_BYTE)
{
//Byte version
//Checks the case where the X value is exactly what was requested
if ( (X == fromTable.axisX[x]) || (x == 0) )
{
@ -41,6 +69,24 @@ int table2D_getValue(struct table2D fromTable, int X)
break;
}
}
else
{
//int version
if ( (X == fromTable.axisX16[x]) || (x == 0) )
{
return fromTable.values16[x]; //Simply return the coresponding value
}
//Normal case
if ( (X <= fromTable.axisX16[x]) && (X > fromTable.axisX16[x-1]) )
{
xMaxValue = fromTable.axisX16[x];
xMinValue = fromTable.axisX16[x-1];
xMax = x;
xMin = x-1;
break;
}
}
}
unsigned int m = X - xMinValue;
unsigned int n = xMaxValue - xMinValue;
@ -52,11 +98,24 @@ int table2D_getValue(struct table2D fromTable, int X)
//Non-Float version
int yVal;
if (fromTable.valueSize == SIZE_BYTE)
{
//Byte version
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; }
else { yVal = fromTable.values[xMin] - yVal; }
}
else
{
//int version
yVal = ((long)(m << 6) / n) * (abs(fromTable.values16[xMax] - fromTable.values16[xMin]));
yVal = (yVal >> 6);
if (fromTable.values[xMax] > fromTable.values16[xMin]) { yVal = fromTable.values16[xMin] + yVal; }
else { yVal = fromTable.values16[xMin] - yVal; }
}
return yVal;
}