Code:
} else if (temp_joystick < 0) {
printf("lookup[temp_joystick] = %d, lookup[temp_joystick-1] = %d, output is %d\r\n", (int)lookup[temp_joystick], (int)lookup[temp_joystick-1], (int)((joystick - lookup[temp_joystick-1])*(lookup[temp_joystick] - lookup[temp_joystick-1]) / 21) + lookup[temp_joystick-1]);
return ((joystick - lookup[temp_joystick-1])*(lookup[temp_joystick] - lookup[temp_joystick-1]) / 21) + lookup[temp_joystick-1];
On this here, you access negative members of an array. The only time you will ever enter this code is when temp_joystick < 0, and then you use it to address your lookuptable.
That's one of a few issues. I'm coding up an integer version right now.
You need to have a power of two number of line points. They are assumed to be evenly spaced between 0 and 255.
Code:
#define BITS_FOR_LOOKUP 3 // we're using the most significant 3 bits of the joystick value
#include <iostream>
using namespace std;
unsigned char lookup[8] = {0,31,63,95, 126,158,190,221};
unsigned char joyFilter(unsigned char joyValue)
{
unsigned char n = joyValue>>(8-BITS_FOR_LOOKUP);
// n is between 0 and 7 for sure
unsigned int last;
unsigned int next;
unsigned int howFarAlong = joyValue - (n<<(8-BITS_FOR_LOOKUP)); // finds out how along joyValue is between n and n+1
unsigned int maxFarAlong = (1<<(8-BITS_FOR_LOOKUP));
unsigned int invHowFarAlong = (maxFarAlong - howFarAlong);
if(n+1 >= 8)
{
last = lookup[7];
next = 256;
}
else
{
last = lookup[n];
next = lookup[n+1];
}
return (invHowFarAlong*last)/maxFarAlong + (howFarAlong*next)/maxFarAlong ;
}