Interesting/Wii | Posted by hyena0 2008. 7. 17. 00:37

Wiimote Library 분석 (1)




Wiimote Library

Wiimote Library 내용을 알아야 응용프로그램을 만들 수 있기 때문에 라이브러리 내용을 살펴보도록 하겠다.

위모트 라이브러리는 C#으로 작성되어 있고, Wiimote.cs, HIDImports.cs, Events.cs, DataTypes.cs 네가지 파일로 구성된다.

Wiimote.cs는 wiimote class가 있고 위모트를 연결하고 동작하기 위한 Method 들을 포함하고 있어서 , 윈도우프로그램에서 초기화와 데이터입출력을 처리하는 부분이 된다.
HIDImports.cs는 인식된 위모트를 연결해주는 역할을 하고, Events.cs는 이벤트 처리에 해당하는 부분이고, DataTypes.cs는 데이터처리에 필요한 구조체들을 정의하고 있다.  

각 해당 파일에 대한 클래스와 메소드를 정리해 보았다.

------------------------
*Wiimote.cs

namespace WiimoteLib
{
public class Wiimote : IDisposable
{
public Wiimote();
public void Connect();
public void Disconnect();
private void BeginAsyncRead();
private void OnReadData(IAsyncResult ar);
private bool ParseInput(byte[] buff);
private void InitializeExtension();
private byte[] DecryptBuffer(byte[] buff);
private void ParseButtons(byte[] buff);
private void ParseAccel(byte[] buff);
private void ParseIR(byte[] buff);
private void ParseExtension(byte[] buff, int offset);
private void ParseReadData(byte[] buff);
private byte GetRumbleBit();
public void SetReportType(InputReport type, bool continuous);
public void SetLEDs(bool led1, bool led2, bool led3, bool led4);
public void SetLEDs(int leds);
public void SetRumble(bool on);
public void GetStatus();
private void EnableIR(IRMode mode);
private void DisableIR();
private void ClearReport();
private void WriteReport();
public byte[] ReadData(int address, short size);
public void WriteData(int address, byte data);
public void WriteData(int address, byte size, byte[] buff);
}
}
---------------------------
* HIDImports.cs

class HIDImports
{
[DllImport("hid.dll")]
public static extern void HidD_GetHidGuid(out Guid gHid);
public static extern Boolean HidD_GetAttributes(IntPtr HidDeviceObject, ref HIDD_ATTRIBUTES Attributes);
internal extern static bool HidD_SetOutputReport(
   IntPtr HidDeviceObject,
   byte[] lpReportBuffer,
   uint ReportBufferLength);
[DllImport(@"setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr SetupDiGetClassDevs(
   ref Guid ClassGuid,
   [MarshalAs(UnmanagedType.LPTStr)] string Enumerator,
   IntPtr hwndParent,
   UInt32 Flags
   );
public static extern Boolean SetupDiEnumDeviceInterfaces(
   IntPtr hDevInfo,
   //ref SP_DEVINFO_DATA devInfo,
   IntPtr devInvo,
   ref Guid interfaceClassGuid,
   Int32 memberIndex,
   ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData
  );
public static extern UInt16 SetupDiDestroyDeviceInfoList( IntPtr hDevInfo );

[DllImport(@"setupapi.dll", SetLastError = true)]
public static extern Boolean SetupDiGetDeviceInterfaceDetail(
   IntPtr hDevInfo,
   ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData,
   IntPtr deviceInterfaceDetailData,
   UInt32 deviceInterfaceDetailDataSize,
   out UInt32 requiredSize,
   IntPtr deviceInfoData
  );
public static extern Boolean SetupDiGetDeviceInterfaceDetail(
   IntPtr hDevInfo,
   ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData,
   ref SP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData,
   UInt32 deviceInterfaceDetailDataSize,
   out UInt32 requiredSize,
   IntPtr deviceInfoData
  );

[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern SafeFileHandle CreateFile(
   string fileName,
   [MarshalAs(UnmanagedType.U4)] FileAccess fileAccess,
   [MarshalAs(UnmanagedType.U4)] FileShare fileShare,
   IntPtr securityAttributes,
   [MarshalAs(UnmanagedType.U4)] FileMode creationDisposition,
   [MarshalAs(UnmanagedType.U4)] EFileAttributes flags,
   IntPtr template);

[DllImport("kernel32.dll", SetLastError=true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CloseHandle(IntPtr hObject);
}
---------------------------
* Events.cs

public delegate void WiimoteChangedEventHandler(object sender, WiimoteChangedEventArgs args);
public delegate void WiimoteExtensionChangedEventHandler(object sender, WiimoteExtensionChangedEventArgs args);
public class WiimoteExtensionChangedEventArgs: EventArgs
public class WiimoteChangedEventArgs: EventArgs
{
public WiimoteExtensionChangedEventArgs(ExtensionType type, bool inserted);
}
public class WiimoteChangedEventArgs: EventArgs
{
public WiimoteChangedEventArgs(WiimoteState ws);
}
---------------------------
*DataTypes.cs

public class WiimoteState
public struct LEDState
public struct NunchukCalibrationInfo
public struct ClassicControllerCalibrationInfo
public struct NunchukState
public struct ClassicControllerButtonState
public struct ClassicControllerState
public struct IRState
public struct AccelState
public struct AccelCalibrationInfo
public struct ButtonState
public enum ExtensionType : byte
public enum IRMode : byte
---------------------------