001package com.ctre.phoenix;
002
003import edu.wpi.first.wpilibj.GenericHID;
004
005/**
006 * Class to handle button events
007 */
008public class ButtonMonitor implements ILoopable
009{
010        GenericHID  _gameCntrlr;
011        int _btnIdx;
012        IButtonPressEventHandler  _handler;
013        boolean _isDown = false;
014        
015        /**
016         * Interface for classes that handle button events
017         */
018        public interface IButtonPressEventHandler {
019                /**
020                 * Method to execute when a button is pressed
021                 * @param idx Index of button pressed
022                 * @param isDown Whether the button is down or not
023                 */
024                void OnButtonPress(int idx, boolean isDown);
025        };
026
027        /**
028         * Constructor for ButtonMonitor
029         * @param controller Controller to monitor
030         * @param buttonIndex Button to monitor
031         * @param ButtonPressEventHandler Class that will handle buttonPresses
032         */
033        public ButtonMonitor(GenericHID controller, int buttonIndex,
034                        IButtonPressEventHandler ButtonPressEventHandler) {
035                _gameCntrlr = controller;
036                _btnIdx = buttonIndex;
037                _handler = ButtonPressEventHandler;
038        }
039
040        /**
041         * Call this every loop, it monitors for button presses
042         */
043        public void process() {
044                boolean down = _gameCntrlr.getRawButton(_btnIdx);
045
046                if (!_isDown && down){
047                        _handler.OnButtonPress(_btnIdx, down);
048                }
049
050                _isDown = down;
051        }
052
053        /**
054         * Do nothing on start
055         */
056        public void onStart() {
057        }
058        /**
059         * Process every loop
060         */
061        public void onLoop() {
062                process();
063        }
064        /**
065         * @return false, this is never done
066         */
067        public boolean isDone() {
068                return false;
069        }
070        /**
071         * Do nothing on stop
072         */
073        public void onStop() {
074        }
075}