-
Notifications
You must be signed in to change notification settings - Fork 0
/
vJoyClient.cpp
325 lines (279 loc) · 9.21 KB
/
vJoyClient.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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// vJoyClient.cpp : Simple feeder application
//
// Supports both types of POV Hats
#include "stdafx.h"
#include "public.h"
#include "vjoyinterface.h"
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#include "Serial.h"
#include <iostream>
using namespace std;
#pragma comment( lib, "VJOYINTERFACE" )
#define _CRT_SECURE_NO_WARNINGS
// Use either ROBUST or EFFICIENT functions
#define ROBUST
//#define EFFICIENT
int
__cdecl
_tmain(__in int argc, __in PZPWSTR argv)
{
USHORT X, Y, Z, ZR, XR; // Position of several axes
JOYSTICK_POSITION iReport; // The structure that holds the full position data
BYTE id=1; // ID of the target vjoy device (Default is 1)
UINT iInterface=1; // Default target vJoy device
BOOL ContinuousPOV=FALSE; // Continuous POV hat (or 4-direction POV Hat)
int count=0;
int channel1;
int channel2;
int channel3;
int channel4;
int channel5;
int channel6;
int poop;
CSerial serial;
if (serial.Open(2, 115200))
{
cout << "Port opened OK" << endl;
}
else
{
cout << "AWW shit, it ain't gonna work" << endl;
}
char* lpBuffer = new char[50];
//Sleep(1000);
//cout << serial.ReadDataWaiting() << endl;
// Get the ID of the target vJoy device
if (argc>1 && wcslen(argv[1]))
sscanf_s((char *)(argv[1]), "%d", &iInterface);
// Get the driver attributes (Vendor ID, Product ID, Version Number)
if (!vJoyEnabled())
{
_tprintf("vJoy driver not enabled: Failed Getting vJoy attributes.\n");
return -2;
}
else
{
_tprintf("Vendor: %S\nProduct :%S\nVersion Number:%S\n", TEXT(GetvJoyManufacturerString()), TEXT(GetvJoyProductString()), TEXT(GetvJoySerialNumberString()));
};
// Get the state of the requested device
VjdStat status = GetVJDStatus(iInterface);
switch (status)
{
case VJD_STAT_OWN:
_tprintf("vJoy Device %d is already owned by this feeder\n", iInterface);
break;
case VJD_STAT_FREE:
_tprintf("vJoy Device %d is free\n", iInterface);
break;
case VJD_STAT_BUSY:
_tprintf("vJoy Device %d is already owned by another feeder\nCannot continue\n", iInterface);
return -3;
case VJD_STAT_MISS:
_tprintf("vJoy Device %d is not installed or disabled\nCannot continue\n", iInterface);
return -4;
default:
_tprintf("vJoy Device %d general error\nCannot continue\n", iInterface);
return -1;
};
// Check which axes are supported
BOOL AxisX = GetVJDAxisExist(iInterface, HID_USAGE_X);
BOOL AxisY = GetVJDAxisExist(iInterface, HID_USAGE_Y);
BOOL AxisZ = GetVJDAxisExist(iInterface, HID_USAGE_Z);
BOOL AxisRX = GetVJDAxisExist(iInterface, HID_USAGE_RX);
BOOL AxisRZ = GetVJDAxisExist(iInterface, HID_USAGE_RZ);
// Get the number of buttons and POV Hat switchessupported by this vJoy device
int nButtons = GetVJDButtonNumber(iInterface);
int ContPovNumber = GetVJDContPovNumber(iInterface);
int DiscPovNumber = GetVJDDiscPovNumber(iInterface);
// Print results
_tprintf("\nvJoy Device %d capabilities:\n", iInterface);
_tprintf("Numner of buttons\t\t%d\n", nButtons);
_tprintf("Numner of Continuous POVs\t%d\n", ContPovNumber);
_tprintf("Numner of Descrete POVs\t\t%d\n", DiscPovNumber);
_tprintf("Axis X\t\t%s\n", AxisX?"Yes":"No");
_tprintf("Axis Y\t\t%s\n", AxisX?"Yes":"No");
_tprintf("Axis Z\t\t%s\n", AxisX?"Yes":"No");
_tprintf("Axis Rx\t\t%s\n", AxisRX?"Yes":"No");
_tprintf("Axis Rz\t\t%s\n", AxisRZ?"Yes":"No");
// Acquire the target
if ((status == VJD_STAT_OWN) || ((status == VJD_STAT_FREE) && (!AcquireVJD(iInterface))))
{
_tprintf("Failed to acquire vJoy device number %d.\n", iInterface);
return -1;
}
else
{
_tprintf("Acquired: vJoy device number %d.\n", iInterface);
}
_tprintf("\npress enter to stat feeding");
getchar();
X = 20;
Y = 30;
Z = 40;
XR = 60;
ZR = 80;
long value = 0;
BOOL res = FALSE;
#ifdef ROBUST
// Reset this device to default values
ResetVJD(iInterface);
// Feed the device in endless loop
while(1)
{
while(serial.ReadDataWaiting() >= 18){
serial.ReadData(lpBuffer, 18);
}
channel1=lpBuffer[2]*256;
if (lpBuffer[3]>=0){channel1+=lpBuffer[3]-128;}
else {channel1 += lpBuffer[3]+128;}
channel2=lpBuffer[4]*256;
if (lpBuffer[5]>=0){channel2+=lpBuffer[5]-128;}
else {channel2 += lpBuffer[5]+128;}
channel3=lpBuffer[6]*256;
if (lpBuffer[7]>=0){channel3+=lpBuffer[7]-128;}
else {channel3 += lpBuffer[7]+128;}
channel4=lpBuffer[8]*256;
if (lpBuffer[9]>=0){channel4+=lpBuffer[9]-128;}
else {channel4 += lpBuffer[9]+128;}
channel5=lpBuffer[10]*256;
if (lpBuffer[11]>=0){channel5+=lpBuffer[11]-128;}
else {channel5 += lpBuffer[11]+128;}
channel6=lpBuffer[12]*256;
if (lpBuffer[13]>=0){channel6+=lpBuffer[13]-128;}
else {channel6 += lpBuffer[13]+128;}
X=32.767*(channel1-1000);
Y=32.767*(channel2-1000);
//Z=32.767*(channel3-1000);
Z=0;
//cout << Y << endl;
// Set position of 4 axes
res = SetAxis(X, iInterface, HID_USAGE_X);
res = SetAxis(Y, iInterface, HID_USAGE_Y);
res = SetAxis(Z, iInterface, HID_USAGE_Z);
res = SetAxis(XR, iInterface, HID_USAGE_RX);
res = SetAxis(ZR, iInterface, HID_USAGE_RZ);
// Press/Release Buttons
//res = SetBtn(TRUE, iInterface, count/50);
//res = SetBtn(FALSE, iInterface, 1+count/50);
// If Continuous POV hat switches installed - make them go round
// For high values - put the switches in neutral state
if (0)//(ContPovNumber)
{
if ((count*70) < 30000)
{
res = SetContPov((DWORD)(count*70) , iInterface, 1);
res = SetContPov((DWORD)(count*70)+2000 , iInterface, 2);
res = SetContPov((DWORD)(count*70)+4000 , iInterface, 3);
res = SetContPov((DWORD)(count*70)+6000 , iInterface, 4);
}
else
{
res = SetContPov(-1 , iInterface, 1);
res = SetContPov(-1 , iInterface, 2);
res = SetContPov(-1 , iInterface, 3);
res = SetContPov(-1 , iInterface, 4);
};
};
// If Discrete POV hat switches installed - make them go round
// From time to time - put the switches in neutral state
if (0)//(DiscPovNumber)
{
if (count < 550)
{
SetDiscPov(((count/20) + 0)%4, iInterface, 1);
SetDiscPov(((count/20) + 1)%4, iInterface, 2);
SetDiscPov(((count/20) + 2)%4, iInterface, 3);
SetDiscPov(((count/20) + 3)%4, iInterface, 4);
}
else
{
SetDiscPov(-1, iInterface, 1);
SetDiscPov(-1, iInterface, 2);
SetDiscPov(-1, iInterface, 3);
SetDiscPov(-1, iInterface, 4);
};
};
Sleep(20);
//cout<<channel1<< " " << channel2 << " " << channel3 << endl;// " " << poop << " " << (poop + channel1) <<endl;
if (channel3 >=1600){res = SetBtn(TRUE, iInterface, 1);}//Sleep(50);res = SetBtn(FALSE, iInterface, 1);}
else{res = SetBtn(FALSE, iInterface, 1);}
if (channel3 <=1400){res = SetBtn(TRUE, iInterface, 2);}//Sleep(50);res = SetBtn(FALSE, iInterface, 2);}
else{res = SetBtn(FALSE, iInterface, 2);}
if (channel4 >=1600){res = SetBtn(TRUE, iInterface, 3);}//Sleep(50);res = SetBtn(FALSE, iInterface, 3);cout<<"poop."<<endl;}
else{res = SetBtn(FALSE, iInterface, 3);}
if (channel4 <=1400){res = SetBtn(TRUE, iInterface, 4);}//Sleep(50);res = SetBtn(FALSE, iInterface, 4);}
else{res = SetBtn(FALSE, iInterface, 4);}
//if (channel3 >=1500){res = SetBtn(TRUE, iInterface, 5);}//Sleep(50);res = SetBtn(FALSE, iInterface, 4);}
//else{res = SetBtn(FALSE, iInterface, 5);}
//X=channel1;
//X+=150;
//Y+=250;
//Z+=350;
//ZR-=200;
count++;
if (count > 640) count=0;
} // While
#endif
#ifdef EFFICIENT
// Start feeding in an endless loop
while (1)
{
/*** Create the data packet that holds the entire position info ***/
id = (BYTE)iInterface;
iReport.bDevice = id;
iReport.wAxisX=X;
iReport.wAxisY=Y;
iReport.wAxisZ=Z;
iReport.wAxisZRot=ZR;
iReport.wAxisXRot=XR;
// Set buttons one by one
iReport.lButtons = 1<<count/20;
if (ContPovNumber)
{
// Make Continuous POV Hat spin
iReport.bHats = (DWORD)(count*70);
iReport.bHatsEx1 = (DWORD)(count*70)+3000;
iReport.bHatsEx2 = (DWORD)(count*70)+5000;
iReport.bHatsEx3 = 15000 - (DWORD)(count*70);
if ((count*70) > 36000)
{
iReport.bHats = -1; // Neutral state
iReport.bHatsEx1 = -1; // Neutral state
iReport.bHatsEx2 = -1; // Neutral state
iReport.bHatsEx3 = -1; // Neutral state
};
}
else
{
// Make 5-position POV Hat spin
unsigned char pov[4];
pov[0] = ((count/20) + 0)%4;
pov[1] = ((count/20) + 1)%4;
pov[2] = ((count/20) + 2)%4;
pov[3] = ((count/20) + 3)%4;
iReport.bHats = (pov[3]<<12) | (pov[2]<<8) | (pov[1]<<4) | pov[0];
if ((count) > 550)
iReport.bHats = -1; // Neutral state
};
/*** Feed the driver with the position packet - is fails then wait for input then try to re-acquire device ***/
if (!UpdateVJD(iInterface, (PVOID)&iReport))
{
_tprintf("Feeding vJoy device number %d failed - try to enable device then press enter\n", iInterface);
getchar();
AcquireVJD(iInterface);
ContinuousPOV = (BOOL)GetVJDContPovNumber(iInterface);
}
Sleep(20);
count++;
if (count > 640) count=0;
X+=150;
Y+=250;
Z+=350;
ZR-=200;
};
#endif
_tprintf("OK\n");
return 0;
}