edgetx
edgetx copied to clipboard
fix(radio): properly center analog USB joystick axis
Analog USB joystick axis are encoded as unsigned integers ranging [0 - 2047]. As a consequence input value 0 is reported as output 1024. This leads to asymmetric output: 1024 values smaller than 0 and 1023 values larger than 0.
mixer.cpp generates values [-1024 to +1024] (11 bit + 1) instead of [-1024 to +1023] (11 bit). Luckily the HID report descriptor already reserves 16 bits so the shift from 11 bit to 12 bit output is easy.
Fix EdgeTX/edgetx#4881 by changing the joystick range from [0 - 2047] to [0 - 2048].
Ignore, text was fixed above (You probably mean to change the range from [0 - 2047] to [0 - 2046] (and not 0-2027 to 0-2026)? In the PR code you have the values correct, the comment above is likely only wrong.)
Why clip the high and low channel values? Wouldn't it be better to change the range to 0-2048 which exactly matches the channel values?
The code can also be simplified:
int16_t value = limit(0, channelOutputs[i] + 1024, 2048);
If you're cleaning up the code, the final '& 0x07' in these lines is redundant as the value range has already been limited:
HID_Buffer[i*2 +4] = static_cast<uint8_t>((value >> 8) & 0x07);
For no-clipping to occur the joystick output has to encode 12 bits instead of 11 bits. As the output already reserves space for 16 bits that's OK.
I've incorporated the suggested code cleanup. There is quite a bit of potential for general code cleanup and de-duplication in the joystick area :sweat_smile:
Not only in the joystick area. You are more than welcome to halo with that 😉.