avoid float functions to compute range mapping for axis adc reading

This commit is contained in:
a-chol 2020-03-20 19:59:23 +01:00
parent 801be60473
commit 6fd7f9d12f
1 changed files with 13 additions and 8 deletions

View File

@ -114,15 +114,20 @@ bool process_joystick_analogread_quantum() {
int16_t axis_val = joystick_axes[axis_index].mid_digit;
# endif
// test the converted value against the lower range
uint16_t ref = joystick_axes[axis_index].mid_digit;
uint16_t range = joystick_axes[axis_index].min_digit;
int16_t ranged_val = -127 * fminf(1.f, (axis_val - (float)(ref)) / (range - (float)ref));
if (ranged_val > 0) {
// the value is in the higher range
range = joystick_axes[axis_index].max_digit;
ranged_val = 127 * fminf(1.f, (axis_val - (float)(ref)) / (range - (float)ref));
//test the converted value against the lower range
int32_t ref = joystick_axes[axis_index].mid_digit;
int32_t range = joystick_axes[axis_index].min_digit;
int32_t ranged_val = ((axis_val - ref)* -127)/(range - ref) ;
if (ranged_val > 0){
//the value is in the higher range
range = joystick_axes[axis_index].max_digit;
ranged_val = ((axis_val - ref)* 127)/(range - ref);
}
//clamp the result in the valid range
ranged_val = ranged_val<-127 ? -127 : ranged_val;
ranged_val = ranged_val>127 ? 127 : ranged_val;
if (ranged_val != joystick_status.axes[axis_index]) {
joystick_status.axes[axis_index] = ranged_val;