001/*----------------------------------------------------------------------------*/
002/* Copyright (c) FIRST 2008-2012. All Rights Reserved.                        */
003/* Open Source Software - may be modified and shared by FRC teams. The code   */
004/* must be accompanied by the FIRST BSD license file in the root directory of */
005/* the project.                                                               */
006/*----------------------------------------------------------------------------*/
007
008package edu.wpi.first.wpilibj.smartdashboard;
009
010import edu.wpi.first.wpilibj.NamedSendable;
011import edu.wpi.first.wpilibj.Sendable;
012import edu.wpi.first.wpilibj.networktables.NetworkTable;
013import edu.wpi.first.wpilibj.networktables.NetworkTableKeyNotDefined;
014import edu.wpi.first.wpilibj.tables.ITable;
015import edu.wpi.first.wpilibj.tables.TableKeyNotDefinedException;
016import edu.wpi.first.wpilibj.HLUsageReporting;
017import java.util.Hashtable;
018import java.util.NoSuchElementException;
019
020/**
021 * The {@link SmartDashboard} class is the bridge between robot programs and the SmartDashboard on the
022 * laptop.
023 *
024 * <p>When a value is put into the SmartDashboard here, it pops up on the SmartDashboard on the laptop.
025 * Users can put values into and get values from the SmartDashboard</p>
026 *
027 * @author Joe Grinstead
028 */
029public class SmartDashboard {
030    /** The {@link NetworkTable} used by {@link SmartDashboard} */
031    private static final NetworkTable table = NetworkTable.getTable("SmartDashboard");
032    /**
033     * A table linking tables in the SmartDashboard to the {@link SmartDashboardData} objects
034     * they came from.
035     */
036    private static final Hashtable tablesToData = new Hashtable();
037
038    static {
039        HLUsageReporting.reportSmartDashboard();
040    }
041
042    /**
043     * Maps the specified key to the specified value in this table.
044     * The key can not be null.
045     * The value can be retrieved by calling the get method with a key that is equal to the original key.
046     * @param key the key
047     * @param data the value
048     * @throws IllegalArgumentException if key is null
049     */
050    public static void putData(String key, Sendable data) {
051        ITable dataTable = table.getSubTable(key);
052        dataTable.putString("~TYPE~", data.getSmartDashboardType());
053        data.initTable(dataTable);
054        tablesToData.put(data, key);
055    }
056
057
058    //TODO should we reimplement NamedSendable?
059    /**
060     * Maps the specified key (where the key is the name of the {@link NamedSendable} SmartDashboardNamedData
061     * to the specified value in this table.
062     * The value can be retrieved by calling the get method with a key that is equal to the original key.
063     * @param value the value
064     * @throws IllegalArgumentException if key is null
065     */
066    public static void putData(NamedSendable value) {
067        putData(value.getName(), value);
068    }
069
070    /**
071     * Returns the value at the specified key.
072     * @param key the key
073     * @return the value
074     * @throws NetworkTableKeyNotDefined if there is no value mapped to by the key
075     * @throws IllegalArgumentException if the key is null
076     */
077    public static Sendable getData(String key) {
078        ITable subtable = table.getSubTable(key);
079        Object data = tablesToData.get(subtable);
080        if (data == null) {
081            throw new IllegalArgumentException("SmartDashboard data does not exist: " + key);
082        } else {
083            return (Sendable) data;
084        }
085    }
086
087    /**
088     * Maps the specified key to the specified value in this table.
089     * The key can not be null.
090     * The value can be retrieved by calling the get method with a key that is equal to the original key.
091     * @param key the key
092     * @param value the value
093     * @throws IllegalArgumentException if key is null
094     */
095    public static void putBoolean(String key, boolean value) {
096        table.putBoolean(key, value);
097    }
098
099    /**
100     * Returns the value at the specified key.
101     * @param key the key
102     * @return the value
103     * @throws NetworkTableKeyNotDefined if there is no value mapped to by the key
104     * @throws IllegalArgumentException if the value mapped to by the key is not a boolean
105     * @throws IllegalArgumentException if the key is null
106     */
107    public static boolean getBoolean(String key) throws TableKeyNotDefinedException {
108        return table.getBoolean(key);
109    }
110
111    /**
112     * Returns the value at the specified key.
113     * @param key the key
114     * @param defaultValue returned if the key doesn't exist
115     * @return the value
116     * @throws IllegalArgumentException if the value mapped to by the key is not a boolean
117     * @throws IllegalArgumentException if the key is null
118     */
119    public static boolean getBoolean(String key, boolean defaultValue) {
120        return table.getBoolean(key, defaultValue);
121    }
122
123    /**
124     * Maps the specified key to the specified value in this table.
125     * The key can not be null.
126     * The value can be retrieved by calling the get method with a key that is equal to the original key.
127     * @param key the key
128     * @param value the value
129     * @throws IllegalArgumentException if key is null
130     */
131    public static void putNumber(String key, double value) {
132        table.putNumber(key, value);
133    }
134
135    /**
136     * Returns the value at the specified key.
137     * @param key the key
138     * @return the value
139     * @throws TableKeyNotDefinedException if there is no value mapped to by the key
140     * @throws IllegalArgumentException if the value mapped to by the key is not a double
141     * @throws IllegalArgumentException if the key is null
142     */
143    public static double getNumber(String key) throws TableKeyNotDefinedException {
144        return table.getNumber(key);
145    }
146
147    /**
148     * Returns the value at the specified key.
149     * @param key the key
150     * @param defaultValue the value returned if the key is undefined
151     * @return the value
152     * @throws NetworkTableKeyNotDefined if there is no value mapped to by the key
153     * @throws IllegalArgumentException if the value mapped to by the key is not a double
154     * @throws IllegalArgumentException if the key is null
155     */
156    public static double getNumber(String key, double defaultValue) {
157        return table.getNumber(key, defaultValue);
158    }
159
160    /**
161     * Maps the specified key to the specified value in this table.
162     * Neither the key nor the value can be null.
163     * The value can be retrieved by calling the get method with a key that is equal to the original key.
164     * @param key the key
165     * @param value the value
166     * @throws IllegalArgumentException if key or value is null
167     */
168    public static void putString(String key, String value) {
169        table.putString(key, value);
170    }
171
172    /**
173     * Returns the value at the specified key.
174     * @param key the key
175     * @return the value
176     * @throws NetworkTableKeyNotDefined if there is no value mapped to by the key
177     * @throws IllegalArgumentException if the value mapped to by the key is not a string
178     * @throws IllegalArgumentException if the key is null
179     */
180    public static String getString(String key) throws TableKeyNotDefinedException {
181        return table.getString(key);
182    }
183
184    /**
185    * Returns the value at the specified key.
186    * @param key the key
187    * @param defaultValue  The value returned if the key is undefined
188    * @return the value
189    * @throws NetworkTableKeyNotDefined if there is no value mapped to by the key
190    * @throws IllegalArgumentException if the value mapped to by the key is not a string
191    * @throws IllegalArgumentException if the key is null
192    */
193    public static String getString(String key, String defaultValue) {
194        return table.getString(key, defaultValue);
195    }
196
197
198
199
200
201
202
203
204
205    /*
206     * Deprecated Methods
207     */
208    /**
209     * Maps the specified key to the specified value in this table.
210     *
211     * The key can not be null.
212     * The value can be retrieved by calling the get method with a key that is equal to the original key.
213     *
214     * @deprecated Use {@link #putNumber(java.lang.String, double) putNumber method} instead
215     * @param key the key
216     * @param value the value
217     * @throws IllegalArgumentException if key is null
218     */
219    public static void putInt(String key, int value) {
220        table.putNumber(key, value);
221    }
222
223    /**
224     * Returns the value at the specified key.
225     *
226     * @deprecated Use {@link #getNumber(java.lang.String) getNumber} instead
227     * @param key the key
228     * @return the value
229     * @throws TableKeyNotDefinedException if there is no value mapped to by the key
230     * @throws IllegalArgumentException if the value mapped to by the key is not an int
231     * @throws IllegalArgumentException if the key is null
232     */
233    public static int getInt(String key) throws TableKeyNotDefinedException {
234        return (int) table.getNumber(key);
235    }
236
237    /**
238     * Returns the value at the specified key.
239     *
240     * @deprecated Use {@link #getNumber(java.lang.String, double) getNumber} instead
241     * @param key the key
242     * @param defaultValue the value returned if the key is undefined
243     * @return the value
244     * @throws TableKeyNotDefinedException if there is no value mapped to by the key
245     * @throws IllegalArgumentException if the value mapped to by the key is not an int
246     * @throws IllegalArgumentException if the key is null
247     */
248    public static int getInt(String key, int defaultValue) throws TableKeyNotDefinedException {
249        try {
250            return (int) table.getNumber(key);
251        } catch (NoSuchElementException ex) {
252            return defaultValue;
253        }
254    }
255
256    /**
257     * Maps the specified key to the specified value in this table.
258     *
259     * The key can not be null.
260     * The value can be retrieved by calling the get method with a key that is equal to the original key.
261     *
262     * @deprecated Use{@link #putNumber(java.lang.String, double) putNumber} instead
263     * @param key the key
264     * @param value the value
265     * @throws IllegalArgumentException if key is null
266     */
267    public static void putDouble(String key, double value) {
268        table.putNumber(key, value);
269    }
270
271    /**
272     * Returns the value at the specified key.
273     *
274     * @deprecated Use {@link #getNumber(java.lang.String) getNumber} instead
275     * @param key the key
276     * @return the value
277     * @throws TableKeyNotDefinedException if there is no value mapped to by the key
278     * @throws IllegalArgumentException if the value mapped to by the key is not a double
279     * @throws IllegalArgumentException if the key is null
280     */
281    public static double getDouble(String key) throws TableKeyNotDefinedException {
282        return table.getNumber(key);
283    }
284
285    /**
286     * Returns the value at the specified key.
287     *
288     * @deprecated Use {@link #getNumber(java.lang.String, double) getNumber} instead.
289     * @param key the key
290     * @param defaultValue the value returned if the key is undefined
291     * @return the value
292     * @throws TableKeyNotDefinedException if there is no value mapped to by the key
293     * @throws IllegalArgumentException if the value mapped to by the key is not a double
294     * @throws IllegalArgumentException if the key is null
295     */
296    public static double getDouble(String key, double defaultValue) {
297        return table.getNumber(key, defaultValue);
298    }
299
300}