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