Reversed axis algorithm issue

I'm currently working on an electronic projet and there's a little problem with the joystick values. The values are "correct" but they looks weird.

A classical axis from a joystick usually work (for example left to right).

  • Totally left : -128
  • Center : 0
  • Totally left : +128

But here's what I read from this one :

  • Totally left : -0
  • Slightly on the left : - 128
  • Center : "Random" (never totally zeroed, float between -125 and +125)
  • Slightly on the right : + 128
  • Totally right : +0

For the moment I'm using the following workaround to get a linear progression from -128 to +128 :

if (value > 0)
    value = -(128 - value);
    test = 128 + value;

The problem is I have to do that on several inputs, 2 axis per joyrstick, 3 joystick per device, 4 total devices so 24 times and I need to need keep a response time under 20ms for the entire operation. And that's freaking cycle consuming !

I can binary manipulate the value. Here's how I actually center it. Raw dump contains array of 0 and 1 read from the controller I/O

for (i = 0; i<8; i++) {
    value |= raw_dump[pos + i] ? (0x80 >> i):0 ;

Do you have any ideas or good algorithm ? I'm starting to be desesperate and I totally suck on binary manipulation... :'(


It looks like whatever mechanism is sampling the joystick actually returns an unsigned byte in the range of 0 .. 255, with 0 at the far left and 255 at the far right.

You can convert that value to the range -128 to 127 with one statement:

value = (value & 0xFF) - 128;

If value is a byte variable, you can shorten that to:

value ^= 0x80;

That conversion should be very quick on any processor, even a 1MHz 6502.

I'm not sure what your second bit of code is about. If you could describe what you're trying to accomplish there, I can offer further insight.

Tags: C++ / C / Algorithm

Similar questions

World axis to local axis
How can I modify/rotate an axis angle from world coords to object coords? See below:
Resolving rotation about an arbitary axis into rotation about X, Y and Z axis
I am currently trying to implement a virtual arcball in OpenGL utilizing GLUT library. As of now I am calculating the axis of rotation and angle of rotation by doing standard arcball computation on old and new coordinates of the mouse. But I want to obtain the angle theta1, theta2, theta3 , such that rotation of the object about X axis by theta1 , ...
Object rotating via world axis and not local axis OpenGL
So I have a function here where x, y, z are the coordinates of a local axis you want to rotate around for this object. I am using (0,1,0) for testing purposes, but yet it still ONLY rotates around world y instead of local y. Here is the logic for one vertex of an object in this function: is there something wrong with my rotation matrix? Am I using ...
Algorithm to detect intersection between an axis-aligned rectangle and an oriented superellipse
I am in the process of writing a function to test for the intersection of a rectangle with a superellipse. The rectangle will always be axis-aligned whereas the superellipse may be oriented with an angle of rotation alpha. In the case of an axis-aligned rectangle intersecting an axis-aligned superellipse I have written these two short functions tha...
the running time for an already sorted array being sorted by selection sort algorithm Vs the time for a reversed sorted array being sorted
I'm trying to figuring out the running time for using selection sort algorithm to sort an already sorted array (e.g. 1,2,3,4,5,..) and the time for using that to sort a reversed array (e.g. 5,4,3,2..). The weird thing I found about this is that on my computer it takes more time to sort an already sorted array than sorting a reversed array. From wha...
Point in axis aligned rectangle test?
My rectangle structure has these members: x, y, width, height. Given a point x, y what would be the fastest way of knowing if x, y is inside of the rectangle? I will be doing lots of these so speed is important.

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.