与Wii控制手柄通信的托管代码库(二)

来源:互联网 发布:广电网络辽中分公司 编辑:程序博客网 时间:2024/06/05 05:34

第7章       使用API

如果不关心这些实现细节,可以直接跳转到本章,学习如何在你自己的应用程序中使用这些API。最简单的方法是通过源代码中带有的WiimoteTest 应用程序来了解如何实现这些工作。

首先在源代码中添加WiimoteLib.dll 引用。然后,在应用程序中使用using/Imports 声明相关的命名空间。之后,就可以创建和使用一个Wiimote类。简单的初始化一个新的Wiimote类实例, 设置相应的事件处理以及希望返回数据的报文类型,并调用Connect方法。

VB

Imports WiimoteLib

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
EventArgs)
   
' create a new instance of the Wiimote

   
Dim wm As Wiimote = New Wiimote()

   
' setup the event to handle state changes

   
AddHandler wm.WiimoteChanged, AddressOf wm_WiimoteChanged

   
' setup the event to handle insertion/removal of extensions

   
AddHandler wm.WiimoteExtensionChanged, AddressOf wm_WiimoteExtensionChanged

   
' connect to the Wiimote

    wm.Connect()

   
' set the report type to return the IR sensor and accelerometer data (buttons always come back)
    wm.SetReportType(Wiimote.InputReport.IRAccel,
True)
End Sub



Private Sub wm_WiimoteExtensionChanged(ByVal sender As Object, ByVal args As WiimoteExtensionChangedEventArgs)
   
If args.Inserted Then

        wm.SetReportType(Wiimote.InputReport.IRExtensionAccel,
True) ' return extension data
   
Else
        wm.SetReportType(Wiimote.InputReport.IRAccel,
True) ' back to original mode
   
End If
End Sub

Private Sub wm_OnWiimoteChanged(ByVal sender As Object, ByVal args As WiimoteChangedEventArgs)
   
' current state information

   
Dim ws As WiimoteState = args.WiimoteState

   
' write out the state of the A button   

    Debug.WriteLine(ws.ButtonState.A)
End Sub

C#

using WiimoteLib;

private void Form1_Load(object
sender, EventArgs e)
{
   
// create a new instance of the Wiimote

    Wiimote wm =
new Wiimote();
   
   
// setup the event to handle state changes

    wm.WiimoteChanged += wm_WiimoteChanged;
   
   
// setup the event to handle insertion/removal of extensions
    wm.WiimoteExtensionChanged += wm_WiimoteExtensionChanged;
   
   
// connect to the Wiimote
    wm.Connect();
   
   
// set the report type to return the IR sensor and accelerometer data (buttons always come back)
    wm.SetReportType(Wiimote.InputReport.IRAccel,
true);
}


void wm_WiimoteExtensionChanged(object
sender, WiimoteExtensionChangedEventArgs args)
{
   
if
(args.Inserted)
        wm.SetReportType(Wiimote.InputReport.IRExtensionAccel,
true);    // return extension data

   
else
        wm.SetReportType(Wiimote.InputReport.IRAccel,
true);            // back to original mode
}

void wm_WiimoteChanged(object sender, WiimoteChangedEventArgs args)
{
   
// current state information

    WiimoteState ws = args.WiimoteState;

   
// write out the state of the A button   
    Debug.WriteLine(ws.ButtonState.A);
}

如果使用多个Wiimote,创建一个WiimoteCollection 对象,调用FindAllWiimotes方法进行初始化,对集合中的每个Wiimote对象作为一个单独的实例进行使用。

VB

Dim wc As New WiimoteCollection()
wc.FindAllWiimotes()

For Each wm As Wiimote In
wc
   
AddHandler wm.WiimoteChanged, AddressOf
wm_WiimoteChanged
   
AddHandler wm.WiimoteExtensionChanged, AddressOf
wm_WiimoteExtensionChanged
   
    wm.Connect()
    wm.SetReportType(InputReport.IRAccel,
True
)
Next wm

 

C#

WiimoteCollection wc = new WiimoteCollection();
wc.FindAllWiimotes();

