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 static edu.wpi.first.util.ErrorMessages.requireNonNullParam;
008
009import edu.wpi.first.networktables.BooleanSubscriber;
010import edu.wpi.first.networktables.BooleanTopic;
011import edu.wpi.first.networktables.NetworkTable;
012import edu.wpi.first.networktables.NetworkTableInstance;
013import org.wpilib.commands3.Trigger;
014
015/** A {@link Trigger} that uses a {@link NetworkTable} boolean field. */
016public class NetworkButton extends Trigger {
017  /**
018   * Creates a NetworkButton that commands can be bound to.
019   *
020   * @param topic The boolean topic that contains the value.
021   */
022  public NetworkButton(BooleanTopic topic) {
023    this(topic.subscribe(false));
024  }
025
026  /**
027   * Creates a NetworkButton that commands can be bound to.
028   *
029   * @param sub The boolean subscriber that provides the value.
030   */
031  public NetworkButton(BooleanSubscriber sub) {
032    super(() -> sub.getTopic().getInstance().isConnected() && sub.get());
033    requireNonNullParam(sub, "sub", "NetworkButton");
034  }
035
036  /**
037   * Creates a NetworkButton that commands can be bound to.
038   *
039   * @param table The table where the networktable value is located.
040   * @param field The field that is the value.
041   */
042  public NetworkButton(NetworkTable table, String field) {
043    this(table.getBooleanTopic(field));
044  }
045
046  /**
047   * Creates a NetworkButton that commands can be bound to.
048   *
049   * @param table The table where the networktable value is located.
050   * @param field The field that is the value.
051   */
052  public NetworkButton(String table, String field) {
053    this(NetworkTableInstance.getDefault(), table, field);
054  }
055
056  /**
057   * Creates a NetworkButton that commands can be bound to.
058   *
059   * @param inst The NetworkTable instance to use
060   * @param table The table where the networktable value is located.
061   * @param field The field that is the value.
062   */
063  public NetworkButton(NetworkTableInstance inst, String table, String field) {
064    this(inst.getTable(table), field);
065  }
066}