Doc

Getting Started

WiiC provides two examples: wiic-example and wiicpp-example. In this tutorial, we will analyze the latter, which is based on WiiCpp C++ layer. You can find wiicpp-example in your /usr/local/bin directory, whether you installed the library, while the source code example.cpp is available in <WIIC_HOME>/src/example. The main advantage of using the C++ layer is that developers can use object-oriented data types, such as classes, to develop their own applications.

Open it with a text editor. The first thing to note is the inclusion of the WiiCpp header:

#include <wiicpp.h>

Searching for Wiimote devices

CWii is the main class that manages and discoveries Wiimotes:

CWii wii; // Defaults to 4 remotes
std::vector<CWiimote>::iterator i;
int reloadWiimotes = 0;
int numFound;
int index;

printf("Searching for wiimotes... Turn them on!\n");

//Find the wiimote
numFound = wii.Find(5);

printf("Found %d Wiimotes\n", numFound);

Note that Find(int timeout) must be called to start Wiimote discovery. The method returns the number of found devices.

Connecting to Wiimote devices

Now, we must connect to the discovered Wiimotes, through the Connect() method:

// Connect to the Wiimotes
std::vector<CWiimote>& wiimotes = wii.Connect();

printf("Connected to %d wiimotes\n", (int) wiimotes.size());

Every Wiimote is represented by an instance of the CWiimote class.

Controlling a Wiimote

A Wiimote supports several commands. In our example, we will see how to turn on one of its leds, activate the rumble, enable data transmission about the three accelerometers and, finally, disable the IR:

// Writing to the Wiimotes
for(index = 0, i = wiimotes.begin(); i != wiimotes.end(); ++i, ++index) {
        CWiimote & wiimote = *i;

        //Set Leds
        wiimote.SetLEDs(LED_MAP[index]);

        //Rumble for 0.2 seconds as a connection ack
        wiimote.SetRumbleMode(CWiimote::ON);
        usleep(200000);
        wiimote.SetRumbleMode(CWiimote::OFF);

        // Receive information about accelerometers
        wiimote.SetMotionSensingMode(CWiimote::ON);

        // Do not receive information about IR
        wiimote.IR.SetMode(CIR::OFF);
}

Acquiring information from the Wiimote

WiiC adopts a non blocking polling mechanism to control whether Wiimote status changed. A Wiimote notifies every changing event, and transmits a status report everytime requested by the user, according to her preferences. In order to poll the connected devices, we use the method Poll():

do {
        if(reloadWiimotes) {
                // Regenerate the list of wiimotes
                wiimotes = wii.GetWiimotes();
                reloadWiimotes = 0;
        }

        //Poll the Wiimotes
        if(wii.Poll()) {
                for(i = wiimotes.begin(); i != wiimotes.end(); ++i) {
                        // Use a reference to make working with the iterator handy.
                        CWiimote & wiimote = *i;
                        switch(wiimote.GetEvent()) {
                                case CWiimote::EVENT_EVENT:
                                // If the accelerometer is turned on then print angles
                                if(wiimote.isUsingACC()) {
                                        float pitch, roll, yaw ;
                                        wiimote.Accelerometer.GetOrientation(pitch, roll, yaw);
                                        RDK_INFO_PRINTF("[Wiimote id %i] roll = %f pitch = %f yaw = %f\n", wiimote.GetID(), roll, pitch, yaw);
                                }
                                break;

                                case CWiimote::EVENT_DISCONNECT:
                                case CWiimote::EVENT_UNEXPECTED_DISCONNECT:
                                RDK_INFO_PRINTF("--- DISCONNECTED [Wiimote id %i] ---\n", wiimote.GetID());
                                reloadWiimotes = 1;
                                break;

                                default:
                                break;
                        }
                }
        }
} while(wiimotes.size()); // Go so long as there are wiimotes left to poll

Once we analyzed this example, try to run wiicpp-example, meanwhile pressing button 1 and 2 of the Wiimote (to put it in discovery mode), and see what happens! For a complete reference of WiiC library, please refer to the Doxygen documentation.

Introduction

Installation

Documentation

edit SideBar

Blix theme adapted by David Gilbert, powered by PmWiki