foreach(Wiimote wm in
wc)
{
    wm.WiimoteChanged += wm_WiimoteChanged;
    wm.WiimoteExtensionChanged += wm_WiimoteExtensionChanged;
   
    wm.Connect();
    wm.SetReportType(InputReport.IRAccel,
true
);
}

 

有两种方式从API中取得数据:事件和轮询。在事件模式中,上面所示,首先订阅WiimoteChanged 事件。然后,当数据从Wiimote发送到PC时,一个事件对象将发送到应用程序中的事件处理器中。如果不使用事件模型,可以简单的在任何时刻从Wiimote类的WiimoteState属性中取得状态信息。

报文类型

目前托管库仅支持Wiimote报文中有限的几种类型, 不过现在实现的报文类型已经能够返回所有必要的数据,以及目前的扩展信息了。这些报文类型包括:

·    Buttons仅按钮数据

·    ButtonsAccel按钮和加速度传感器数据

·    IRAccel按钮,加速度传感器和IR数据

·    ButtonsExtension按钮和扩展数据

·    ExtensionAccel -按钮,加速度传感器和扩展数据

·    IRExtensionAccel -按钮,加速度传感器,IR和扩展数据

报文类型可以通过SetReportType 方法设置,使用不同的报文类型,决定是否不停的发送数据,或只在控制器状态的改变时得到报文。

第8章       扩展

目前支持三种Wii扩展:Nunchuk, Classic ControllerGuitar Hero controller。如果希望使用这些扩展, 需要设置WiimoteExtensionChanged的事件处理器。当该事件被调用,可以通过检查事件变量确定是否有扩展插入或移除,以及插入扩展的类型。在事件处理器中,需要使用SetReportType改为支持扩展数据的报文类型,否则扩展数据不会返回。

如果使用一种严格的轮询操作,应当检查WiimoteState 属性中的Extension ExtensionType参数,以确定扩展模块的插入和移除。

平衡板被视为一种带有扩展功能的Wiimote控制器。报文类型为内部设置,对该设备的任何报文类型改变将被忽略。平衡板的开关按钮对应WiimoteA按钮, LED对应WiimoteLED1。其它Wiimote属性被忽略。其它平衡板的信息在WiimoteState 对象中的BalanceBoard 结构中。

第9章       帮助信息

整个库的核心在WiimoteState对象中。在下载包中的chm帮助文件中可以查找全部可用的属性。 

第10章       将来的工作

目前为止,还不能支持Wiimote中的扬声器。我有可能在将来的更新中增加这些功能。另外还有几种报文类型没有实现,不过现在实现的报文类型已经能够返回所有必要的信息了。我还希望能够添加一个“高层”的功能,例如返回倾斜/翻转角度,IR传感器的鼠标位置等。留意本文和我的博客中关于本库的更新信息。

第11章       结论

到此,我们已经达到了目的。一个全功能的Wiimote托管代码库。尝试一下,将它集成到你现有的应用程序中,或者做些全新的东西!我期待你能够利用它进行创造性的应用...

如果对库的使用有任何问题,或者有其它的功能需求(声音除外),可以直接联系我或在这个项目的专有论坛上发贴。

第12章       链接

·    WiiLi Wiki

·    WiiBrew Wiki

·    Making USB C# Friendly

·    P/Invoke Wiki

·    Applications Using This Library

第13章       作者介绍

Brian是一名 Microsoft C# MVP,自从2000.NET发布早期测试版以来一直致力于.NET的开发,他在利用微软的技术和平台提供解决方案方面有更长的历史。除了.NETBrian在各种CPU上的C, C++和汇编语言方面也有深入的研究。他精通很多种技术, 包括WEB开发,文档图像,GIS, 图形,游戏开发和硬件接口。 Brian在卫生保健行业的应用开发有很深厚的背景,提供多种便携设备解决方案,包括平板电脑和PDA。此外,他还是New Riders出版社出版的"深入 ASP.NET"的合作者,目前正在合著一本名为"程序员,爱好者和游戏开发人员的10.NET Coding4Fun项目 "的书籍,该书将于2008年由 O'Reilly出版社出版。 Brian同时为 MSDNCoding4Fun 网站按月提交文章。

http://blogs.msdn.com/coding4fun/archive/2007/03/14/1879033.aspx

原创粉丝点击