210 likes | 808 Views
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
E N D
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 1. When you see this window, tick “My device is set up and ready to be found.” and click Next;
Wii remote Connection (Cont.) 2. Press and hold button 1 and 2 of your Wii remote until the connection is done;
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”);
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.
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.
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/)
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’.
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;
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.
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
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
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
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
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.
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
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.
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
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
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
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