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