Wiimote auto connect toegevoegd mbv wiiscan. Lirc afstandsbedieningen verbeterd en begonnen aan send mechanisme.

This commit is contained in:
2011-06-07 13:40:04 +00:00
parent b0d6aa79b2
commit 5b038b496a
14 changed files with 262 additions and 139 deletions

View File

@@ -1,80 +0,0 @@
% Configfile_begin
% config file for wiiscan
% all usb device ids, not used yet in the scanning
all_usb_hubs=""
% single power controlling usb hub, only nessesary when powerering over usb, for automatic resetting. If not used, put ""
% active_usb_hub="@PCI\VEN_8086&DEV_2934&SUBSYS_20F017AA&REV_03\3&B1BFB68&0&E8"
active_usb_hub=""
% a list of allowed wiimotes in the form xx:xx:xx:xx:xx:xx, wildcard match is 00:00:00:00:00:00
%allowed_wiimote_adr=00:00:00:00:00:00
% my set of wiimotes
allowed_wiimote_adr=00:19:?D:??:6?:?? 00:1F:C5:??:??:?? 00:17:AB:39:69:87
% launching exe file have a problem with spaces, hence renamed exe, replaced space with underscores
% The original Johnny Chung Lee wb software version 0.3, downloaded from [http://johnnylee.net/projects/wii/]
% may not work yet, missing library integration
%whiteboard_software="d:/WiimoteWhiteboard/WiimoteWhiteboard_v0.3.exe"
% same stuff, but binary build from sources and redistributed in this package
%whiteboard_software="WiimoteWhiteboard.exe"
% Boon Jin's wb software version 0.6, does not work yet, missing library integration
%whiteboard_software="d:/wsb/WiimoteSmoothboard.exe"
% wiiscan parameters
%option_device="Nintendo RVL-CNT-01"
% number of bt scanning attempts in a autoscan, timout is increased for every scan as retryattempt*option_timeout
option_scanretries=1
% long time (4000 and above, or increase option_scanretries) when matching name, short (1000-2000) when matching address
option_timeout=4000
% wiimote scanning time, only applicable used for wiilib
option_wiitimeout=1000
% delay bewteen powerdown/up, see man for details
option_usbsleep=1000
option_usbmsleep=1000
%option_btsleep=2000
% start wb at successfull connect, 0 or 1
option_startwhiteboard=0
% output verbose flag, 0 or 1
option_debug=0
% logging output file, put nothing if no logging is wanted
option_logfile=
% wiimote connection library, use none, wiiuse, or wiimotelib
option_wiilib=wiimotelib
% 0=no power cycle, 1=USB hub control, 2=USBm IO, 3=USB Devcon
option_usbpowermode=3
% 0=remove if not connectable, 1=always remove, 2=never remove
%option_forceremove=0
% bt radio to use, not working yet
%option_btradio=00:50:B6:A0:48:8C
% wiiscantray options
% number of connection attempts before autoscan i restarted
option_tray_pool_retries=2
% retry sleep, keep it low (<1000) to catch an open connection
option_tray_sleep_between_pool_retries=250
% sleep bewteen polls, when wiimote is succeful connected, keep it hight (>2000) to avoid to much CPU usage
option_tray_sleep_between_pool_loops=2000
% final connection step reqires some sleep before windows PnP is ready
option_sleep_before_final_connect=1500
% delay after launch of wb software, keep it at a medium value (2000-10000)
option_sleep_after_startwhiteboard=4000
% Configfile_end

View File

@@ -6,7 +6,6 @@ import java.util.Map;
import javax.swing.JToggleButton;
import mimis.exception.worker.ActivateException;
import mimis.manager.Exitable;
import mimis.manager.SelectButton;
import mimis.manager.Titled;
@@ -28,10 +27,10 @@ public class Manager<T extends Worker & Titled & Exitable> extends Worker {
}
public void stop() {
super.stop();
for (T manageable : manageableArray) {
manageable.stop();
}
super.stop();
}
protected void createButtons() {

View File

@@ -74,13 +74,14 @@ public class Mimis extends EventHandler {
}
public void stop() {
super.stop();
log.debug("Stop event router");
eventRouter.stop();
log.debug("Stop managers");
applicationManager.stop();
deviceManager.stop();
super.stop();
}
protected void end(Action action) {

View File

@@ -19,7 +19,7 @@ public abstract class Worker implements Runnable {
running = true;
if (thread) {
log.debug("Start thread");
new Thread(this).start();
new Thread(this, getClass().getName()).start();
} else {
log.debug("Run directly");
run();
@@ -79,6 +79,9 @@ public abstract class Worker implements Runnable {
public void deactivate() throws DeactivateException {
active = false;
synchronized (this) {
notifyAll();
}
}
public final void run() {

View File

@@ -3,5 +3,8 @@ package mimis.device.lirc;
import mimis.Button;
public interface LircButton extends Button {
public static final String NAME = null;
public String getCode();
public String getName();
}

View File

@@ -59,4 +59,10 @@ public class LircDevice extends Device implements LircButtonListener, SignalList
break;
}
}
public void stop() {
multiplexer.stop();
lircService.stop();
super.stop();
}
}

View File

@@ -12,11 +12,12 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Scanner;
import mimis.Worker;
import mimis.device.lirc.button.PhiliphsRCLE011Button;
import mimis.exception.button.UnknownButtonException;
import mimis.exception.worker.ActivateException;
import mimis.exception.worker.DeactivateException;
import mimis.util.Native;
public class LircService extends Worker {
public static final String IP = "127.0.0.1";
@@ -31,10 +32,11 @@ public class LircService extends Worker {
protected BufferedReader bufferedReader;
protected PrintWriter printWriter;
protected HashMap<String, LircButton[]> buttonMap;
protected String send;
public LircService(HashMap<String, LircButton[]> buttonMap) {
this(buttonMap, IP, PORT);
//String send = Native.getValue("HKEY_CURRENT_USER\\Software\\LIRC", "password");
send = Native.getValue("HKEY_CURRENT_USER\\Software\\LIRC", "password");
}
public LircService(HashMap<String, LircButton[]> buttonMap, String ip, int port) {
@@ -73,6 +75,7 @@ public class LircService extends Worker {
public void deactivate() throws DeactivateException {
try {
bufferedReader.close();
inputStream.close();
outputStream.close();
socket.close();
@@ -84,15 +87,23 @@ public class LircService extends Worker {
}
public void work() {
log.debug("wrk");
try {
String string = bufferedReader.readLine();
String string;
//synchronized (this) {
string = bufferedReader.readLine();
//}
try {
System.out.println(string);
LircButton lircButton = parseButton(new Scanner(string));
log.trace("LircButton: " + lircButton);
for (LircButtonListener lircbuttonListener : lircButtonListenerList) {
lircbuttonListener.add(lircButton);
}
} catch (UnknownButtonException e) {}
log.info(send(PhiliphsRCLE011Button.MUTE));
} catch (UnknownButtonException e) {
log.error(e);
}
} catch (IOException e) {
log.error(e);
}
@@ -104,7 +115,7 @@ public class LircService extends Worker {
scanner.next();
String code = scanner.next();
String remote = scanner.next();
//log.debug(String.format("%s: %s", remote, code));
log.trace(String.format("%s: %s", remote, code));
LircButton[] buttonArray = buttonMap.get(remote);
if (buttonArray != null) {
for (LircButton button : buttonArray) {
@@ -118,4 +129,30 @@ public class LircService extends Worker {
}
throw new UnknownButtonException();
}
public boolean send(LircButton button) {
return send(button, 0);
}
public boolean send(LircButton button, int repeat) {
if (send == null) {
return false;
}
String command = String.format("%s %s %s\n", send, button.getName(), button.getCode());
log.debug(command);
printWriter.append(command);
printWriter.flush();
//synchronized (this) {
try {
bufferedReader.readLine();
bufferedReader.readLine();
String result = bufferedReader.readLine();
bufferedReader.readLine();
return result.equals("SUCCESS");
} catch (IOException e) {
log.error(e);
}
return false;
//}
}
}

View File

@@ -43,4 +43,8 @@ public enum DenonRC176 implements LircButton {
public String getCode() {
return code;
}
public String getName() {
return NAME;
}
}

View File

@@ -18,26 +18,26 @@ public enum PhiliphsRCLE011Button implements LircButton {
LEFT ("Left"),
RIGHT ("Right"),
VOLUME_UP ("Volume+"),
VOLUME_DOWN ("Volume-");
/*Mute,
Program+,
Program-,
1,
2,
3,
4,
5,
6
7,
8,
9,
0,
Clock,
Out,
i+,
screenup,
screendown,
question;*/
VOLUME_DOWN ("Volume-"),
MUTE ("Mute"),
PROGRAM_UP ("Program+"),
PROGRUM_DOWN ("Program-"),
ONE ("1"),
TWO ("2"),
THREE ("3"),
FOUR ("4"),
FIVE ("5"),
SIX ("6"),
SEVEN ("7"),
EIGHT ("8"),
NINE ("9"),
ZERO ("0"),
CLOCK ("Clock"),
OUT ("Out"),
INFO ("i+"),
SCREEN_UP ("screenup"),
SCREEN_DOWN ("screendown"),
QUESTION ("question");
public static final String NAME = "Philips_RCLE011";
@@ -50,4 +50,8 @@ public enum PhiliphsRCLE011Button implements LircButton {
public String getCode() {
return code;
}
public String getName() {
return NAME;
}
}

View File

@@ -63,4 +63,8 @@ public enum SamsungBN5901015AButton implements LircButton {
public String getCode() {
return code;
}
public String getName() {
return NAME;
}
}

View File

@@ -8,6 +8,7 @@ import mimis.event.Task;
import mimis.exception.button.UnknownButtonException;
import mimis.exception.device.DeviceNotFoundException;
import mimis.exception.worker.ActivateException;
import mimis.exception.worker.DeactivateException;
import mimis.sequence.state.Press;
import mimis.sequence.state.Release;
import mimis.value.Action;
@@ -26,13 +27,16 @@ public class WiimoteDevice extends Device implements GestureListener {
protected static final int CONNECT_MAX = 10;
protected static final int RUMBLE = 150;
public static final int TIMEOUT = 200;
protected static WiimoteService wiimoteService;
protected Wiimote wiimote;
protected Calibration calibration;
protected GestureDevice gestureDevice;
protected int gestureId = 0;
protected int gestureId;
protected WiimoteDiscovery wiimoteDiscovery;
protected boolean connected;
static {
WiimoteDevice.wiimoteService = new WiimoteService();
@@ -41,20 +45,15 @@ public class WiimoteDevice extends Device implements GestureListener {
public WiimoteDevice() {
super(TITLE);
wiimoteDiscovery = new WiimoteDiscovery(this);
gestureDevice = new GestureDevice();
gestureDevice.add(this);
gestureId = 0;
}
/* Activation */
public void activate() throws ActivateException {
super.activate();
try {
wiimote = wiimoteService.getDevice(this);
} catch (DeviceNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
wiimote.activateMotionSensing();
connect();
/*add(
new Hold(WiimoteButton.A),
new Task(Action.TRAIN),
@@ -117,7 +116,21 @@ public class WiimoteDevice extends Device implements GestureListener {
new Task(Target.APPLICATION, Action.DISLIKE));
} catch (StateOrderException e) {}*/
}
public void deactivate() throws DeactivateException {
super.deactivate();
if (wiimote != null) {
wiimote.deactivateMotionSensing();
}
wiimoteDiscovery.disconnect();
}
public void stop() {
super.stop();
wiimoteService.exit();
}
/* Events */
public void action(Action action) {
switch (action) {
case TRAIN:
@@ -145,17 +158,98 @@ public class WiimoteDevice extends Device implements GestureListener {
}
}
public void feedback(Feedback feedback) {
System.out.println("Wiimote feedback");
wiimote.rumble(RUMBLE);
}
/* Connectivity */
public void connect() {
wiimote = null;
try {
wiimote = wiimoteService.getDevice(this);
super.activate();
} catch (DeviceNotFoundException e) {
log.error(e);
} catch (ActivateException e) {
log.error(e);
}
if (wiimote == null) {
wiimoteDiscovery.work();
if (wiimote == null) {
wiimoteDiscovery.disconnect();
try {
wiimoteDiscovery.activate();
} catch (ActivateException e) {
log.error(e);
}
}
}
}
public void connected() {
try {
wiimote = wiimoteService.getDevice(this);
//wiimote.activateMotionSensing();
try {
wiimoteDiscovery.deactivate();
super.activate();
} catch (DeactivateException e) {
log.error(e);
} catch (ActivateException e) {
log.error(e);
}
} catch (DeviceNotFoundException e) {
log.error(e);
}
}
public void disconnect() {
wiimote.disconnect();
wiimote = null;
wiimoteDiscovery.disconnect();
}
public void disconnected() {
try {
wiimoteDiscovery.activate();
} catch (ActivateException e) {
log.error(e);
}
}
public boolean active() {
if (wiimote != null) {
log.debug("Check activity");
connected = false;
wiimote.getStatus();
synchronized (this) {
try {
wait(TIMEOUT);
} catch (InterruptedException e) {
log.error(e);
}
}
if (!connected) {
disconnect();
active = false;
}
}
return active;
}
/* Listeners */
public void onButtonsEvent(WiimoteButtonsEvent event) {
int pressed = event.getButtonsJustPressed() - event.getButtonsHeld();
int released = event.getButtonsJustReleased();
try {
if (pressed != 0 && released == 0) {
Button button = WiimoteButton.create(pressed);
System.out.println("Press: " + button);
log.trace("Press: " + button);
add(new Press(button));
} else if (pressed == 0 && released != 0) {
Button button = WiimoteButton.create(released);
System.out.println("Release: " + button);
log.trace("Release: " + button);
add(new Release(button));
}
} catch (UnknownButtonException e) {}
@@ -170,18 +264,4 @@ public class WiimoteDevice extends Device implements GestureListener {
System.out.printf("id #%d, prob %.0f%%, valid %b\n", event.getId(), 100 * event.getProbability(), event.isValid());
}
}
public void feedback(Feedback feedback) {
System.out.println("Wiimote feedback");
wiimote.rumble(RUMBLE);
}
public void deactivate() {
wiimote.deactivateMotionSensing();
}
public void stop() {
wiimoteService.exit();
super.stop();
}
}

View File

@@ -0,0 +1,50 @@
package mimis.device.wiimote;
import java.io.IOException;
import java.util.Scanner;
import mimis.Worker;
public class WiimoteDiscovery extends Worker {
protected WiimoteDevice wiimoteDevice;
public WiimoteDiscovery(WiimoteDevice wiimoteDevice) {
this.wiimoteDevice = wiimoteDevice;
}
protected boolean connect() {
return execute("-c nintendo");
}
protected boolean disconnect() {
return execute("-d nintendo");
}
public boolean execute(String parameters) {
String command = "native/wiiscan.exe -l none " + parameters;
try {
Process process = Runtime.getRuntime().exec(command);
Scanner scanner = new Scanner(process.getInputStream());
while (scanner.hasNext()) {
if (scanner.next().equals("[OK]")) {
return true;
}
}
} catch (IOException e) {
log.error(e);
}
return false;
}
protected void work() {
log.debug("Discover wiimotes");
if (connect()) {
wiimoteDevice.connected();
}
}
public void stop() {
super.stop();
disconnect();
}
}

View File

@@ -3,6 +3,9 @@ package mimis.device.wiimote;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import mimis.exception.device.DeviceNotFoundException;
import wiiusej.WiiUseApiManager;
@@ -23,6 +26,8 @@ import wiiusej.wiiusejevents.wiiuseapievents.NunchukRemovedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.StatusEvent;
public class WiimoteService extends WiiUseApiManager implements WiimoteListener {
protected Log log = LogFactory.getLog(getClass());
protected final boolean RUMBLE = false;
protected ArrayList<Integer> wiimoteList;
@@ -51,7 +56,7 @@ public class WiimoteService extends WiiUseApiManager implements WiimoteListener
if (!wiimoteList.contains(id)) {
wiimote.addWiiMoteEventListeners(this);
wiimoteList.add(id);
wiimoteDeviceMap.put(id, wiimoteDevice); // Todo: controleren of dit nodig is. Ligt aan hoe uniek het id is na bijvoorbeeld een reconnect. Wellicht voldoet een arrayList ook.
wiimoteDeviceMap.put(id, wiimoteDevice);
return wiimote;
}
}
@@ -76,12 +81,20 @@ public class WiimoteService extends WiiUseApiManager implements WiimoteListener
public void onStatusEvent(StatusEvent event) {
if (event.isConnected()) {
WiimoteDevice wiimoteDevice = getWiimoteDevice(event);
wiimoteDevice.connected = true;
synchronized (wiimoteDevice) {
wiimoteDevice.notifyAll();
}
}
}
public void onIrEvent(IREvent e) {}
public void onExpansionEvent(ExpansionEvent event) {}
public void onDisconnectionEvent(DisconnectionEvent event) {}
public void onDisconnectionEvent(DisconnectionEvent event) {
log.debug("disconnect!");
}
public void onNunchukInsertedEvent(NunchukInsertedEvent event) {}
public void onNunchukRemovedEvent(NunchukRemovedEvent event) {}
public void onGuitarHeroInsertedEvent(GuitarHeroInsertedEvent event) {}

View File

@@ -42,7 +42,6 @@ public class SequenceListener {
}
public void add(State state) {
log.trace(state);
for (Sequence sequence : sequenceList) {
activeList.add(new Active(sequence));
}
@@ -64,7 +63,7 @@ public class SequenceListener {
activeList.remove(active);
}
}
protected void add(Event event, Signal signal) {
if (event instanceof Task) {
event = ((Task) event).setSignal(signal);