Here is some code that can be used to scale the joysticks:
Code:
/* Typedefs so that compiler-independent types can be used. These should be
* defined in a single header file somewhere and included in all of your code.
* While not strictly necessary, it is a good habit to get into. */
typedef char s8;
typedef short s16;
typedef long s32;
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned long u32;
/* Always calibrate your joysticks so that they output 127 when in their
* resting position. Use the IFI Dashboard if necessary to help with this. */
#define JOYSTICK_MID 127
/* This function takes in a joystick value from the OI as well as a known
* minimum and maximum value for this stick and scales it such that the value
* is in the full 0-254 range. The return value from the function is the new,
* scaled value. */
u8 scale_joystick(u8 joystick_orig, u8 joystick_min, u8 joystick_max)
{
s16 scaled_val = JOYSTICK_MID;
/* First, make sure the original joystick value falls in between
* joystick_min and joystick_max, in case it's slightly higher or lower
* than we expected. */
if(joystick_orig > joystick_max)
{
joystick_orig = joystick_max;
}
else if(joystick_orig < joystick_min)
{
joystick_orig = joystick_min;
}
if(joystick_orig > JOYSTICK_MID)
{
/* Formula: ((positive joystick deflection / positive range) * 127) +
* 127. Multiply by 127 before dividing by the range in order to
* avoid floating-point operations. */
scaled_val = ((s16)JOYSTICK_MID) +
((((s16)joystick_orig - ((s16)JOYSTICK_MID)) *
((s16)JOYSTICK_MID)) /
((s16)joystick_max - ((s16)JOYSTICK_MID)));
}
else if(joystick_orig < JOYSTICK_MID)
{
/* Formula: 127 - ((negative joystick deflection / negative range) *
* 127). Multiply by 127 before dividing by the range in order to
* avoid floating-point operations. */
scaled_val = ((s16)JOYSTICK_MID) -
(((((s16)JOYSTICK_MID) - (s16)joystick_orig) *
((s16)JOYSTICK_MID)) /
(((s16)JOYSTICK_MID) - (s16)joystick_min));
}
else
{
/* Do nothing - the incoming joystick value was 127, and the scaled
* value is already defaulted to this. */
}
return scaled_val;
}
To use this code, you should determine the minimum and maximum values for each axis of each joystick and store them in your code as #defines. Then, call this function like this:
Code:
/* #defines as an example */
#define PORT1_X_MIN 24
#define PORT1_X_MAX 249
#define PORT1_Y_MIN 22
#define PORT1_Y_MAX 250
...
p1_x = scale_joystick(p1_x, PORT1_X_MIN, PORT1_X_MAX);
p1_y = scale_joystick(p1_y, PORT1_Y_MIN, PORT1_Y_MAX);
You'll want to put that code shortly after the Getdata() call in Process_Data_From_Master_uP(), before p1_x, p1_y, etc. get used by anything else. Don't forget to change your #defines if you change your joysticks.