A Java Question

This is where you talk about the NXJ hardware related topics such as the brick, sensors, LEGO pieces, etc.

Moderators: 99jonathan, roger, imaqine

A Java Question

Postby Mac » Sat Sep 17, 2011 2:37 pm

I made a Wii Nunchuk Sensor for my NXT.
And I also need a program to work.
I found two codes.
But I don't konw how to compile these.
Please help me.
Please post detailed step.
Thanks a lot!

The sensor source code :
Code: Select all
package v2;
import lejos.nxt.*;

/**
* I2C mastering for the Nunchuk
*
* @version  v0.1
* @project  Nunchuk leJOS
* @author  Si-Mohamed Lamraoui
*/
public class NunchukSensor extends I2CSensor
{

   public final static int  NUNCHUCK_ADDR = 0x52;
   private I2CPort port ;
   private byte[] data = new byte[6];
   private byte[] init = new byte[2];
   
   /**
   * Create a new Nunchuk sensor plug on the NXT port
   * @param sensorPort NXT sensor port
   */
   public NunchukSensor(I2CPort sensorPort)
   {
      super(sensorPort);
      this.port = sensorPort;
      this.setAddress(NUNCHUCK_ADDR);
       this.port.setType(SensorConstants.TYPE_LOWSPEED);
       this.port.setMode(SensorConstants.MODE_RAW);
   }

   /**
    * Initializes the Nunchuk to communicate in I2C
    */
   public void initialize()
   {
      this.init[0] = 0x00;
      this.sendData(0x40, init,1);
   }
   
   /**
   * Method for retrieving the data from the Nunchuk
   * @return value of 8-bit I2C bus port
   */
   public byte[] getNunchukData()
   {   
      this.getData(0x00, this.data, 6);
      return data;
   }
   
   /**
    * Ask 6 Bytes of sensor value
    */
   public void AskNunchukData()
   {
      this.sendData(0x00, (byte) 0x00);
   }
   
   /**
   * Method for decrypt the data from the Nunchuk
   * @return value decrypted
   */
   public byte decryptData(byte value){
      return (byte) ((value ^ 0x17) + 0x17);
   }
   
}


The test source code :
Code: Select all
package v2;
import javax.microedition.lcdui.Graphics;
import lejos.nxt.*;


/**
* Test the Nunchuk with the LCD screen
*
* @version  v0.2
* @project  Nunchuk leJOS
* @author  Si-Mohamed Lamraoui
*/
public class TestLCD
{

   /**
    * Main
    */
   public static void main()
   {
     
      NunchukSensor nunchuk = new NunchukSensor(SensorPort.S4);
      Graphics graph = new Graphics();
      byte[] data = new byte[6];
     
      graph.drawString(" NUNCHUK NXT v0.1",1,1);
      graph.clear();
       
      while(!Button.ESCAPE.isPressed())
      {
         graph.drawString(" === NUNCHUK === ",0,0);
         nunchuk.initialize();
           nunchuk.AskNunchukData();

         data = nunchuk.getNunchukData();
         
         //Z-Button
         if((nunchuk.decryptData(data[5]) & 0x01) > 0)
         {   
            graph.drawString("Z : RELEAZED",0,9);
         }
         else
         {
            graph.drawString("Z : PRESSED",0,9);
         }
         //C-Button
         if((nunchuk.decryptData(data[5]) & 0x02) > 0)
         {   
            graph.drawString("C : RELEAZED",0,17);
         }
         else
         {
            graph.drawString("C : PRESSED",0,17);
         }
         
         //Joystick
         graph.drawString("Joystick X:",1,24);
         LCD.drawInt(nunchuk.decryptData(data[0]),0,12,3);
         graph.drawString("Joystick Y:",1,32);
         LCD.drawInt(nunchuk.decryptData(data[1]),0,12,4);         
         
         //Accelerometer
         graph.drawString("Accel X:",1,40);
         LCD.drawInt(nunchuk.decryptData(data[2]),0,12,5);   
         graph.drawString("Accel Y:",1,48);
         LCD.drawInt(nunchuk.decryptData(data[3]),0,12,6);   
         graph.drawString("Accel Z:",1,56);
         LCD.drawInt(nunchuk.decryptData(data[4]),0,12,7);   
         
         graph.refresh();
         try
         {
             Thread.sleep(10);
         }
         catch (InterruptedException e) {}
         graph.clear();
      }
   }

}
Mac
New User
 
Posts: 3
Joined: Sat Jul 23, 2011 7:18 am

Return to NXJ Hardware

Who is online

Users browsing this forum: No registered users and 1 guest

more stuff