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.GenericHID; 008 009/** Class to control a simulated generic joystick. */ 010public class GenericHIDSim { 011 protected final int m_port; 012 013 /** 014 * Constructs from a GenericHID object. 015 * 016 * @param joystick joystick to simulate 017 */ 018 public GenericHIDSim(GenericHID joystick) { 019 m_port = joystick.getPort(); 020 } 021 022 /** 023 * Constructs from a joystick port number. 024 * 025 * @param port port number 026 */ 027 public GenericHIDSim(int port) { 028 m_port = port; 029 } 030 031 /** Updates joystick data so that new values are visible to the user program. */ 032 public void notifyNewData() { 033 DriverStationSim.notifyNewData(); 034 } 035 036 /** 037 * Set the value of a given button. 038 * 039 * @param button the button to set 040 * @param value the new value 041 */ 042 public void setRawButton(int button, boolean value) { 043 DriverStationSim.setJoystickButton(m_port, button, value); 044 } 045 046 /** 047 * Set the value of a given axis. 048 * 049 * @param axis the axis to set 050 * @param value the new value 051 */ 052 public void setRawAxis(int axis, double value) { 053 DriverStationSim.setJoystickAxis(m_port, axis, value); 054 } 055 056 /** 057 * Set the value of a given POV. 058 * 059 * @param pov the POV to set 060 * @param value the new value 061 */ 062 public void setPOV(int pov, int value) { 063 DriverStationSim.setJoystickPOV(m_port, pov, value); 064 } 065 066 /** 067 * Set the value of the default POV (port 0). 068 * 069 * @param value the new value 070 */ 071 public void setPOV(int value) { 072 setPOV(0, value); 073 } 074 075 /** 076 * Set the axis count of this device. 077 * 078 * @param count the new axis count 079 */ 080 public void setAxisCount(int count) { 081 DriverStationSim.setJoystickAxisCount(m_port, count); 082 } 083 084 /** 085 * Set the POV count of this device. 086 * 087 * @param count the new POV count 088 */ 089 public void setPOVCount(int count) { 090 DriverStationSim.setJoystickPOVCount(m_port, count); 091 } 092 093 /** 094 * Set the button count of this device. 095 * 096 * @param count the new button count 097 */ 098 public void setButtonCount(int count) { 099 DriverStationSim.setJoystickButtonCount(m_port, count); 100 } 101 102 /** 103 * Set the type of this device. 104 * 105 * @param type the new device type 106 */ 107 public void setType(GenericHID.HIDType type) { 108 DriverStationSim.setJoystickType(m_port, type.value); 109 } 110 111 /** 112 * Set the name of this device. 113 * 114 * @param name the new device name 115 */ 116 public void setName(String name) { 117 DriverStationSim.setJoystickName(m_port, name); 118 } 119 120 /** 121 * Set the type of an axis. 122 * 123 * @param axis the axis 124 * @param type the type 125 */ 126 public void setAxisType(int axis, int type) { 127 DriverStationSim.setJoystickAxisType(m_port, axis, type); 128 } 129 130 /** 131 * Read the output of a button. 132 * 133 * @param outputNumber the button number 134 * @return the value of the button (true = pressed) 135 */ 136 public boolean getOutput(int outputNumber) { 137 long outputs = getOutputs(); 138 return (outputs & (1 << (outputNumber - 1))) != 0; 139 } 140 141 /** 142 * Get the encoded 16-bit integer that passes button values. 143 * 144 * @return the button values 145 */ 146 public long getOutputs() { 147 return DriverStationSim.getJoystickOutputs(m_port); 148 } 149 150 /** 151 * Get the joystick rumble. 152 * 153 * @param type the rumble to read 154 * @return the rumble value 155 */ 156 public double getRumble(GenericHID.RumbleType type) { 157 int value = 158 DriverStationSim.getJoystickRumble( 159 m_port, type == GenericHID.RumbleType.kLeftRumble ? 0 : 1); 160 return value / 65535.0; 161 } 162}