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 edu.wpi.first.wpilibj.simulation; 006 007import edu.wpi.first.wpilibj.DriverStation; 008import edu.wpi.first.wpilibj.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 axis count of this device. 079 * 080 * @param count the new axis count 081 */ 082 public void setAxisCount(int count) { 083 DriverStationSim.setJoystickAxisCount(m_port, count); 084 } 085 086 /** 087 * Set the POV count of this device. 088 * 089 * @param count the new POV count 090 */ 091 public void setPOVCount(int count) { 092 DriverStationSim.setJoystickPOVCount(m_port, count); 093 } 094 095 /** 096 * Set the button count of this device. 097 * 098 * @param count the new button count 099 */ 100 public void setButtonCount(int count) { 101 DriverStationSim.setJoystickButtonCount(m_port, count); 102 } 103 104 /** 105 * Set the type of this device. 106 * 107 * @param type the new device type 108 */ 109 public void setType(GenericHID.HIDType type) { 110 DriverStationSim.setJoystickType(m_port, type.value); 111 } 112 113 /** 114 * Set the name of this device. 115 * 116 * @param name the new device name 117 */ 118 public void setName(String name) { 119 DriverStationSim.setJoystickName(m_port, name); 120 } 121 122 /** 123 * Set the type of the provided axis channel. 124 * 125 * @param axis the axis 126 * @param type the type 127 */ 128 public void setAxisType(int axis, int type) { 129 DriverStationSim.setJoystickAxisType(m_port, axis, type); 130 } 131 132 /** 133 * Read the output of a button. 134 * 135 * @param outputNumber the button number 136 * @return the value of the button (true = pressed) 137 */ 138 public boolean getOutput(int outputNumber) { 139 long outputs = getOutputs(); 140 return (outputs & (1L << (outputNumber - 1))) != 0; 141 } 142 143 /** 144 * Get the encoded 16-bit integer that passes button values. 145 * 146 * @return the button values 147 */ 148 public long getOutputs() { 149 return DriverStationSim.getJoystickOutputs(m_port); 150 } 151 152 /** 153 * Get the joystick rumble. 154 * 155 * @param type the rumble to read 156 * @return the rumble value 157 */ 158 public double getRumble(GenericHID.RumbleType type) { 159 int value = 160 DriverStationSim.getJoystickRumble( 161 m_port, type == GenericHID.RumbleType.kLeftRumble ? 0 : 1); 162 return value / 65535.0; 163 } 164}