Steps Making Java Application That Use WiiMote – Creating a simple Project

Steps Making Java Application That Use WiiMote – Creating a simple Project

Hi there! We meet again here. In my last post here:  preparing the project , we have prepared the project so that we can begin to make the application. The application that we want to just a simple application, just for learning of course. I hope that you understand how to make it, so you can build your own application or interactive multimedia application such as game or CAI, etc.

Let’s begin.

We continue what we delayed before. I’m assuming that you already prepared your project from connection the wiimote, setting up additional library, until creating the main class. If you don’t prepared the project yet, read my last post, preparing the project.

The first simple application that we want to create is a console that read any event that triggered by wiimote. The second application is simple GUI application that can both read and write data to wiimote.

The console application

These are steps that we have to do:

–          Create wiimote variable. Just simply like this:

private Wiimote wiimote;

Don’t worry about the access modifier, you can set it protected, public or package. It’s up to you, depending on your need and purpose.

but don’t forget to import the wiiusej package.

import wiiusej.Wiimote;

–          Implement the class with wiimotelistener and override all method.

public class Lesson01 implements WiimoteListener{}

In eclipse, we can simply click add all unimplemented methods to generate all method that we have to override. Those methods are:

public void onButtonsEvent(WiimoteButtonsEvent e)

public void onClassicControllerInsertedEvent(ClassicControllerInsertedEvent e)

public void onClassicControllerRemovedEvent(ClassicControllerRemovedEvent e)

public void onDisconnectionEvent(DisconnectionEvent e)

public void onExpansionEvent(ExpansionEvent e)

public void onGuitarHeroInsertedEvent(GuitarHeroInsertedEvent e)

public void onGuitarHeroRemovedEvent(GuitarHeroRemovedEvent e)

public void onIrEvent(IREvent e)

public void onMotionSensingEvent(MotionSensingEvent e)

public void onNunchukInsertedEvent(NunchukInsertedEvent e)

public void onNunchukRemovedEvent(NunchukRemovedEvent e)

public void onStatusEvent(StatusEvent e)

It’s easy to understand each purpose of those methods. We can tell just by looking it’s name.

public void onButtonsEvent(WiimoteButtonsEvent e)

Occurs when any button on wiimote pressed, held, or released. There are many ways to get information what button that we press, held, or just released. You can get it by using the method

e.getButtonsHeld();

e.getButtonsJustPressed();

e.getButtonsJustReleased();

Those methods return data type short, the code of each button.  A = 8, B = 4, one = 2, two = 1, minus = 16, plus = 4096, etc…

But if you want to specifically get if button A pressed, you can use this:

if(e.isButtonAJustPressed())

That method return boolean, true or false.

public void onIrEvent(IREvent e)

Occurs when the wiimote detected any IR lights. This event not active by default. So we have to activate it first. The informations that you can get from the event are:

o   How many IR points detected and each coordinates.

o   The distance between the first and second IR point.

o   IR sensitivity

public void onMotionSensingEvent(MotionSensingEvent e)

Occurs when the wiimote moved. The Event almost every time because the motion sensor is too sensitive. Any little moves will be detected. The information that we can get are:

o   Gravity force x, y, and z.

o   Roll, pitch, and Yaw in degrees. But I don’t know why the yaw point always zero.

public void onStatusEvent(StatusEvent e)

Occurs when we call getStatus() from wiimote object. This event argument contains all basic information of the wiimote such as:

o   Battery level

o   Whether wiimote is connected or not

o   What number of any leds that turned on

o   Speaker enabled or not

o   Any Attachment to WiiMote

o   Whether Wiimote vibrating or not.

o   Continuous or not

o   IR active or not

o   Motion sensing active or not.

The rest of the override methods occur when there is an extension such as classic controller, nunchuck, guitar, etc,  whether inserted or removed.

public void onClassicControllerInsertedEvent(ClassicControllerInsertedEvent e)

public void onClassicControllerRemovedEvent(ClassicControllerRemovedEvent e)

public void onDisconnectionEvent(DisconnectionEvent e)

public void onExpansionEvent(ExpansionEvent e)

public void onGuitarHeroInsertedEvent(GuitarHeroInsertedEvent e)

public void onGuitarHeroRemovedEvent(GuitarHeroRemovedEvent e)

public void onNunchukInsertedEvent(NunchukInsertedEvent e)

public void onNunchukRemovedEvent(NunchukRemovedEvent e)

–          Get all connected wiimote.

I recommend that we put this code inside of the class constructor.

public Lesson01(){

Wiimote wiimotes[] = WiiUseApiManager.getWiimotes(1, true); //get all connected wiimote max 1 wiimote, and vibrate when connected.

if(wiimotes!= null && wiimotes.length > 0)

{

wiimote = wiimotes[0]; // get the first wiimote

wiimote.addWiiMoteEventListeners(this); //register the event listener

}

}

WiiUseApiManager.getWiimotes(int nb, boolean rumble)

