Xbox Controller class

To make it easier for me and other programmers especially new ones, I have made this class that uses the Joystick class to get the state of the buttons and axis on an XboxController in a way that enables others to not have to remember the button and axis numbers. To that it is also able to tell if the button has just changed state that run making it easier to create programs that use toggle. The only dounside is that at the start of every run a refresh method has to be called.
We have been using this code with out problems and I would like to know what others think about it.
Thanks

import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Joystick.RumbleType;

/**
 *
 * @author TomasR
 */
public class XboxController {
    private final Joystick joy;
    private Button] button=new Button[10];
    
    /**
     * Axis state as of the last refresh
     */
    public Axis LeftStick=new Axis(0,0), RightStick=new Axis(0,0);
    /**
     * trigger states as of the last refresh
     */
    public triggers Triggers=new triggers(0,0);
    /**
     * Button states as of the last refresh
     */
    public buttons Buttons;
    
    public class triggers{
    	public double Right;
    	public double Left;
    	public double Combined;
    	public triggers(double r, double l){
    		Right=r;
    		Left=l;
    		combine();
    	}
    	private void combine(){
    		Combined=Right-Left;
    	}
    }
    class Axis{
        public double X,Y;
        public Axis(double x,double y){
            X=x;
            Y=y;
        }
    }
    class buttons{
        public Button A =button[0];
        public Button B =button[1];
        public Button X =button[2];
        public Button Y =button[3];
        public Button LB =button[4];
        public Button RB =button[5];
        public Button Back =button[6];
        public Button Start =button[7];
        public Button LeftStick =button[8];
        public Button RightStick =button[9];
    	
    }
    class Button{
        public boolean current=false , last=false,changedDown=false,changedUp=false;
        private void set(boolean c){
        	last=current;
        	current=c;
        	changedDown=!last && current;
        	changedUp=last && !current;
        }
    }
    
    /** 
     * @return the angle in degrees or -1 if not pressed
     */
    public int getDpad(){
    	return joy.getPOV(0);
    }
    private void leftStick(){
    	LeftStick.X=joy.getRawAxis(0);
    	LeftStick.Y=joy.getRawAxis(1);
    }
    private void rightStick(){
    	RightStick.X=joy.getRawAxis(4);
    	RightStick.Y=joy.getRawAxis(5);
    }
    private void trigger(){
        Triggers.Left = joy.getRawAxis(2);
        Triggers.Right = joy.getRawAxis(3);
        Triggers.combine();
    }
    
    /**
     * refreshes all button and axis values
     * Should be called only once per run
     */
    public void refresh(){
    	for(int i=0;i<10;i++){
    		button*.set(joy.getRawButton(i));
    	}
        leftStick();
        rightStick();
        trigger();
    }
    
    /**
     * @param i The controller number
     */
    public XboxController(int i) {
        joy=new Joystick(i);
        refresh();
        Buttons=new buttons();
    }
    
    /**
     * 
     * @param type Left or Right rumble
     * @param value A value from 0 to 1 for the intensity
     */
    public void vibrate(RumbleType type,float value){
    	joy.setRumble(type, value);
    }
}

Just a small nitpick from first glance, but is there a reason you didn’t extend the Joystick class? This would allow you to use this class in any place a Joystick is used. We’re currently working on one that overrides all of the joystick methods to map them to the buttons and sticks correctly.

That is because I have never worked with extend

Actually, this makes sense to me. The x box controller has a joystick.

Well, that’s a valid reason :slight_smile:

If you are interested you can look up information about Inheritance, Java is an Object Oriented Programming language, so it is a good topic to read up on.

If you have any questions let me know.

In the loosest term of joystick. The OP’s reasoning for not extending the Joystick class is a more valid reason not to.

As I been looking, extending Joystick class might make the choices of methods which I want to keep as few as possible too crowded for the programmer that is using my class.

It definitely adds a bunch of new methods to the class, which can both be a good and bad thing. And there is nothing wrong with a simple class that sets all of the mappings to meaningful method names that aren’t getRawAxis(2).

Overall, looks good. Well done.