todbot / wiichuck_adapter

Very simple adpater for hooking a Wii Nunchuck to your Arduino or other microcontroller
http://todbot.com/blog/2008/02/18/wiichuck-wii-nunchuck-adapter-available/
76 stars 33 forks source link

Inversion of LSB data read from the nunchuck #1

Closed MartyMacGyver closed 11 years ago

MartyMacGyver commented 11 years ago

As I was working through your code for this, I noticed the way the LSBs for the accelerometer data were being decoded looked strange (This bug applies to the decoding of all three axes, X being used as an example here.)

Let's say nunchuck_buf[5] contains binary ----10-- (where '-' = don't care). If we shift off the first two bits and mask the rest we get binary 00000010, which should be the correct LSB for x. However, in your code:

    if ((nunchuck_buf[5] >> 2) & 1)        accel_x_axis += 2;
    if ((nunchuck_buf[5] >> 3) & 1)        accel_x_axis += 1;

This swaps the bits in the LSB, decoding it as 00000001.

To accomplish this without reversing the LSB bits:

Bit_Offset_x = 2;
LSB_x = (nunchuck_buf[5] >> Bit_Offset_x) & 3;

If we use uint16_t for the nunchuck_buf entries you can decode this more concisely:

Bit_Offset_x = 2;
accel_x_axis = ((nunchuck_buf[5] >> Bit_Offset_x) & 3) |
                        (nunchuck_buf[2] << 2);

(Bit_Offset_x is broken out for illustration in these examples)

Given how widespread your code is (and it's appreciated!) you'll find variations of this almost everywhere in the Arduino community. However, other platforms that use the Wii nunchuck decode the LSB normally, so I strongly suspect this is simply a longstanding bug.

todbot commented 11 years ago

Thanks Marty, I think I normally have the LSB stuff commented out in the code I use.

I did the least-possible-work version of your fix and swapped the +1/+2 parts instead of doing proper bitmasking.