diff --git a/wpilibc/src/main/native/cpp/DoubleSolenoid.cpp b/wpilibc/src/main/native/cpp/DoubleSolenoid.cpp index ec8af951aa..815e3a076e 100644 --- a/wpilibc/src/main/native/cpp/DoubleSolenoid.cpp +++ b/wpilibc/src/main/native/cpp/DoubleSolenoid.cpp @@ -148,6 +148,14 @@ void DoubleSolenoid::Toggle() { } } +int DoubleSolenoid::GetFwdChannel() const { + return m_forwardChannel; +} + +int DoubleSolenoid::GetRevChannel() const { + return m_reverseChannel; +} + bool DoubleSolenoid::IsFwdSolenoidBlackListed() const { int blackList = GetPCMSolenoidBlackList(m_moduleNumber); return (blackList & m_forwardMask) != 0; diff --git a/wpilibc/src/main/native/cpp/Solenoid.cpp b/wpilibc/src/main/native/cpp/Solenoid.cpp index 5fb2c2bb2e..ec54e15df4 100644 --- a/wpilibc/src/main/native/cpp/Solenoid.cpp +++ b/wpilibc/src/main/native/cpp/Solenoid.cpp @@ -79,6 +79,10 @@ void Solenoid::Toggle() { Set(!Get()); } +int Solenoid::GetChannel() const { + return m_channel; +} + bool Solenoid::IsBlackListed() const { int value = GetPCMSolenoidBlackList(m_moduleNumber) & (1 << m_channel); return (value != 0); diff --git a/wpilibc/src/main/native/cpp/SolenoidBase.cpp b/wpilibc/src/main/native/cpp/SolenoidBase.cpp index 7f70976938..c8b64f4b6a 100644 --- a/wpilibc/src/main/native/cpp/SolenoidBase.cpp +++ b/wpilibc/src/main/native/cpp/SolenoidBase.cpp @@ -58,3 +58,7 @@ void SolenoidBase::ClearAllPCMStickyFaults() { } SolenoidBase::SolenoidBase(int moduleNumber) : m_moduleNumber(moduleNumber) {} + +int SolenoidBase::GetModuleNumber() const { + return m_moduleNumber; +} diff --git a/wpilibc/src/main/native/cpp/simulation/DoubleSolenoidSim.cpp b/wpilibc/src/main/native/cpp/simulation/DoubleSolenoidSim.cpp new file mode 100644 index 0000000000..df8a525bdc --- /dev/null +++ b/wpilibc/src/main/native/cpp/simulation/DoubleSolenoidSim.cpp @@ -0,0 +1,91 @@ +// 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. + +#include "frc/simulation/DoubleSolenoidSim.h" + +#include "frc/SensorUtil.h" +#include "frc/simulation/PCMSim.h" + +using namespace frc; +using namespace frc::sim; + +DoubleSolenoidSim::DoubleSolenoidSim(int fwd, int rev) + : m_fwd{fwd}, m_rev{rev} {} + +DoubleSolenoidSim::DoubleSolenoidSim(int module, int fwd, int rev) + : m_pcm{module}, m_fwd{fwd}, m_rev{rev} {} + +DoubleSolenoidSim::DoubleSolenoidSim(PCMSim& pcm, int fwd, int rev) + : m_pcm{pcm}, m_fwd{fwd}, m_rev{rev} {} + +DoubleSolenoidSim::DoubleSolenoidSim(DoubleSolenoid& solenoid) + : m_pcm{solenoid.GetModuleNumber()}, + m_fwd{solenoid.GetFwdChannel()}, + m_rev{solenoid.GetRevChannel()} {} + +std::unique_ptr +DoubleSolenoidSim::RegisterFwdInitializedCallback(NotifyCallback callback, + bool initialNotify) { + return m_pcm.RegisterSolenoidInitializedCallback(m_fwd, callback, + initialNotify); +} + +bool DoubleSolenoidSim::GetFwdInitialized() const { + return m_pcm.GetSolenoidInitialized(m_fwd); +} + +void DoubleSolenoidSim::SetFwdInitialized(bool initialized) { + m_pcm.SetSolenoidInitialized(m_fwd, initialized); +} + +std::unique_ptr +DoubleSolenoidSim::RegisterRevInitializedCallback(NotifyCallback callback, + bool initialNotify) { + return m_pcm.RegisterSolenoidInitializedCallback(m_rev, callback, + initialNotify); +} + +bool DoubleSolenoidSim::GetRevInitialized() const { + return m_pcm.GetSolenoidInitialized(m_rev); +} + +void DoubleSolenoidSim::SetRevInitialized(bool initialized) { + m_pcm.SetSolenoidInitialized(m_rev, initialized); +} + +void DoubleSolenoidSim::Set(DoubleSolenoid::Value value) { + bool forward = false; + bool reverse = false; + + switch (value) { + case DoubleSolenoid::Value::kOff: + forward = false; + reverse = false; + break; + case DoubleSolenoid::Value::kForward: + forward = true; + reverse = false; + break; + case DoubleSolenoid::Value::kReverse: + forward = false; + reverse = true; + break; + } + + m_pcm.SetSolenoidOutput(m_fwd, forward); + m_pcm.SetSolenoidOutput(m_rev, reverse); +} + +DoubleSolenoid::Value DoubleSolenoidSim::Get() const { + bool valueForward = m_pcm.GetSolenoidOutput(m_fwd); + bool valueReverse = m_pcm.GetSolenoidOutput(m_rev); + + if (valueForward) { + return DoubleSolenoid::Value::kForward; + } else if (valueReverse) { + return DoubleSolenoid::Value::kReverse; + } else { + return DoubleSolenoid::Value::kOff; + } +} diff --git a/wpilibc/src/main/native/cpp/simulation/SolenoidSim.cpp b/wpilibc/src/main/native/cpp/simulation/SolenoidSim.cpp new file mode 100644 index 0000000000..4e432b7c49 --- /dev/null +++ b/wpilibc/src/main/native/cpp/simulation/SolenoidSim.cpp @@ -0,0 +1,50 @@ +// 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. + +#include "frc/simulation/SolenoidSim.h" + +#include "frc/SensorUtil.h" +#include "frc/simulation/PCMSim.h" + +using namespace frc; +using namespace frc::sim; + +SolenoidSim::SolenoidSim(int channel) : m_channel{channel} {} + +SolenoidSim::SolenoidSim(int module, int channel) + : m_pcm{module}, m_channel{channel} {} + +SolenoidSim::SolenoidSim(PCMSim& pcm, int channel) + : m_pcm{pcm}, m_channel{channel} {} + +SolenoidSim::SolenoidSim(Solenoid& solenoid) + : m_pcm{solenoid.GetModuleNumber()}, m_channel{solenoid.GetChannel()} {} + +std::unique_ptr SolenoidSim::RegisterInitializedCallback( + NotifyCallback callback, bool initialNotify) { + return m_pcm.RegisterSolenoidInitializedCallback(m_channel, callback, + initialNotify); +} + +bool SolenoidSim::GetInitialized() const { + return m_pcm.GetSolenoidInitialized(m_channel); +} + +void SolenoidSim::SetInitialized(bool initialized) { + m_pcm.SetSolenoidInitialized(m_channel, initialized); +} + +std::unique_ptr SolenoidSim::RegisterOutputCallback( + NotifyCallback callback, bool initialNotify) { + return m_pcm.RegisterSolenoidOutputCallback(m_channel, callback, + initialNotify); +} + +bool SolenoidSim::GetOutput() const { + return m_pcm.GetSolenoidOutput(m_channel); +} + +void SolenoidSim::SetOutput(bool output) { + m_pcm.SetSolenoidOutput(m_channel, output); +} diff --git a/wpilibc/src/main/native/include/frc/DoubleSolenoid.h b/wpilibc/src/main/native/include/frc/DoubleSolenoid.h index 7edf255794..f06eca1605 100644 --- a/wpilibc/src/main/native/include/frc/DoubleSolenoid.h +++ b/wpilibc/src/main/native/include/frc/DoubleSolenoid.h @@ -74,6 +74,20 @@ class DoubleSolenoid : public SolenoidBase, */ void Toggle(); + /** + * Get the forward channel. + * + * @return the forward channel. + */ + int GetFwdChannel() const; + + /** + * Get the reverse channel. + * + * @return the reverse channel. + */ + int GetRevChannel() const; + /** * Check if the forward solenoid is blacklisted. * diff --git a/wpilibc/src/main/native/include/frc/Solenoid.h b/wpilibc/src/main/native/include/frc/Solenoid.h index 1cdaaa375a..892e94f3af 100644 --- a/wpilibc/src/main/native/include/frc/Solenoid.h +++ b/wpilibc/src/main/native/include/frc/Solenoid.h @@ -66,6 +66,11 @@ class Solenoid : public SolenoidBase, */ void Toggle(); + /** + * Get the channel this solenoid is connected to. + */ + int GetChannel() const; + /** * Check if solenoid is blacklisted. * diff --git a/wpilibc/src/main/native/include/frc/SolenoidBase.h b/wpilibc/src/main/native/include/frc/SolenoidBase.h index e2e1efb197..c0acc4334f 100644 --- a/wpilibc/src/main/native/include/frc/SolenoidBase.h +++ b/wpilibc/src/main/native/include/frc/SolenoidBase.h @@ -14,6 +14,13 @@ namespace frc { */ class SolenoidBase : public ErrorBase { public: + /** + * Get the CAN ID of the module this solenoid is connected to. + * + * @return the module number. + */ + int GetModuleNumber() const; + /** * Read all 8 solenoids as a single byte * diff --git a/wpilibc/src/main/native/include/frc/simulation/DoubleSolenoidSim.h b/wpilibc/src/main/native/include/frc/simulation/DoubleSolenoidSim.h new file mode 100644 index 0000000000..80ec19a55d --- /dev/null +++ b/wpilibc/src/main/native/include/frc/simulation/DoubleSolenoidSim.h @@ -0,0 +1,116 @@ +// 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. + +#pragma once + +#include + +#include "frc/DoubleSolenoid.h" +#include "frc/simulation/CallbackStore.h" +#include "frc/simulation/PCMSim.h" + +namespace frc::sim { + +/** + * Class to control a simulated Pneumatic Control Module (PCM). + */ +class DoubleSolenoidSim { + public: + /** + * Constructs for a solenoid on the default PCM. + * + * @param channel the solenoid channel. + */ + DoubleSolenoidSim(int fwd, int rev); + + /** + * Constructs for a solenoid on the given PCM. + * + * @param pcm the PCM the solenoid is connected to. + * @param channel the solenoid channel. + */ + DoubleSolenoidSim(int module, int fwd, int rev); + + /** + * Constructs from a PCMSim object. + * + * @param pcm the PCM the solenoid is connected to. + */ + DoubleSolenoidSim(PCMSim& pcm, int fwd, int rev); + + /** + * Constructs for the given solenoid. + * + * @param solenoid the solenoid to simulate. + */ + explicit DoubleSolenoidSim(DoubleSolenoid& solenoid); + + /** + * Register a callback to be run when the forward solenoid is initialized. + * + * @param callback the callback + * @param initialNotify should the callback be run with the initial state + * @return the {@link CallbackStore} object associated with this callback. + */ + [[nodiscard]] std::unique_ptr RegisterFwdInitializedCallback( + NotifyCallback callback, bool initialNotify); + + /** + * Check if the forward solenoid has been initialized. + * + * @return true if initialized + */ + bool GetFwdInitialized() const; + + /** + * Define whether the forward solenoid has been initialized. + * + * @param initialized whether the solenoid is intiialized. + */ + void SetFwdInitialized(bool initialized); + + /** + * Register a callback to be run when the reverse solenoid is initialized. + * + * @param callback the callback + * @param initialNotify should the callback be run with the initial state + * @return the {@link CallbackStore} object associated with this callback. + */ + [[nodiscard]] std::unique_ptr RegisterRevInitializedCallback( + NotifyCallback callback, bool initialNotify); + + /** + * Define whether the reverse solenoid has been initialized. + * + * @param initialized whether the solenoid is intiialized. + */ + void SetRevInitialized(bool initialized); + + /** + * Check if the reverse solenoid has been initialized. + * + * @return true if initialized + */ + bool GetRevInitialized() const; + + /** + * Set the value of the double solenoid output. + * + * @param value The value to set (Off, Forward, Reverse) + */ + void Set(DoubleSolenoid::Value value); + + /** + * Check the value of the double solenoid output. + * + * @return the output value of the double solenoid. + */ + DoubleSolenoid::Value Get() const; + + private: + PCMSim m_pcm; + int m_fwd; + int m_rev; +}; +} // namespace frc::sim diff --git a/wpilibc/src/main/native/include/frc/simulation/SolenoidSim.h b/wpilibc/src/main/native/include/frc/simulation/SolenoidSim.h new file mode 100644 index 0000000000..5b803665c3 --- /dev/null +++ b/wpilibc/src/main/native/include/frc/simulation/SolenoidSim.h @@ -0,0 +1,104 @@ +// 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. + +#pragma once + +#include + +#include "frc/Solenoid.h" +#include "frc/simulation/CallbackStore.h" +#include "frc/simulation/PCMSim.h" + +namespace frc::sim { + +/** + * Class to control a simulated Pneumatic Control Module (PCM). + */ +class SolenoidSim { + public: + /** + * Constructs for a solenoid on the default PCM. + * + * @param channel the solenoid channel. + */ + explicit SolenoidSim(int channel); + + /** + * Constructs for the given solenoid. + * + * @param doubleSolenoid the solenoid to simulate. + */ + explicit SolenoidSim(Solenoid& solenoid); + + /** + * Constructs for a solenoid. + * + * @param module the CAN ID of the PCM the solenoid is connected to. + * @param channel the solenoid channel. + * + * @see PCMSim#PCMSim(int) + */ + SolenoidSim(int module, int channel); + + /** + * Constructs for a solenoid on the given PCM. + * + * @param pcm the PCM the solenoid is connected to. + * @param channel the solenoid channel. + */ + SolenoidSim(PCMSim& pcm, int channel); + + /** + * Register a callback to be run when this solenoid is initialized. + * + * @param callback the callback + * @param initialNotify should the callback be run with the initial state + * @return the {@link CallbackStore} object associated with this callback. + */ + [[nodiscard]] std::unique_ptr RegisterInitializedCallback( + NotifyCallback callback, bool initialNotify); + + /** + * Check if this solenoid has been initialized. + * + * @return true if initialized + */ + bool GetInitialized() const; + + /** + * Define whether this solenoid has been initialized. + * + * @param initialized whether the solenoid is intiialized. + */ + void SetInitialized(bool initialized); + + /** + * Register a callback to be run when the output of this solenoid has changed. + * + * @param callback the callback + * @param initialNotify should the callback be run with the initial value + * @return the {@link CallbackStore} object associated with this callback. + */ + [[nodiscard]] std::unique_ptr RegisterOutputCallback( + NotifyCallback callback, bool initialNotify); + + /** + * Check the solenoid output. + * + * @return the solenoid output + */ + bool GetOutput() const; + + /** + * Change the solenoid output. + * + * @param output the new solenoid output + */ + void SetOutput(bool output); + + private: + PCMSim m_pcm; + int m_channel; +}; +} // namespace frc::sim diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/DoubleSolenoid.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/DoubleSolenoid.java index b8519c75f8..ef7234eef9 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/DoubleSolenoid.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/DoubleSolenoid.java @@ -29,6 +29,8 @@ public class DoubleSolenoid extends SolenoidBase implements Sendable, AutoClosea private byte m_reverseMask; // The mask for the reverse channel. private int m_forwardHandle; private int m_reverseHandle; + private final int m_forwardChannel; + private final int m_reverseChannel; /** * Constructor. Uses the default PCM ID (defaults to 0). @@ -51,6 +53,9 @@ public class DoubleSolenoid extends SolenoidBase implements Sendable, AutoClosea final int moduleNumber, final int forwardChannel, final int reverseChannel) { super(moduleNumber); + m_forwardChannel = forwardChannel; + m_reverseChannel = reverseChannel; + SensorUtil.checkSolenoidModule(m_moduleNumber); SensorUtil.checkSolenoidChannel(forwardChannel); SensorUtil.checkSolenoidChannel(reverseChannel); @@ -148,6 +153,24 @@ public class DoubleSolenoid extends SolenoidBase implements Sendable, AutoClosea } } + /** + * Get the forward channel. + * + * @return the forward channel. + */ + public int getFwdChannel() { + return m_forwardChannel; + } + + /** + * Get the reverse channel. + * + * @return the reverse channel. + */ + public int getRevChannel() { + return m_reverseChannel; + } + /** * Check if the forward solenoid is blacklisted. If a solenoid is shorted, it is added to the * blacklist and disabled until power cycle, or until faults are cleared. diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/Solenoid.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/Solenoid.java index de20b215eb..cf93f77e46 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/Solenoid.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/Solenoid.java @@ -84,6 +84,11 @@ public class Solenoid extends SolenoidBase implements Sendable, AutoCloseable { set(!get()); } + /** Get the channel this solenoid is connected to. */ + public int getChannel() { + return m_channel; + } + /** * Check if solenoid is blacklisted. If a solenoid is shorted, it is added to the blacklist and * disabled until power cycle, or until faults are cleared. diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/SolenoidBase.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/SolenoidBase.java index c0e70868d9..43932fad2a 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/SolenoidBase.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/SolenoidBase.java @@ -22,6 +22,15 @@ public class SolenoidBase { m_moduleNumber = moduleNumber; } + /** + * Get the CAN ID of the module this solenoid is connected to. + * + * @return the module number. + */ + public int getModuleNumber() { + return m_moduleNumber; + } + /** * Read all 8 solenoids from the specified module as a single byte. * diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/simulation/DoubleSolenoidSim.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/simulation/DoubleSolenoidSim.java new file mode 100644 index 0000000000..d1e373070b --- /dev/null +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/simulation/DoubleSolenoidSim.java @@ -0,0 +1,182 @@ +// 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. + +package edu.wpi.first.wpilibj.simulation; + +import edu.wpi.first.hal.simulation.NotifyCallback; +import edu.wpi.first.wpilibj.DoubleSolenoid; + +/** Class to control a simulated {@link edu.wpi.first.wpilibj.DoubleSolenoid}. */ +public class DoubleSolenoidSim { + private final PCMSim m_pcm; + private final int m_fwd; + private final int m_rev; + + /** + * Constructs for a double solenoid on the default PCM. + * + * @param fwd the forward solenoid channel. + * @param rev the reverse solenoid channel. + * @see PCMSim#PCMSim() + */ + public DoubleSolenoidSim(int fwd, int rev) { + this.m_pcm = new PCMSim(); + this.m_fwd = fwd; + this.m_rev = rev; + } + + /** + * Constructs for a double solenoid. + * + * @param fwd the forward solenoid channel. + * @param rev the reverse solenoid channel. + * @see PCMSim#PCMSim(int) + */ + public DoubleSolenoidSim(int module, int fwd, int rev) { + this(new PCMSim(module), fwd, rev); + } + + /** + * Constructs for a double solenoid on the given PCM. + * + * @param pcm the PCM the double solenoid is on. + * @param fwd the forward solenoid channel. + * @param rev the reverse solenoid channel. + */ + public DoubleSolenoidSim(PCMSim pcm, int fwd, int rev) { + this.m_pcm = pcm; + this.m_fwd = fwd; + this.m_rev = rev; + } + + /** + * Constructs for the given solenoid. + * + * @param solenoid the solenoid to simulate. + */ + public DoubleSolenoidSim(DoubleSolenoid solenoid) { + this(solenoid.getModuleNumber(), solenoid.getFwdChannel(), solenoid.getRevChannel()); + } + + /** + * Register a callback to be run when the forward solenoid is initialized. + * + * @param callback the callback + * @param initialNotify should the callback be run with the initial state + * @return the {@link CallbackStore} object associated with this callback. Save a reference to + * this object so GC doesn't cancel the callback. + */ + public CallbackStore registerFwdInitializedCallback( + NotifyCallback callback, boolean initialNotify) { + return m_pcm.registerSolenoidInitializedCallback(m_fwd, callback, initialNotify); + } + + /** + * Check if the forward solenoid has been initialized. + * + * @return true if initialized + */ + public boolean getFwdInitialized() { + return m_pcm.getSolenoidInitialized(m_fwd); + } + + /** + * Define whether the forward solenoid has been initialized. + * + * @param initialized whether the solenoid is intiialized. + */ + public void setFwdInitialized(boolean initialized) { + m_pcm.setSolenoidInitialized(m_fwd, initialized); + } + + /** + * Register a callback to be run when the reverse solenoid is initialized. + * + * @param callback the callback + * @param initialNotify should the callback be run with the initial state + * @return the {@link CallbackStore} object associated with this callback. Save a reference to + * this object so GC doesn't cancel the callback. + */ + public CallbackStore registerRevInitializedCallback( + NotifyCallback callback, boolean initialNotify) { + return m_pcm.registerSolenoidInitializedCallback(m_rev, callback, initialNotify); + } + + /** + * Check if the reverse solenoid has been initialized. + * + * @return true if initialized + */ + public boolean getRevInitialized() { + return m_pcm.getSolenoidInitialized(m_rev); + } + + /** + * Define whether the reverse solenoid has been initialized. + * + * @param initialized whether the solenoid is intiialized. + */ + public void setRevInitialized(boolean initialized) { + m_pcm.setSolenoidInitialized(m_rev, initialized); + } + + /** + * Set the value of the double solenoid output. + * + * @param value The value to set (Off, Forward, Reverse) + */ + public void set(final DoubleSolenoid.Value value) { + boolean forward = false; + boolean reverse = false; + + switch (value) { + case kOff: + forward = false; + reverse = false; + break; + case kForward: + forward = true; + reverse = false; + break; + case kReverse: + forward = false; + reverse = true; + break; + default: + throw new AssertionError("Illegal value: " + value); + } + + m_pcm.setSolenoidOutput(m_fwd, forward); + m_pcm.setSolenoidOutput(m_rev, reverse); + } + + /** + * Check the value of the double solenoid output. + * + * @return the output value of the double solenoid. + */ + public DoubleSolenoid.Value get() { + boolean fwdState = m_pcm.getSolenoidOutput(m_fwd); + boolean revState = m_pcm.getSolenoidOutput(m_rev); + if (!fwdState && !revState) { + return DoubleSolenoid.Value.kOff; + } else if (fwdState && !revState) { + return DoubleSolenoid.Value.kForward; + } else if (!fwdState && revState) { + return DoubleSolenoid.Value.kReverse; + } else { + throw new AssertionError( + "In a double solenoid, both fwd and rev can't be on at the same time."); + } + } + + /** + * Get the wrapped {@link PCMSim} object. + * + * @return the wrapped {@link PCMSim} object. + */ + public PCMSim getPCMSim() { + return m_pcm; + } +} diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/simulation/SolenoidSim.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/simulation/SolenoidSim.java new file mode 100644 index 0000000000..1fba4e27d2 --- /dev/null +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/simulation/SolenoidSim.java @@ -0,0 +1,125 @@ +// 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. + +package edu.wpi.first.wpilibj.simulation; + +import edu.wpi.first.hal.simulation.NotifyCallback; +import edu.wpi.first.wpilibj.Solenoid; + +/** Class to control a simulated {@link edu.wpi.first.wpilibj.Solenoid}. */ +public class SolenoidSim { + private final PCMSim m_pcm; + private final int m_channel; + + /** + * Constructs for a solenoid on the default PCM. + * + * @param channel the solenoid channel. + * @see PCMSim#PCMSim() + */ + public SolenoidSim(int channel) { + this.m_pcm = new PCMSim(); + this.m_channel = channel; + } + + /** + * Constructs for a solenoid. + * + * @param module the CAN ID of the PCM the solenoid is connected to. + * @param channel the solenoid channel. + * @see PCMSim#PCMSim(int) + */ + public SolenoidSim(int module, int channel) { + this(new PCMSim(module), channel); + } + + /** + * Constructs for a solenoid on the given PCM. + * + * @param pcm the PCM the solenoid is connected to. + * @param channel the solenoid channel. + */ + public SolenoidSim(PCMSim pcm, int channel) { + this.m_pcm = pcm; + this.m_channel = channel; + } + + /** + * Constructs for the given solenoid. + * + * @param solenoid the solenoid to simulate. + */ + public SolenoidSim(Solenoid solenoid) { + this(solenoid.getModuleNumber(), solenoid.getChannel()); + } + + /** + * Register a callback to be run when this solenoid is initialized. + * + * @param callback the callback + * @param initialNotify should the callback be run with the initial state + * @return the {@link CallbackStore} object associated with this callback. Save a reference to + * this object so GC doesn't cancel the callback. + */ + public CallbackStore registerInitializedCallback(NotifyCallback callback, boolean initialNotify) { + return m_pcm.registerSolenoidInitializedCallback(m_channel, callback, initialNotify); + } + + /** + * Check if this solenoid has been initialized. + * + * @return true if initialized + */ + public boolean getInitialized() { + return m_pcm.getSolenoidInitialized(m_channel); + } + + /** + * Define whether this solenoid has been initialized. + * + * @param initialized whether the solenoid is intiialized. + */ + public void setInitialized(boolean initialized) { + m_pcm.setSolenoidInitialized(m_channel, initialized); + } + + /** + * Register a callback to be run when the output of this solenoid has changed. + * + * @param callback the callback + * @param initialNotify should the callback be run with the initial value + * @return the {@link CallbackStore} object associated with this callback. Save a reference to + * this object so GC doesn't cancel the callback. + */ + public CallbackStore registerOutputCallback(NotifyCallback callback, boolean initialNotify) { + return m_pcm.registerSolenoidOutputCallback(m_channel, callback, initialNotify); + } + + /** + * Check the solenoid output. + * + * @return the solenoid output + */ + public boolean getOutput() { + return m_pcm.getSolenoidOutput(m_channel); + } + + /** + * Change the solenoid output. + * + * @param output the new solenoid output + */ + public void setOutput(boolean output) { + m_pcm.setSolenoidOutput(m_channel, output); + } + + /** + * Get the wrapped {@link PCMSim} object. + * + * @return the wrapped {@link PCMSim} object. + */ + public PCMSim getPCMSim() { + return m_pcm; + } +}