forked from Tasssadar/multirom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtouch_tracker.c
78 lines (69 loc) · 2.21 KB
/
touch_tracker.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* This file is part of MultiROM.
*
* MultiROM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MultiROM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MultiROM. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include "touch_tracker.h"
#include "util.h"
touch_tracker *touch_tracker_create(void)
{
touch_tracker *t = mzalloc(sizeof(touch_tracker));
return t;
}
void touch_tracker_destroy(touch_tracker *t)
{
free(t);
}
void touch_tracker_start(touch_tracker *t, touch_event *ev)
{
t->distance_abs_x = t->distance_abs_y = 0;
t->distance_x = t->distance_y = 0;
t->start_x = ev->x;
t->start_y = ev->y;
t->last_x = ev->x;
t->last_y = ev->y;
t->prev_x = ev->x;
t->prev_y = ev->y;
memcpy(&t->time_start, &ev->time, sizeof(struct timeval));
}
void touch_tracker_finish(touch_tracker *t, touch_event *ev)
{
t->period = timeval_us_diff(ev->time, t->time_start);
}
void touch_tracker_add(touch_tracker *t, touch_event *ev)
{
t->prev_x = t->last_x;
t->prev_y = t->last_y;
t->distance_x += ev->x - t->last_x;
t->distance_y += ev->y - t->last_y;
t->distance_abs_x += iabs(ev->x - t->last_x);
t->distance_abs_y += iabs(ev->y - t->last_y);
t->last_x = ev->x;
t->last_y = ev->y;
}
float touch_tracker_get_velocity(touch_tracker *t, int axis)
{
if(axis == TRACKER_X)
return ((((float)t->distance_x) / t->period) * 1000000) / DPI_MUL;
else
return ((((float)t->distance_y) / t->period) * 1000000) / DPI_MUL;
}
float touch_tracker_get_velocity_abs(touch_tracker *t, int axis)
{
if(axis == TRACKER_X)
return ((((float)t->distance_abs_x) / t->period) * 1000000) / DPI_MUL;
else
return ((((float)t->distance_abs_y) / t->period) * 1000000) / DPI_MUL;
}