Jul
01
2009

Athena: Wiimote and Mac OS X

Identifying the Wiimote using HID in Code

If you haven’t already read my entries, Athena: XBox 360 Pad and Mac OS X (Part 1, Part 2, Part 3), then I recommend you do, they explain how to setup the HID Manager to search for devices and connect, so I’ll skip most of that part, and just explain what you’re looking for.

The Vendor ID of the Wiimote is 0×057e.

The Product ID of the Wiimote is 0×0306.

Control the LEDs and Motor

From what the documentation says, one of the first things you should do when you have connected to the Wiimote is set the LEDs. It is a simple two byte long message, one which specifies the message id, and the other which is the LED flags and whether the Wiimote should vibrate.

//
//
#define WIIMOTE_LED_P1 0x10
#define WIIMOTE_LED_P2 0x20
#define WIIMOTE_LED_P3 0x40
#define WIIMOTE_LED_P4 0x80

//
#define WIIMOTE_VIBRATE 0x01
#define WIIMOTE_NOVIBRATE 0x00

//
//
void HIDInput::Wiimote::SetLEDs( IOHIDDeviceRef inDeviceRef, const unsigned int inLEDFlags, const bool inVibrate )
{
//
const unsigned char kFlags = inLEDFlags | ( inVibrate ? WIIMOTE_VIBRATE : WIIMOTE_NOVIBRATE );

//
const unsigned char kReportType = 0x11;
const unsigned char kReportSize = 2;

const unsigned char kReportData[kReportSize] =
{
kReportType,
kFlags
};

//
DeviceSendReport( inDeviceRef, kReportType, kReportData, kReportSize );
}

As you can see its really simple just setting this up, it is probably the easy thing to get working. A strange thing about the Wiimote is that all messages that you send it can control the motor, and it is always bit 0 on the first byte that controls whether it is on or not.

You may also receive a message back from the Wiimote where the first byte is 0×22, this is a command status report, letting you know if what you sent was valid and accepted by the Wiimote.

The first two bytes contains data on the buttons, which I’ll go into next, for the moment though it is sent to a function to extract that information since it is quite a common thing with other messages as well.

If the fourth byte of this message determines whether there was an error or not, if the value is 0×00, the message was acknowledged and is okay. The third byte tells us which report we sent that has the error.

//
//
void HIDInput::Wiimote::DeviceIOHIDReportCallback( const unsigned char * inReportData, const unsigned int inSize )
{
switch( inReportData[0] )
{
// Status Report
case 0x22:
{
//
GetButtonData( &inReport[1] );

//
if( inReport[4] == 0x00 )
{
printf( "Sent Report '0x%02x' was acknowledged.\n", inReport[3] );
}
else
{
printf( "Sent Report '0x%02x' has an error.\n", inReport[3] );
}
}
break;

default:
break;
}
}

Not all reports sent to the Wiimote get this response, but the ones it is relevant to are the only ones you really need it for.

[Previous Page...] [Next Page...]

Written by Kaluriel in: Athena, Code, Tutorials | Tags: , , , , , , , ,

Pages: 1 2 3 4


No Comments »

RSS feed for comments on this post. TrackBack URL

Leave a comment

You must be logged in to post a comment.

Theme: TheBuckmaker.com Blog Themes | The best Webhosting Plans, Eigenes Internet Radio