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.wpilibj2.command.button;
006
007import java.util.concurrent.atomic.AtomicBoolean;
008
009/**
010 * This class is intended to be used within a program. The programmer can manually set its value.
011 * Also includes a setting for whether it should invert its value.
012 *
013 * <p>This class is provided by the NewCommands VendorDep
014 */
015@SuppressWarnings("deprecation")
016public class InternalButton extends Button {
017  // need to be references, so they can be mutated after being captured in the constructor.
018  private final AtomicBoolean m_pressed;
019  private final AtomicBoolean m_inverted;
020
021  /** Creates an InternalButton that is not inverted. */
022  public InternalButton() {
023    this(false);
024  }
025
026  /**
027   * Creates an InternalButton which is inverted depending on the input.
028   *
029   * @param inverted if false, then this button is pressed when set to true, otherwise it is pressed
030   *     when set to false.
031   */
032  public InternalButton(boolean inverted) {
033    this(new AtomicBoolean(), new AtomicBoolean(inverted));
034  }
035
036  /*
037   * Mock constructor so the AtomicBoolean objects can be constructed before the super
038   * constructor invocation.
039   */
040  private InternalButton(AtomicBoolean state, AtomicBoolean inverted) {
041    super(() -> state.get() != inverted.get());
042    this.m_pressed = state;
043    this.m_inverted = inverted;
044  }
045
046  public void setInverted(boolean inverted) {
047    m_inverted.set(inverted);
048  }
049
050  public void setPressed(boolean pressed) {
051    m_pressed.set(pressed);
052  }
053}