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.PWMDataJNI; 009import edu.wpi.first.wpilibj.PWM; 010 011/** Class to control a simulated PWM output. */ 012public class PWMSim { 013 private final int m_index; 014 015 /** 016 * Constructs from a PWM object. 017 * 018 * @param pwm PWM to simulate 019 */ 020 public PWMSim(PWM pwm) { 021 m_index = pwm.getChannel(); 022 } 023 024 /** 025 * Constructs from a PWM channel number. 026 * 027 * @param channel Channel number 028 */ 029 public PWMSim(int channel) { 030 m_index = channel; 031 } 032 033 /** 034 * Register a callback to be run when the PWM 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 registerInitializedCallback(NotifyCallback callback, boolean initialNotify) { 042 int uid = PWMDataJNI.registerInitializedCallback(m_index, callback, initialNotify); 043 return new CallbackStore(m_index, uid, PWMDataJNI::cancelInitializedCallback); 044 } 045 046 /** 047 * Check whether the PWM has been initialized. 048 * 049 * @return true if initialized 050 */ 051 public boolean getInitialized() { 052 return PWMDataJNI.getInitialized(m_index); 053 } 054 055 /** 056 * Define whether the PWM has been initialized. 057 * 058 * @param initialized whether this object is initialized 059 */ 060 public void setInitialized(boolean initialized) { 061 PWMDataJNI.setInitialized(m_index, initialized); 062 } 063 064 /** 065 * Register a callback to be run when the PWM raw value changes. 066 * 067 * @param callback the callback 068 * @param initialNotify whether to run the callback with 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 registerRawValueCallback(NotifyCallback callback, boolean initialNotify) { 073 int uid = PWMDataJNI.registerRawValueCallback(m_index, callback, initialNotify); 074 return new CallbackStore(m_index, uid, PWMDataJNI::cancelRawValueCallback); 075 } 076 077 /** 078 * Get the PWM raw value. 079 * 080 * @return the PWM raw value 081 */ 082 public int getRawValue() { 083 return PWMDataJNI.getRawValue(m_index); 084 } 085 086 /** 087 * Set the PWM raw value. 088 * 089 * @param rawValue the PWM raw value 090 */ 091 public void setRawValue(int rawValue) { 092 PWMDataJNI.setRawValue(m_index, rawValue); 093 } 094 095 /** 096 * Register a callback to be run when the PWM speed changes. 097 * 098 * @param callback the callback 099 * @param initialNotify whether to run the callback with 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 registerSpeedCallback(NotifyCallback callback, boolean initialNotify) { 104 int uid = PWMDataJNI.registerSpeedCallback(m_index, callback, initialNotify); 105 return new CallbackStore(m_index, uid, PWMDataJNI::cancelSpeedCallback); 106 } 107 108 /** 109 * Get the PWM speed. 110 * 111 * @return the PWM speed (-1.0 to 1.0) 112 */ 113 public double getSpeed() { 114 return PWMDataJNI.getSpeed(m_index); 115 } 116 117 /** 118 * Set the PWM speed. 119 * 120 * @param speed the PWM speed (-1.0 to 1.0) 121 */ 122 public void setSpeed(double speed) { 123 PWMDataJNI.setSpeed(m_index, speed); 124 } 125 126 /** 127 * Register a callback to be run when the PWM position changes. 128 * 129 * @param callback the callback 130 * @param initialNotify whether to run the callback with the initial value 131 * @return the {@link CallbackStore} object associated with this callback. Save a reference to 132 * this object so GC doesn't cancel the callback. 133 */ 134 public CallbackStore registerPositionCallback(NotifyCallback callback, boolean initialNotify) { 135 int uid = PWMDataJNI.registerPositionCallback(m_index, callback, initialNotify); 136 return new CallbackStore(m_index, uid, PWMDataJNI::cancelPositionCallback); 137 } 138 139 /** 140 * Get the PWM position. 141 * 142 * @return the PWM position (0.0 to 1.0) 143 */ 144 public double getPosition() { 145 return PWMDataJNI.getPosition(m_index); 146 } 147 148 /** 149 * Set the PWM position. 150 * 151 * @param position the PWM position (0.0 to 1.0) 152 */ 153 public void setPosition(double position) { 154 PWMDataJNI.setPosition(m_index, position); 155 } 156 157 /** 158 * Register a callback to be run when the PWM period scale changes. 159 * 160 * @param callback the callback 161 * @param initialNotify whether to run the callback with the initial value 162 * @return the {@link CallbackStore} object associated with this callback. Save a reference to 163 * this object so GC doesn't cancel the callback. 164 */ 165 public CallbackStore registerPeriodScaleCallback(NotifyCallback callback, boolean initialNotify) { 166 int uid = PWMDataJNI.registerPeriodScaleCallback(m_index, callback, initialNotify); 167 return new CallbackStore(m_index, uid, PWMDataJNI::cancelPeriodScaleCallback); 168 } 169 170 /** 171 * Get the PWM period scale. 172 * 173 * @return the PWM period scale 174 */ 175 public int getPeriodScale() { 176 return PWMDataJNI.getPeriodScale(m_index); 177 } 178 179 /** 180 * Set the PWM period scale. 181 * 182 * @param periodScale the PWM period scale 183 */ 184 public void setPeriodScale(int periodScale) { 185 PWMDataJNI.setPeriodScale(m_index, periodScale); 186 } 187 188 /** 189 * Register a callback to be run when the PWM zero latch state changes. 190 * 191 * @param callback the callback 192 * @param initialNotify whether to run the callback with the initial state 193 * @return the {@link CallbackStore} object associated with this callback. Save a reference to 194 * this object so GC doesn't cancel the callback. 195 */ 196 public CallbackStore registerZeroLatchCallback(NotifyCallback callback, boolean initialNotify) { 197 int uid = PWMDataJNI.registerZeroLatchCallback(m_index, callback, initialNotify); 198 return new CallbackStore(m_index, uid, PWMDataJNI::cancelZeroLatchCallback); 199 } 200 201 /** 202 * Check whether the PWM is zero latched. 203 * 204 * @return true if zero latched 205 */ 206 public boolean getZeroLatch() { 207 return PWMDataJNI.getZeroLatch(m_index); 208 } 209 210 /** 211 * Define whether the PWM has been zero latched. 212 * 213 * @param zeroLatch true to indicate zero latched 214 */ 215 public void setZeroLatch(boolean zeroLatch) { 216 PWMDataJNI.setZeroLatch(m_index, zeroLatch); 217 } 218 219 /** Reset all simulation data. */ 220 public void resetData() { 221 PWMDataJNI.resetData(m_index); 222 } 223}