Gmail Didier Girard Remote Wii Didier Girard Fri, Aug 24, 2007 at 3:54 PM To: Didier Girard Wii Remote API 08.14.07 Tags: API JavaScript Opera Wii The folks over at the Opera Developer Community recently released an article on the Wii remote API. Fairly soon after the article was published it was replaced with an error reading 'Access denied.' Maybe they were not supposed to release this information and Nintendo made them pull the plug. Maybe not. Yet, they still haven't restricted access to the example pages (1, 2, 3, 4). If you look at the source you will see the API in action. The API is intended for use the Wii's Internet Browser. It allows web developers the option to develop a site with added Wii remote (a.k.a. Wiimote) functionality. Using JavaScript you can tell what buttons they press, if the remote is or is not enabled, if the remote is the primary, etc. Here's the API: isEnabled Purpose: Determines if the remote is currently enabled. Returns: Integer 1 - Enabled 0 - Disabled or unavailable Note: The remote appears as disabled if unavailable. isDataValid Purpose: Determines if data is successfully read from the remote. Returns: Integer 1 - Successful 0 - Unsuccessful Note: Data may not be successfully read due to devices being turned on or off, being connected or disconnected or while accessing the Home Menu. isBrowsing Purpose: Determines if the given remote is the primary remote. Returns: Integer 1 - Primary 0 - Not primary Note: This will always be the remote connected with the lowest connection number (seen by the blue square on the bottom of the remote). The primary remote is able to navigate using the Internet toolbar. In other words, the primary remote is the remote being used for browsing. dpdScreenX Purpose: Retrieves the horizontal position of the remote cursor. Returns: Integer Note: The value is normally between 0 and 800, unless if the page is scrolled. If the remote is pointed outside the page and toolbar the value will be returned as undefined. dpdScreenY Purpose: Retrieves the vertical position of the remote cursor. Returns: Integer Note: This works the same as the JavaScript function event.pageX using a mouseover event. If the remote is pointed outside the page and toolbar the value will be returned as undefined. dpdX Purpose: Retrieves the horizontal position of the remote cursor. Returns: Float 1 - Right side 0 - Center -1 - Left side Note: The maximum values change based dpdX Purpose: Retrieves the horizontal position of the remote cursor. Returns: Float 1 - Right 0 - Center -1 - Left Note: The maximum values change based on how far the user is from the screen. dpdY Purpose: Retrieves the vertical position of the remote cursor. Returns: Float 1 - Bottom 0 - Center -1 - Top Note: The maximum values change based on how far the user is from the screen. hold Purpose: Determine which buttons are being held down on a (non-primary) remote. Returns: Integer (bitmask) 1 - Left 2 - Right 4 - Down 8 - Up 16 - Plus 256 - 2 512 - 1 1024 - B 2048 - A 4096 - Minus 8192 - Z 16384 - C Note: This function does not work with a primary remote. Primary remotes will always return 0. Use normal JavaScript key events to determine which buttons are held on a primary remote. Z and C can not be detected on the primary remote (most likely because they are located on the nunchuck). dpdRollX Purpose: Retrieves the cosine of the rotation of the remote (rotation along the X axis). Returns: Float -1 - Upside down 1 - Right-side up Note: To find the degree in radians use Math.atan2(KpadStatus.dpdRollY,KpadStatus.dpdRollX). dpdRollY Purpose: Retrieves the sine of the rotation of the remote (rotation along the X axis). Returns: Float -1 - Rotated left 1 - Rotated right Note: To find the degree in radians use Math.atan2(KpadStatus.dpdRollY,KpadStatus.dpdRollX). dpdDistance Purpose: Retrieves the distance of the user from the remote sensor in meters. Returns: Float Note: Distance should be between 0.55 and 3. dpdValidity Purpose: Determines the validity of data transferred based on a data sample. Returns: Integer 2 - Good 1 - Poor 0 - Invalid -1 - Very poor -2 - Extremely poor Note: If the remote is not directed towards the screen it will return invalid. Speed of movement can affect this value. To use the API you will have to reference Opera. Make sure you use the following IF statement to ensure that the browser is Opera and that the remote is supported. if ( window.opera && opera.wiiremote ) { } The next step is to set a JavaScript variable to the KpadStatus object for the given remote. In this example, update(1) is retrieving the KpadStatus object of the second control (via an array). var remote; if ( window.opera && opera.wiiremote ) { remote = opear.wiiremote.update(1); } Now, let's say we just want to initiate a JavaScript alert if the third remote is enabled. The code would look something like this: var remote; if ( window.opera && opera.wiiremote ) { remote = opear.wiiremote.update(2); if ( remote.isEnabled ) { alert( 'Remote 3 is enabled' ); } } If we want to take it a step further we can figure out what buttons the user is pressing on the third remote using the bitwise AND operator (&). var remote, buttons = {}; if ( window.opera && opera.wiiremote ) { remote = opear.wiiremote.update(2); if ( remote.isEnabled ) { buttons.Left = remote.hold & 1; buttons.Right = remote.hold & 2; buttons.Up = remote.hold & 4; buttons.Down = remote.hold & 8; buttons.Plus = remote.hold & 16; buttons.2 = remote.hold & 256; buttons.1 = remote.hold & 512; buttons.B = remote.hold & 1024; buttons.A = remote.hold & 2048; buttons.Minus = remote.hold & 4096; buttons.Z = remote.hold & 8192; buttons.C = remote.hold & 16384; } } -- Didier http://blog.dgirard.eu http://www.application-servers.com http://www.onGWT.com