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. 039 */ 040 public CallbackStore registerInitializedCallback(NotifyCallback callback, boolean initialNotify) { 041 int uid = PWMDataJNI.registerInitializedCallback(m_index, callback, initialNotify); 042 return new CallbackStore(m_index, uid, PWMDataJNI::cancelInitializedCallback); 043 } 044 045 /** 046 * Check whether the PWM has been initialized. 047 * 048 * @return true if initialized 049 */ 050 public boolean getInitialized() { 051 return PWMDataJNI.getInitialized(m_index); 052 } 053 054 /** 055 * Define whether the PWM has been initialized. 056 * 057 * @param initialized whether this object is initialized 058 */ 059 public void setInitialized(boolean initialized) { 060 PWMDataJNI.setInitialized(m_index, initialized); 061 } 062 063 /** 064 * Register a callback to be run when the PWM raw value changes. 065 * 066 * @param callback the callback 067 * @param initialNotify whether to run the callback with the initial value 068 * @return the {@link CallbackStore} object associated with this callback. 069 */ 070 public CallbackStore registerPulseMicrosecondCallback( 071 NotifyCallback callback, boolean initialNotify) { 072 int uid = PWMDataJNI.registerPulseMicrosecondCallback(m_index, callback, initialNotify); 073 return new CallbackStore(m_index, uid, PWMDataJNI::cancelPulseMicrosecondCallback); 074 } 075 076 /** 077 * Get the PWM pulse microsecond value. 078 * 079 * @return the PWM pulse microsecond value 080 */ 081 public int getPulseMicrosecond() { 082 return PWMDataJNI.getPulseMicrosecond(m_index); 083 } 084 085 /** 086 * Set the PWM pulse microsecond value. 087 * 088 * @param microsecondPulseTime the PWM pulse microsecond value 089 */ 090 public void setPulseMicrosecond(int microsecondPulseTime) { 091 PWMDataJNI.setPulseMicrosecond(m_index, microsecondPulseTime); 092 } 093 094 /** 095 * Register a callback to be run when the PWM period scale changes. 096 * 097 * @param callback the callback 098 * @param initialNotify whether to run the callback with the initial value 099 * @return the {@link CallbackStore} object associated with this callback. 100 */ 101 public CallbackStore registerOutputPeriodCallback( 102 NotifyCallback callback, boolean initialNotify) { 103 int uid = PWMDataJNI.registerOutputPeriodCallback(m_index, callback, initialNotify); 104 return new CallbackStore(m_index, uid, PWMDataJNI::cancelOutputPeriodCallback); 105 } 106 107 /** 108 * Get the PWM period scale. 109 * 110 * @return the PWM period scale 111 */ 112 public int getOutputPeriod() { 113 return PWMDataJNI.getOutputPeriod(m_index); 114 } 115 116 /** 117 * Set the PWM period scale. 118 * 119 * @param period the PWM period scale 120 */ 121 public void setOutputPeriod(int period) { 122 PWMDataJNI.setOutputPeriod(m_index, period); 123 } 124 125 /** Reset all simulation data. */ 126 public void resetData() { 127 PWMDataJNI.resetData(m_index); 128 } 129}