GithubHelp home page GithubHelp logo

ryanmcgrath / wii-js Goto Github PK

View Code? Open in Web Editor NEW
156.0 7.0 12.0 956 KB

A sane, documented, (hopefully) performant event-based library for Wiimote webpage interaction.

Home Page: http://venodesigns.net/wii/

License: MIT License

Python 4.42% JavaScript 95.58%

wii-js's Introduction

wii-js

The Nintendo Wii is an entertainment system with an utterly massive install base, and when you couple it with the fact that it's got a web browser (mostly) built in, there's a lot of potential for third party development. Sadly, few have opted to do any sort of development for it. While it doesn't help that Nintendo pretty much dropped the ball on this opportunity, the experience of browsing the web on the Wii isn't actually that compelling to begin with.

That said, I think this can serve one other purpose: it's an ideal environment to teach children how to program! I created this library to sanitize Wii interaction with webpages in the browser, as it's notoriously crippled. It aims to offer a solid, documented, performant API that's easy to understand and pick up. With this library, you can have up to 4 Wii-motes interacting with your webpage at once, a dynamic not found in other web browsing mediums.

You can find a built source file and a minified source file for production use in the /js/ directory. To play with a live example, load up the demo (index.html) on your own server, or feel free to use mine:

wii-js Demo: http://venodesigns.net/wii/

Working with the Wii's browser can be odd - it has moderately good support for CSS, so you're never really as bad off as you'd be with a version of Internet Explorer - that said, if you're looking for a good read on what's supported, check out this article on Opera Wii supported technologies.

Questions, comments, criticism and praise can be directed to me at the following outlets:

Example Usage

var wiimote = new Wii.Remote(1, {horizontal: true}),
    wiimote2 = new Wii.Remote(2, {horizontal: true});

wiimote.when('pressed_a', function() {
    alert('Wiimote #1 pressed the A Button!');
});

wiimote2.when('pressed_a', function() {
	alert('Wiimote #2 pressed the A Button!');
});

Wii.listen();

Technical Documentation

The largest issue with making interactive pages that work with the Wii has been that the API has been nothing short of a black hole. When you actually begin to dig in and figure out what's going on, it gets even uglier to see - the primary wiimote, for instance, has a totally different set of signals than the other three.

wii-js abstracts away most of these differences and/or problems, and works on a very simple event-dispatch system. What this means is that you create an instance of a Wii Remote, subscribe to events, and provide a function to get called when that event has occurred. The following syntax should explain this:

wiimote.when('event_name_here', function() { /* My callback function */ });

When instantiating a Wii Remote instance, you can choose to have the library interpret directional pad controls in horizontal or vertical mode. You can change this at any point, if you want, by simply swapping the property.

var wiimote = new Wii.Remote(1, {horizontal: true}); // Horizontal controls
var wiimote = new Wii.Remote(1, {horizontal: false}); // Vertical controls

wiimote.opts.horizontal = true; // Change to horizontal scheme.

The final important piece is to start the Wii-event loop; this manages the event dispatcher internally. To do this, simply...

Wii.listen();

You can listen for the following events on all controllers:

  • pressed_up - The up button was pressed.
  • pressed_down - The down button was pressed.
  • pressed_left - The left button was pressed.
  • pressed_right - The right button was pressed.
  • pressed_a - The A button was pressed.
  • pressed_1 - The 1 button was pressed. (Note: On controller 1, this triggers a menu - work in progress...)
  • pressed_2 - The 2 button was pressed.
  • pressed_plus - The plus (+) button was pressed.
  • pressed_minus - The minus (-) button was pressed.
  • roll_change - The roll of the controller (balance) changed. You can get the current roll in radians with "this.roll"; positive is upright, negative is the other.
  • distance_change - The distance of the wiimote (in meters) from the TV/sensor bar has changed. This event isn't totally reliable, but should work for most cases.

You can listen for the following events on extra controllers, but not the primary controller.

  • pressed_b - The B button was pressed.
  • pressed_c - The C button (on the Nunchuk) was pressed.
  • pressed_z - The Z button (on the Nunchuk) was pressed.

You can also get the following properties from any Wii Remote instance; they will return "undefined" if the remote isn't able to see the TV/sensor bar, so be sure to check this!

  • x - The x coordinate where the Wii Remote is pointing to on the screen. Generally between 0 and 800, but can be more on wide pages.
  • y - The y coordinate where the Wii Remote is pointing to on the screen. Odd one; can be found as low as -48, as high as the height of the current webpage + toolbar height, if enabled. Tinker with this one for your purposes.

Extra Tips and Tricks (Debugging)

One semi-useful trick to point out about this library is that each of your callback functions get passed two arguments by default - a reference to the Wiimote you're working with, and the raw Wiimote status object that the Wii reports back to the library. You get this in a normalized fashion, instead of having to deal with the odd polling issues present in the browser.

var wiimote = new Wii.Remote(1, {horizontal: false});

wiimote.when('pressed_a', function(wii_remote, wii_remote_status) {
	/*	Alert an internal confidence level provided by the Wii. */
	alert(wii_remote_status.dpdValidity);
});

Debugging Javascript on the Wii is also nothing short of incredibly annoying, so I've made some efforts to patch this up and make life a bit easier. My typical debugging strategy with any Wii-related code would always start with the following. The first thing to do is set the Wii listener to run in debug mode, like so:

Wii.listen({debug: true});

With this set, you can log errors with any of the following functions. error can be a string or a complex object.

  • console.log(error); - Tried and true, now supported.
  • console.debug(error); - Same as console.log here, but syntax is supported.
  • throw new Error(error); - Throw them, they'll be logged.
  • Wii.util.debug(error); - The core function that handles logging internally.

