/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.wiieditor.wiimote;
import com.wiieditor.other.ProgramConfig;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.Timer;
import motej.Mote;
import motej.event.IrCameraEvent;
import motej.event.IrCameraListener;
import motej.request.ReportModeRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author zohaibrauf
*/
public class WiiMoteController implements Runnable{
private SimpleMoteFinder moteFinder=null;
private Mote mote=null;
private Logger log=LoggerFactory.getLogger(WiiMoteController.class);
private ArrayList<WiiMoteListener> wiimoteListenerList=new ArrayList<WiiMoteListener>();
private Timer timer=null;
private static WiiMoteController wiiMoteController=null;
private IRPoint prevPoint=null;
public static WiiMoteController createWiiMoteController(){
if(wiiMoteController==null){
wiiMoteController=new WiiMoteController();
}
return wiiMoteController;
}
private WiiMoteController(){
moteFinder=new SimpleMoteFinder();
}
public void find(){
mote=moteFinder.findMote();
mote.enableIrCamera();
}
public void disconnect(){
mote.setReportMode(ReportModeRequest.DATA_REPORT_0x30);
mote.disableIrCamera();
mote.disconnect();
}
public void startListening(){
ActionListener actionListener=new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
log.debug("Timeout");
int size=wiimoteListenerList.size();
log.error("SIZE Listener List: "+size);
for(int i=0;i<size;i++){
try{
WiiMoteListener l=wiimoteListenerList.get(i);
log.error("Im HERE STOPPED");
if(l!=null && prevPoint!=null){
log.error("Im HERE HERE");
log.error(prevPoint.toString());
l.irStopped((IRPoint)prevPoint.clone());
}
}
catch(Exception e){
log.error("IR Stopped Error:"+e.toString());
}
}
prevPoint=null;
timer.stop();
}
};
int delay=ProgramConfig.STOP_DELAY*1000;
timer=new Timer(delay,actionListener);
IrCameraListener listener = new IrCameraListener() {
public void irImageChanged(IrCameraEvent evt) {
//System.out.println(evt.getIrPoint(0).getX() + " : " + evt.getIrPoint(0).getY());
int size=wiimoteListenerList.size();
double x=evt.getIrPoint(0).getX();
double y=evt.getIrPoint(0).getY();
if(x!=1023.0 && y!=1023.0){
for(int i=0;i<size;i++){
WiiMoteListener l=wiimoteListenerList.get(i);
if(l!=null){
if(prevPoint==null){
l.irStarted(new IRPoint(x, y));
}
else{
l.irMoved(new IRPoint(x, y));
}
}
}
prevPoint=new IRPoint(x, y);
timer.stop();
}else if( !timer.isRunning()){
timer.start();
}
}
};
mote.addIrCameraListener(listener);
mote.enableIrCamera();
mote.setReportMode(ReportModeRequest.DATA_REPORT_0x36);
log.debug("Started Listening");
}
public void registerListener(WiiMoteListener listener){
if(!wiimoteListenerList.contains(listener)){
log.debug("WiiMoteListener Registered");
wiimoteListenerList.add(listener);
}
}
public void unregisterListener(WiiMoteListener listener){
wiimoteListenerList.remove(listener);
}
@Override
public void run() {
throw new UnsupportedOperationException("Not supported yet.");
}
}