-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNeptuneController.cs
172 lines (151 loc) · 5.55 KB
/
NeptuneController.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
using hidapi;
using neptune_hidapi.net.Hid;
using neptune_hidapi.net.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace neptune_hidapi.net
{
public class NeptuneController
{
private HidDevice _hidDevice;
private ushort _vid = 0x28de, _pid = 0x1205;
private Task _configureTask;
private bool _active = false;
public bool LizardMouseEnabled { get; set; }
public bool LizardButtonsEnabled { get; set; }
public string SerialNumber { get; private set; }
public Func<NeptuneControllerInputEventArgs, Task> OnControllerInputReceived;
public NeptuneController()
{
_hidDevice = new HidDevice(_vid, _pid, 64);
_hidDevice.OnInputReceived = input => Task.Run(() => OnInputReceived(input));
}
private void OnInputReceived(HidDeviceInputReceivedEventArgs e)
{
if (e.Buffer[0] == 1)
{
SDCInput input = e.Buffer.ToStructure<SDCInput>();
NeptuneControllerInputState state = new NeptuneControllerInputState(input);
if (OnControllerInputReceived != null)
OnControllerInputReceived(new NeptuneControllerInputEventArgs(state));
}
else
{
}
}
private double MapValue(double a, double b, double c) => a / b * c;
public async Task<bool> SetHaptic(byte position, ushort amplitude, ushort period, ushort count)
{
SDCHapticPacket haptic = new SDCHapticPacket();
haptic.packet_type = 0x8f;
haptic.len = 0x07;
haptic.position = position;
haptic.amplitude = amplitude;
haptic.period = period;
haptic.count = count;
byte[] data = GetHapticDataBytes(haptic);
await _hidDevice.RequestFeatureReportAsync(data);
return true;
}
private byte[] GetHapticDataBytes(SDCHapticPacket packet)
{
int size = Marshal.SizeOf(packet);
byte[] arr = new byte[size];
IntPtr ptr = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(packet, ptr, false);
Marshal.Copy(ptr, arr, 0, size);
Marshal.FreeHGlobal(ptr);
return arr;
}
private async Task<bool> SetLizardMode(bool mouse, bool buttons)
{
try
{
if (!mouse)
{
//Disable mouse emulation
byte[] data = new byte[] { 0x87, 0x03, 0x08, 0x07 };
await _hidDevice.RequestFeatureReportAsync(data);
}
else
{
//Enable mouse emulation
byte[] data = new byte[] { 0x8e, 0x00 };
await _hidDevice.RequestFeatureReportAsync(data);
}
if (!buttons)
{
//Disable keyboard/mouse button emulation
byte[] data = new byte[] { 0x81, 0x00 };
await _hidDevice.RequestFeatureReportAsync(data);
}
else
{
//Enable keyboard/mouse button emulation
byte[] data = new byte[] { 0x85, 0x00 };
await _hidDevice.RequestFeatureReportAsync(data);
}
}
catch (Exception ex)
{
return false;
}
return true;
}
private async Task ConfigureLoop()
{
while (_active)
{
await SetLizardMode(LizardMouseEnabled, LizardButtonsEnabled);
await Task.Delay(250);
}
}
public async Task<string> ReadSerialNumberAsync()
{
byte[] request = new byte[] { 0xAE, 0x15, 0x01 };
byte[] response = await _hidDevice.RequestFeatureReportAsync(request);
byte[] serial = new byte[response.Length - 5];
Array.Copy(response, 4, serial, 0, serial.Length);
return Encoding.ASCII.GetString(serial).TrimEnd((Char)0);
}
public string ReadSerialNumber()
{
byte[] request = new byte[] { 0xAE, 0x15, 0x01 };
byte[] response = _hidDevice.RequestFeatureReport(request);
byte[] serial = new byte[response.Length - 5];
Array.Copy(response, 4, serial, 0, serial.Length);
return Encoding.ASCII.GetString(serial).TrimEnd((Char)0);
}
public async Task OpenAsync()
{
if (!await _hidDevice.OpenDeviceAsync())
throw new Exception("Could not open device!");
SerialNumber = await ReadSerialNumberAsync();
_hidDevice.BeginRead();
_active = true;
_configureTask = ConfigureLoop();
}
public void Open()
{
if (!_hidDevice.OpenDevice())
throw new Exception("Could not open device!");
SerialNumber = ReadSerialNumber();
_hidDevice.BeginRead();
_active = true;
_configureTask = ConfigureLoop();
}
public Task CloseAsync() => Task.Run(() => Close());
public void Close()
{
if (_hidDevice.IsDeviceValid)
_hidDevice.EndRead();
_hidDevice.Dispose();
_active = false;
}
}
}