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