-
Notifications
You must be signed in to change notification settings - Fork 0
/
axes.c
64 lines (48 loc) · 1.43 KB
/
axes.c
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#include "config.h"
static double axes[4];
void update_axes(int16_t delta_x, int16_t delta_y, bool scroll)
{
axes[scroll * 2 + 0] += delta_x;
axes[scroll * 2 + 1] += delta_y;
}
bool get_axes(int16_t *p)
{
/* Scale the sensed pointer coordinates, before passing them
* on. (Also flip one of the axes since, this being a trackball,
* the sensor is mounted upside-down.) */
{
const double x = POINTER_SENSITIVITY * axes[0];
const double y = POINTER_SENSITIVITY * -axes[1];
double ix, iy;
const double fx = modf(x, &ix);
const double fy = modf(y, &iy);
p[0] = (int16_t)ix;
p[1] = (int16_t)iy;
/* Scale any fractional remainders back and accummulate
* them. */
axes[0] = fx / POINTER_SENSITIVITY;
axes[1] = -fy / POINTER_SENSITIVITY;
}
/* Same thing for the wheel. */
{
const double x = WHEEL_SENSITIVITY_X * axes[2];
const double y = WHEEL_SENSITIVITY_Y * axes[3];
double ix, iy;
const double fx = modf(x, &ix);
const double fy = modf(y, &iy);
p[2] = (int16_t)ix;
p[3] = (int16_t)iy;
axes[2] = fx / WHEEL_SENSITIVITY_X;
axes[3] = fy / WHEEL_SENSITIVITY_Y;
}
for (int i = 0; i < 4; i++) {
if (p[i]) {
return true;
}
}
return false;
}