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