WiiMote:
Filter:
Classes | External Control

WiiMote : Object

use the Nintendo (tm) Wii Remote
Source: WII.sc

Description

The WiiMote class allows you to access the WiiMote from SuperCollider, both to receive data from the device, as well as send data to the device.

Some Important Issues Regarding WiiMote

This class has been developed to work both on the Mac and on Linux. The interface is mostly the same, but there are some usage issues on the Mac. Personally, I found that it works better with an external BlueTooth receiver, than with the internal one (tested on the MacBook Pro). I also found that on the Mac, I have to connect, disconnect and then reconnect to get everything to work properly.

The IR options, as well as the Classic controller have not been tested (due to lack of access to either complementary device by the developer).

This class is not implemented for Windows, and thus will not work on that platform.

Class Methods

WiiMote.new(id)

NOTE: Should not be called directly. See *discover.

WiiMote.start(updtime: 50)

Starts the eventloop. Called automatically by *discover, so no real need to call this method.

Arguments:

updtime

updatetime of the eventloop in milliseconds.

WiiMote.discover

Discovers a new device. This calls for the creation of a new device and class instance by calling the method *new. ( new should not be called directly). This method is synchronous, and will block until a device is found or until it times out.

When discover is called, the buttons 1 and 2 on the Wii Remote should be pushed to put the device in discovery mode.

Returns:

A new WiiMote object for the device discovered, or nil if no device was discovered.

Discussion:

Example to start up:

w = WiiMote.discover;   // discover a new device
w.battery;              // post the battery status of the device

// cleanup:
WiiMote.closeAll;       // close all devices
WiiMote.stop;

WiiMote.all

Returns an Array with all WiiMote devices.

WiiMote.closeAll

Close all WiiMote devices.

WiiMote.stop

Stops the eventloop. Only really necessary on Mac, but use it for cross platform robustness.

Inherited class methods

Undocumented class methods

WiiMote.devicesMap

WiiMote.eventLoopIsRunning

Instance Methods

.dumpEvents

.dumpEvents = value

dump incoming events for debugging purposes.

.spec

Returns the device specification, with symbolic names for each item. Each name links to the current value.

.actionSpec

Returns the device action specification, with symbolic names for each item. Each name in the dictionary links to an action to be performed upon receiving a new value.

.closeAction

.closeAction = value

Set an action to be performed when the device closes.

.connectAction

.connectAction = value

Set an action to be performed when the device connects.

.disconnectAction

.disconnectAction = value

Set an action to be performed when the device disconnects.

.at(controlName)

Get the value of a device property at the given key.

.setAction(key, keyAction)

Set an action to be performed when the value of key changes. The key name must be one that occurs in the spec.

.removeAction(key)

Remove the previously defined action at the key.

.close

Close the device.

The properties of the Wii Remote

.battery

Returns the current battery status of the device.

.ext_type

Returns the extension type that is connected.

.remote_buttons

.remote_buttons = value

Returns an Array with the current button values.

.remote_motion

.remote_motion = value

Returns an Array with the current acceleration values (x,y,z, orientation). Orientation is Mac only.

.remote_ir

.remote_ir = value

Returns an Array with the found IR objects. (not tested!).

.remote_led

Returns an Array with the current LED values.

.setLEDState(id, state)

Set the LED with number id to value value (1=on, 0=off).

.rumble(onoff)

Turn on the rumble, value (1=on, 0=off).

.enable(onoff)

Enable the device.

.enableExpansion(onoff)

Enable the device expansion (nunchuk or classic controller).

.enableButtons(onoff)

Enable the buttons on the device.

.enableMotionSensor(onoff)

Enable the motion sensor on the device.

.enableIRSensor(onoff)

Enable the IR sensor on the device.

The properties of the NunChuk

.nunchuk_buttons

.nunchuk_buttons = value

Returns an Array with the current button values.

.nunchuk_motion

.nunchuk_motion = value

Returns an Array with the current acceleration values (x,y,z, orientation). Orientation is Mac only.

.nunchuk_stick

.nunchuk_stick = value

Returns an Array with the current stick values.

The properties of the Classic Controller

.classic_buttons

.classic_buttons = value

Returns an Array with the current button values.

.classic_stick1

.classic_stick1 = value

Returns an Array with the current stick values of stick 1.

.classic_stick2

.classic_stick2 = value

Returns an Array with the current stick values of stick 2.

.classic_analog

.classic_analog = value

Returns an Array with the current analog values.

Inherited instance methods

Undocumented instance methods

.address

.calibration

.connect

.deviceSpec

.disconnect

.id

.id = value

.isOpen

.reconnect

Examples

// Example to start up and view values
WiiMote.start;    // start the eventloop
w = WiiMote.discover; // discover a new device (wait for post about connected)

WiiMote.all;      // post an array of all devices

x = WiiMoteGUI.new( w ); // create a GUI (only covers the WiiMote and NunChuk currently)

w.enableMotionSensor( 1 );
w.enableExpansion( 1 );

w.setLEDState( 0,1 ); // turn the first LED on
w.rumble( 1 ); // rumble the device
w.rumble( 0 ); // rumble the device

w.setAction( \bA, { |v| v.postln; } ); // post the value when button A changes.
w.removeAction( \bA );

// (macOS) if you do not see any changes in the motion sensors, then the connection is bad.
// push the red button inside the battery compartment, or the buttons 1 and 2 on the WiiMote and start over again to discover...

WiiMote.discover; // discover a new device
WiiMote.all;      // post an array of all devices

w = WiiMote.all[1];
x.w.close; // close previous window
x = WiiMoteGUI.new( w ); // create a GUI (only covers the WiiMote and NunChuk currently)

// now it should work..., if not, repeat the exercise...


// clean up
WiiMote.closeAll; // close all devices
WiiMote.stop;
x.w.close;

IR tracking

This example shows a window displaying the objects being tracked by the WiiMote IR camera.

(
var q, w, u;
q = WiiMote.discover;
if(q.isNil) {
    "No wiimote found, aborting.".error;
} {
    q.enableIRSensor(1);
    w = Window("IR sensor", Rect(100, 100, 400, 400)).front;
    w.onClose = { q.close };
    u = UserView(w, Rect(0, 0, 400, 400));
    u.background = Color.black;
    u.drawFunc = {
        var p;
        q.remote_ir.do {|ir, i|
            if(ir.valid>0) {
                p = Point(ir.posx*400, ir.posy*400);
                Pen.addArc(p, ir.size*500.0, 0, pi*2);
                Pen.fillColor = Color.hsv(i/5, 1, 1);
                Pen.fill;
                Pen.stringAtPoint(
                    "% @ % %".format(
                        ir.posx.round(0.01),
                        ir.posy.round(0.01),
                        (ir.size*1000).floor
                    ),
                    p,
                    Font(Font.defaultSansFace, 9),
                    Color.white
                );
            };
        };
    };
    u.animate = true;
};
)