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.hardware.discrete.AnalogInput; 008import org.wpilib.hardware.hal.simulation.AnalogInDataJNI; 009import org.wpilib.hardware.hal.simulation.NotifyCallback; 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. 039 */ 040 public CallbackStore registerInitializedCallback(NotifyCallback callback, boolean initialNotify) { 041 int uid = AnalogInDataJNI.registerInitializedCallback(m_index, callback, initialNotify); 042 return new CallbackStore(m_index, uid, AnalogInDataJNI::cancelInitializedCallback); 043 } 044 045 /** 046 * Check if this analog input has been initialized. 047 * 048 * @return true if initialized 049 */ 050 public boolean getInitialized() { 051 return AnalogInDataJNI.getInitialized(m_index); 052 } 053 054 /** 055 * Change whether this analog input has been initialized. 056 * 057 * @param initialized the new value 058 */ 059 public void setInitialized(boolean initialized) { 060 AnalogInDataJNI.setInitialized(m_index, initialized); 061 } 062 063 /** 064 * Register a callback on the voltage. 065 * 066 * @param callback the callback that will be called whenever the voltage is changed. 067 * @param initialNotify if true, the callback will be run on the initial value 068 * @return the {@link CallbackStore} object associated with this callback. 069 */ 070 public CallbackStore registerVoltageCallback(NotifyCallback callback, boolean initialNotify) { 071 int uid = AnalogInDataJNI.registerVoltageCallback(m_index, callback, initialNotify); 072 return new CallbackStore(m_index, uid, AnalogInDataJNI::cancelVoltageCallback); 073 } 074 075 /** 076 * Get the voltage. 077 * 078 * @return the voltage 079 */ 080 public double getVoltage() { 081 return AnalogInDataJNI.getVoltage(m_index); 082 } 083 084 /** 085 * Change the voltage. 086 * 087 * @param voltage the new value 088 */ 089 public void setVoltage(double voltage) { 090 AnalogInDataJNI.setVoltage(m_index, voltage); 091 } 092 093 /** Reset all simulation data for this object. */ 094 public void resetData() { 095 AnalogInDataJNI.resetData(m_index); 096 } 097}