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.NotifyCallback; 008import edu.wpi.first.hal.simulation.RelayDataJNI; 009import edu.wpi.first.wpilibj.Relay; 010 011/** Class to control a simulated relay. */ 012public class RelaySim { 013 private final int m_index; 014 015 /** 016 * Constructs from a Relay object. 017 * 018 * @param relay Relay to simulate 019 */ 020 public RelaySim(Relay relay) { 021 m_index = relay.getChannel(); 022 } 023 024 /** 025 * Constructs from a relay channel number. 026 * 027 * @param channel Channel number 028 */ 029 public RelaySim(int channel) { 030 m_index = channel; 031 } 032 033 /** 034 * Register a callback to be run when the forward direction is initialized. 035 * 036 * @param callback the callback 037 * @param initialNotify whether to run the callback with the initial state 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 registerInitializedForwardCallback( 042 NotifyCallback callback, boolean initialNotify) { 043 int uid = RelayDataJNI.registerInitializedForwardCallback(m_index, callback, initialNotify); 044 return new CallbackStore(m_index, uid, RelayDataJNI::cancelInitializedForwardCallback); 045 } 046 047 /** 048 * Check whether the forward direction has been initialized. 049 * 050 * @return true if initialized 051 */ 052 public boolean getInitializedForward() { 053 return RelayDataJNI.getInitializedForward(m_index); 054 } 055 056 /** 057 * Define whether the forward direction has been initialized. 058 * 059 * @param initializedForward whether this object is initialized 060 */ 061 public void setInitializedForward(boolean initializedForward) { 062 RelayDataJNI.setInitializedForward(m_index, initializedForward); 063 } 064 065 /** 066 * Register a callback to be run when the reverse direction is initialized. 067 * 068 * @param callback the callback 069 * @param initialNotify whether to run the callback with the initial state 070 * @return the {@link CallbackStore} object associated with this callback. Save a reference to 071 * this object so GC doesn't cancel the callback. 072 */ 073 public CallbackStore registerInitializedReverseCallback( 074 NotifyCallback callback, boolean initialNotify) { 075 int uid = RelayDataJNI.registerInitializedReverseCallback(m_index, callback, initialNotify); 076 return new CallbackStore(m_index, uid, RelayDataJNI::cancelInitializedReverseCallback); 077 } 078 079 /** 080 * Check whether the reverse direction has been initialized. 081 * 082 * @return true if initialized 083 */ 084 public boolean getInitializedReverse() { 085 return RelayDataJNI.getInitializedReverse(m_index); 086 } 087 088 /** 089 * Define whether the reverse direction has been initialized. 090 * 091 * @param initializedReverse whether this object is initialized 092 */ 093 public void setInitializedReverse(boolean initializedReverse) { 094 RelayDataJNI.setInitializedReverse(m_index, initializedReverse); 095 } 096 097 /** 098 * Register a callback to be run when the forward direction changes state. 099 * 100 * @param callback the callback 101 * @param initialNotify whether to run the callback with the initial state 102 * @return the {@link CallbackStore} object associated with this callback. Save a reference to 103 * this object so GC doesn't cancel the callback. 104 */ 105 public CallbackStore registerForwardCallback(NotifyCallback callback, boolean initialNotify) { 106 int uid = RelayDataJNI.registerForwardCallback(m_index, callback, initialNotify); 107 return new CallbackStore(m_index, uid, RelayDataJNI::cancelForwardCallback); 108 } 109 110 /** 111 * Check whether the forward direction is active. 112 * 113 * @return true if active 114 */ 115 public boolean getForward() { 116 return RelayDataJNI.getForward(m_index); 117 } 118 119 /** 120 * Set whether the forward direction is active. 121 * 122 * @param forward true to make active 123 */ 124 public void setForward(boolean forward) { 125 RelayDataJNI.setForward(m_index, forward); 126 } 127 128 /** 129 * Register a callback to be run when the reverse direction changes state. 130 * 131 * @param callback the callback 132 * @param initialNotify whether to run the callback with the initial state 133 * @return the {@link CallbackStore} object associated with this callback. Save a reference to 134 * this object so GC doesn't cancel the callback. 135 */ 136 public CallbackStore registerReverseCallback(NotifyCallback callback, boolean initialNotify) { 137 int uid = RelayDataJNI.registerReverseCallback(m_index, callback, initialNotify); 138 return new CallbackStore(m_index, uid, RelayDataJNI::cancelReverseCallback); 139 } 140 141 /** 142 * Check whether the reverse direction is active. 143 * 144 * @return true if active 145 */ 146 public boolean getReverse() { 147 return RelayDataJNI.getReverse(m_index); 148 } 149 150 /** 151 * Set whether the reverse direction is active. 152 * 153 * @param reverse true to make active 154 */ 155 public void setReverse(boolean reverse) { 156 RelayDataJNI.setReverse(m_index, reverse); 157 } 158 159 /** Reset all simulation data. */ 160 public void resetData() { 161 RelayDataJNI.resetData(m_index); 162 } 163}