Renamed package namespaces to assure a better usability with other libraries.

git-svn-id: svn://svn.code.sf.net/p/wiigee/code/trunk@87 c7eff9ee-dd40-0410-8832-91a4d88773cf
This commit is contained in:
bepo23
2009-06-05 08:54:45 +00:00
parent 520c0507c0
commit d6a6705bef
8 changed files with 4 additions and 1197 deletions

View File

@@ -1,108 +0,0 @@
/*
* wiigee - accelerometerbased gesture recognition
* Copyright (C) 2007, 2008, 2009 Benjamin Poppinga
*
* Developed at University of Oldenburg
* Contact: benjamin.poppinga@informatik.uni-oldenburg.de
*
* This file is part of wiigee.
*
* wiigee is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.wiigee.control;
import org.wiigee.device.AndroidDevice;
import org.wiigee.event.GestureListener;
import org.wiigee.filter.Filter;
import org.wiigee.util.Log;
/**
* This is for using wiigee on Android Smartphones. This port has been
* initiated by Maarten 'MrSnowflake' Krijn and updated by 'zl25drexel'
* under the pseudonym 'Andgee'.
*
* It has been re-integrated into wiigee on 29th May of 2009.
*
* @author Maarten 'MrSnowflake' Krijn
* @author zl25drexel
* @author Benjamin 'BePo' Poppinga
*/
public class AndroidWiigee {
protected static String version = "1.0 alpha";
protected static String releasedate = "20090529";
protected static AndroidWiigee instance;
private AndroidDevice device;
private AndroidWiigee() {
device = new AndroidDevice();
}
public static synchronized AndroidWiigee getInstance() {
Log.write("This is AndroidWiigee (Andgee) version "+version+" ("+releasedate+")");
Log.write("This is an Android adaptation of Wiigee (http://wiigee.sourceforge.net/)");
Log.write("So many thanks to the Wiigee team for their awsome recognition lib!");
if(instance == null) {
instance = new AndroidWiigee();
return instance;
} else {
return instance;
}
}
public void addGestureListener(GestureListener listener) {
device.addGestureListener(listener);
}
public void addFilter(Filter filter) {
device.addFilter(filter);
}
public AndroidDevice getDevice() {
return device;
}
/**
* Sets the Trainbutton for all wiimotes;
*
* @param b Button encoding, see static Wiimote values
*/
public void setTrainButton(int b) {
device.setTrainButton(b);
}
/**
* Sets the Recognitionbutton for all wiimotes;
*
* @param b Button encoding, see static Wiimote values
*/
public void setRecognitionButton(int b) {
device.setRecognitionButton(b);
}
/**
* Sets the CloseGesturebutton for all wiimotes;
*
* @param b Button encoding, see static Wiimote values
*/
public void setCloseGestureButton(int b) {
device.setCloseGestureButton(b);
}
}

View File

