diff --git a/wpilibc/src/main/native/cpp/DoubleSolenoid.cpp b/wpilibc/src/main/native/cpp/DoubleSolenoid.cpp index c896ea88c8..67eb0b7f83 100644 --- a/wpilibc/src/main/native/cpp/DoubleSolenoid.cpp +++ b/wpilibc/src/main/native/cpp/DoubleSolenoid.cpp @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2008-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -92,6 +92,7 @@ void DoubleSolenoid::Set(Value value) { bool forward = false; bool reverse = false; + switch (value) { case kOff: forward = false; @@ -106,6 +107,7 @@ void DoubleSolenoid::Set(Value value) { reverse = true; break; } + int fstatus = 0; HAL_SetSolenoid(m_forwardHandle, forward, &fstatus); int rstatus = 0; @@ -117,6 +119,7 @@ void DoubleSolenoid::Set(Value value) { DoubleSolenoid::Value DoubleSolenoid::Get() const { if (StatusIsFatal()) return kOff; + int fstatus = 0; int rstatus = 0; bool valueForward = HAL_GetSolenoid(m_forwardHandle, &fstatus); @@ -125,9 +128,23 @@ DoubleSolenoid::Value DoubleSolenoid::Get() const { wpi_setHALError(fstatus); wpi_setHALError(rstatus); - if (valueForward) return kForward; - if (valueReverse) return kReverse; - return kOff; + if (valueForward) { + return kForward; + } else if (valueReverse) { + return kReverse; + } else { + return kOff; + } +} + +void DoubleSolenoid::Toggle() { + Value value = Get(); + + if (value == kForward) { + Set(kReverse); + } else if (value == kReverse) { + Set(kForward); + } } bool DoubleSolenoid::IsFwdSolenoidBlackListed() const { diff --git a/wpilibc/src/main/native/cpp/Solenoid.cpp b/wpilibc/src/main/native/cpp/Solenoid.cpp index ebb4a7b7bc..b5abf2052a 100644 --- a/wpilibc/src/main/native/cpp/Solenoid.cpp +++ b/wpilibc/src/main/native/cpp/Solenoid.cpp @@ -56,6 +56,7 @@ Solenoid::~Solenoid() { HAL_FreeSolenoidPort(m_solenoidHandle); } void Solenoid::Set(bool on) { if (StatusIsFatal()) return; + int32_t status = 0; HAL_SetSolenoid(m_solenoidHandle, on, &status); wpi_setHALError(status); @@ -63,12 +64,16 @@ void Solenoid::Set(bool on) { bool Solenoid::Get() const { if (StatusIsFatal()) return false; + int32_t status = 0; bool value = HAL_GetSolenoid(m_solenoidHandle, &status); wpi_setHALError(status); + return value; } +void Solenoid::Toggle() { Set(!Get()); } + bool Solenoid::IsBlackListed() const { int value = GetPCMSolenoidBlackList(m_moduleNumber) & (1 << m_channel); return (value != 0); diff --git a/wpilibc/src/main/native/include/frc/DoubleSolenoid.h b/wpilibc/src/main/native/include/frc/DoubleSolenoid.h index 67229765c1..302c52c6b9 100644 --- a/wpilibc/src/main/native/include/frc/DoubleSolenoid.h +++ b/wpilibc/src/main/native/include/frc/DoubleSolenoid.h @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2008-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -68,6 +68,15 @@ class DoubleSolenoid : public SolenoidBase, */ virtual Value Get() const; + /** + * Toggle the value of the solenoid. + * + * If the solenoid is set to forward, it'll be set to reverse. If the solenoid + * is set to reverse, it'll be set to forward. If the solenoid is set to off, + * nothing happens. + */ + void Toggle(); + /** * 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 86a2839f54..9939b41647 100644 --- a/wpilibc/src/main/native/include/frc/Solenoid.h +++ b/wpilibc/src/main/native/include/frc/Solenoid.h @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2008-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -61,6 +61,14 @@ class Solenoid : public SolenoidBase, */ virtual bool Get() const; + /** + * Toggle the value of the solenoid. + * + * If the solenoid is set to on, it'll be turned off. If the solenoid is set + * to off, it'll be turned on. + */ + void Toggle(); + /** * Check if solenoid is blacklisted. * 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 d612d24ff3..d6d490886c 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/DoubleSolenoid.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/DoubleSolenoid.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2008-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -133,11 +133,27 @@ public class DoubleSolenoid extends SolenoidBase implements Sendable, AutoClosea if (valueForward) { return Value.kForward; - } - if (valueReverse) { + } else if (valueReverse) { return Value.kReverse; + } else { + return Value.kOff; + } + } + + /** + * Toggle the value of the solenoid. + * + *
If the solenoid is set to forward, it'll be set to reverse. If the solenoid is set to + * reverse, it'll be set to forward. If the solenoid is set to off, nothing happens. + */ + public void toggle() { + Value value = get(); + + if (value == Value.kForward) { + set(Value.kReverse); + } else if (value == Value.kReverse) { + set(Value.kForward); } - return Value.kOff; } /** 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 163415c724..6245c2b547 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/Solenoid.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/Solenoid.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2008-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -77,6 +77,16 @@ public class Solenoid extends SolenoidBase implements Sendable, AutoCloseable { return SolenoidJNI.getSolenoid(m_solenoidHandle); } + /** + * Toggle the value of the solenoid. + * + *
If the solenoid is set to on, it'll be turned off. If the solenoid is set to off, it'll be + * turned on. + */ + public void toggle() { + set(!get()); + } + /** * 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.