SHARE
TWEET

Untitled

a guest Jan 25th, 2013 23 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package edu.wpi.first.wpilibj.templates;
  6.  
  7. import edu.wpi.first.wpilibj.GenericHID;
  8. import edu.wpi.first.wpilibj.buttons.Button;
  9. import edu.wpi.first.wpilibj.buttons.Trigger;
  10. import edu.wpi.first.wpilibj.command.Command;
  11. /**
  12.  *
  13.  * @author Arhowk
  14.  * Use as a normal JoystickButton. The exception is that the whileHeld() command will
  15.  * replace the last issued command rather than stacking all instantiated commands
  16.  * together
  17.  */
  18. public class ChangeButton extends Button{
  19.     Command runningCommand;
  20.     buttonDummy dummy;
  21.     GenericHID m_joystick;
  22.     int m_buttonNumber;
  23.    
  24.     public ChangeButton(GenericHID joystick, int buttonNumber) {
  25.         m_joystick = joystick;
  26.         m_buttonNumber = buttonNumber;
  27.     }
  28.    
  29.     /**
  30.      * Gets the value of the joystick button
  31.      * @return The value of the joystick button
  32.      */
  33.     public boolean get() {
  34.         return m_joystick.getRawButton(m_buttonNumber);
  35.     }
  36.    
  37.     public void whileHeld(final Command command){
  38.         runningCommand = command;
  39.         if(dummy == null){
  40.             dummy = new buttonDummy();
  41.             whileActive(dummy);
  42.         }else if(command == null){
  43.             dummy.cancel();
  44.         }
  45.     }
  46.     private class buttonDummy extends Command{
  47.         protected void initialize() {
  48.             runningCommand.start();
  49.         }
  50.  
  51.         // Called repeatedly when this Command is scheduled to run
  52.         protected void execute() {
  53.            
  54.         }
  55.  
  56.         // Make this return true when this Command no longer needs to run execute()
  57.         protected boolean isFinished() {
  58.              return runningCommand.isCanceled();
  59.         }
  60.  
  61.         // Called once after isFinished returns true
  62.         protected void end() {
  63.         }
  64.  
  65.         // Called when another command which requires one or more of the same
  66.         // subsystems is scheduled to run
  67.         protected void interrupted() {
  68.             runningCommand.cancel();
  69.         }
  70.     }
  71.    
  72. }
RAW Paste Data
Top