2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2018-05-11 12:38:23 -07:00
|
|
|
|
2025-11-07 19:55:43 -05:00
|
|
|
package org.wpilib.simulation;
|
2018-05-11 12:38:23 -07:00
|
|
|
|
2025-11-07 19:55:43 -05:00
|
|
|
import org.wpilib.hardware.hal.simulation.NotifyCallback;
|
|
|
|
|
import org.wpilib.hardware.hal.simulation.PWMDataJNI;
|
|
|
|
|
import org.wpilib.hardware.discrete.PWM;
|
2018-05-11 12:38:23 -07:00
|
|
|
|
2020-12-29 22:45:16 -08:00
|
|
|
/** Class to control a simulated PWM output. */
|
2018-05-11 12:38:23 -07:00
|
|
|
public class PWMSim {
|
2018-06-03 10:00:53 -07:00
|
|
|
private final int m_index;
|
2018-05-11 12:38:23 -07:00
|
|
|
|
2020-07-04 10:10:43 -07:00
|
|
|
/**
|
|
|
|
|
* Constructs from a PWM object.
|
|
|
|
|
*
|
|
|
|
|
* @param pwm PWM to simulate
|
|
|
|
|
*/
|
|
|
|
|
public PWMSim(PWM pwm) {
|
|
|
|
|
m_index = pwm.getChannel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructs from a PWM channel number.
|
|
|
|
|
*
|
|
|
|
|
* @param channel Channel number
|
|
|
|
|
*/
|
|
|
|
|
public PWMSim(int channel) {
|
|
|
|
|
m_index = channel;
|
2018-05-11 12:38:23 -07:00
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Register a callback to be run when the PWM is initialized.
|
|
|
|
|
*
|
|
|
|
|
* @param callback the callback
|
|
|
|
|
* @param initialNotify whether to run the callback with the initial state
|
2024-07-28 14:30:19 -07:00
|
|
|
* @return the {@link CallbackStore} object associated with this callback.
|
2021-01-11 21:55:45 -08:00
|
|
|
*/
|
2018-05-11 12:38:23 -07:00
|
|
|
public CallbackStore registerInitializedCallback(NotifyCallback callback, boolean initialNotify) {
|
|
|
|
|
int uid = PWMDataJNI.registerInitializedCallback(m_index, callback, initialNotify);
|
|
|
|
|
return new CallbackStore(m_index, uid, PWMDataJNI::cancelInitializedCallback);
|
|
|
|
|
}
|
2020-12-29 22:45:16 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Check whether the PWM has been initialized.
|
|
|
|
|
*
|
|
|
|
|
* @return true if initialized
|
|
|
|
|
*/
|
2018-05-11 12:38:23 -07:00
|
|
|
public boolean getInitialized() {
|
|
|
|
|
return PWMDataJNI.getInitialized(m_index);
|
|
|
|
|
}
|
2020-12-29 22:45:16 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Define whether the PWM has been initialized.
|
|
|
|
|
*
|
|
|
|
|
* @param initialized whether this object is initialized
|
|
|
|
|
*/
|
2018-05-11 12:38:23 -07:00
|
|
|
public void setInitialized(boolean initialized) {
|
|
|
|
|
PWMDataJNI.setInitialized(m_index, initialized);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Register a callback to be run when the PWM raw value changes.
|
|
|
|
|
*
|
|
|
|
|
* @param callback the callback
|
|
|
|
|
* @param initialNotify whether to run the callback with the initial value
|
2024-07-28 14:30:19 -07:00
|
|
|
* @return the {@link CallbackStore} object associated with this callback.
|
2021-01-11 21:55:45 -08:00
|
|
|
*/
|
2023-06-22 19:43:16 -07:00
|
|
|
public CallbackStore registerPulseMicrosecondCallback(
|
|
|
|
|
NotifyCallback callback, boolean initialNotify) {
|
|
|
|
|
int uid = PWMDataJNI.registerPulseMicrosecondCallback(m_index, callback, initialNotify);
|
|
|
|
|
return new CallbackStore(m_index, uid, PWMDataJNI::cancelPulseMicrosecondCallback);
|
2018-05-11 12:38:23 -07:00
|
|
|
}
|
2020-12-29 22:45:16 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
2023-06-22 19:43:16 -07:00
|
|
|
* Get the PWM pulse microsecond value.
|
2021-01-11 21:55:45 -08:00
|
|
|
*
|
2023-06-22 19:43:16 -07:00
|
|
|
* @return the PWM pulse microsecond value
|
2021-01-11 21:55:45 -08:00
|
|
|
*/
|
2023-06-22 19:43:16 -07:00
|
|
|
public int getPulseMicrosecond() {
|
|
|
|
|
return PWMDataJNI.getPulseMicrosecond(m_index);
|
2018-05-11 12:38:23 -07:00
|
|
|
}
|
2020-12-29 22:45:16 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
2023-06-22 19:43:16 -07:00
|
|
|
* Set the PWM pulse microsecond value.
|
2021-01-11 21:55:45 -08:00
|
|
|
*
|
2023-06-22 19:43:16 -07:00
|
|
|
* @param microsecondPulseTime the PWM pulse microsecond value
|
2021-01-11 21:55:45 -08:00
|
|
|
*/
|
2023-06-22 19:43:16 -07:00
|
|
|
public void setPulseMicrosecond(int microsecondPulseTime) {
|
|
|
|
|
PWMDataJNI.setPulseMicrosecond(m_index, microsecondPulseTime);
|
2018-05-11 12:38:23 -07:00
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Register a callback to be run when the PWM period scale changes.
|
|
|
|
|
*
|
|
|
|
|
* @param callback the callback
|
|
|
|
|
* @param initialNotify whether to run the callback with the initial value
|
2024-07-28 14:30:19 -07:00
|
|
|
* @return the {@link CallbackStore} object associated with this callback.
|
2021-01-11 21:55:45 -08:00
|
|
|
*/
|
2025-03-20 19:23:22 -07:00
|
|
|
public CallbackStore registerOutputPeriodCallback(
|
|
|
|
|
NotifyCallback callback, boolean initialNotify) {
|
|
|
|
|
int uid = PWMDataJNI.registerOutputPeriodCallback(m_index, callback, initialNotify);
|
|
|
|
|
return new CallbackStore(m_index, uid, PWMDataJNI::cancelOutputPeriodCallback);
|
2018-05-11 12:38:23 -07:00
|
|
|
}
|
2020-12-29 22:45:16 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Get the PWM period scale.
|
|
|
|
|
*
|
|
|
|
|
* @return the PWM period scale
|
|
|
|
|
*/
|
2025-03-20 19:23:22 -07:00
|
|
|
public int getOutputPeriod() {
|
|
|
|
|
return PWMDataJNI.getOutputPeriod(m_index);
|
2018-05-11 12:38:23 -07:00
|
|
|
}
|
2020-12-29 22:45:16 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Set the PWM period scale.
|
|
|
|
|
*
|
2025-03-20 19:23:22 -07:00
|
|
|
* @param period the PWM period scale
|
2021-01-11 21:55:45 -08:00
|
|
|
*/
|
2025-03-20 19:23:22 -07:00
|
|
|
public void setOutputPeriod(int period) {
|
|
|
|
|
PWMDataJNI.setOutputPeriod(m_index, period);
|
2018-05-11 12:38:23 -07:00
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/** Reset all simulation data. */
|
2018-05-11 12:38:23 -07:00
|
|
|
public void resetData() {
|
|
|
|
|
PWMDataJNI.resetData(m_index);
|
|
|
|
|
}
|
|
|
|
|
}
|