1 / 21

T2. Wii remote connection and simple programming

T2. Wii remote connection and simple programming ELEC2807 Integrated Project (InfoE) Wii remote Connection Go to Control Panel > Bluetooth Devices , provided that you have connected and configured your Bluetooth adapter correctly

betty_james
Télécharger la présentation

T2. Wii remote connection and simple programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. T2. Wii remote connection and simple programming ELEC2807 Integrated Project (InfoE)

  2. Wii remote Connection • Go to Control Panel > Bluetooth Devices, provided that you have connected and configured your Bluetooth adapter correctly 1. When you see this window, tick “My device is set up and ready to be found.” and click Next;

  3. Wii remote Connection (Cont.) 2. Press and hold button 1 and 2 of your Wii remote until the connection is done;

  4. Wii remote Connection (Cont.) 3. Select and connect to your Wii remote, note that sometimes the device address will be shown instead of the device name (“Nintendo RVL-CNT-01”);

  5. Wii remote Connection (Cont.) 4. Select “Don’t use a passkey” and next. Release button 1 and 2 after you see the Wii remote appear in your device list.

  6. Preparations • For simplicity, we will use the Managed Library for Nintendo's Wiimote to handle HID data encoding and transfer; • Please download the latest stable version 1.7 from (its URL at: http://wiimotelib.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=21997) • The library is written under .NET framework, meaning that you can write your code in Visual C++, Visual C# or Visual Basic.

  7. Preparations (Cont.) • Here we use Visual C++ for demonstration; • You may want to download Visual C++ 2008 Express Edition from the URL at: (http://www.microsoft.com/Express/VC/)

  8. Get Started • Download the program skeleton template; • Since your lab’s PC already have Visual Studio 2005 installed, we will use VC 2005 today • Open Visual Studio 2005 • Click File > Open > Project / Solution; • Open ‘Wii_Test.sln’.

  9. Include the library • Extract the library archive which we have already downloaded; • Under Solution Explorer, right click on your project; • Click Reference; • Click New Reference in the pop up window;

  10. Include the library (Cont.) • Click on the Browse tab of the Add Reference Popup window; • Search and select WiimoteLib.dll which come with the library.

  11. A simple testing program • Right click on Form1.h > View code to start coding. • Before you start, you may want to add the WiimoteLib namespace. E.g • The Wiimote library provide a WiimoteCollection class for searching Wiimote. Code sample (this part is provided in the template). using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; using namespace WiimoteLib; // Add WiimoteLib namespace Add This Part wmc = gcnew WiimoteCollection(); try { wmc->FindAllWiimotes(); } // Find Wiimote catch (WiimoteNotFoundException^ ex) { MessageBox::Show("Error, Cannot find Wiimote“); Environment::Exit(0); } wm = wmc[0]; // Use the first Wiimote

  12. A simple testing program (Cont.) • Now we get a Wiimote object, the following job is to set it up in 3 steps. • 1. Connect • 2. Set report typeSet the type of data you are interested and the report frequency.Two frequently used report types: ButtonsAccel Button and Accelerometer dataIRAccel Button, Accelerometer and IR data

  13. A simple testing program (Cont.) • 3. Hook up event handlerThe library use a event-driven approach to receive Wii remote data report. The WiimoteChanged event will be fired if the Wiimote state changed. • Code Segment: private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { ... ... wm->Connect(); wm->SetReportType(InputReport::IRAccel, true); wm->WiimoteChanged += gcnew System::EventHandler<WiimoteChangedEventArgs^>(this, &Form1::wm_WiimoteChanged); } Add This Part

  14. A simple testing program (Cont.) • The WiimoteChanged event will include a WiimoteState encapsulated in a WiimoteChangedEventArgs. Here we want to use the data to update our GUI. • Note that we use BeginInvoke() here for GUI update, this can avoid synchronization problem. voidwm_WiimoteChanged(Object^ sender, WiimoteChangedEventArgs^ args) { WiimoteState^ ws = args->WiimoteState; BeginInvoke(gcnewUpdateUI_Delegate(this, &Form1::UpdateUI), ws); } Add This Part

  15. How to read data from Wiimote? • The wm_WiimoteChanged event handler will be invoked when the Wii remote report its status. • WiimoteStateencapsulate all the Wii remote’s current status data, including button state, acceleration data and infra-red signal data.

  16. How to read data from Wiimote? (Cont’) • WiimoteState->ButtonStatecontains information of Wii remote’s button. Press status of button is represented by a boolean value. • E.g. if ButtonState.Upis true , Up button of Wii remote is being pressed

  17. How to read data from Wiimote? (Cont’) • WiimoteState->AccelStatecontains acceleration info. • Normally, we will use AccelState.Values, which is the result acceleration data after calibration. But if you want to use the uncalibrated value, you can use AccelState.RawValues • E.g. If you want to the know the acceleration along X-axis, you can use the value of AccelState.Values.X.

  18. How to read data from Wiimote? (Cont’) • WiimoteState->IRState contains information of infra red signal received by the infra red camera of Wii remote. • One Wii remote can tracks up to 4 infra red signals, so WiimoteState->IRState.IRSensors is an array of four elements • Each IRSensor instance is corresponded to one infra red signal. • If infra red signal can be found, IRSensor.Found will be set to true. If not, it will be set to false. • E.g. If you want to know the absolute position of infra signal of index i, you can use the value of IRState.IRSensors[i].RawPosition • You may also want to use IRState.IRSensors[i].Position, which give you normalized position range between 0.0 to 1.0

  19. A simple testing program (Cont.) • Here we show how to disconnect your Wii remote. • Remember to unhook the event handler before and turn off all LED before disconnecting your Wii remote. protected: ~Form1() { if (components) { delete components; } if (wm) { wm->WiimoteChanged -= gcnew System::EventHandler<WiimoteChangedEventArgs^>(this, &Form1::wm_WiimoteChanged); wm->SetLEDs(0); wm->Disconnect(); } } Add This Part

  20. A simple testing program (Cont.) • The remaining task is to finish the UpdateUI() and UpdateIRPlot() function. private: void UpdateUI(WiimoteState^ ws) { // Battery ... // Button . . . } private: void UpdateIRPlot(IRState is) { } Finish These Two Functions

  21. Hand in • You have to finish and hand in this little Wii remote testing program individually. • Make sure your code can be compiled before submission. Zip the whole project and name the archive file with your U. No. • The submission deadline is 29 Jan, 2010. • If you encounter any difficulties, please send email to work2807@eee.hku.hk

More Related