-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
76 lines (62 loc) · 1.35 KB
/
main.cpp
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
#include <iostream>
#include <thread>
#include <chrono>
#include <string>
#include <stdlib.h>
#include <signal.h>
#include "uinputdev.h"
#include "iioaccell.h"
#include "argpopt.h"
bool stop = false;
void sigTerm(int dummy)
{
stop = true;
}
int main(int argc, char** argv)
{
UinputDevice dev;
if(!dev.openDev("/dev/uinput", "AccelerometerJoystick", 0x46d, 0xc214))
{
std::cerr<<"Failed to open /dev/uinput: ";
perror(NULL);
return -1;
}
std::string accellPath;
Config config;
argp_parse(&argp, argc, argv, 0, 0, &config);
if(config.device.empty())
{
config.device = Accelerometer::findAccellerometer();
if(config.device.empty())
{
std::cout<<"No accelerometer specifyed and none found\n";
return 0;
}
}
Accelerometer accel;
int startingRate = accel.getRate();
if(!accel.openDevice(config.device))
{
std::cerr<<"failed to open iio device "<<config.device<<std::endl;
return -1;
}
signal(SIGINT, sigTerm);
signal(SIGTERM, sigTerm);
signal(SIGHUP, sigTerm);
if(config.rate > 0)
accel.setRate(config.rate);
while(!stop)
{
Accelerometer::Frame frame = accel.getFrame();
frame.scale(255/9.81);
if(abs(frame.x) > 512)
frame.x = 0;
if(abs(frame.y) > 512)
frame.y = 0;
if(abs(frame.z) > 512)
frame.z = 0;
dev.sendAbs(frame.x,frame.y,frame.z);
}
accel.setRate(startingRate);
return 0;
}