@@ -1,108 +0,0 @@
/*
* wiigee - accelerometerbased gesture recognition
* Copyright (C) 2007, 2008 Benjamin Poppinga
*
* Developed at University of Oldenburg
* Contact: benjamin.poppinga@informatik.uni-oldenburg.de
*
* This file is part of wiigee.
*
* wiigee is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.wiigee.control;
import java.io.IOException;
import java.util.Vector;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import org.wiigee.util.Log;
import org.wiigee.device.Wiimote;
public class WiimoteDeviceDiscovery implements DiscoveryListener {
private Vector<RemoteDevice> devices;
private boolean isInquiring;
private Object lock;
public WiimoteDeviceDiscovery(Object lock) {
super();
this.lock=lock;
this.devices = new Vector<RemoteDevice>();
//this.isInquiring=true;
}
public void deviceDiscovered(RemoteDevice newdevice, DeviceClass devclass) {
Log.write("Device discovered: "
+ newdevice.getBluetoothAddress() + " - ");
// add the device to the vector
if (!devices.contains(newdevice)
&& devclass.getMajorDeviceClass() == 1280
&& devclass.getMinorDeviceClass() == 4) {
Log.write("Is a Wiimote!");
devices.addElement(newdevice);
} else {
Log.write("Is NOT a Wiimote!");
}
}
public void inquiryCompleted(int discType) {
switch (discType) {
case WiimoteDeviceDiscovery.INQUIRY_COMPLETED:
Log.write("Inquiry completed.");
break;
case WiimoteDeviceDiscovery.INQUIRY_ERROR:
Log.write("Inquiry error.");
break;
case WiimoteDeviceDiscovery.INQUIRY_TERMINATED:
Log.write("Inquiry terminated.");
break;
}
synchronized(this.lock) {
this.lock.notify();
}
//this.isInquiring=false;
}
public void serviceSearchCompleted(int arg0, int arg1) {
// not necessary
}
public void servicesDiscovered(int arg0, ServiceRecord[] arg1) {
// not necessary
}
public boolean isInquirying() {
return this.isInquiring;
}
public Vector<Wiimote> getDiscoveredWiimotes() throws IOException {
Vector<Wiimote> wiimotes = new Vector<Wiimote>();
for (int i = 0; i < devices.size(); i++) {
wiimotes.add(new Wiimote(devices.elementAt(i).getBluetoothAddress()));
}
return wiimotes;
}
}

View File

@@ -1,193 +0,0 @@
/*
* wiigee - accelerometerbased gesture recognition
* Copyright (C) 2007, 2008 Benjamin Poppinga
*
* Developed at University of Oldenburg
* Contact: benjamin.poppinga@informatik.uni-oldenburg.de
*
* This file is part of wiigee.
*
* wiigee is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.wiigee.control;
import java.io.IOException;
import java.util.Vector;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import org.wiigee.util.Log;
import org.wiigee.device.Wiimote;
import org.wiigee.event.DeviceListener;
import org.wiigee.event.GestureListener;
import org.wiigee.filter.Filter;
// Singleton
public class WiimoteWiigee {
protected static String version = "1.5 alpha";
protected static String releasedate = "20090524";
protected static WiimoteWiigee instance;
private static Object lock = new Object();
private Vector<Wiimote> devices;
private WiimoteWiigee() {
String stack;
String stackVersion;
String l2capFeature;
String bluecoveVersion;
Log.write("This is wiigee version " + version + " (" + releasedate + ")");
// Bluecove
bluecoveVersion = LocalDevice.getProperty("bluecove");
if(!bluecoveVersion.equals("")) {
stack = LocalDevice.getProperty("bluecove.stack");
stackVersion = LocalDevice.getProperty("bluecove.stack.version");
Log.write("You are using the "+stack+" Bluetooth stack (Version "+stackVersion+")");
l2capFeature = LocalDevice.getProperty("bluecove.feature.l2cap");
Log.write("L2CAP supported: "+l2capFeature);
if(l2capFeature.equals("true")) {
Log.write("wiigee: found a supported stack!");
// set min id for Bluecove
Log.write(Log.DEBUG, "JSR82 PSM Minimum Restriction -- OFF", null);
System.setProperty("bluecove.jsr82.psm_minimum_off", "true");
}
} else {
Log.write("No Bluecove Library detected - trying anyway...");
}
}
public static WiimoteWiigee getInstance() {
if (instance == null) {
instance = new WiimoteWiigee();
return instance;
} else {
return instance;
}
}
/**
* Returns an array of discovered wiimotes.
*
* @return Array of discovered wiimotes or null if
* none discoverd.
*/
public Wiimote[] getDevices() throws IOException {
this.devices = this.discoverWiimotes();
for (int i = 0; i < this.devices.size(); i++) {
this.devices.elementAt(i).setLED(i + 1);
}
Wiimote[] out = new Wiimote[this.devices.size()];
for (int i = 0; i < this.devices.size(); i++) {
out[i] = this.devices.elementAt(i);
}
return out;
}
/**
* Discover the wiimotes around the bluetooth host and
* make them available public via getWiimotes method.
*
* @return Array of discovered wiimotes.
*/
private Vector<Wiimote> discoverWiimotes() throws IOException {
WiimoteDeviceDiscovery deviceDiscovery = new WiimoteDeviceDiscovery(lock);
LocalDevice localDevice = LocalDevice.getLocalDevice();
Log.write("Your Computers Bluetooth MAC: " + localDevice.getBluetoothAddress());
Log.write("Starting device inquiry...");
DiscoveryAgent discoveryAgent = localDevice.getDiscoveryAgent();
discoveryAgent.startInquiry(DiscoveryAgent.GIAC, deviceDiscovery);
try {
synchronized (lock) {
lock.wait();
}
} catch (InterruptedException e) {
Log.write("Problems during device discovery.");
e.printStackTrace();
}
Log.write("Device discovery completed!");
return deviceDiscovery.getDiscoveredWiimotes();
}
/**
* Returns the number of wiimotes discovered.
*
* @return Number of wiimotes discovered.
*/
public int getNumberOfDevices() {
return this.devices.size();
}
/**
* Sets the Trainbutton for all wiimotes;
*
* @param b Button encoding, see static Wiimote values
*/
public void setTrainButton(int b) {
for (int i = 0; i < this.devices.size(); i++) {
this.devices.elementAt(i).setTrainButton(b);
}
}
/**
* Sets the Recognitionbutton for all wiimotes;
*
* @param b Button encoding, see static Wiimote values
*/
public void setRecognitionButton(int b) {
for (int i = 0; i < this.devices.size(); i++) {
this.devices.elementAt(i).setRecognitionButton(b);
}
}
/**
* Sets the CloseGesturebutton for all wiimotes;
*
* @param b Button encoding, see static Wiimote values
*/
public void setCloseGestureButton(int b) {
for (int i = 0; i < this.devices.size(); i++) {
this.devices.elementAt(i).setCloseGestureButton(b);
}
}
public void addDeviceListener(DeviceListener listener) {
for (int i = 0; i < this.devices.size(); i++) {
this.devices.elementAt(i).addDeviceListener(listener);
}
}
public void addGestureListener(GestureListener listener) {
for (int i = 0; i < this.devices.size(); i++) {
this.devices.elementAt(i).addGestureListener(listener);
}
}
public void addFilter(Filter filter) {
for (int i = 0; i < this.devices.size(); i++) {
this.devices.elementAt(i).addFilter(filter);
}
}
}

