
import wiimote.*;

public class Wii
{
   public double   getX1      (int n)              { return wiiEvent[n].ir1x  ; }
   public double   getY1      (int n)              { return wiiEvent[n].ir1y  ; }
   public double   getX2      (int n)              { return wiiEvent[n].ir2x  ; }
   public double   getY2      (int n)              { return wiiEvent[n].ir2y  ; }
   public double   getPitch   (int n)              { return pitch       [n]   ; }
   public double   getRoll    (int n)              { return roll        [n]   ; }
   public int      getState   (int n, int i)       { return state       [n][i]; }
   public boolean  isConnected(int n)              { return usingWiiMote[n]   ; }
   public void     setOff     (int n, int i)       { wii[n][i] = false; }
   public void     setOn      (int n, int i)       { wii[n][i] = true; }
   public void     setPitch   (int n, double v)    { pitch[n] = v; }
   public void     setRoll    (int n, double v)    { roll [n] = v; }
   public void     setVibrate (int n, boolean tf)  {
      switch (n) {
      case 0: WiiRemote.vibrate(tf); break;
      case 1: WiiRemote2.vibrate(tf); break;
      }
   }

   public final static int A = 0;
   public final static int B = 1;
   public final static int D = 2;
   public final static int H = 3;
   public final static int L = 4;
   public final static int M = 5;
   public final static int O = 6;
   public final static int P = 7;
   public final static int R = 8;
   public final static int T = 9;
   public final static int U = 10;

   void stop() {
      if (wiiEventThread1 != null) { wiiEventThread1.stop(); wiiEventThread1 = null; }
      if (wiiEventThread2 != null) { wiiEventThread2.stop(); wiiEventThread2 = null; }
      WiiRemote.disconnect();
      WiiRemote2.disconnect();
   }

   void update() {

      if (wiiEventThread1 == null) {
         wiiEventThread1 = new WiiEventThread();
         wiiEvent[0] = new WiiEvent();
         usingWiiMote[0] = WiiRemote.connect();
         try {
            if (usingWiiMote[0]) { wiiEventThread1.start(); }
         }
         catch (Exception e) { e.printStackTrace(); }
      }

      if (wiiEventThread2 == null) {
         wiiEventThread2 = new WiiEventThread2();
         wiiEvent[1] = new WiiEvent();
         usingWiiMote[1] = WiiRemote2.connect();
         try {
            if (usingWiiMote[1]) { wiiEventThread2.start(); }
         }
         catch (Exception e) { e.printStackTrace(); }
      }

      for (int n = 0 ; n < 2 ; n++) {
         if (usingWiiMote[n]) {
            WiiEvent event = wiiEvent[n];
            boolean w[] = wii[n];

            event.snapshot(n==0 ? wiiEventThread1.event : wiiEventThread2.event);

            double x = event.x1;
            double y = event.y1;
            double z = event.z1;
            roll[n]  = -Math.atan2(x, z);
            pitch[n] = -Math.asin(y);

            w[A] = event.a;
            w[B] = event.b;
            w[D] = event.down;
            w[H] = event.home;
            w[L] = event.left;
            w[M] = event.minus;
            w[O] = event.one;
            w[P] = event.plus;
            w[R] = event.right;
            w[T] = event.two;
            w[U] = event.up;

         }
         for (int i = 0 ; i < 11 ; i++) {
            state  [n][i] = !wiiPrev[n][i]  ?  !wii[n][i] ? 0 : 1  :  wii[n][i] ? 2 : 3;
            wiiPrev[n][i] = wii[n][i];
         }
      }
   }

   private int NWIIMOTES = 2;

   private boolean usingWiiMote[] = new boolean[NWIIMOTES];
   private boolean wii    [][] = new boolean[NWIIMOTES][11];
   private boolean wiiPrev[][] = new boolean[NWIIMOTES][11];
   private double roll[] = new double[NWIIMOTES];
   private double pitch[] = new double[NWIIMOTES];
   private int state[][] = new int[NWIIMOTES][11];

   private double x1[] = new double[NWIIMOTES];
   private double y1[] = new double[NWIIMOTES];
   private double x2[] = new double[NWIIMOTES];
   private double y2[] = new double[NWIIMOTES];

   private WiiEventThread  wiiEventThread1;
   private WiiEventThread2 wiiEventThread2;
   private WiiEvent wiiEvent[] = new WiiEvent[NWIIMOTES];
}


