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