View File

@@ -1,83 +0,0 @@
/*
* wiigee - accelerometerbased gesture recognition
* Copyright (C) 2007, 2008, 2009 Benjamin Poppinga
*
* Developed at University of Oldenburg
* Contact: benjamin.poppinga@informatik.uni-oldenburg.de
*
* This file is part of wiigee.
*
* wiigee is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.wiigee.device;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
/**
* Android based device implementation
*
* @author liangj01
*
*/
public class AndroidDevice extends Device implements SensorEventListener {
private float x0, y0, z0, x1, y1, z1;
public AndroidDevice() {
// 'Calibrate' values
this.x0 = 0;
this.y0 = -SensorManager.STANDARD_GRAVITY;
this.z0 = 0;
this.x1 = SensorManager.STANDARD_GRAVITY;
this.y1 = 0;
this.z1 = SensorManager.STANDARD_GRAVITY;
}
public void onSensorChanged(SensorEvent sevent) {
Sensor sensor = sevent.sensor;
float[] values = sevent.values;
if (this.accelerationEnabled() && sensor.getType() == SensorManager.SENSOR_ACCELEROMETER) {
double x, y, z;
float xraw, yraw, zraw;
/*
* calculation of acceleration vectors starts here. further
* information about normation exist in the public papers or
* the various www-sources.
*
*/
xraw = values[SensorManager.DATA_X];
yraw = values[SensorManager.DATA_Y];
zraw = values[SensorManager.DATA_Z];
x = (double) (xraw - x0) / (double) (x1 - x0);
y = (double) (yraw - y0) / (double) (y1 - y0);
z = (double) (zraw - z0) / (double) (z1 - z0);
this.fireAccelerationEvent(new double[] {x, y, z});
}
}
public void onAccuracyChanged(Sensor arg0, int arg1) {
// Nothing to do.
}
}

View File

@@ -24,8 +24,6 @@
package org.wiigee.device;
import org.wiigee.logic.ProcessingUnit;
import org.wiigee.logic.TriggeredProcessingUnit;
import java.io.IOException;
import java.util.Vector;

View File