If the typical Wii debugging flow isn't enough for you, go aggressive with this - just be aware that you can crash the Wii's browser if you're using try/catch all over the place, as it's not cheap in Javascript.

try {
    // Whatever function to execute
} catch(e) { Wii.util.debug(e); }

Why the button limitations?

The Nintendo Wii treats the primary controller differently than the other ones, and doesn't report any action from the Nunchuk, nor does it report when someone has pressed the "B" button on the primary controller (as it's used for scrolling a page).

The Wii Browser also doesn't report data for Gamecube controllers, the Classic controller, or any other accessories.

It's a work in progress to see what can be done about these, but it's impossible to guarantee anything will come out of it unless Nintendo and/or Opera can come out with something new.

Known Issues

Primary Wiimote is a bit more responsive than the extra 3
This library works by polling the status of the three extra Wii-remotes in 100ms intervals and dispatching events based on this. Anything lower than 100ms causes the Wii to run into memory limitations, and the single-threaded nature of the browser doesn't really help this issue.

The primary Wii-remote uses an odd combination of DOM-esque callbacks; due to this, it reports more frequently than the other Wii-remotes. It's not a showstopper by any means, but for small games it would in theory give a weighted advantage. I'll probably end up throttling this through the library by means of a flag, e.g "game_mode": true in the initial options.

Todo List

  • Build in functionality for multiple button presses at the same time (difficult to get right in this environment)
  • Determine canceling B-button/scrolling on pages ("app"/"game" style)
  • Determine feasibility of canceling out "1" press on the primary Wii-remote.

Building and Developing

If you'd like to help with this library, you're more than welcome to. Simply fork it on GitHub, work away, then issue me a pull request. I generally respond within 24 hours.

The build system here is pretty simple - edits and changes go into the /js/src/ files, and you can run

python build.py  

from the main directory to build a new version. The minifier here is YUI; Closure/UglifyJS are more aggressive, and for some reason throw ridiculous issues in the Wii's browser that I've been unable to track down (and I don't have more time to throw at it).

In short, the builds require Python/Java, but once you've got them all installed you should only need the command above.

How is this different from...?

I sadly did not find out about wii.js until after I released this library; with respect to the original author, his work only covers the primary Wii Remote and not the extra ones, nor has it been updated in years. While his approach appears to be the same as mine (or mine the same as his), neither one influenced the other, and they're totally separate works.

With the exception of wii.js, I do not know of any other (remaining) Wii interaction Javascript libraries. It's for these reasons (and my desire for a simpler API) that I built this. ;)

Licensing, etc

wii-js is released under an MIT license. Just provide credit where need be if you choose to use this, it's taken quite a bit of my time to decipher the utterly random pieces and intricacies of this Javascript engine. ;)

wii-js's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

wii-js's Issues

Getting error on Chrome

Found this interesting project, when I trying to run the demo, I'm getting with the latest Chrome on Macos, Wii connected to bluetooth:

Error with Permissions-Policy header: Unrecognized feature: 'interest-cohort'.
(index):73 Uncaught ReferenceError: Wii is not defined
at (index):73
(anonymous) @ (index):73

Would be really cool to use Wiimote acceleration data as an input to machine learning project.

Detect when a button is released

Hello,

Thanks for this library. Seems great!

I do not have a Nintendo Wii right now with me so I cannot test anything. But reading the documentation and looking at the source code I do not see a way to check whether a button has been released or not. This is very important for games. I am developing a game engine framework that will probably use your library to support Wii if you do not mind (showing your name in the credits, of course).

The events are named "pressed_a", etc. but what about "released_a", etc.?

I need a way to access to every controller's object at any time and be able to check whether a chosen button (or even more than one) is being pressed or not. I am not sure, but I guess the "hold" property (from the "official" Wii JS API), which is a bitmask, could be useful here. But I cannot even find the official documentation about using JavaScript with the old Wii console (I can only find it about the new Wii U).

My idea is to have the following array (just a simplified example):

var wiimotes =
[
    {
        horizontal: true, //Are there any way to detect when this changes? I want to use horizontal by default.
        UP: true, //"true" means the button is being pressed.
        DOWN: false,
        LEFT: false,
        RIGHT: false,
        A: false,
        B: undefined, //"undefined" means the button status cannot be read.
        C: undefined,
        Z: undefined,
        "1": false,
        "2": false,
        "PLUS": false,
        "MINUS": false,
        roll: float?,
        distance: float?,
        x: float?,
        y: float?,
        ... //Maybe more properties.
    },
    ... //Three more objects with the same structure (because the total of possible controllers is four).
];

That array just holds all the possible four controllers as an object and every of those objects holds the status of the corresponding buttons. In the beginning, all values should be set to false/undefined. Now I want to use your library to update the array using the following way:

//Updating array for Wiimote 1:
var wiimote1 = new Wii.Remote(1, {horizontal: true});
wiimote1.when("pressed_up", function(wii_remote, wii_remote_status) { wiimotes[0].UP = true; }); //"UP" pressed.
wiimote1.when("pressed_down", function(wii_remote, wii_remote_status) { wiimotes[0].DOWN = true; }); //"DOWN" pressed.
//... (more events that would update the status of the rest of the buttons).

//... To update the array for the rest of the Wiimotes (2, 3 and 4), just proceed the same way.

Obviously, the code above could be in a loop for the four controllers. That is not important now.

Please, could you tell me how I could use your library to update the values of that array and set the buttons to "false" when the button is released?

I am sorry about the long message. Thank you very much for all!

Regards,
Joan

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.