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);
240 for (
int i = 0; i < 8; i++)
242 OK&=Pac64SetLEDStates(Index,i+1,Data[i]);
247 public bool PacLed64SetLEDStates(
int Index,
int Group,
bool[] Data)
251 for (
int i = 0; i < Data.Length; i++)
252 if (Data[i]) dataSend |= (byte)(1 << i);
254 return Pac64SetLEDStates(Index, Group, dataSend);
257 public bool PacLed64SetLEDStatesRandom(
int Index)
259 return Pac64SetLEDStatesRandom(Index);
262 public bool PacLed64SetLEDIntensities(
int Index, byte[] Data)
264 return Pac64SetLEDIntensities(Index, Data);
267 public bool PacLed64SetLEDIntensity(
int Index,
int Port, byte Intensity)
269 return Pac64SetLEDIntensity(Index, Port, Intensity);
272 public bool PacLed64SetLEDFadeTime(
int Index, byte FadeTime)
274 return Pac64SetLEDFadeTime(Index, FadeTime);
279 return Pac64SetLEDFlashSpeeds(Index, (byte)FlashSpeed);
284 return Pac64SetLEDFlashSpeed(Index, Port, (byte)FlashSpeed);
287 public bool PacLed64StartScriptRecording(
int Index)
289 return Pac64StartScriptRecording(Index);
292 public bool PacLed64StopScriptRecording(
int Index)
294 return Pac64StopScriptRecording(Index);
297 public bool PacLed64SetScriptStepDelay(
int Index, byte StepDelay)
299 return Pac64SetScriptStepDelay(Index, StepDelay);
302 public bool PacLed64RunScript(
int Index)
304 return Pac64RunScript(Index);
307 public bool PacLed64ClearFlash(
int Index)
309 return Pac64ClearFlash(Index);
312 public bool PacLed64SetDeviceId(
int Index,
int NewId)
314 return Pac64SetDeviceId(Index, NewId);
317 public int PacLed64GetDeviceId(
int Index)
319 string S = GetDevicePath(Index);
322 int IdPos = S.IndexOf(
"&pid_140");
328 int.TryParse(S.Substring(IdPos,1), out Id);
333 public int PacLed64GetIndexForDeviceId(
int Id)
335 for (
int i = 0; i < NumDevices; i++)
337 if (PacLed64GetDeviceId(i) == Id)
352 public int GetVendorId(
int Index)
354 return PacGetVendorId(Index);
357 public int GetProductId(
int Index)
359 return PacGetProductId(Index);
362 public int GetVersionNumber(
int Index)
364 return PacGetVersionNumber(Index);
367 public string GetVendorName(
int Index)
369 StringBuilder sb =
new StringBuilder(256);
371 PacGetVendorName(Index, sb);
373 return sb.ToString();
376 public string GetProductName(
int Index)
378 StringBuilder sb =
new StringBuilder(256);
380 PacGetProductName(Index, sb);
382 return sb.ToString();
385 public string GetSerialNumber(
int Index)
387 StringBuilder sb =
new StringBuilder(256);
389 PacGetSerialNumber(Index, sb);
391 return sb.ToString();
394 public string GetDevicePath(
int Index)
396 StringBuilder sb =
new StringBuilder(256);
398 PacGetDevicePath(Index, sb);
400 return sb.ToString();
407 public List<int> PacLed64GetIdList()
409 List<int> L =
new List<int>();
412 for (
int i = 0; i < NumDevices; i++)
416 L.Add(PacLed64GetDeviceId(i));
431 public int NumDevices
433 get {
return m_numDevices; }