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.DutyCycleDataJNI; 008import edu.wpi.first.hal.simulation.NotifyCallback; 009import edu.wpi.first.wpilibj.DutyCycle; 010 011/** Class to control a simulated duty cycle digital input. */ 012public class DutyCycleSim { 013 private final int m_index; 014 015 /** 016 * Constructs from a DutyCycle object. 017 * 018 * @param dutyCycle DutyCycle to simulate 019 */ 020 public DutyCycleSim(DutyCycle dutyCycle) { 021 m_index = dutyCycle.getSourceChannel(); 022 } 023 024 private DutyCycleSim(int index) { 025 m_index = index; 026 } 027 028 /** 029 * Creates a DutyCycleSim for a SmartIO channel. 030 * 031 * @param channel SmartIO channel 032 * @return Simulated object 033 */ 034 public static DutyCycleSim createForChannel(int channel) { 035 return new DutyCycleSim(channel); 036 } 037 038 /** 039 * Register a callback to be run when this duty cycle input is initialized. 040 * 041 * @param callback the callback 042 * @param initialNotify whether to run the callback with the initial state 043 * @return the {@link CallbackStore} object associated with this callback. 044 */ 045 public CallbackStore registerInitializedCallback(NotifyCallback callback, boolean initialNotify) { 046 int uid = DutyCycleDataJNI.registerInitializedCallback(m_index, callback, initialNotify); 047 return new CallbackStore(m_index, uid, DutyCycleDataJNI::cancelInitializedCallback); 048 } 049 050 /** 051 * Check whether this duty cycle input has been initialized. 052 * 053 * @return true if initialized 054 */ 055 public boolean getInitialized() { 056 return DutyCycleDataJNI.getInitialized(m_index); 057 } 058 059 /** 060 * Define whether this duty cycle input has been initialized. 061 * 062 * @param initialized whether this object is initialized 063 */ 064 public void setInitialized(boolean initialized) { 065 DutyCycleDataJNI.setInitialized(m_index, initialized); 066 } 067 068 /** 069 * Register a callback to be run whenever the frequency changes. 070 * 071 * @param callback the callback 072 * @param initialNotify whether to call the callback with the initial state 073 * @return the {@link CallbackStore} object associated with this callback. 074 */ 075 public CallbackStore registerFrequencyCallback(NotifyCallback callback, boolean initialNotify) { 076 int uid = DutyCycleDataJNI.registerFrequencyCallback(m_index, callback, initialNotify); 077 return new CallbackStore(m_index, uid, DutyCycleDataJNI::cancelFrequencyCallback); 078 } 079 080 /** 081 * Measure the frequency. 082 * 083 * @return the duty cycle frequency 084 */ 085 public double getFrequency() { 086 return DutyCycleDataJNI.getFrequency(m_index); 087 } 088 089 /** 090 * Change the duty cycle frequency. 091 * 092 * @param frequency the new frequency 093 */ 094 public void setFrequency(double frequency) { 095 DutyCycleDataJNI.setFrequency(m_index, frequency); 096 } 097 098 /** 099 * Register a callback to be run whenever the output changes. 100 * 101 * @param callback the callback 102 * @param initialNotify whether to call the callback with the initial state 103 * @return the {@link CallbackStore} object associated with this callback. 104 */ 105 public CallbackStore registerOutputCallback(NotifyCallback callback, boolean initialNotify) { 106 int uid = DutyCycleDataJNI.registerOutputCallback(m_index, callback, initialNotify); 107 return new CallbackStore(m_index, uid, DutyCycleDataJNI::cancelOutputCallback); 108 } 109 110 /** 111 * Measure the output from this duty cycle port. 112 * 113 * @return the output value 114 */ 115 public double getOutput() { 116 return DutyCycleDataJNI.getOutput(m_index); 117 } 118 119 /** 120 * Change the duty cycle output. 121 * 122 * @param output the new output value 123 */ 124 public void setOutput(double output) { 125 DutyCycleDataJNI.setOutput(m_index, output); 126 } 127 128 /** Reset all simulation data for the duty cycle output. */ 129 public void resetData() { 130 DutyCycleDataJNI.resetData(m_index); 131 } 132}