# validated: 2017-01-12 DS 8f67f2c24cb9 athena/java/edu/wpi/first/wpilibj/XboxController.java
import hal
from .driverstation import DriverStation
from .interfaces.gamepadbase import GamepadBase
from wpilib.interfaces.generichid import GenericHID
[docs]class XboxController(GamepadBase):
"""
Handle input from Xbox 360 or Xbox One controllers connected to the Driver Station.
This class handles Xbox input that comes from the Driver Station. Each time a value is
requested the most recent value is returned. There is a single class instance for each controller
and the mapping of ports to hardware buttons depends on the code in the Driver Station.
"""
def __init__(self, port):
"""Construct an instance of an XBoxController. The XBoxController index is the USB port on the Driver Station.
:param port: The port on the Driver Station that the joystick is plugged into
"""
super().__init__(port)
self.ds = DriverStation.getInstance()
self.outputs = 0
self.leftRumble = 0
self.rightRumble = 0
hal.report(hal.UsageReporting.kResourceType_Joystick, port)
[docs] def getX(self, hand):
"""Get the X axis value of the controller.
:param hand: Side of controller whose value should be returned
:return: The X axis value of the controller
:rtype: float
"""
if hand == GenericHID.Hand.kLeft:
return self.getRawAxis(0)
else:
return self.getRawAxis(4)
[docs] def getY(self, hand):
"""Get the Y axis value of the controller.
:param hand: Side of controller whose value should be returned
:return: The Y axis value of the controller
:rtype: float
"""
if hand == GenericHID.Hand.kLeft:
return self.getRawAxis(1)
else:
return self.getRawAxis(5)
[docs] def getRawAxis(self, axis):
"""Get the value of the axis
:param axis: The axis to read, starting at 0
:return: The value of the axis
:rtype: float
"""
return self.ds.getStickAxis(self.port, axis)
[docs] def getBumper(self, hand):
"""Read the values of the bumper button on the controller.
:param hand: Side of controller whose value should be returned.
:return: The state of the button
:rtype: boolean
"""
if hand == GenericHID.Hand.kLeft:
return self.getRawButton(5)
else:
return self.getRawButton(6)
[docs] def getTriggerAxis(self, hand):
"""Get the trigger axis value of the controller.
:param hand: Side of controller whose value should be returned
:return: The trigger axis value of the controller
:rtype: float
"""
if hand == GenericHID.Hand.kLeft:
return self.getRawAxis(2)
else:
return self.getRawAxis(3)
[docs] def getPOV(self, pov):
return self.ds.getStickPOV(self.port, pov)
[docs] def getPOVCount(self):
return self.ds.getPOVCount(self.port)
[docs] def getType(self):
return self.ds.getJoystickType(self.port)
[docs] def getName(self):
return self.ds.getJoystickName(self.port)
[docs] def setOutput(self, outputNumber, value):
self.outputs = ((self.outputs & ~(1 << (outputNumber - 1)))
| ((1 if value else 0) << (outputNumber - 1)))
hal.setJoystickOutputs(self.port, self.outputs, self.leftRumble, self.rightRumble)
[docs] def setOutputs(self, value):
self.outputs = value
hal.setJoystickOutputs(self.port, self.outputs, self.leftRumble, self.rightRumble)
[docs] def setRumble(self, type_, value):
if value < 0:
value = 0
if value > 1:
value = 1
if type_ == GenericHID.RumbleType.kLeftRumble:
self.leftRumble = int(value * 65535)
else:
self.rightRumble = int(value * 65535)
hal.setJoystickOutputs(
self.port,
self.outputs,
self.leftRumble,
self.rightRumble)