@@ -1,401 +0,0 @@
/*
* wiigee - accelerometerbased gesture recognition
* Copyright (C) 2007, 2008 Benjamin Poppinga
*
* Developed at University of Oldenburg
* Contact: benjamin.poppinga@informatik.uni-oldenburg.de
*
* This file is part of wiigee.
*
* wiigee is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.wiigee.device;
import java.io.IOException;
import java.util.Random;
import javax.bluetooth.L2CAPConnection;
import javax.microedition.io.Connector;
import org.wiigee.event.*;
import org.wiigee.util.Log;
/**
* @author Benjamin 'BePo' Poppinga
* This class represents the basic functions of the wiimote.
* If you want your wiimote to e.g. vibrate you'll do this here.
*
*/
public class Wiimote extends Device {
// Fixed number values.
public static final int MOTION = 0;
public static final int BUTTON_2 = 1;
public static final int BUTTON_1 = 2;
public static final int BUTTON_B = 3;
public static final int BUTTON_A = 4;
public static final int BUTTON_MINUS = 5;
public static final int BUTTON_HOME = 8;
public static final int BUTTON_LEFT = 9;
public static final int BUTTON_RIGHT = 10;
public static final int BUTTON_DOWN = 11;
public static final int BUTTON_UP = 12;
public static final int BUTTON_PLUS = 13;
// Reports
public static final byte CMD_SET_REPORT = 0x52;
// IR Modes
public static final byte IR_MODE_STANDARD = 1;
public static final byte IR_MODE_EXTENDED = 0x03;
// Modes / Channels
public static final byte MODE_BUTTONS = 0x30;
public static final byte MODE_BUTTONS_ACCELERATION = 0x31;
public static final byte MODE_BUTTONS_ACCELERATION_INFRARED = 0x33;
// Bluetooth-adress as string representation
private String btaddress;
// LED encoded as byte
byte ledencoding;
// control connection, send commands to wiimote
private L2CAPConnection controlCon;
// receive connection, receive answers from wiimote
private L2CAPConnection receiveCon;
// Functional
private boolean vibrating;
private boolean calibrated;
private boolean infraredenabled;
private WiimoteStreamer wms;
/**
* Creates a new wiimote-device with a specific bluetooth mac-adress.
*
* @param btaddress
* String representation of the mac-adress e.g. 00191D68B57C
*/
public Wiimote(String btaddress) throws IOException {
btaddress = this.removeChar(btaddress, ':');
this.btaddress=btaddress;
this.vibrating=false;
this.connect();
this.calibrateAccelerometer();
this.streamData(true);
this.enableAccelerationSensors();
//this.enableInfraredCamera();
}
/**
* Creates the two needed connections to send and receive commands
* to and from the wiimote-device.
*
*/
public void connect() throws IOException {
this.controlCon = (L2CAPConnection)Connector.open("btl2cap://"+
this.btaddress+":11;authenticate=false;encrypt=false;master=false",
Connector.WRITE); // 11
this.receiveCon = (L2CAPConnection)Connector.open("btl2cap://"+
this.btaddress+":13;authenticate=false;encrypt=false;master=false",
Connector.READ); // 13
}
/**
* Disconnects the wiimote and closes the two connections.
*/
public void disconnect() {
this.vibrating=false;
try {
this.controlCon.close();
this.receiveCon.close();
Log.write("Disconnected wiimote.");
} catch(Exception e) {
Log.write("Failure during disconnect of wiimote.");
}
}
/**
* @return
* Receiving data connection
*/
public L2CAPConnection getReceiveConnection() {
return this.receiveCon;
}
/**
* This method makes the Wiimote-Class reacting to incoming data.
* For just controlling and sending commands to the wiimote
* (vibration, LEDs, ...) it's not necessary to call this method.
*
* @param value
* true, if the class should react to incoming data.
* false, if you only want to send commands to wiimote and
* only the control-connection is used.
*/
public void streamData(boolean value) {
if(value==true) {
if(this.wms==null) {
this.wms = new WiimoteStreamer(this);
}
wms.start(); }
else if(this.wms!=null) {
wms.stopThread();
}
}
/**
* Write data to a register inside of the wiimote.
*
* @param offset The memory offset, 3 bytes.
* @param data The data to be written, max. 16 bytes.
* @throws IOException
*/
public void writeRegister(byte[] offset, byte[] data) throws IOException {
byte[] raw = new byte[23];
raw[0] = CMD_SET_REPORT;
raw[1] = 0x16; // Write channel
raw[2] = 0x04; // Register
for(int i=0; i<offset.length; i++) {
raw[3+i] = offset[i];
}
raw[6] = (byte)data.length;
for(int i=0; i<data.length; i++) {
raw[7+i] = data[i];
}
this.sendRaw(raw);
}
/**
* Makes the Wiimote respond the data of an register. The wiimotestreamer
* doesn't react to the reponse yet.
*
* @param offset The memory offset.
* @param size The size which has to be read out.
* @throws IOException
*/
public void readRegister(byte[] offset, byte[] size) throws IOException {
byte[] raw = new byte[8];
raw[0] = CMD_SET_REPORT;
raw[1] = 0x17; // Read channel
raw[2] = 0x04; // Register
for(int i=0; i<offset.length; i++) {
raw[3+i] = offset[i];
}
for(int i=0; i<size.length; i++) {
raw[6+i] = size[i];
}
this.sendRaw(raw);
}
/**
* Reads data out of the EEPROM of the wiimote.
* At the moment this method is only used to read out the
* calibration data, so the wiimotestreamer doesn't react for
* every answer on this request.
*
* @param offset The memory offset.
* @param size The size.
* @throws IOException
*/
public void readEEPROM(byte[] offset, byte[] size) throws IOException {
byte[] raw = new byte[8];
raw[0] = CMD_SET_REPORT;
raw[1] = 0x17; // Read channel
raw[2] = 0x00; // EEPROM
for(int i=0; i<offset.length; i++) {
raw[3+i] = offset[i];
}
for(int i=0; i<size.length; i++) {
raw[6+i] = size[i];
}
this.sendRaw(raw);
}
/**
* Sends pure hexdata to the wiimote. If you want your wiimote
* to vibrate use sendRaw(new byte[] {0x52, 0x13, 0x01}). For other raw-commands use
* the specific wiki-sites around the web (wiili.org, wiibrew.org, ...)
* @param raw
* byte representation of an command
*/
public void sendRaw(byte[] raw) throws IOException {
if(this.controlCon!=null) {
this.controlCon.send(raw);
try {
Thread.sleep(20l);
} catch (InterruptedException e) {
System.out.println("sendRaw() interrupted");
}
}
}
/**
* Enables one or more LEDs, where the value could be between 0 and 8.
* If value=1 only the left LED would light up, for value=2 the second
* led would light up, for value=3 the first and second led would light up,
* and so on...
*
* @param value Between 0 and 8, indicating which LEDs should light up
* @throws IOException
*/
public void setLED(int value) throws IOException {
if(value<16 && value>0) {
byte tmp = (byte)value;
this.ledencoding=(byte)(tmp<<4);
this.sendRaw(new byte[] {CMD_SET_REPORT, 0x11, this.ledencoding});
} else {
// Random LED change :)
this.setLED(new Random().nextInt(16));
}
}
/**
* Initializes the calibration of the accerlarometer. This is done once
* per each controller in program lifetime.
*
* @throws IOException
*/
private void calibrateAccelerometer() throws IOException {
// calibration command
this.readEEPROM(new byte[] {0x00, 0x00, 0x20}, new byte[] {0x00, 0x07});
this.calibrated=true;
}
/**
* Activates the acceleration sensor. You have to call the
* streamData(true) method to react to this acceleration data.
* Otherwise the wiimote would send data the whole time and
* nothing else would happen.
*
*/
@Override
public void enableAccelerationSensors() throws IOException {
super.enableAccelerationSensors();
if(!this.calibrated) {
this.calibrateAccelerometer();
}
// enable acceleration in continuous mode
this.sendRaw(new byte[] {CMD_SET_REPORT, 0x12, 0x04, 0x31});
}
/**
* Deactivates the acceleration sensors.
*
*/
@Override
public void disableAccelerationSensors() throws IOException {
super.disableAccelerationSensors();
this.sendRaw(new byte[] {CMD_SET_REPORT, 0x12, 0x00, 0x30});
}
/**
* Enables the infrared camera in front of the wiimote to track
* IR sources in the field of view of the camera. This could be used
* to a lot of amazing stuff. Using this Mode could slow down the
* recognition of acceleration gestures during the increased data
* size transmitted.
*/
public void enableInfraredCamera() throws IOException {
this.accelerationenabled=true;
this.infraredenabled=true;
if(!this.calibrated) {
this.calibrateAccelerometer();
}
//write 0x04 to output 0x13
this.sendRaw(new byte[] {CMD_SET_REPORT, 0x13, 0x04});
// write 0x04 to output 0x1a
this.sendRaw(new byte[] {CMD_SET_REPORT, 0x1a, 0x04});
// write 0x08 to reguster 0xb00030
this.writeRegister(new byte[] {(byte)0xb0, 0x00, 0x30}, new byte[] {0x08});
// write sensivity block 1 to register 0xb00000
this.writeRegister(new byte[] {(byte)0xb0, 0x00, 0x00}, new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte)0x90, 0x00, (byte)0xc0});
// write sensivity block 2 to register 0xb0001a
this.writeRegister(new byte[] {(byte)0xb0, 0x00, (byte)0x1a}, new byte[] {0x40, 0x00});
// write ir-mode to register 0xb00033
this.writeRegister(new byte[] {(byte)0xb0, 0x00, 0x33}, new byte[] {0x03});
// enable continuous acceleration and IR cam on channel 33
this.sendRaw(new byte[] {CMD_SET_REPORT, 0x12, 0x00, 0x33});
}
/**
* With this method you gain access over the vibrate function of
* the wiimote. You got to try which time in milliseconds would
* fit your requirements.
*
* @param milliseconds
* time the wiimote would vibrate
*/
public void vibrateForTime(long milliseconds) throws IOException {
try {
if(!vibrating) {
this.vibrating=true;
byte tmp = (byte)(this.ledencoding | 0x01);
this.sendRaw(new byte[] {CMD_SET_REPORT, 0x11, tmp});
Thread.sleep(milliseconds);
this.sendRaw(new byte[] {CMD_SET_REPORT, 0x11, this.ledencoding});
this.vibrating=false;
}
} catch (InterruptedException e) {
System.out.println("WiiMoteThread interrupted.");
}
}
public boolean infraredEnabled() {
return this.infraredenabled;
}
// ###### Hilfsmethoden
// TODO
private String removeChar(String s, char c) {
String r = "";
for (int i = 0; i < s.length(); i ++) {
if (s.charAt(i) != c) r += s.charAt(i);
}
return r;
}
/**
* Fires a infrared event
*
* @param coordinates
* @param size
*/
public void fireInfraredEvent(int[][] coordinates, int[] size) {
InfraredEvent w = new InfraredEvent(this, coordinates, size);
for(int i=0; i<this.devicelistener.size(); i++) {
this.devicelistener.get(i).infraredReceived(w);
}
}
}

