-
Notifications
You must be signed in to change notification settings - Fork 0
/
tilt-wizard.cc
233 lines (203 loc) · 7.09 KB
/
tilt-wizard.cc
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* Tilt-wizard
* Copyright (C) 2018 John D. Strunk
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <ostream>
#include <string>
#include <getopt.h>
#include <unistd.h>
#define DIRECTINPUT_VERSION 0x0800
#include <comdef.h>
#include <dinput.h>
//#include <dinputd.h>
#include <Windows.h>
#include "Device.h"
#include "EMStat.h"
#include "TWError.h"
#ifndef GIT_VERSION
#define GIT_VERSION (unknown)
#endif
#ifndef GIT_DATE
#define GIT_DATE (unknown)
#endif
#define _TEXTIFY1(x) #x
#define TEXTIFY(x) _TEXTIFY1(x)
static const int DEFAULT_RANGE = 100;
static const double DEFAULT_MOMENTUM = 0.95;
static void
enumerateDevices()
{
Device::DescriptionList devs = Device::enumerateDevices();
std::cout << std::setw(42) << std::left << "Device GUID"
<< "Name" << std::endl;
std::cout << std::setw(70) << std::setfill('=') << "=" << std::endl;
for (Device::DescriptionList::const_iterator i = devs.begin();
i != devs.end(); ++i) {
std::cout << i->guidString << " " << i->deviceName << std::endl;
}
}
static void
calibrateDevice(std::string guidString, int axisRange, double momentum)
{
std::cout << "Looking for device: " << guidString << std::endl;
Device dev(guidString);
std::cout << "Device name: " << dev.name() << std::endl;
// Grab values once to initialize the stats
dev.poll();
sleep(1); // Let the device start sending data
/*
* Create stats objects
* We take 10 samples/sec, and the new data has weight 1%, so each
* second is ~10% of the statistic
*/
EMStat xStats(momentum), yStats(momentum);
dev.poll();
xStats.set(dev.position(DIJOFS_X), 100*100);
yStats.set(dev.position(DIJOFS_Y), 100*100);
std::cout << std::right
<< std::setw(13) << "current"
<< std::setw(7) << "avg"
<< std::setw(7) << "DZ%"
<< std::setw(13) << "current"
<< std::setw(7) << "avg"
<< std::setw(7) << "DZ%"
<< std::endl;
for (unsigned i=1; ;++i) {
dev.poll();
LONG x = dev.position(DIJOFS_X);
// Reject extreme values (nudges); ensure we always accept something
if (abs(x - xStats.avg()) < std::max(2.0*xStats.stdev(), 10.0))
xStats.insert(x);
LONG y = dev.position(DIJOFS_Y);
if (abs(y - yStats.avg()) < std::max(2.0*yStats.stdev(), 10.0))
yStats.insert(y);
double xDeadzone = std::min(4.0*xStats.stdev()*100.0/axisRange, 100.0);
double yDeadzone = std::min(4.0*yStats.stdev()*100.0/axisRange, 100.0);
// Update calibration 1/sec
if (i % 10 == 0) {
dev.deadzone(DIJOFS_X, xDeadzone);
dev.calibration(DIJOFS_X, xStats.avg() - axisRange,
xStats.avg(),
xStats.avg() + axisRange);
dev.deadzone(DIJOFS_Y, yDeadzone);
dev.calibration(DIJOFS_Y, yStats.avg() - axisRange,
yStats.avg(),
yStats.avg() + axisRange);
}
// Print stats
std::cout << std::setprecision(0) << std::fixed;
std::cout << "\r x: " << std::setw(6) << x
<< " " << std::setw(6) << xStats.avg()
<< " " << std::setw(6) << xDeadzone
<< " y: " << std::setw(6) << y
<< " " << std::setw(6) << yStats.avg()
<< " " << std::setw(6) << yDeadzone;
// Pause 0.1s between samples
usleep(100000);
}
}
static void
usage(std::string pname)
{
std::cout << std::endl << "Usage:" << std::endl
<< " " << pname.c_str() << " -h|--help" << std::endl
<< " " << pname.c_str() << " -l|--list" << std::endl
<< " " << pname.c_str() << " -d|--device device_guid [options...]" << std::endl
<< std::endl
<< " -d, --device device_uuid auto-calibrate specified device"
<< std::endl
<< " -h, --help this help message"
<< std::endl
<< " -l, --list list available devices"
<< std::endl
<< " -m, --momentum EMA momentum (0 - 1.0)"
<< " (default: " << DEFAULT_MOMENTUM << ")"
<< std::endl
<< " -r, --range axis range, center to max"
<< " (default: " << DEFAULT_RANGE << ")"
<< std::endl;
}
static void
printVersion()
{
std::cout << "tilt-wizard - https://github.com/JohnStrunk/tilt-wizard"
<< std::endl
<< "License: AGPL v3 or later" << std::endl
<< "Version: " << TEXTIFY(GIT_VERSION) << std::endl
<< "Date: " << TEXTIFY(GIT_DATE) << std::endl << std::endl;
}
int main(int argc, char *argv[])
{
printVersion();
std::string devGuid;
int range = DEFAULT_RANGE;
double momentum = DEFAULT_MOMENTUM;
char opt;
struct option longopts[] = {
{"device", required_argument, 0, 'd'},
{"help", no_argument, 0, 'h'},
{"list", no_argument, 0, 'l'},
{"momentum", required_argument, 0, 'm'},
{"range", required_argument, 0, 'r'},
{0, 0, 0, 0}
};
while ((opt = getopt_long(argc, argv, "d:hlm:r:", longopts, 0)) != -1) {
switch (opt) {
case 'd':
devGuid = optarg;
break;
case 'h':
usage(argv[0]);
exit(EXIT_SUCCESS);
break;
case 'l':
enumerateDevices();
exit(EXIT_SUCCESS);
break;
case 'm':
momentum = atof(optarg);
break;
case 'r':
range = atoi(optarg);
break;
default: /* '?' */
usage(argv[0]);
exit(EXIT_FAILURE);
}
}
if (momentum < 0 || 1.0 < momentum) {
std::cout << "Momentum must be in the range 0 - 1.0" << std::endl;
usage(argv[0]);
exit(EXIT_FAILURE);
}
if (range <= 0) {
std::cout << "Axis range must be > 0" << std::endl;
usage(argv[0]);
exit(EXIT_FAILURE);
}
if (!devGuid.empty()) {
calibrateDevice(devGuid, range, momentum);
return 0;
} else {
std::cout << "Must specify one of -d, -h or -l" << std::endl;
usage(argv[0]);
exit(EXIT_FAILURE);
}
}