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 */
015public class InternalButton extends Trigger {
016  // need to be references, so they can be mutated after being captured in the constructor.
017  private final AtomicBoolean m_pressed;
018  private final AtomicBoolean m_inverted;
019
020  /** Creates an InternalButton that is not inverted. */
021  public InternalButton() {
022    this(false);
023  }
024
025  /**
026   * Creates an InternalButton which is inverted depending on the input.
027   *
028   * @param inverted if false, then this button is pressed when set to true, otherwise it is pressed
029   *     when set to false.
030   */
031  public InternalButton(boolean inverted) {
032    this(new AtomicBoolean(), new AtomicBoolean(inverted));
033  }
034
035  /*
036   * Mock constructor so the AtomicBoolean objects can be constructed before the super
037   * constructor invocation.
038   */
039  private InternalButton(AtomicBoolean state, AtomicBoolean inverted) {
040    super(() -> state.get() != inverted.get());
041    this.m_pressed = state;
042    this.m_inverted = inverted;
043  }
044
045  /**
046   * Sets whether to invert button state.
047   *
048   * @param inverted Whether button state should be inverted.
049   */
050  public void setInverted(boolean inverted) {
051    m_inverted.set(inverted);
052  }
053
054  /**
055   * Sets whether button is pressed.
056   *
057   * @param pressed Whether button is pressed.
058   */
059  public void setPressed(boolean pressed) {
060    m_pressed.set(pressed);
061  }
062}