View File

@@ -1,298 +0,0 @@
/*
* wiigee - accelerometerbased gesture recognition
* Copyright (C) 2007, 2008 Benjamin Poppinga
*
* Developed at University of Oldenburg
* Contact: benjamin.poppinga@informatik.uni-oldenburg.de
*
* This file is part of wiigee.
*
* wiigee is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.wiigee.device;
import java.io.IOException;
import java.util.EventObject;
import javax.bluetooth.L2CAPConnection;
import org.wiigee.event.ButtonPressedEvent;
import org.wiigee.event.ButtonReleasedEvent;
import org.wiigee.util.Log;
/**
* This class listens to data sended by the wiimote and generates specific
* events for acceleration, buttonpress, ...
*
* @author Benjamin 'BePo' Poppinga
*
*/
public class WiimoteStreamer extends Thread {
boolean running;
double x0, x1, y0, y1, z0, z1;
Wiimote wiimote;
private L2CAPConnection receiveCon;
EventObject lastevent;
protected WiimoteStreamer(Wiimote wiimote) {
this.wiimote = wiimote;
this.receiveCon = wiimote.getReceiveConnection();
}
@Override
public void run() {
this.running = true;
int xraw, yraw, zraw;
double x, y, z;
try {
while (running) {
// connection has data and we're ready.
byte[] b = this.getRaw(); // blocks application
String[] input = this.byte2hex(b);
// debug output
/*
* for(int i=0; i<input.length; i++) {
* System.out.print(input[i]); if(i==input.length-1) {
* System.out.println(""); } else { System.out.print(":"); } }
* /* for(int i=0; i<b.length; i++) {
* System.out.print((int)b[i]&0xFF); if(i==input.length-1) {
* System.out.println(""); } else { System.out.print(":"); } }
*/
// wiimote wants to tell the calibration data
if ((b[1] & 0xFF) == 33) {
// if(((b[1] & 0xFF) & 0x21 ) == 0x21) {
this.x0 = b[7] & 0xFF;
this.y0 = b[8] & 0xFF;
this.z0 = b[9] & 0xFF;
this.x1 = b[11] & 0xFF;
this.y1 = b[12] & 0xFF;
this.z1 = b[13] & 0xFF;
Log.write("Autocalibration successful!");
continue;
}
// infrared is enabled, so have a look at the last bytes
if (this.wiimote.infraredEnabled()
&& (((b[1] & 0xFF) & 0x31) == 0x31 || ((b[1] & 0xFF) & 0x33) == 0x33)) {
int[][] coordinates = new int[4][2];
int[] size = new int[4];
int j = 0;
for (int i = 7; i < 18; i += 3) { // for each IR byte
// triple
int tailX = b[i] & 0xFF;
int tailY = b[i + 1] & 0xFF;
int preY = (b[i + 2] & 0xC0) << 2;
int preX = (b[i + 2] & 0x30) << 4;
coordinates[j][0] = tailX + preX;
coordinates[j][1] = tailY + preY;
size[j++] = (b[i + 2] & 0x0F);
}
this.wiimote.fireInfraredEvent(coordinates, size);
}
// if the wiimote is sending acceleration data...
if (this.wiimote.accelerationEnabled()
&& (((b[1] & 0xFF) & 0x31) == 0x31 || ((b[1] & 0xFF) & 0x33) == 0x33)) {
/*
* calculation of acceleration vectors starts here. further
* information about normation exist in the public papers or
* the various www-sources.
*
*/
xraw = (b[4] & 0xFF);
yraw = (b[5] & 0xFF);
zraw = (b[6] & 0xFF);
x = (double) (xraw - x0) / (double) (x1 - x0);
y = (double) (yraw - y0) / (double) (y1 - y0);
z = (double) (zraw - z0) / (double) (z1 - z0);
// try to fire event, there could be filters added to the
// wiimote class which may prevents from firing.
this.wiimote.fireAccelerationEvent(new double[] {x, y, z});
}
// Button 1 matches button "2" on wiimote
if (((b[3] & 0xFF) & 0x01) == 0x01
&& !(this.lastevent instanceof ButtonPressedEvent)) {
this.wiimote.fireButtonPressedEvent(1);
this.lastevent = new ButtonPressedEvent(
this.wiimote, 1);
continue;
}
// Button 2 matches button "1" on wiimote
else if (((b[3] & 0xFF) & 0x02) == 0x02
&& !(this.lastevent instanceof ButtonPressedEvent)) {
this.wiimote.fireButtonPressedEvent(2);
this.lastevent = new ButtonPressedEvent(
this.wiimote, 2);
continue;
}
// Button 3 matches button "B" on wiimote
else if (((b[3] & 0xFF) & 0x04) == 0x04
&& !(this.lastevent instanceof ButtonPressedEvent)) {
this.wiimote.fireButtonPressedEvent(3);
this.lastevent = new ButtonPressedEvent(
this.wiimote, 3);
continue;
}
// Button 4 matches button "A" on wiimote
else if (((b[3] & 0xFF) & 0x08) == 0x08
&& !(this.lastevent instanceof ButtonPressedEvent)) {
this.wiimote.fireButtonPressedEvent(4);
this.lastevent = new ButtonPressedEvent(
this.wiimote, 4);
continue;
}
// Button 5 matches button "MINUS" on wiimote
else if (((b[3] & 0xFF) & 0x10) == 0x10
&& !(this.lastevent instanceof ButtonPressedEvent)) {
this.wiimote.fireButtonPressedEvent(5);
this.lastevent = new ButtonPressedEvent(
this.wiimote, 5);
continue;
}
// Button 6 unknown
// Button 7 unknown
// Button 8 matches button "HOME" on wiimote
else if (((b[3] & 0xFF) & 0x80) == 0x80
&& !(this.lastevent instanceof ButtonPressedEvent)) {
this.wiimote.fireButtonPressedEvent(8);
this.lastevent = new ButtonPressedEvent(
this.wiimote, 8);
continue;
}
// Button 9 matches "CROSS LEFT" on wiimote
else if (((b[2] & 0xFF) & 0x01) == 0x01
&& !(this.lastevent instanceof ButtonPressedEvent)) {
this.wiimote.fireButtonPressedEvent(9);
this.lastevent = new ButtonPressedEvent(
this.wiimote, 9);
continue;
}
// Button 10 matches "CROSS RIGHT" on wiimote
else if (((b[2] & 0xFF) & 0x02) == 0x02
&& !(this.lastevent instanceof ButtonPressedEvent)) {
this.wiimote.fireButtonPressedEvent(10);
this.lastevent = new ButtonPressedEvent(
this.wiimote, 10);
continue;
}
// Button 11 matches "CROSS DOWN" on wiimote
else if (((b[2] & 0xFF) & 0x04) == 0x04
&& !(this.lastevent instanceof ButtonPressedEvent)) {
this.wiimote.fireButtonPressedEvent(11);
this.lastevent = new ButtonPressedEvent(
this.wiimote, 11);
continue;
}
// Button 12 matches "CROSS UP" on wiimote
else if (((b[2] & 0xFF) & 0x08) == 0x08
&& !(this.lastevent instanceof ButtonPressedEvent)) {
this.wiimote.fireButtonPressedEvent(12);
this.lastevent = new ButtonPressedEvent(
this.wiimote, 12);
continue;
}
// Button 13 matches button "PLUS" on wiimote
else if (((b[2] & 0xFF) & 0x10) == 0x10
&& !(this.lastevent instanceof ButtonPressedEvent)) {
this.wiimote.fireButtonPressedEvent(13);
this.lastevent = new ButtonPressedEvent(
this.wiimote, 13);
continue;
}
// Button 14 unknown
// Button 15 unknown
// Button 16 unknown
// Button released
else if ((input[2].equals("20") || input[2].equals("40") || input[2]
.equals("60"))
&& (input[3].equals("00") || input[3].equals("20")
|| input[3].equals("40") || input[3]
.equals("60"))
&& !(this.lastevent instanceof ButtonReleasedEvent)) {
this.wiimote.fireButtonReleasedEvent();
this.lastevent = new ButtonReleasedEvent(
this.wiimote);
continue;
}
} // while(running)
} catch (IOException e) {
Log.write("Streamer: Connection to Wiimote lost.");
this.running = false;
}
}
private byte[] getRaw() throws IOException {
byte[] b = new byte[19];
this.receiveCon.receive(b);
return b;
}
/**
* stops this thread.
*/
protected void stopThread() {
this.running = false;
}
protected boolean isRunning() {
return this.running;
}
private String[] byte2hex(byte[] b) {
String[] out = new String[b.length];
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1)
out[n] = ("0" + stmp).toUpperCase();
else
out[n] = stmp.toUpperCase();
}
return out;
}
}

View File

@@ -25,16 +25,16 @@
package org.wiigee.event;
import java.util.EventObject;
import org.wiigee.device.Wiimote;
import org.wiigee.device.Device;
public class InfraredEvent extends EventObject {
protected Wiimote wiimote;
protected Device wiimote;
protected int[][] coordinates;
protected int[] size;
protected boolean[] valid;
public InfraredEvent(Wiimote source, int[][] coordinates, int[] size) {
public InfraredEvent(Device source, int[][] coordinates, int[] size) {
super(source);
this.coordinates=coordinates;
this.size=size;
@@ -44,7 +44,7 @@ public class InfraredEvent extends EventObject {
}
}
public Wiimote getSource() {
public Device getSource() {
return this.wiimote;
}