Educational materials

WiiLAB Example Code - Wiimote Class: Connect/Disconnect

Note: MATLAB R2007a or greater is required for this code to run correctly.

Connect

The Connect method of the Wiimote class is one of the main methods of the class and contains all the necessary steps to set up a connection with the Wiimote.
It begins by making a call to the Connect() function of the WiiLAB COM. Then based on the return value of Connect() it displays a command line message.

If the Connect() was successful it will return a value greater then zero. Based on that number we set the Wiimote's LEDs.

%% Connect
% /**
%  * Connect
%  * Connects to the wiimote and sets the input report type
%  * @param wiimote The wiimote object
%  * @return The updated wiimote object
%  */
function Connect(wiimote)
       
%% Connect / Disconnect
%  Connect
%  Connects to the wiimote and sets the input report type
%  @param wiimote The wiimote object
function Connect(wiimote)
       
    if(wiimote.isConnected <= 0)
        % Connect to the wiimote
        wiimote.isConnected = wiimote.wm.Connect();

        if(wiimote.isConnected > 0)                
            % Set LEDs property
            wiimote.SetLEDs((bitand(wiimote.isConnected, 1) > 0), ...
                            (bitand(wiimote.isConnected, 2) > 0), ...
                            (bitand(wiimote.isConnected, 4) > 0), ...
                            (bitand(wiimote.isConnected, 8) > 0));
            % Display Connection Message
            disp(['Wiimote ' int2str(wiimote.isConnected)...
            ' Successfully Connected']);
        elseif (wiimote.isConnected == 0)
            % There are wiimotes connected to this computer but they
            % are all in use
            disp('All Wiimotes connected to the computer are in use.');
        elseif (wiimote.isConnected == -1)
            % There are no wiimotes connected to this computer
            disp('There are no Wiimotes connected to this computer.');
        end
    else
        disp('The wiimote is already connected.');
    end

end % Connect

Disconnect

The Disconnect method cleans up the Wiimote and the Wiimote object. It sets the Rumble to off, sets all the LEDs to on (this is done to show that the Wiimote is not connected but still on and consuming battery power), and resets all the Wiimote objects variables back to their default values. Finally it disconnects the actual Wiimote from the computer and displays the appropriate command line message.

%  Disconnect
%  Disconnects the wiimote
%  @param wiimote The wiimote object
function Disconnect(wiimote)
       
    % Check Connection
    wiimote.CheckConnection();
            
    % Clean up the Wiimote
    % Turn off Rumble if it is on
    wiimote.SetRumble(false);
    % Set All LEDs On
    wiimote.SetLEDs(true, true, true, true);
    % temp wiimote id
    tempID = wiimote.isConnected;
    % Disconnect the wiimote
    wiimote.isConnected = ~wiimote.wm.Disconnect();          
    % Reset Properties
    wiimote.ResetProperties();          
    % Display Disconnect Message
    if(~wiimote.isConnected)
        disp(['Wiimote ' int2str(tempID) ' Successfully Disconnected']);
    else
        disp(['Wiimote ' int2str(tempID) ' Not Disconnected']);
        wiimote.isConnected = tmpID;
    end

end % Disconnect

Disconnect All Wiimotes

Disconnect All Wiimotes is a method that allows the user to disconnect all Wiimotes that are paired with this computer. This was added because of how Wiimotes are connected in WiiLAB. You cannot reconnect to a Wiimote without first disconnecting. If a Wiimote is not disconnected correctly at the end of a program the user may have to call this method to free up the Wiimotes for connection.

%% DisconnectAllWiimotes
% /**
%  * DisconnectAllWiimotes
%  * Attempts to disconnect all the wiimotes connected
%  * @param wiimote The wiimote object
%  */
function DisconnectAllWiimotes(wiimote)
       
    wiimote.wm.DisconnectAll();
    disp('All Wiimotes Disconnected');
            
end % DisconnectAllWiimotes

-- JordanBrindza - 26 Jun 2008

r7 - 21 Jul 2008 - 02:49:36 - JordanBrindza
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback
Syndicate this site RSSATOM