forked from Lenshang/TimeBoxJoyDriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
joystick.cs
413 lines (394 loc) · 12.3 KB
/
joystick.cs
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
using InTheHand.Net;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Sockets;
using Nefarius.ViGEm.Client;
using Nefarius.ViGEm.Client.Targets;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using TimeBoxJoy.Utils;
namespace TimeBoxJoy
{
public class JoyStick
{
private BluetoothClient cli;
private BluetoothEndPoint ep;
private NetworkStream peerStream;
private uint joyStickId;
private IJoyMap joyMap;
private int HeartBeatInterval=0;
private byte[] buffer = new byte[18];
private byte[] sign;
//按键相关Buffer
byte[] _checkHeader = new byte[3];
byte[] _check = new byte[1];
byte[] _keyArray = new byte[4];
byte[] _placeHolder = new byte[1];
byte[] _leftX = new byte[1];
byte[] _leftY = new byte[1];
byte[] _rightX = new byte[1];
byte[] _rightY = new byte[1];
byte[] _leftTrigger = new byte[1];
byte[] _rightTrigger = new byte[1];
byte[] _btSum = new byte[3];
public Action<byte[]> OnReceive;
public JoyStick(string macAddr)
{
BluetoothAddress address = BluetoothAddress.Parse(macAddr);
Guid serialPort = BluetoothService.SerialPort;
this.ep = new BluetoothEndPoint(address, serialPort);
this.cli = new BluetoothClient();
this.sign = HexStrTobyte("EEC10F");
}
public void SetJoyMap(IJoyMap map)
{
this.joyMap = map;
}
public bool StartConnect(int id)
{
bool result;
try
{
this.cli.Connect(this.ep);
this.joyStickId = (uint)(id + 1);
this.peerStream = this.cli.GetStream();
Stream _stream = this.peerStream;
byte[] secret = new byte[]
{
3,
85,
170,
1,
11,
1,
0,
0
};
secret[6] = (byte)this.joyStickId;
_stream.Write(this.getSignStr(secret), 0, 8);
result = true;
}
catch (Exception arg_5D_0)
{
Console.WriteLine(arg_5D_0);
result = false;
}
return result;
}
public void startFeed()
{
StartRead();
}
#region 传输+解析
public void StartRead()
{
//版本1
//this.BeginReadStream(_checkHeader, 3, ParseHeader);
//版本2
this.BeginReadStream(buffer, 18, ParseAll);
//this.peerStream.BeginRead(_checkHeader, 0,3,new AsyncCallback(ParseHeader),null);
}
/// <summary>
/// 版本2 一口气读取18个字节
/// </summary>
/// <param name="ar"></param>
public void ParseAll(IAsyncResult ar)
{
if (EndReadStream(ar) >= 0)
{
MemoryStream ms = new MemoryStream(buffer);
ms.Read(_checkHeader, 0, 3);
if (!byteEqual(this.sign, _checkHeader))
{
this.BeginReadStream(buffer, 18, ParseAll);
return;
}
ms.Read(_check, 0, 1);
ms.Read(_keyArray, 0, 4);
ms.Read(_placeHolder, 0, 1);
ms.Read(_leftX, 0, 1);
ms.Read(_leftY, 0, 1);
ms.Read(_rightX, 0, 1);
ms.Read(_rightY, 0, 1);
ms.Read(_leftTrigger, 0, 1);
ms.Read(_rightTrigger, 0, 1);
ms.Read(_btSum, 0, 3);
ParseAction();
this.BeginReadStream(buffer, 18, ParseAll);
}
}
public void ParseHeader(IAsyncResult ar)
{
if (EndReadStream(ar) >= 0)
{
if (!byteEqual(this.sign, _checkHeader))
{
StartRead();
}
else
{
this.BeginReadStream(_check, 1, ParseCheck);
//this.peerStream.BeginRead(_check, 0, 1, new AsyncCallback(ParseCheck), null);
}
}
}
public void ParseCheck(IAsyncResult ar)
{
if (EndReadStream(ar) >= 0)
{
//TODO 验证Check
this.BeginReadStream(_keyArray, 4, ParseKeyArray);
//this.peerStream.BeginRead(_keyArray, 0, 4, new AsyncCallback(ParseKeyArray), null);
}
}
public void ParseKeyArray(IAsyncResult ar)
{
if (EndReadStream(ar) >= 0)
{
this.BeginReadStream(_placeHolder, 1, ParsePlaceHolder);
//this.peerStream.BeginRead(_placeHolder, 0, 1, new AsyncCallback(ParsePlaceHolder), null);
}
}
public void ParsePlaceHolder(IAsyncResult ar)
{
if (EndReadStream(ar) >= 0)
{
this.BeginReadStream(_leftX, 1, ParseLeftX);
//this.peerStream.BeginRead(_leftX, 0, 1, new AsyncCallback(ParseLeftX), null);
}
}
/// <summary>
/// 解析左摇杆X轴
/// </summary>
/// <param name="ar"></param>
public void ParseLeftX(IAsyncResult ar)
{
if (EndReadStream(ar) >= 0)
{
//TODO
this.BeginReadStream(_leftY, 1, ParseLeftY);
//this.peerStream.BeginRead(_leftY, 0, 1, new AsyncCallback(ParseLeftY), null);
}
}
/// <summary>
/// 解析左摇杆Y轴
/// </summary>
/// <param name="ar"></param>
public void ParseLeftY(IAsyncResult ar)
{
if (EndReadStream(ar) >= 0)
{
this.BeginReadStream(_rightX, 1, ParseRightX);
//this.peerStream.BeginRead(_rightX, 0, 1, new AsyncCallback(ParseRightX), null);
}
}
/// <summary>
/// 解析右摇杆X轴
/// </summary>
/// <param name="ar"></param>
public void ParseRightX(IAsyncResult ar)
{
if (EndReadStream(ar) >= 0)
{
//TODO
this.BeginReadStream(_rightY, 1, ParseRightY);
//this.peerStream.BeginRead(_rightY, 0, 1, new AsyncCallback(ParseRightY), null);
}
}
/// <summary>
/// 解析右摇杆Y轴
/// </summary>
/// <param name="ar"></param>
public void ParseRightY(IAsyncResult ar)
{
if (EndReadStream(ar) >= 0)
{
this.BeginReadStream(_leftTrigger, 1, ParseLeftTrigger);
//this.peerStream.BeginRead(_leftTrigger, 0, 1, new AsyncCallback(ParseLeftTrigger), null);
}
}
/// <summary>
/// 解析左扳机键
/// </summary>
/// <param name="ar"></param>
public void ParseLeftTrigger(IAsyncResult ar)
{
if (EndReadStream(ar) >= 0)
{
this.BeginReadStream(_rightTrigger, 1, ParseRightTrigger);
//this.peerStream.BeginRead(_rightTrigger, 0, 1, new AsyncCallback(ParseRightTrigger), null);
}
}
/// <summary>
/// 解析右扳机键
/// </summary>
/// <param name="ar"></param>
public void ParseRightTrigger(IAsyncResult ar)
{
if (EndReadStream(ar) >= 0)
{
this.BeginReadStream(_btSum, 3, ParseButtomSum);
//this.peerStream.BeginRead(_btSum, 0, 3, new AsyncCallback(ParseButtomSum), null);
}
}
/// <summary>
/// 解析按键和值计算验证
/// </summary>
/// <param name="ar"></param>
public void ParseButtomSum(IAsyncResult ar)
{
if (EndReadStream(ar) >= 0)
{
ParseAction();
StartRead();
}
}
private bool BeginReadStream(byte[] buffer,int size,Action<IAsyncResult> callback)
{
try
{
this.peerStream.BeginRead(buffer, 0, size, new AsyncCallback(callback), null);
return true;
}
catch
{
this.Disconnect();
return false;
}
}
private int EndReadStream(IAsyncResult ar)
{
try
{
var count = this.peerStream.EndRead(ar);
return count;
}
catch
{
this.Disconnect();
return -1;
}
}
#endregion
public void ParseAction()
{
//验证Check
uint num = 0;
foreach (byte b in _keyArray)
{
num += b;
}
num += _leftX[0];
num += _leftY[0];
num += _rightX[0];
num += _rightY[0];
num += _leftTrigger[0];
num += _rightTrigger[0];
foreach (byte b in _btSum)
{
num += b;
}
num = (num - 15u) % 256u;
if (num == _check[0])
{
#if DEBUG
//测试用
byte[] all = new byte[18];
_checkHeader.CopyTo(all, 0);
_check.CopyTo(all, 3);
_keyArray.CopyTo(all, 4);
_leftX.CopyTo(all, 9);
_leftY.CopyTo(all, 10);
_rightX.CopyTo(all, 11);
_rightY.CopyTo(all, 12);
_leftTrigger.CopyTo(all, 13);
_rightTrigger.CopyTo(all, 14);
_btSum.CopyTo(all, 15);
this.OnReceive?.Invoke(all);
//System.Diagnostics.Debug.WriteLine(HexHelper.byteToHexStr(all, all.Length));
#endif
//TODO
this.joyMap.OnBuffer(_keyArray, _leftTrigger, _rightTrigger, _leftX, _leftY, _rightX, _rightY);
}
}
public bool CheckConnect()
{
HeartBeatInterval += 1;
if (HeartBeatInterval < 5)
{
return true;
}
HeartBeatInterval = 0;
try
{
this.peerStream.Write(new byte[1], 0, 1);
return true;
}
catch
{
this.Disconnect();
return false;
}
}
public void Disconnect()
{
try
{
this.peerStream.Dispose();
this.cli.Dispose();
this.joyMap.Dispose();
}
catch
{
}
}
public void SendData(byte[] data)
{
this.peerStream.Write(data,0,data.Length);
}
private byte[] getSignStr(byte[] signStr)
{
int i = 0;
byte b = 2;
while (i < signStr.Length - 1)
{
b = (byte)(b - signStr[i] & 255);
i++;
}
signStr[7] = b;
return signStr;
}
private bool byteEqual(byte[] b1, byte[] b2)
{
var count = b1.Count();
if (count != b2.Count())
{
return false;
}
for(int i=0;i< count; i++)
{
if (b1[i] != b2[i])
{
return false;
}
}
return true;
}
private byte[] HexStrTobyte(string hexString)
{
hexString = hexString.Replace(" ", "");
if ((hexString.Length % 2) != 0)
hexString += " ";
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Trim(), 16);
return returnBytes;
}
}
}