That method above is to get all connected wiimote. The first parameter is the number of wiimotes that we trying to get. I set it 1 because I just want to connect 1 wiimote. The second parameter is to set if the wiimote connected, it will vibrate or not. The return value is all connected wiimotes in Array. If there is no wiimote connected, it value is null. So, we have to valide whether null or the length is zero. If a connected wiimote found, we set the wiimote variable from the first index ( 0 ) from wiimotes. After that add wiimote event listener from this class to the wiimote.

–          Create main loop in main method.

We need one boolean variable to determine if the application ended or not.

private static boolean isEnd = false;

this is the loop:

public static void main(String[] args) {

// TODO Auto-generated method stub

Lesson01 lesson01 = new Lesson01();

while(!isEnd){

}

System.exit(0);

}

–          Now, you can read all of event that occurs in your wiimote. How to get it? Just print the object of eventArgs in each overrided method example :

public void onButtonsEvent(WiimoteButtonsEvent e) {

// TODO Auto-generated method stub

System.out.println(e);

}

–          There are some feature of wiimote that not active by default, such as IREvent and  MotionSensingEvent. To activate it simply use this code.

wiimote.activateIRTRacking();

wiimote.activateMotionSensing();

–          I assume you already understand what the function of the codes above. So I just present the source code that I make. This is just the console application. The GUI application will catch up later.

import wiiusej.Wiimote;

import wiiusej.WiiUseApiManager;

import wiiusej.wiiusejevents.physicalevents.ExpansionEvent;

import wiiusej.wiiusejevents.physicalevents.IREvent;

import wiiusej.wiiusejevents.physicalevents.MotionSensingEvent;

import wiiusej.wiiusejevents.physicalevents.WiimoteButtonsEvent;

import wiiusej.wiiusejevents.utils.WiimoteListener;

import wiiusej.wiiusejevents.wiiuseapievents.ClassicControllerInsertedEvent;

import wiiusej.wiiusejevents.wiiuseapievents.ClassicControllerRemovedEvent;

import wiiusej.wiiusejevents.wiiuseapievents.DisconnectionEvent;

import wiiusej.wiiusejevents.wiiuseapievents.GuitarHeroInsertedEvent;

import wiiusej.wiiusejevents.wiiuseapievents.GuitarHeroRemovedEvent;

import wiiusej.wiiusejevents.wiiuseapievents.NunchukInsertedEvent;

import wiiusej.wiiusejevents.wiiuseapievents.NunchukRemovedEvent;

import wiiusej.wiiusejevents.wiiuseapievents.StatusEvent;

public class Lesson01 implements WiimoteListener{

/**

* @param args

*/

private Wiimote wiimote;

private static boolean isEnd=false;

private MotionSensingEvent motion = null;

public static void main(String[] args) {

// TODO Auto-generated method stub

Lesson01 lesson01 = new Lesson01();

while(!isEnd){

}

System.exit(0);

}

public Lesson01(){

Wiimote wiimotes[] = WiiUseApiManager.getWiimotes(1, true);

if(wiimotes!= null && wiimotes.length > 0)

{

wiimote = wiimotes[0];

wiimote.addWiiMoteEventListeners(this);

wiimote.activateIRTRacking();

wiimote.activateMotionSensing();

wiimote.activateContinuous();

wiimote.getStatus();

}

}

@Override

public void onButtonsEvent(WiimoteButtonsEvent e) {

// TODO Auto-generated method stub

if(e.getButtonsJustPressed()!= 0)

System.out.println(e);

if(e.isButtonHomePressed())

isEnd = true;

if(e.isButtonAPressed())

wiimote.getStatus();

if(e.isButtonBPressed() && motion != null)

System.out.println(motion);

}

@Override

public void onClassicControllerInsertedEvent(

ClassicControllerInsertedEvent e) {

// TODO Auto-generated method stub

}

@Override

public void onClassicControllerRemovedEvent(ClassicControllerRemovedEvent e) {

// TODO Auto-generated method stub

}

@Override

public void onDisconnectionEvent(DisconnectionEvent e) {

// TODO Auto-generated method stub

}

@Override

public void onExpansionEvent(ExpansionEvent e) {

// TODO Auto-generated method stub

}

@Override

public void onGuitarHeroInsertedEvent(GuitarHeroInsertedEvent e) {

// TODO Auto-generated method stub

}

@Override

public void onGuitarHeroRemovedEvent(GuitarHeroRemovedEvent e) {

// TODO Auto-generated method stub

}

@Override

public void onIrEvent(IREvent e) {

// TODO Auto-generated method stub

}

@Override

public void onMotionSensingEvent(MotionSensingEvent e) {

// TODO Auto-generated method stub

motion = e;

}

@Override

public void onNunchukInsertedEvent(NunchukInsertedEvent e) {

// TODO Auto-generated method stub

}

@Override

public void onNunchukRemovedEvent(NunchukRemovedEvent e) {

// TODO Auto-generated method stub

}

@Override

public void onStatusEvent(StatusEvent e) {

// TODO Auto-generated method stub

System.out.println(e);

}

}

About thejackal

7th semester undergraduate binus university in 2010.
This entry was posted in Binusian Blog. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *