-
-
Notifications
You must be signed in to change notification settings - Fork 314
Expand file tree
/
Copy pathanalog_filters.cpp
More file actions
29 lines (25 loc) · 1.07 KB
/
analog_filters.cpp
File metadata and controls
29 lines (25 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "util/analog_filters.hpp"
#include <math.h>
#define SIGNUM(x) ((x > 0) - (x < 0))
uint8_t apply_deadzone(uint8_t value, uint8_t deadzone, bool scale) {
int8_t value_signed = value - 128;
if (abs(value_signed) > deadzone) {
// If outside deadzone, must subtract deadzone from result so that axis values start from 1
// instead of having lower values cut off.
int8_t post_deadzone = value_signed - deadzone * SIGNUM(value_signed);
// If a radius value is passed in, scale up the values linearly so that the same effective
// value is given on the rim.
if (scale) {
int8_t sign = SIGNUM(post_deadzone);
int8_t post_scaling = min(127, abs(post_deadzone) * 128.0 / (128 - deadzone)) * sign;
return post_scaling + 128;
}
return post_deadzone + 128;
}
return 128;
}
uint8_t apply_radius(uint8_t value, int radius) {
int8_t value_signed = value - 128;
int8_t sign = SIGNUM(value_signed);
return min(127, (int)(abs(value_signed) * radius / 128.0)) * sign + 128;
}