-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwingman_input.h
151 lines (141 loc) · 4.75 KB
/
wingman_input.h
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
#ifndef WINGMAN_INPUT_H
#define WINGMAN_INPUT_H
#ifdef NO_HID
struct WingmanInput
{
bool update() { return false; }
double gas() { return 0; }
double brake() { return 0; }
bool update_buttons() { return false; }
bool left_click() { return false; }
bool right_click() { return false; }
bool update_wheel() { return false; }
bool steering_left() { return false; }
bool steering_right() { return false; }
bool wheel_neutral() { return false; }
double wheel() { return 0; }
bool valid() { return false; }
};
#else
#include <HID.h>
// WingMan Formula GP
// 9: geht bei gas von 127=>0, bremse: 127=>255
// 10&11: bei gas: 255=>0, bremse: 255=>65535
// 8: lenkrad von links (0) bis rechts (255)
//4-7: Knöpfe (0 / 1), links hinten, rechts hinten, links vorne, rechts vorne
class WingmanInput
{
public:
WingmanInput() {
const std::string wingman = "WingMan Formula GP";
hid.ScanDevices();
int c = hid.GetDeviceCount();
for (int i = 0; i < c; i++) {
IHIDevice* d = hid.GetDevice(i);
const std::string name = d->GetDeviceName();
if (name.find(wingman) != std::string::npos) {
dev = d;
break;
}
}
printf("%s %s\n", wingman.c_str(), dev ? "found" : "not found");
if (dev) {
element = dev->GetElement(10);
wheel_element = dev->GetElement(8);
left_button_element = dev->GetElement(6);
right_button_element = dev->GetElement(7);
left_back_button_element = dev->GetElement(4);
right_back_button_element = dev->GetElement(5);
}
}
bool update() {
const int old_value = value;
value = element->GetValue();
return value != old_value;
}
bool update_wheel() {
const int old_value = wheel_value;
wheel_value = wheel_element->GetValue();
// if (wheel_value != old_value)
// printf("wheel: %i\n", wheel_value);
return wheel_value != old_value;
}
bool update_buttons() {
bool update = false;
bool old_val = left_button_cur;
left_button_cur = left_button_element->GetValue();
if (left_button_cur && old_val != left_button_cur) {
left_button_click = true;
update = true;
}
old_val = right_button_cur;
right_button_cur = right_button_element->GetValue();
if (right_button_cur && old_val != right_button_cur) {
right_button_click = true;
update = true;
}
return update;
}
bool update_back_buttons() {
bool update = false;
bool old_val = left_right_cur;
left_right_cur = left_back_button_element->GetValue() && right_back_button_element->GetValue();
if (left_right_cur && old_val != left_right_cur) {
left_right_click_ = true;
update = true;
}
return update;
}
const int neutral_range = 7;
double gas() { return value < 255 ? (255-value)/255. : 0; }
double brake() { return value > 255 ? (value-255)/65280. : 0; }
double wheel() {
/*printf("%.3f\n", (wheel_value - 128) / 128.);*/
int val = wheel_value - 128;
if (abs(val) <= neutral_range)
return 0;
//val += (val > 0) ? -neutral_range : neutral_range;
//qDebug() << val << (double) (val) / (128-neutral_range);
return pow((double) (val) / (128-neutral_range), 3);
}
bool wheel_neutral() { /*qDebug() << wheel_value; */return wheel_value >= 121 && wheel_value <= 135; }
// bool steering_left() { return wheel() < -0.5; }
// bool steering_right() { return wheel() > 0.5; }
bool left_click() {
const bool ret = left_button_click;
left_button_click = false;
return ret;
}
bool right_click() {
const bool ret = right_button_click;
right_button_click = false;
return ret;
}
bool left_right_click() {
const bool ret = left_right_click_;
left_right_click_ = false;
return ret;
}
bool valid() {
return !!dev && !!element && !!wheel_element;
}
protected:
HIDManager hid;
IHIDevice* dev = NULL;
IHIDElement* element; // this is the element for gas & braking
IHIDElement* wheel_element;
IHIDElement* left_button_element;
IHIDElement* right_button_element;
IHIDElement* left_back_button_element;
IHIDElement* right_back_button_element;
int value = 255; // this is for gas & braking
int wheel_value = 128;
bool left_button_cur = false;
bool right_button_cur = false;
bool left_button_click = false;
bool right_button_click = false;
bool left_right_cur = false;
bool left_right_click_ = false;
};
#endif // NO_HID
#endif // WINGMAN_INPUT_H