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