Wiimote auto connect toegevoegd mbv wiiscan. Lirc afstandsbedieningen verbeterd en begonnen aan send mechanisme.
This commit is contained in:
@@ -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() {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -59,4 +59,10 @@ public class LircDevice extends Device implements LircButtonListener, SignalList
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
multiplexer.stop();
|
||||
lircService.stop();
|
||||
super.stop();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,4 +43,8 @@ public enum DenonRC176 implements LircButton {
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return NAME;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,4 +63,8 @@ public enum SamsungBN5901015AButton implements LircButton {
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return NAME;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
50
java/src/mimis/device/wiimote/WiimoteDiscovery.java
Normal file
50
java/src/mimis/device/wiimote/WiimoteDiscovery.java
Normal 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();
|
||||
}
|
||||
}
|
||||
@@ -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) {}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user