-
Notifications
You must be signed in to change notification settings - Fork 0
/
wiiweight.c
97 lines (84 loc) · 2.06 KB
/
wiiweight.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>
#include <time.h>
#include <unistd.h>
int32_t time_interval = 1;
double calculated_weight = 0.0;
double simple_weight = 0.0;
const double scale = 94.31;
const double kalman = 0.1;
const double inv_kal = (1.0 - kalman);
struct weight_values{
int32_t x0, x1, y0, y1;
} weight, measured_weight;
void calculateWeight(){
calculated_weight = (weight.x0 + weight.x1 + weight.y0 + weight.y1) / scale;
simple_weight = (measured_weight.x0 + measured_weight.x1 + measured_weight.y0 + measured_weight.y1) / scale;
}
void printWeight(){
printf("\e[1;1H\e[2J");
printf("Weight: %f kg\nSimple: %f kg\n", calculated_weight, simple_weight);
}
void setWeight(u_int16_t co, int32_t val){
switch(co){
case ABS_HAT0X:
measured_weight.x0 = val;
weight.x0 = kalman * val + (inv_kal) * weight.x0;
break;
case ABS_HAT1X:
measured_weight.x1 = val;
weight.x1 = kalman * val + (inv_kal) * weight.x1;
break;
case ABS_HAT0Y:
measured_weight.y0 = val;
weight.y0 = kalman * val + (inv_kal) * weight.y0;
break;
case ABS_HAT1Y:
measured_weight.y1 = val;
weight.y1 = kalman * val + (inv_kal) * weight.y1;
break;
}
}
int main(int argc, char** argv){
char* input_device=NULL;
int fd, bytes;
struct input_event ev;
time_t current_time, last_time;
time(&last_time);
if(argc<2){
printf("Insufficient arguments\n");
exit(1);
}
input_device=argv[1];
fd=open(input_device, O_RDONLY);
if(fd<0){
printf("Failed to open device\n");
exit(1);
}
//get exclusive control
bytes=ioctl(fd, EVIOCGRAB, 1);
while(true){
bytes=read(fd, &ev, sizeof(struct input_event));
if(bytes<0){
printf("read() error\n");
close(fd);
exit(1);
}
if(ev.type==EV_ABS){
//printf("Type: %d; Scancode %d; Value: %i \n", ev.type, ev.code, ev.value);
setWeight(ev.code, ev.value);
}
time(¤t_time);
if(current_time - last_time > time_interval){
last_time = current_time;
printWeight();
}
calculateWeight();
}
return 0;
}