2 using System.Collections.Generic;
4 using System.Runtime.InteropServices;
5 using System.Diagnostics;
7 using System.Windows.Forms;
9 namespace DirectOutput.Cab.Out.Pac
35 [DllImport(
"PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
36 private static extern void PacSetCallbacks(PAC_ATTACHED_CALLBACK pacAttachedCallback, PAC_REMOVED_CALLBACK pacRemovedCallback);
38 [DllImport(
"PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
39 private static extern int PacInitialize();
41 [DllImport(
"PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
42 private static extern void PacShutdown();
44 [DllImport(
"PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
45 [
return: MarshalAs(UnmanagedType.Bool)]
46 private static extern bool PacSetLEDStates(
int Index, ushort data);
48 [DllImport(
"PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
49 [
return: MarshalAs(UnmanagedType.Bool)]
50 private static extern bool PacSetLEDState(
int Index,
int port, [MarshalAs(UnmanagedType.Bool)]
bool state);
52 [DllImport(
"PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
53 [
return: MarshalAs(UnmanagedType.Bool)]
54 private static extern bool Pac64SetLEDStates(
int Index,
int group, byte data);
56 [DllImport(
"PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
57 [
return: MarshalAs(UnmanagedType.Bool)]
58 private static extern bool Pac64SetLEDState(
int Index,
int group,
int port, [MarshalAs(UnmanagedType.Bool)]
bool state);
60 [DllImport(
"PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
61 private static extern bool Pac64SetLEDStatesRandom(
int Index);
63 [DllImport(
"PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
64 [
return: MarshalAs(UnmanagedType.Bool)]
65 private static extern bool Pac64SetLEDIntensities(
int Index, byte[] data);
67 [DllImport(
"PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
68 [
return: MarshalAs(UnmanagedType.Bool)]
69 private static extern bool Pac64SetLEDIntensity(
int Index,
int port, byte intensity);
71 [DllImport(
"PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
72 [
return: MarshalAs(UnmanagedType.Bool)]
73 private static extern bool Pac64SetLEDFadeTime(
int Index, byte fadeTime);
75 [DllImport(
"PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
76 [
return: MarshalAs(UnmanagedType.Bool)]
77 private static extern bool Pac64SetLEDFlashSpeeds(
int Index, byte flashSpeed);
79 [DllImport(
"PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
80 [
return: MarshalAs(UnmanagedType.Bool)]
81 private static extern bool Pac64SetLEDFlashSpeed(
int Index,
int port, byte flashSpeed);
83 [DllImport(
"PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
84 [
return: MarshalAs(UnmanagedType.Bool)]
85 private static extern bool Pac64StartScriptRecording(
int Index);
87 [DllImport(
"PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
88 [
return: MarshalAs(UnmanagedType.Bool)]
89 private static extern bool Pac64StopScriptRecording(
int Index);
91 [DllImport(
"PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
92 [
return: MarshalAs(UnmanagedType.Bool)]
93 private static extern bool Pac64SetScriptStepDelay(
int Index, byte stepDelay);
95 [DllImport(
"PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
96 [
return: MarshalAs(UnmanagedType.Bool)]
97 private static extern bool Pac64RunScript(
int Index);
99 [DllImport(
"PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
100 [
return: MarshalAs(UnmanagedType.Bool)]
101 private static extern bool Pac64ClearFlash(
int Index);
103 [DllImport(
"PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
104 [
return: MarshalAs(UnmanagedType.Bool)]
105 private static extern bool Pac64SetDeviceId(
int Index,
int newId);
107 [DllImport(
"PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
108 private static extern int PacGetDeviceType(
int Index);
110 [DllImport(
"PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
111 private static extern int PacGetVendorId(
int Index);
113 [DllImport(
"PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
114 private static extern int PacGetProductId(
int Index);
116 [DllImport(
"PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
117 private static extern int PacGetVersionNumber(
int Index);
119 [DllImport(
"PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
120 private static extern void PacGetVendorName(
int Index, StringBuilder VendorName);
122 [DllImport(
"PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
123 private static extern void PacGetProductName(
int Index, StringBuilder ProductName);
125 [DllImport(
"PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
126 private static extern void PacGetSerialNumber(
int Index, StringBuilder SerialNumber);
128 [DllImport(
"PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
129 private static extern void PacGetDevicePath(
int Index, StringBuilder DevicePath);
131 [DllImport(
"PacDrive.dll", CallingConvention = CallingConvention.StdCall)]
132 [
return: MarshalAs(UnmanagedType.Bool)]
133 private static extern bool PacProgramUHID(
int Index, StringBuilder FileName);
138 private delegate
void PAC_ATTACHED_CALLBACK(
int Index);
139 private delegate
void PAC_REMOVED_CALLBACK(
int Index);
141 public delegate
void PacAttachedDelegate(
int Index);
142 public delegate
void PacRemovedDelegate(
int Index);
144 public event PacAttachedDelegate OnPacAttached = null;
145 public event PacRemovedDelegate OnPacRemoved = null;
147 [MarshalAs(UnmanagedType.FunctionPtr)]
148 PAC_ATTACHED_CALLBACK PacAttachedCallbackPtr = null;
150 [MarshalAs(UnmanagedType.FunctionPtr)]
151 PAC_REMOVED_CALLBACK PacRemovedCallbackPtr = null;
154 private int m_numDevices = 0;
157 private static readonly Lazy<PacDriveSingleton> lazy =
new Lazy<PacDriveSingleton>(() =>
new PacDriveSingleton());
166 PacAttachedCallbackPtr =
new PAC_ATTACHED_CALLBACK(PacAttachedCallback);
167 PacRemovedCallbackPtr =
new PAC_REMOVED_CALLBACK(PacRemovedCallback);
169 PacSetCallbacks(PacAttachedCallbackPtr, PacRemovedCallbackPtr);
173 private void PacAttachedCallback(
int Index)
177 if (OnPacAttached != null)
178 OnPacAttached(Index);
181 private void PacRemovedCallback(
int Index)
185 if (OnPacRemoved != null)
189 private void Initialize()
191 m_numDevices = PacInitialize();
198 public void Shutdown()
203 #region PacDrive & UHID Led methods
205 public bool PacDriveUHIDSetLEDStates(
int Index, ushort Data)
207 return PacSetLEDStates(Index, Data);
210 public bool PacDriveUHIDSetLEDState(
int Index,
int Port,
bool State)
212 return PacSetLEDState(Index, Port, State);
215 public bool PacDriveUHIDSetLEDStates(
int Index,
bool[] Data)
219 for (
int i = 0; i < Data.Length; i++)
220 if (Data[i]) dataSend |= (ushort)(1 << i);
222 return PacSetLEDStates(Index, dataSend);
226 #region PacLed64 methods
227 public bool PacLed64SetLEDState(
int Index,
int Group,
int Port,
bool State)
229 return Pac64SetLEDState(Index, Group, Port, State);
232 public bool PacLed64SetLEDStates(
int Index,
int Group, byte Data)
234 return Pac64SetLEDStates(Index, Group, Data);
238 public bool PacLed64SetLEDStates(
int Index, byte[] Data)
241 for (
int i = 0; i < 8; i++)
243 OK &= Pac64SetLEDStates(Index, i + 1, Data[i]);
248 public bool PacLed64SetLEDStates(
int Index,
int Group,
bool[] Data)
252 for (
int i = 0; i < Data.Length; i++)
253 if (Data[i]) dataSend |= (byte)(1 << i);
255 return Pac64SetLEDStates(Index, Group, dataSend);
258 public bool PacLed64SetLEDStatesRandom(
int Index)
260 return Pac64SetLEDStatesRandom(Index);
263 public bool PacLed64SetLEDIntensities(
int Index, byte[] Data)
265 return Pac64SetLEDIntensities(Index, Data);
268 public bool PacLed64SetLEDIntensity(
int Index,
int Port, byte Intensity)
270 return Pac64SetLEDIntensity(Index, Port, Intensity);
273 public bool PacLed64SetLEDFadeTime(
int Index, byte FadeTime)
275 return Pac64SetLEDFadeTime(Index, FadeTime);
280 return Pac64SetLEDFlashSpeeds(Index, (byte)FlashSpeed);
285 return Pac64SetLEDFlashSpeed(Index, Port, (byte)FlashSpeed);
288 public bool PacLed64StartScriptRecording(
int Index)
290 return Pac64StartScriptRecording(Index);
293 public bool PacLed64StopScriptRecording(
int Index)
295 return Pac64StopScriptRecording(Index);
298 public bool PacLed64SetScriptStepDelay(
int Index, byte StepDelay)
300 return Pac64SetScriptStepDelay(Index, StepDelay);
303 public bool PacLed64RunScript(
int Index)
305 return Pac64RunScript(Index);
308 public bool PacLed64ClearFlash(
int Index)
310 return Pac64ClearFlash(Index);
313 public bool PacLed64SetDeviceId(
int Index,
int NewId)
315 return Pac64SetDeviceId(Index, NewId);
318 public int PacLed64GetDeviceId(
int Index)
320 string S = GetDevicePath(Index);
323 int IdPos = S.IndexOf(
"&pid_140");
331 if (
int.TryParse(S.Substring(IdPos, 1), out Id))
339 public int PacLed64GetIndexForDeviceId(
int Id)
342 for (
int i = 0; i < NumDevices; i++)
344 if (PacLed64GetDeviceId(i) == Id)
359 public int GetVendorId(
int Index)
361 return PacGetVendorId(Index);
364 public int GetProductId(
int Index)
366 return PacGetProductId(Index);
369 public int GetVersionNumber(
int Index)
371 return PacGetVersionNumber(Index);
374 public string GetVendorName(
int Index)
376 StringBuilder sb =
new StringBuilder(256);
378 PacGetVendorName(Index, sb);
380 return sb.ToString();
383 public string GetProductName(
int Index)
385 StringBuilder sb =
new StringBuilder(256);
387 PacGetProductName(Index, sb);
389 return sb.ToString();
392 public string GetSerialNumber(
int Index)
394 StringBuilder sb =
new StringBuilder(256);
396 PacGetSerialNumber(Index, sb);
398 return sb.ToString();
401 public string GetDevicePath(
int Index)
403 StringBuilder sb =
new StringBuilder(256);
405 PacGetDevicePath(Index, sb);
407 return sb.ToString();
414 public List<int> PacLed64GetIdList()
416 List<int> L =
new List<int>();
419 for (
int i = 0; i < NumDevices; i++)
423 L.Add(PacLed64GetDeviceId(i));
436 public int PacDriveGetIndex()
438 List<int> L =
new List<int>();
441 for (
int i = 0; i < NumDevices; i++)
461 public int NumDevices
463 get {
return m_numDevices; }