diff --git a/java/native/wiiscan.ini b/java/native/wiiscan.ini deleted file mode 100644 index d8d9a2c..0000000 --- a/java/native/wiiscan.ini +++ /dev/null @@ -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 diff --git a/java/src/mimis/Manager.java b/java/src/mimis/Manager.java index 99444fd..947f19d 100644 --- a/java/src/mimis/Manager.java +++ b/java/src/mimis/Manager.java @@ -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 extends Worker { } public void stop() { + super.stop(); for (T manageable : manageableArray) { manageable.stop(); } - super.stop(); } protected void createButtons() { diff --git a/java/src/mimis/Mimis.java b/java/src/mimis/Mimis.java index 945df44..132d945 100644 --- a/java/src/mimis/Mimis.java +++ b/java/src/mimis/Mimis.java @@ -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) { diff --git a/java/src/mimis/Worker.java b/java/src/mimis/Worker.java index 4d41517..8fb5917 100644 --- a/java/src/mimis/Worker.java +++ b/java/src/mimis/Worker.java @@ -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() { diff --git a/java/src/mimis/device/lirc/LircButton.java b/java/src/mimis/device/lirc/LircButton.java index 1b23f4f..d6695a1 100644 --- a/java/src/mimis/device/lirc/LircButton.java +++ b/java/src/mimis/device/lirc/LircButton.java @@ -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(); } diff --git a/java/src/mimis/device/lirc/LircDevice.java b/java/src/mimis/device/lirc/LircDevice.java index 8f9e0b5..9c828e5 100644 --- a/java/src/mimis/device/lirc/LircDevice.java +++ b/java/src/mimis/device/lirc/LircDevice.java @@ -59,4 +59,10 @@ public class LircDevice extends Device implements LircButtonListener, SignalList break; } } + + public void stop() { + multiplexer.stop(); + lircService.stop(); + super.stop(); + } } diff --git a/java/src/mimis/device/lirc/LircService.java b/java/src/mimis/device/lirc/LircService.java index 9cfaa64..65447fc 100644 --- a/java/src/mimis/device/lirc/LircService.java +++ b/java/src/mimis/device/lirc/LircService.java @@ -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 buttonMap; + protected String send; public LircService(HashMap 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 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; + //} + } } diff --git a/java/src/mimis/device/lirc/button/DenonRC176.java b/java/src/mimis/device/lirc/button/DenonRC176.java index e69e28f..ff647cf 100644 --- a/java/src/mimis/device/lirc/button/DenonRC176.java +++ b/java/src/mimis/device/lirc/button/DenonRC176.java @@ -43,4 +43,8 @@ public enum DenonRC176 implements LircButton { public String getCode() { return code; } + + public String getName() { + return NAME; + } } diff --git a/java/src/mimis/device/lirc/button/PhiliphsRCLE011Button.java b/java/src/mimis/device/lirc/button/PhiliphsRCLE011Button.java index 636886f..54204d4 100644 --- a/java/src/mimis/device/lirc/button/PhiliphsRCLE011Button.java +++ b/java/src/mimis/device/lirc/button/PhiliphsRCLE011Button.java @@ -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; + } } diff --git a/java/src/mimis/device/lirc/button/SamsungBN5901015AButton.java b/java/src/mimis/device/lirc/button/SamsungBN5901015AButton.java index 503ede5..66d826d 100644 --- a/java/src/mimis/device/lirc/button/SamsungBN5901015AButton.java +++ b/java/src/mimis/device/lirc/button/SamsungBN5901015AButton.java @@ -63,4 +63,8 @@ public enum SamsungBN5901015AButton implements LircButton { public String getCode() { return code; } + + public String getName() { + return NAME; + } } diff --git a/java/src/mimis/device/wiimote/WiimoteDevice.java b/java/src/mimis/device/wiimote/WiimoteDevice.java index d8b9db3..70ce75d 100644 --- a/java/src/mimis/device/wiimote/WiimoteDevice.java +++ b/java/src/mimis/device/wiimote/WiimoteDevice.java @@ -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(); - } } diff --git a/java/src/mimis/device/wiimote/WiimoteDiscovery.java b/java/src/mimis/device/wiimote/WiimoteDiscovery.java new file mode 100644 index 0000000..8731bb2 --- /dev/null +++ b/java/src/mimis/device/wiimote/WiimoteDiscovery.java @@ -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(); + } +} diff --git a/java/src/mimis/device/wiimote/WiimoteService.java b/java/src/mimis/device/wiimote/WiimoteService.java index 41efaa0..66a514c 100644 --- a/java/src/mimis/device/wiimote/WiimoteService.java +++ b/java/src/mimis/device/wiimote/WiimoteService.java @@ -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 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) {} diff --git a/java/src/mimis/sequence/SequenceListener.java b/java/src/mimis/sequence/SequenceListener.java index fde3a06..f572a27 100644 --- a/java/src/mimis/sequence/SequenceListener.java +++ b/java/src/mimis/sequence/SequenceListener.java @@ -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);