001// Copyright (c) FIRST and other WPILib contributors. 002// Open Source Software; you can modify and/or share it under the terms of 003// the WPILib BSD license file in the root directory of this project. 004 005package org.wpilib.simulation; 006 007import org.wpilib.driverstation.DriverStation; 008import org.wpilib.driverstation.GenericHID; 009 010/** Class to control a simulated generic joystick. */ 011public class GenericHIDSim { 012 /** GenericHID port. */ 013 protected final int m_port; 014 015 /** 016 * Constructs from a GenericHID object. 017 * 018 * @param joystick joystick to simulate 019 */ 020 public GenericHIDSim(GenericHID joystick) { 021 m_port = joystick.getPort(); 022 } 023 024 /** 025 * Constructs from a joystick port number. 026 * 027 * @param port port number 028 */ 029 public GenericHIDSim(int port) { 030 m_port = port; 031 } 032 033 /** Updates joystick data so that new values are visible to the user program. */ 034 public void notifyNewData() { 035 DriverStationSim.notifyNewData(); 036 } 037 038 /** 039 * Set the value of a given button. 040 * 041 * @param button the button to set 042 * @param value the new value 043 */ 044 public void setRawButton(int button, boolean value) { 045 DriverStationSim.setJoystickButton(m_port, button, value); 046 } 047 048 /** 049 * Set the value of a given axis. 050 * 051 * @param axis the axis to set 052 * @param value the new value 053 */ 054 public void setRawAxis(int axis, double value) { 055 DriverStationSim.setJoystickAxis(m_port, axis, value); 056 } 057 058 /** 059 * Set the value of a given POV. 060 * 061 * @param pov the POV to set 062 * @param value the new value 063 */ 064 public void setPOV(int pov, DriverStation.POVDirection value) { 065 DriverStationSim.setJoystickPOV(m_port, pov, value); 066 } 067 068 /** 069 * Set the value of the default POV (port 0). 070 * 071 * @param value the new value 072 */ 073 public void setPOV(DriverStation.POVDirection value) { 074 setPOV(0, value); 075 } 076 077 /** 078 * Set the maximum axis index for this device. 079 * 080 * @param maximumIndex the new maximum axis index 081 */ 082 public void setAxesMaximumIndex(int maximumIndex) { 083 DriverStationSim.setJoystickAxesMaximumIndex(m_port, maximumIndex); 084 } 085 086 /** 087 * Set the axis count of this device. 088 * 089 * @param count the new axis count 090 */ 091 public void setAxesAvailable(int count) { 092 DriverStationSim.setJoystickAxesAvailable(m_port, count); 093 } 094 095 /** 096 * Set the maximum POV index for this device. 097 * 098 * @param maximumIndex the new maximum POV index 099 */ 100 public void setPOVsMaximumIndex(int maximumIndex) { 101 DriverStationSim.setJoystickPOVsMaximumIndex(m_port, maximumIndex); 102 } 103 104 /** 105 * Set the POV count of this device. 106 * 107 * @param count the new POV count 108 */ 109 public void setPOVsAvailable(int count) { 110 DriverStationSim.setJoystickPOVsAvailable(m_port, count); 111 } 112 113 /** 114 * Set the maximum button index for this device. 115 * 116 * @param maximumIndex the new maximum button index 117 */ 118 public void setButtonsMaximumIndex(int maximumIndex) { 119 DriverStationSim.setJoystickButtonsMaximumIndex(m_port, maximumIndex); 120 } 121 122 /** 123 * Set the button count of this device. 124 * 125 * @param count the new button count 126 */ 127 public void setButtonsAvailable(long count) { 128 DriverStationSim.setJoystickButtonsAvailable(m_port, count); 129 } 130 131 /** 132 * Set the type of this device. 133 * 134 * @param type the new device type 135 */ 136 public void setGamepadType(GenericHID.HIDType type) { 137 DriverStationSim.setJoystickGamepadType(m_port, type.value); 138 } 139 140 /** 141 * Set the supported outputs of this device. 142 * 143 * @param supportedOutputs the new supported outputs 144 */ 145 public void setSupportedOutputs(int supportedOutputs) { 146 DriverStationSim.setJoystickSupportedOutputs(m_port, supportedOutputs); 147 } 148 149 /** 150 * Set the name of this device. 151 * 152 * @param name the new device name 153 */ 154 public void setName(String name) { 155 DriverStationSim.setJoystickName(m_port, name); 156 } 157 158 /** 159 * Get the led color set. 160 * 161 * @return the led color set 162 */ 163 public int getLeds() { 164 return DriverStationSim.getJoystickLeds(m_port); 165 } 166 167 /** 168 * Get the joystick rumble. 169 * 170 * @param type the rumble to read 171 * @return the rumble value 172 */ 173 public double getRumble(GenericHID.RumbleType type) { 174 int intType = 175 switch (type) { 176 case kLeftRumble -> 0; 177 case kRightRumble -> 1; 178 case kLeftTriggerRumble -> 2; 179 case kRightTriggerRumble -> 3; 180 }; 181 int value = DriverStationSim.getJoystickRumble(m_port, intType); 182 return value / 65535.0; 183 } 184}