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.hal.simulation.AnalogInDataJNI; 008import edu.wpi.first.hal.simulation.NotifyCallback; 009import edu.wpi.first.wpilibj.AnalogInput; 010 011/** Class to control a simulated analog input. */ 012public class AnalogInputSim { 013 private final int m_index; 014 015 /** 016 * Constructs from an AnalogInput object. 017 * 018 * @param analogInput AnalogInput to simulate 019 */ 020 public AnalogInputSim(AnalogInput analogInput) { 021 m_index = analogInput.getChannel(); 022 } 023 024 /** 025 * Constructs from an analog input channel number. 026 * 027 * @param channel Channel number 028 */ 029 public AnalogInputSim(int channel) { 030 m_index = channel; 031 } 032 033 /** 034 * Register a callback on whether the analog input is initialized. 035 * 036 * @param callback the callback that will be called whenever the analog input is initialized 037 * @param initialNotify if true, the callback will be run on the initial value 038 * @return the {@link CallbackStore} object associated with this callback. Save a reference to 039 * this object so GC doesn't cancel the callback. 040 */ 041 public CallbackStore registerInitializedCallback(NotifyCallback callback, boolean initialNotify) { 042 int uid = AnalogInDataJNI.registerInitializedCallback(m_index, callback, initialNotify); 043 return new CallbackStore(m_index, uid, AnalogInDataJNI::cancelInitializedCallback); 044 } 045 046 /** 047 * Check if this analog input has been initialized. 048 * 049 * @return true if initialized 050 */ 051 public boolean getInitialized() { 052 return AnalogInDataJNI.getInitialized(m_index); 053 } 054 055 /** 056 * Change whether this analog input has been initialized. 057 * 058 * @param initialized the new value 059 */ 060 public void setInitialized(boolean initialized) { 061 AnalogInDataJNI.setInitialized(m_index, initialized); 062 } 063 064 /** 065 * Register a callback on the number of average bits. 066 * 067 * @param callback the callback that will be called whenever the number of average bits is changed 068 * @param initialNotify if true, the callback will be run on the initial value 069 * @return the {@link CallbackStore} object associated with this callback. Save a reference to 070 * this object so GC doesn't cancel the callback. 071 */ 072 public CallbackStore registerAverageBitsCallback(NotifyCallback callback, boolean initialNotify) { 073 int uid = AnalogInDataJNI.registerAverageBitsCallback(m_index, callback, initialNotify); 074 return new CallbackStore(m_index, uid, AnalogInDataJNI::cancelAverageBitsCallback); 075 } 076 077 /** 078 * Get the number of average bits. 079 * 080 * @return the number of average bits 081 */ 082 public int getAverageBits() { 083 return AnalogInDataJNI.getAverageBits(m_index); 084 } 085 086 /** 087 * Change the number of average bits. 088 * 089 * @param averageBits the new value 090 */ 091 public void setAverageBits(int averageBits) { 092 AnalogInDataJNI.setAverageBits(m_index, averageBits); 093 } 094 095 /** 096 * Register a callback on the amount of oversampling bits. 097 * 098 * @param callback the callback that will be called whenever the oversampling bits are changed. 099 * @param initialNotify if true, the callback will be run on the initial value 100 * @return the {@link CallbackStore} object associated with this callback. Save a reference to 101 * this object so GC doesn't cancel the callback. 102 */ 103 public CallbackStore registerOversampleBitsCallback( 104 NotifyCallback callback, boolean initialNotify) { 105 int uid = AnalogInDataJNI.registerOversampleBitsCallback(m_index, callback, initialNotify); 106 return new CallbackStore(m_index, uid, AnalogInDataJNI::cancelOversampleBitsCallback); 107 } 108 109 /** 110 * Get the amount of oversampling bits. 111 * 112 * @return the amount of oversampling bits 113 */ 114 public int getOversampleBits() { 115 return AnalogInDataJNI.getOversampleBits(m_index); 116 } 117 118 /** 119 * Change the amount of oversampling bits. 120 * 121 * @param oversampleBits the new value 122 */ 123 public void setOversampleBits(int oversampleBits) { 124 AnalogInDataJNI.setOversampleBits(m_index, oversampleBits); 125 } 126 127 /** 128 * Register a callback on the voltage. 129 * 130 * @param callback the callback that will be called whenever the voltage is changed. 131 * @param initialNotify if true, the callback will be run on the initial value 132 * @return the {@link CallbackStore} object associated with this callback. Save a reference to 133 * this object so GC doesn't cancel the callback. 134 */ 135 public CallbackStore registerVoltageCallback(NotifyCallback callback, boolean initialNotify) { 136 int uid = AnalogInDataJNI.registerVoltageCallback(m_index, callback, initialNotify); 137 return new CallbackStore(m_index, uid, AnalogInDataJNI::cancelVoltageCallback); 138 } 139 140 /** 141 * Get the voltage. 142 * 143 * @return the voltage 144 */ 145 public double getVoltage() { 146 return AnalogInDataJNI.getVoltage(m_index); 147 } 148 149 /** 150 * Change the voltage. 151 * 152 * @param voltage the new value 153 */ 154 public void setVoltage(double voltage) { 155 AnalogInDataJNI.setVoltage(m_index, voltage); 156 } 157 158 /** 159 * Register a callback on whether the accumulator is initialized. 160 * 161 * @param callback the callback that will be called whenever the accumulator is initialized 162 * @param initialNotify if true, the callback will be run on the initial value 163 * @return the {@link CallbackStore} object associated with this callback. Save a reference to 164 * this object so GC doesn't cancel the callback. 165 */ 166 public CallbackStore registerAccumulatorInitializedCallback( 167 NotifyCallback callback, boolean initialNotify) { 168 int uid = 169 AnalogInDataJNI.registerAccumulatorInitializedCallback(m_index, callback, initialNotify); 170 return new CallbackStore(m_index, uid, AnalogInDataJNI::cancelAccumulatorInitializedCallback); 171 } 172 173 /** 174 * Check if the accumulator has been initialized. 175 * 176 * @return true if initialized 177 */ 178 public boolean getAccumulatorInitialized() { 179 return AnalogInDataJNI.getAccumulatorInitialized(m_index); 180 } 181 182 /** 183 * Change whether the accumulator has been initialized. 184 * 185 * @param accumulatorInitialized the new value 186 */ 187 public void setAccumulatorInitialized(boolean accumulatorInitialized) { 188 AnalogInDataJNI.setAccumulatorInitialized(m_index, accumulatorInitialized); 189 } 190 191 /** 192 * Register a callback on the accumulator value. 193 * 194 * @param callback the callback that will be called whenever the accumulator value is changed. 195 * @param initialNotify if true, the callback will be run on the initial value 196 * @return the {@link CallbackStore} object associated with this callback. Save a reference to 197 * this object so GC doesn't cancel the callback. 198 */ 199 public CallbackStore registerAccumulatorValueCallback( 200 NotifyCallback callback, boolean initialNotify) { 201 int uid = AnalogInDataJNI.registerAccumulatorValueCallback(m_index, callback, initialNotify); 202 return new CallbackStore(m_index, uid, AnalogInDataJNI::cancelAccumulatorValueCallback); 203 } 204 205 /** 206 * Get the accumulator value. 207 * 208 * @return the accumulator value 209 */ 210 public long getAccumulatorValue() { 211 return AnalogInDataJNI.getAccumulatorValue(m_index); 212 } 213 214 /** 215 * Change the accumulator value. 216 * 217 * @param accumulatorValue the new value 218 */ 219 public void setAccumulatorValue(long accumulatorValue) { 220 AnalogInDataJNI.setAccumulatorValue(m_index, accumulatorValue); 221 } 222 223 /** 224 * Register a callback on the accumulator count. 225 * 226 * @param callback the callback that will be called whenever the accumulator count is changed. 227 * @param initialNotify if true, the callback will be run on the initial value 228 * @return the {@link CallbackStore} object associated with this callback. Save a reference to 229 * this object so GC doesn't cancel the callback. 230 */ 231 public CallbackStore registerAccumulatorCountCallback( 232 NotifyCallback callback, boolean initialNotify) { 233 int uid = AnalogInDataJNI.registerAccumulatorCountCallback(m_index, callback, initialNotify); 234 return new CallbackStore(m_index, uid, AnalogInDataJNI::cancelAccumulatorCountCallback); 235 } 236 237 /** 238 * Get the accumulator count. 239 * 240 * @return the accumulator count. 241 */ 242 public long getAccumulatorCount() { 243 return AnalogInDataJNI.getAccumulatorCount(m_index); 244 } 245 246 /** 247 * Change the accumulator count. 248 * 249 * @param accumulatorCount the new count. 250 */ 251 public void setAccumulatorCount(long accumulatorCount) { 252 AnalogInDataJNI.setAccumulatorCount(m_index, accumulatorCount); 253 } 254 255 /** 256 * Register a callback on the accumulator center. 257 * 258 * @param callback the callback that will be called whenever the accumulator center is changed 259 * @param initialNotify if true, the callback will be run on the initial value 260 * @return the {@link CallbackStore} object associated with this callback. Save a reference to 261 * this object so GC doesn't cancel the callback. 262 */ 263 public CallbackStore registerAccumulatorCenterCallback( 264 NotifyCallback callback, boolean initialNotify) { 265 int uid = AnalogInDataJNI.registerAccumulatorCenterCallback(m_index, callback, initialNotify); 266 return new CallbackStore(m_index, uid, AnalogInDataJNI::cancelAccumulatorCenterCallback); 267 } 268 269 /** 270 * Get the accumulator center. 271 * 272 * @return the accumulator center 273 */ 274 public int getAccumulatorCenter() { 275 return AnalogInDataJNI.getAccumulatorCenter(m_index); 276 } 277 278 /** 279 * Change the accumulator center. 280 * 281 * @param accumulatorCenter the new center 282 */ 283 public void setAccumulatorCenter(int accumulatorCenter) { 284 AnalogInDataJNI.setAccumulatorCenter(m_index, accumulatorCenter); 285 } 286 287 /** 288 * Register a callback on the accumulator deadband. 289 * 290 * @param callback the callback that will be called whenever the accumulator deadband is changed 291 * @param initialNotify if true, the callback will be run on the initial value 292 * @return the {@link CallbackStore} object associated with this callback. Save a reference to 293 * this object so GC doesn't cancel the callback. 294 */ 295 public CallbackStore registerAccumulatorDeadbandCallback( 296 NotifyCallback callback, boolean initialNotify) { 297 int uid = AnalogInDataJNI.registerAccumulatorDeadbandCallback(m_index, callback, initialNotify); 298 return new CallbackStore(m_index, uid, AnalogInDataJNI::cancelAccumulatorDeadbandCallback); 299 } 300 301 /** 302 * Get the accumulator deadband. 303 * 304 * @return the accumulator deadband 305 */ 306 public int getAccumulatorDeadband() { 307 return AnalogInDataJNI.getAccumulatorDeadband(m_index); 308 } 309 310 /** 311 * Change the accumulator deadband. 312 * 313 * @param accumulatorDeadband the new deadband 314 */ 315 public void setAccumulatorDeadband(int accumulatorDeadband) { 316 AnalogInDataJNI.setAccumulatorDeadband(m_index, accumulatorDeadband); 317 } 318 319 /** Reset all simulation data for this object. */ 320 public void resetData() { 321 AnalogInDataJNI.resetData(m_index); 322 } 323}