2016-11-18 23:05:37 -08:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-08-23 22:06:13 -07:00
|
|
|
/* Copyright (c) 2016-2017 FIRST. All Rights Reserved. */
|
2016-11-18 23:05:37 -08:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "XboxController.h"
|
|
|
|
|
|
2017-08-27 00:11:52 -07:00
|
|
|
#include <HAL/HAL.h>
|
|
|
|
|
|
2016-11-18 23:05:37 -08:00
|
|
|
using namespace frc;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Construct an instance of an Xbox controller.
|
|
|
|
|
*
|
2017-10-27 21:45:56 -07:00
|
|
|
* The controller index is the USB port on the Driver Station.
|
2016-11-18 23:05:37 -08:00
|
|
|
*
|
2017-10-27 21:45:56 -07:00
|
|
|
* @param port The port on the Driver Station that the controller is plugged
|
|
|
|
|
* into (0-5).
|
2016-11-18 23:05:37 -08:00
|
|
|
*/
|
2017-10-27 21:45:56 -07:00
|
|
|
XboxController::XboxController(int port) : GenericHID(port) {
|
2016-11-18 23:05:37 -08:00
|
|
|
// HAL_Report(HALUsageReporting::kResourceType_XboxController, port);
|
|
|
|
|
HAL_Report(HALUsageReporting::kResourceType_Joystick, port);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the X axis value of the controller.
|
|
|
|
|
*
|
|
|
|
|
* @param hand Side of controller whose value should be returned.
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
double XboxController::GetX(JoystickHand hand) const {
|
2016-11-18 23:05:37 -08:00
|
|
|
if (hand == kLeftHand) {
|
|
|
|
|
return GetRawAxis(0);
|
|
|
|
|
} else {
|
|
|
|
|
return GetRawAxis(4);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the Y axis value of the controller.
|
|
|
|
|
*
|
|
|
|
|
* @param hand Side of controller whose value should be returned.
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
double XboxController::GetY(JoystickHand hand) const {
|
2016-11-18 23:05:37 -08:00
|
|
|
if (hand == kLeftHand) {
|
|
|
|
|
return GetRawAxis(1);
|
|
|
|
|
} else {
|
|
|
|
|
return GetRawAxis(5);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-27 21:45:56 -07:00
|
|
|
/**
|
|
|
|
|
* Get the trigger axis value of the controller.
|
|
|
|
|
*
|
|
|
|
|
* @param hand Side of controller whose value should be returned.
|
|
|
|
|
*/
|
|
|
|
|
double XboxController::GetTriggerAxis(JoystickHand hand) const {
|
|
|
|
|
if (hand == kLeftHand) {
|
|
|
|
|
return GetRawAxis(2);
|
|
|
|
|
} else {
|
|
|
|
|
return GetRawAxis(3);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-18 23:05:37 -08:00
|
|
|
/**
|
|
|
|
|
* Read the value of the bumper button on the controller.
|
|
|
|
|
*
|
|
|
|
|
* @param hand Side of controller whose value should be returned.
|
|
|
|
|
*/
|
|
|
|
|
bool XboxController::GetBumper(JoystickHand hand) const {
|
|
|
|
|
if (hand == kLeftHand) {
|
2017-10-27 21:45:56 -07:00
|
|
|
return GetRawButton(static_cast<int>(Button::kBumperLeft));
|
|
|
|
|
} else {
|
|
|
|
|
return GetRawButton(static_cast<int>(Button::kBumperRight));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Whether the bumper was pressed since the last check.
|
|
|
|
|
*
|
|
|
|
|
* @param hand Side of controller whose value should be returned.
|
|
|
|
|
* @return Whether the button was pressed since the last check.
|
|
|
|
|
*/
|
|
|
|
|
bool XboxController::GetBumperPressed(JoystickHand hand) {
|
|
|
|
|
if (hand == kLeftHand) {
|
|
|
|
|
return GetRawButtonPressed(static_cast<int>(Button::kBumperLeft));
|
|
|
|
|
} else {
|
|
|
|
|
return GetRawButtonPressed(static_cast<int>(Button::kBumperRight));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Whether the bumper was released since the last check.
|
|
|
|
|
*
|
|
|
|
|
* @param hand Side of controller whose value should be returned.
|
|
|
|
|
* @return Whether the button was released since the last check.
|
|
|
|
|
*/
|
|
|
|
|
bool XboxController::GetBumperReleased(JoystickHand hand) {
|
|
|
|
|
if (hand == kLeftHand) {
|
|
|
|
|
return GetRawButtonReleased(static_cast<int>(Button::kBumperLeft));
|
2016-11-18 23:05:37 -08:00
|
|
|
} else {
|
2017-10-27 21:45:56 -07:00
|
|
|
return GetRawButtonReleased(static_cast<int>(Button::kBumperRight));
|
2016-11-18 23:05:37 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read the value of the stick button on the controller.
|
|
|
|
|
*
|
|
|
|
|
* @param hand Side of controller whose value should be returned.
|
|
|
|
|
* @return The state of the button.
|
|
|
|
|
*/
|
|
|
|
|
bool XboxController::GetStickButton(JoystickHand hand) const {
|
|
|
|
|
if (hand == kLeftHand) {
|
2017-10-27 21:45:56 -07:00
|
|
|
return GetRawButton(static_cast<int>(Button::kStickLeft));
|
2016-11-18 23:05:37 -08:00
|
|
|
} else {
|
2017-10-27 21:45:56 -07:00
|
|
|
return GetRawButton(static_cast<int>(Button::kStickRight));
|
2016-11-18 23:05:37 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-10-27 21:45:56 -07:00
|
|
|
* Whether the stick button was pressed since the last check.
|
2016-11-18 23:05:37 -08:00
|
|
|
*
|
|
|
|
|
* @param hand Side of controller whose value should be returned.
|
2017-10-27 21:45:56 -07:00
|
|
|
* @return Whether the button was pressed since the last check.
|
2016-11-18 23:05:37 -08:00
|
|
|
*/
|
2017-10-27 21:45:56 -07:00
|
|
|
bool XboxController::GetStickButtonPressed(JoystickHand hand) {
|
2016-11-18 23:05:37 -08:00
|
|
|
if (hand == kLeftHand) {
|
2017-10-27 21:45:56 -07:00
|
|
|
return GetRawButtonPressed(static_cast<int>(Button::kStickLeft));
|
2016-11-18 23:05:37 -08:00
|
|
|
} else {
|
2017-10-27 21:45:56 -07:00
|
|
|
return GetRawButtonPressed(static_cast<int>(Button::kStickRight));
|
2016-11-18 23:05:37 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-10-27 21:45:56 -07:00
|
|
|
* Whether the stick button was released since the last check.
|
2016-11-18 23:05:37 -08:00
|
|
|
*
|
|
|
|
|
* @param hand Side of controller whose value should be returned.
|
2017-10-27 21:45:56 -07:00
|
|
|
* @return Whether the button was released since the last check.
|
|
|
|
|
*/
|
|
|
|
|
bool XboxController::GetStickButtonReleased(JoystickHand hand) {
|
|
|
|
|
if (hand == kLeftHand) {
|
|
|
|
|
return GetRawButtonReleased(static_cast<int>(Button::kStickLeft));
|
|
|
|
|
} else {
|
|
|
|
|
return GetRawButtonReleased(static_cast<int>(Button::kStickRight));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read the value of the A button on the controller.
|
|
|
|
|
*
|
2016-11-18 23:05:37 -08:00
|
|
|
* @return The state of the button.
|
|
|
|
|
*/
|
2017-10-27 21:45:56 -07:00
|
|
|
bool XboxController::GetAButton() const {
|
|
|
|
|
return GetRawButton(static_cast<int>(Button::kA));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Whether the A button was pressed since the last check.
|
|
|
|
|
*
|
|
|
|
|
* @return Whether the button was pressed since the last check.
|
|
|
|
|
*/
|
|
|
|
|
bool XboxController::GetAButtonPressed() {
|
|
|
|
|
return GetRawButtonPressed(static_cast<int>(Button::kA));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Whether the A button was released since the last check.
|
|
|
|
|
*
|
|
|
|
|
* @return Whether the button was released since the last check.
|
|
|
|
|
*/
|
|
|
|
|
bool XboxController::GetAButtonReleased() {
|
|
|
|
|
return GetRawButtonReleased(static_cast<int>(Button::kA));
|
|
|
|
|
}
|
2016-11-18 23:05:37 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read the value of the B button on the controller.
|
|
|
|
|
*
|
|
|
|
|
* @return The state of the button.
|
|
|
|
|
*/
|
2017-10-27 21:45:56 -07:00
|
|
|
bool XboxController::GetBButton() const {
|
|
|
|
|
return GetRawButton(static_cast<int>(Button::kB));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Whether the B button was pressed since the last check.
|
|
|
|
|
*
|
|
|
|
|
* @return Whether the button was pressed since the last check.
|
|
|
|
|
*/
|
|
|
|
|
bool XboxController::GetBButtonPressed() {
|
|
|
|
|
return GetRawButtonPressed(static_cast<int>(Button::kB));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Whether the B button was released since the last check.
|
|
|
|
|
*
|
|
|
|
|
* @return Whether the button was released since the last check.
|
|
|
|
|
*/
|
|
|
|
|
bool XboxController::GetBButtonReleased() {
|
|
|
|
|
return GetRawButtonReleased(static_cast<int>(Button::kB));
|
|
|
|
|
}
|
2016-11-18 23:05:37 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read the value of the X button on the controller.
|
|
|
|
|
*
|
|
|
|
|
* @return The state of the button.
|
|
|
|
|
*/
|
2017-10-27 21:45:56 -07:00
|
|
|
bool XboxController::GetXButton() const {
|
|
|
|
|
return GetRawButton(static_cast<int>(Button::kX));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Whether the X button was pressed since the last check.
|
|
|
|
|
*
|
|
|
|
|
* @return Whether the button was pressed since the last check.
|
|
|
|
|
*/
|
|
|
|
|
bool XboxController::GetXButtonPressed() {
|
|
|
|
|
return GetRawButtonPressed(static_cast<int>(Button::kX));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Whether the X button was released since the last check.
|
|
|
|
|
*
|
|
|
|
|
* @return Whether the button was released since the last check.
|
|
|
|
|
*/
|
|
|
|
|
bool XboxController::GetXButtonReleased() {
|
|
|
|
|
return GetRawButtonReleased(static_cast<int>(Button::kX));
|
|
|
|
|
}
|
2016-11-18 23:05:37 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read the value of the Y button on the controller.
|
|
|
|
|
*
|
|
|
|
|
* @return The state of the button.
|
|
|
|
|
*/
|
2017-10-27 21:45:56 -07:00
|
|
|
bool XboxController::GetYButton() const {
|
|
|
|
|
return GetRawButton(static_cast<int>(Button::kY));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Whether the Y button was pressed since the last check.
|
|
|
|
|
*
|
|
|
|
|
* @return Whether the button was pressed since the last check.
|
|
|
|
|
*/
|
|
|
|
|
bool XboxController::GetYButtonPressed() {
|
|
|
|
|
return GetRawButtonPressed(static_cast<int>(Button::kY));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Whether the Y button was released since the last check.
|
|
|
|
|
*
|
|
|
|
|
* @return Whether the button was released since the last check.
|
|
|
|
|
*/
|
|
|
|
|
bool XboxController::GetYButtonReleased() {
|
|
|
|
|
return GetRawButtonReleased(static_cast<int>(Button::kY));
|
|
|
|
|
}
|
2016-11-18 23:05:37 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read the value of the back button on the controller.
|
|
|
|
|
*
|
|
|
|
|
* @param hand Side of controller whose value should be returned.
|
|
|
|
|
* @return The state of the button.
|
|
|
|
|
*/
|
2017-10-27 21:45:56 -07:00
|
|
|
bool XboxController::GetBackButton() const {
|
|
|
|
|
return GetRawButton(static_cast<int>(Button::kBack));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Whether the back button was pressed since the last check.
|
|
|
|
|
*
|
|
|
|
|
* @return Whether the button was pressed since the last check.
|
|
|
|
|
*/
|
|
|
|
|
bool XboxController::GetBackButtonPressed() {
|
|
|
|
|
return GetRawButtonPressed(static_cast<int>(Button::kBack));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Whether the back button was released since the last check.
|
|
|
|
|
*
|
|
|
|
|
* @return Whether the button was released since the last check.
|
|
|
|
|
*/
|
|
|
|
|
bool XboxController::GetBackButtonReleased() {
|
|
|
|
|
return GetRawButtonReleased(static_cast<int>(Button::kBack));
|
|
|
|
|
}
|
2016-11-18 23:05:37 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read the value of the start button on the controller.
|
|
|
|
|
*
|
|
|
|
|
* @param hand Side of controller whose value should be returned.
|
|
|
|
|
* @return The state of the button.
|
|
|
|
|
*/
|
2017-10-27 21:45:56 -07:00
|
|
|
bool XboxController::GetStartButton() const {
|
|
|
|
|
return GetRawButton(static_cast<int>(Button::kStart));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Whether the start button was pressed since the last check.
|
|
|
|
|
*
|
|
|
|
|
* @return Whether the button was pressed since the last check.
|
|
|
|
|
*/
|
|
|
|
|
bool XboxController::GetStartButtonPressed() {
|
|
|
|
|
return GetRawButtonPressed(static_cast<int>(Button::kStart));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Whether the start button was released since the last check.
|
|
|
|
|
*
|
|
|
|
|
* @return Whether the button was released since the last check.
|
|
|
|
|
*/
|
|
|
|
|
bool XboxController::GetStartButtonReleased() {
|
|
|
|
|
return GetRawButtonReleased(static_cast<int>(Button::kStart));
|
|
|
|
|
}
|