|
@@ -1,10 +1,10 @@
|
1
|
1
|
/*
|
2
|
2
|
* Saitek X52 Arduino USB Host Shield Library.
|
3
|
3
|
* Copyright 2016 by Thomas Buck <xythobuz@xythobuz.de>
|
4
|
|
- *
|
|
4
|
+ *
|
5
|
5
|
* Based on the USB Host Library HID Joystick example
|
6
|
6
|
* https://www.circuitsathome.com/mcu/hid-joystick-code-sample
|
7
|
|
- *
|
|
7
|
+ *
|
8
|
8
|
* This program is free software; you can redistribute it and/or
|
9
|
9
|
* modify it under the terms of the GNU General Public License as
|
10
|
10
|
* published by the Free Software Foundation, version 2.
|
|
@@ -15,92 +15,109 @@
|
15
|
15
|
//#define DEBUG_OUTPUT_RAW
|
16
|
16
|
#define DEBUG_OUTPUT
|
17
|
17
|
|
|
18
|
+const GamePadEventData JoystickEventsDeadZone::deadZone(
|
|
19
|
+ 0, 0, 0, 0, 0, 50, 0
|
|
20
|
+);
|
|
21
|
+const uint8_t JoystickEventsDeadZone::deadZoneMouseX = 0;
|
|
22
|
+const uint8_t JoystickEventsDeadZone::deadZoneMouseY = 0;
|
18
|
23
|
|
19
|
|
-/*
|
20
|
|
- * Uuuhhh TODO!!
|
21
|
|
- * This is not a deadzone, this is something like sensitivity.
|
22
|
|
- * For deadzone, we need to only apply it from the center outwards.
|
23
|
|
- */
|
|
24
|
+const GamePadEventData JoystickEventsDeadZone::centerValue(
|
|
25
|
+ 0x3FF, 0x3FF, 0x7F,
|
|
26
|
+ 0x7F, 0x7F, 0x1FF, 0x7F
|
|
27
|
+);
|
|
28
|
+const uint8_t JoystickEventsDeadZone::centerMouseX = 0x7F;
|
|
29
|
+const uint8_t JoystickEventsDeadZone::centerMouseY = 0x7F;
|
24
|
30
|
|
25
|
|
-
|
26
|
|
-JoystickEvents::JoystickEvents()
|
27
|
|
- : lastData(0), lastMouseX(0), lastMouseY(0), deadZone(0) {
|
28
|
|
- deadZone.X = 0;
|
29
|
|
- deadZone.Y = 0;
|
30
|
|
- deadZone.Z = 0;
|
31
|
|
- deadZone.Rx = 0;
|
32
|
|
- deadZone.Ry = 0;
|
33
|
|
- deadZone.Rz = 50;
|
34
|
|
- deadZoneMouseX = 0;
|
35
|
|
- deadZoneMouseY = 0;
|
36
|
|
-}
|
37
|
|
-
|
38
|
|
-void JoystickEvents::OnGamePadChanged(const GamePadEventData *evt) {
|
|
31
|
+void JoystickEventsDeadZone::OnGamePadChanged(const GamePadEventData& evt) {
|
39
|
32
|
#ifdef DEBUG_OUTPUT_RAW
|
40
|
33
|
Serial.print("X: ");
|
41
|
|
- PrintHex<uint16_t > (evt->X, 0x80);
|
|
34
|
+ PrintHex<uint16_t > (evt.X, 0x80);
|
42
|
35
|
Serial.print(" Y: ");
|
43
|
|
- PrintHex<uint16_t > (evt->Y, 0x80);
|
|
36
|
+ PrintHex<uint16_t > (evt.Y, 0x80);
|
44
|
37
|
Serial.print(" Z: ");
|
45
|
|
- PrintHex<uint8_t > (evt->Z, 0x80);
|
|
38
|
+ PrintHex<uint8_t > (evt.Z, 0x80);
|
46
|
39
|
Serial.print(" Rx: ");
|
47
|
|
- PrintHex<uint8_t > (evt->Rx, 0x80);
|
|
40
|
+ PrintHex<uint8_t > (evt.Rx, 0x80);
|
48
|
41
|
Serial.print(" Ry: ");
|
49
|
|
- PrintHex<uint8_t > (evt->Ry, 0x80);
|
|
42
|
+ PrintHex<uint8_t > (evt.Ry, 0x80);
|
50
|
43
|
Serial.print(" Rz: ");
|
51
|
|
- PrintHex<uint16_t > (evt->Rz, 0x80);
|
|
44
|
+ PrintHex<uint16_t > (evt.Rz, 0x80);
|
52
|
45
|
Serial.print(" S: ");
|
53
|
|
- PrintHex<uint8_t > (evt->Slider, 0x80);
|
|
46
|
+ PrintHex<uint8_t > (evt.Slider, 0x80);
|
54
|
47
|
Serial.println("");
|
55
|
48
|
#endif
|
56
|
49
|
|
57
|
|
- GamePadEventData newData = lastData;
|
|
50
|
+ GamePadEventData newData = centerValue;
|
|
51
|
+
|
|
52
|
+#ifdef DEBUG_OUTPUT
|
58
|
53
|
uint8_t updated = 0;
|
|
54
|
+#endif
|
|
55
|
+
|
|
56
|
+ if ((evt.X > (centerValue.X + deadZone.X))
|
|
57
|
+ || (evt.X < (centerValue.X - deadZone.X))) {
|
|
58
|
+ newData.X = evt.X;
|
59
|
59
|
|
60
|
|
- if ((evt->X > (lastData.X + deadZone.X))
|
61
|
|
- || (evt->X < (lastData.X - deadZone.X))) {
|
62
|
|
- newData.X = evt->X;
|
|
60
|
+#ifdef DEBUG_OUTPUT
|
63
|
61
|
updated = 1;
|
|
62
|
+#endif
|
64
|
63
|
}
|
65
|
64
|
|
66
|
|
- if ((evt->Y > (lastData.Y + deadZone.Y))
|
67
|
|
- || (evt->Y < (lastData.Y - deadZone.Y))) {
|
68
|
|
- newData.Y = evt->Y;
|
|
65
|
+ if ((evt.Y > (centerValue.Y + deadZone.Y))
|
|
66
|
+ || (evt.Y < (centerValue.Y - deadZone.Y))) {
|
|
67
|
+ newData.Y = evt.Y;
|
|
68
|
+
|
|
69
|
+#ifdef DEBUG_OUTPUT
|
69
|
70
|
updated = 1;
|
|
71
|
+#endif
|
70
|
72
|
}
|
71
|
73
|
|
72
|
|
- if ((evt->Z > (lastData.Z + deadZone.Z))
|
73
|
|
- || (evt->Z < (lastData.Z - deadZone.Z))) {
|
74
|
|
- newData.Z = evt->Z;
|
|
74
|
+ if ((evt.Z > (centerValue.Z + deadZone.Z))
|
|
75
|
+ || (evt.Z < (centerValue.Z - deadZone.Z))) {
|
|
76
|
+ newData.Z = evt.Z;
|
|
77
|
+
|
|
78
|
+#ifdef DEBUG_OUTPUT
|
75
|
79
|
updated = 1;
|
|
80
|
+#endif
|
76
|
81
|
}
|
77
|
82
|
|
78
|
|
- if ((evt->Rx > (lastData.Rx + deadZone.Rx))
|
79
|
|
- || (evt->Rx < (lastData.Rx - deadZone.Rx))) {
|
80
|
|
- newData.Rx = evt->Rx;
|
|
83
|
+ if ((evt.Rx > (centerValue.Rx + deadZone.Rx))
|
|
84
|
+ || (evt.Rx < (centerValue.Rx - deadZone.Rx))) {
|
|
85
|
+ newData.Rx = evt.Rx;
|
|
86
|
+
|
|
87
|
+#ifdef DEBUG_OUTPUT
|
81
|
88
|
updated = 1;
|
|
89
|
+#endif
|
82
|
90
|
}
|
83
|
91
|
|
84
|
|
- if ((evt->Ry > (lastData.Ry + deadZone.Ry))
|
85
|
|
- || (evt->Ry < (lastData.Ry - deadZone.Ry))) {
|
86
|
|
- newData.Ry = evt->Ry;
|
|
92
|
+ if ((evt.Ry > (centerValue.Ry + deadZone.Ry))
|
|
93
|
+ || (evt.Ry < (centerValue.Ry - deadZone.Ry))) {
|
|
94
|
+ newData.Ry = evt.Ry;
|
|
95
|
+
|
|
96
|
+#ifdef DEBUG_OUTPUT
|
87
|
97
|
updated = 1;
|
|
98
|
+#endif
|
88
|
99
|
}
|
89
|
100
|
|
90
|
|
- if ((evt->Rz > (lastData.Rz + deadZone.Rz))
|
91
|
|
- || (evt->Rz < (lastData.Rz - deadZone.Rz))) {
|
92
|
|
- newData.Rz = evt->Rz;
|
|
101
|
+ if ((evt.Rz > (centerValue.Rz + deadZone.Rz))
|
|
102
|
+ || (evt.Rz < (centerValue.Rz - deadZone.Rz))) {
|
|
103
|
+ newData.Rz = evt.Rz;
|
|
104
|
+
|
|
105
|
+#ifdef DEBUG_OUTPUT
|
93
|
106
|
updated = 1;
|
|
107
|
+#endif
|
94
|
108
|
}
|
95
|
109
|
|
96
|
|
- if ((evt->Slider > (lastData.Slider + deadZone.Slider))
|
97
|
|
- || (evt->Slider < (lastData.Slider - deadZone.Slider))) {
|
98
|
|
- newData.Slider = evt->Slider;
|
|
110
|
+ if ((evt.Slider > (centerValue.Slider + deadZone.Slider))
|
|
111
|
+ || (evt.Slider < (centerValue.Slider - deadZone.Slider))) {
|
|
112
|
+ newData.Slider = evt.Slider;
|
|
113
|
+
|
|
114
|
+#ifdef DEBUG_OUTPUT
|
99
|
115
|
updated = 1;
|
|
116
|
+#endif
|
100
|
117
|
}
|
101
|
118
|
|
102
|
|
- if (updated) {
|
103
|
119
|
#ifdef DEBUG_OUTPUT
|
|
120
|
+ if (updated) {
|
104
|
121
|
Serial.print("X: ");
|
105
|
122
|
PrintHex<uint16_t > (newData.X, 0x80);
|
106
|
123
|
Serial.print(" Y: ");
|
|
@@ -116,35 +133,49 @@ void JoystickEvents::OnGamePadChanged(const GamePadEventData *evt) {
|
116
|
133
|
Serial.print(" S: ");
|
117
|
134
|
PrintHex<uint8_t > (newData.Slider, 0x80);
|
118
|
135
|
Serial.println("");
|
119
|
|
-#endif
|
120
|
136
|
}
|
|
137
|
+#endif
|
121
|
138
|
|
122
|
|
- lastData = *evt;
|
|
139
|
+ if (client) {
|
|
140
|
+ client->OnGamePadChanged(newData);
|
|
141
|
+ }
|
123
|
142
|
}
|
124
|
143
|
|
125
|
|
-void JoystickEvents::OnHatSwitch(uint8_t hat) {
|
|
144
|
+void JoystickEventsDeadZone::OnHatSwitch(uint8_t hat) {
|
126
|
145
|
#ifdef DEBUG_OUTPUT
|
127
|
146
|
Serial.print("Hat Switch: ");
|
128
|
147
|
PrintHex<uint8_t > (hat, 0x80);
|
129
|
148
|
Serial.println("");
|
130
|
149
|
#endif
|
|
150
|
+
|
|
151
|
+ if (client) {
|
|
152
|
+ client->OnHatSwitch(hat);
|
|
153
|
+ }
|
131
|
154
|
}
|
132
|
155
|
|
133
|
|
-void JoystickEvents::OnButtonUp(uint8_t but_id) {
|
|
156
|
+void JoystickEventsDeadZone::OnButtonUp(uint8_t but_id) {
|
134
|
157
|
#ifdef DEBUG_OUTPUT
|
135
|
158
|
Serial.print("Up: ");
|
136
|
159
|
Serial.println(but_id, DEC);
|
137
|
160
|
#endif
|
|
161
|
+
|
|
162
|
+ if (client) {
|
|
163
|
+ client->OnButtonUp(but_id);
|
|
164
|
+ }
|
138
|
165
|
}
|
139
|
166
|
|
140
|
|
-void JoystickEvents::OnButtonDn(uint8_t but_id) {
|
|
167
|
+void JoystickEventsDeadZone::OnButtonDn(uint8_t but_id) {
|
141
|
168
|
#ifdef DEBUG_OUTPUT
|
142
|
169
|
Serial.print("Down: ");
|
143
|
170
|
Serial.println(but_id, DEC);
|
144
|
171
|
#endif
|
|
172
|
+
|
|
173
|
+ if (client) {
|
|
174
|
+ client->OnButtonDn(but_id);
|
|
175
|
+ }
|
145
|
176
|
}
|
146
|
177
|
|
147
|
|
-void JoystickEvents::OnMouseMoved(uint8_t x, uint8_t y) {
|
|
178
|
+void JoystickEventsDeadZone::OnMouseMoved(uint8_t x, uint8_t y) {
|
148
|
179
|
#ifdef DEBUG_OUTPUT_RAW
|
149
|
180
|
Serial.print("Mouse X: ");
|
150
|
181
|
PrintHex<uint8_t >(x, 0x80);
|
|
@@ -153,33 +184,43 @@ void JoystickEvents::OnMouseMoved(uint8_t x, uint8_t y) {
|
153
|
184
|
Serial.println("");
|
154
|
185
|
#endif
|
155
|
186
|
|
156
|
|
- uint8_t newX = lastMouseX;
|
157
|
|
- uint8_t newY = lastMouseY;
|
|
187
|
+ uint8_t newX = centerMouseX;
|
|
188
|
+ uint8_t newY = centerMouseY;
|
|
189
|
+
|
|
190
|
+#ifdef DEBUG_OUTPUT
|
158
|
191
|
uint8_t updated = 0;
|
159
|
|
-
|
160
|
|
- if ((x > (lastMouseX + deadZoneMouseX))
|
161
|
|
- || (x < (lastMouseX - deadZoneMouseX))) {
|
|
192
|
+#endif
|
|
193
|
+
|
|
194
|
+ if ((x > (centerMouseX + deadZoneMouseX))
|
|
195
|
+ || (x < (centerMouseX - deadZoneMouseX))) {
|
162
|
196
|
newX = x;
|
|
197
|
+
|
|
198
|
+#ifdef DEBUG_OUTPUT
|
163
|
199
|
updated = 1;
|
|
200
|
+#endif
|
164
|
201
|
}
|
165
|
202
|
|
166
|
|
- if ((y > (lastMouseY + deadZoneMouseY))
|
167
|
|
- || (y < (lastMouseY - deadZoneMouseY))) {
|
|
203
|
+ if ((y > (centerMouseY + deadZoneMouseY))
|
|
204
|
+ || (y < (centerMouseY - deadZoneMouseY))) {
|
168
|
205
|
newY = y;
|
|
206
|
+
|
|
207
|
+#ifdef DEBUG_OUTPUT
|
169
|
208
|
updated = 1;
|
|
209
|
+#endif
|
170
|
210
|
}
|
171
|
211
|
|
172
|
|
- if (updated) {
|
173
|
212
|
#ifdef DEBUG_OUTPUT
|
|
213
|
+ if (updated) {
|
174
|
214
|
Serial.print("Mouse X: ");
|
175
|
215
|
PrintHex<uint8_t >(newX, 0x80);
|
176
|
216
|
Serial.print("\tY: ");
|
177
|
217
|
PrintHex<uint8_t >(newY, 0x80);
|
178
|
218
|
Serial.println("");
|
179
|
|
-#endif
|
180
|
219
|
}
|
|
220
|
+#endif
|
181
|
221
|
|
182
|
|
- lastMouseX = x;
|
183
|
|
- lastMouseY = y;
|
|
222
|
+ if (client) {
|
|
223
|
+ client->OnMouseMoved(x, y);
|
|
224
|
+ }
|
184
|
225
|
}
|
185
|
226
|
|