WPILibC++ 2024.3.2
SmartDashboard.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <functional>
8#include <memory>
9#include <span>
10#include <string>
11#include <string_view>
12#include <vector>
13
16
17namespace wpi {
18class Sendable;
19} // namespace wpi
20
21namespace frc {
22
24 public:
25 SmartDashboard() = delete;
26
27 static void init();
28
29 /**
30 * Determines whether the given key is in this table.
31 *
32 * @param key the key to search for
33 * @return true if the table as a value assigned to the given key
34 */
35 static bool ContainsKey(std::string_view key);
36
37 /**
38 * @param types bitmask of types; 0 is treated as a "don't care".
39 * @return keys currently in the table
40 */
41 static std::vector<std::string> GetKeys(int types = 0);
42
43 /**
44 * Makes a key's value persistent through program restarts.
45 *
46 * @param key the key to make persistent
47 */
49
50 /**
51 * Stop making a key's value persistent through program restarts.
52 * The key cannot be null.
53 *
54 * @param key the key name
55 */
57
58 /**
59 * Returns whether the value is persistent through program restarts.
60 * The key cannot be null.
61 *
62 * @param key the key name
63 */
65
66 /**
67 * Returns an NT Entry mapping to the specified key
68 *
69 * This is useful if an entry is used often, or is read and then modified.
70 *
71 * @param key the key
72 * @return the entry for the key
73 */
75
76 /**
77 * Maps the specified key to the specified value in this table.
78 *
79 * The value can be retrieved by calling the get method with a key that is
80 * equal to the original key.
81 *
82 * In order for the value to appear in the dashboard, it must be registered
83 * with SendableRegistry. WPILib components do this automatically.
84 *
85 * @param key the key
86 * @param data the value
87 */
88 static void PutData(std::string_view key, wpi::Sendable* data);
89
90 /**
91 * Maps the specified key (where the key is the name of the Sendable)
92 * to the specified value in this table.
93 *
94 * The value can be retrieved by calling the get method with a key that is
95 * equal to the original key.
96 *
97 * In order for the value to appear in the dashboard, it must be registered
98 * with SendableRegistry. WPILib components do this automatically.
99 *
100 * @param value the value
101 */
102 static void PutData(wpi::Sendable* value);
103
104 /**
105 * Returns the value at the specified key.
106 *
107 * @param keyName the key
108 * @return the value
109 */
111
112 /**
113 * Maps the specified key to the specified value in this table.
114 *
115 * The value can be retrieved by calling the get method with a key that is
116 * equal to the original key.
117 *
118 * @param keyName the key
119 * @param value the value
120 * @return False if the table key already exists with a different type
121 */
122 static bool PutBoolean(std::string_view keyName, bool value);
123
124 /**
125 * Gets the current value in the table, setting it if it does not exist.
126 * @param key the key
127 * @param defaultValue the default value to set if key doesn't exist.
128 * @returns False if the table key exists with a different type
129 */
130 static bool SetDefaultBoolean(std::string_view key, bool defaultValue);
131
132 /**
133 * Returns the value at the specified key.
134 *
135 * If the key is not found, returns the default value.
136 *
137 * @param keyName the key
138 * @param defaultValue the default value to set if key doesn't exist
139 * @return the value
140 */
141 static bool GetBoolean(std::string_view keyName, bool defaultValue);
142
143 /**
144 * Maps the specified key to the specified value in this table.
145 *
146 * The value can be retrieved by calling the get method with a key that is
147 * equal to the original key.
148 *
149 * @param keyName the key
150 * @param value the value
151 * @return False if the table key already exists with a different type
152 */
153 static bool PutNumber(std::string_view keyName, double value);
154
155 /**
156 * Gets the current value in the table, setting it if it does not exist.
157 *
158 * @param key The key.
159 * @param defaultValue The default value to set if key doesn't exist.
160 * @returns False if the table key exists with a different type
161 */
162 static bool SetDefaultNumber(std::string_view key, double defaultValue);
163
164 /**
165 * Returns the value at the specified key.
166 *
167 * If the key is not found, returns the default value.
168 *
169 * @param keyName the key
170 * @param defaultValue the default value to set if the key doesn't exist
171 * @return the value
172 */
173 static double GetNumber(std::string_view keyName, double defaultValue);
174
175 /**
176 * Maps the specified key to the specified value in this table.
177 *
178 * The value can be retrieved by calling the get method with a key that is
179 * equal to the original key.
180 *
181 * @param keyName the key
182 * @param value the value
183 * @return False if the table key already exists with a different type
184 */
185 static bool PutString(std::string_view keyName, std::string_view value);
186
187 /**
188 * Gets the current value in the table, setting it if it does not exist.
189 *
190 * @param key the key
191 * @param defaultValue the default value to set if key doesn't exist.
192 * @returns False if the table key exists with a different type
193 */
195 std::string_view defaultValue);
196
197 /**
198 * Returns the value at the specified key.
199 *
200 * If the key is not found, returns the default value.
201 *
202 * @param keyName the key
203 * @param defaultValue the default value to set if the key doesn't exist
204 * @return the value
205 */
206 static std::string GetString(std::string_view keyName,
207 std::string_view defaultValue);
208
209 /**
210 * Put a boolean array in the table.
211 *
212 * @param key the key to be assigned to
213 * @param value the value that will be assigned
214 * @return False if the table key already exists with a different type
215 *
216 * @note The array must be of int's rather than of bool's because
217 * std::vector<bool> is special-cased in C++. 0 is false, any
218 * non-zero value is true.
219 */
220 static bool PutBooleanArray(std::string_view key, std::span<const int> value);
221
222 /**
223 * Gets the current value in the table, setting it if it does not exist.
224 *
225 * @param key the key
226 * @param defaultValue the default value to set if key doesn't exist.
227 * @returns False if the table key exists with a different type
228 */
230 std::span<const int> defaultValue);
231
232 /**
233 * Returns the boolean array the key maps to.
234 *
235 * If the key does not exist or is of different type, it will return the
236 * default value.
237 *
238 * @param key The key to look up.
239 * @param defaultValue The value to be returned if no value is found.
240 * @return the value associated with the given key or the given default value
241 * if there is no value associated with the key
242 *
243 * @note This makes a copy of the array. If the overhead of this is a concern,
244 * use GetValue() instead.
245 *
246 * @note The returned array is std::vector<int> instead of std::vector<bool>
247 * because std::vector<bool> is special-cased in C++. 0 is false, any
248 * non-zero value is true.
249 */
250 static std::vector<int> GetBooleanArray(std::string_view key,
251 std::span<const int> defaultValue);
252
253 /**
254 * Put a number array in the table.
255 *
256 * @param key The key to be assigned to.
257 * @param value The value that will be assigned.
258 * @return False if the table key already exists with a different type
259 */
261 std::span<const double> value);
262
263 /**
264 * Gets the current value in the table, setting it if it does not exist.
265 *
266 * @param key The key.
267 * @param defaultValue The default value to set if key doesn't exist.
268 * @returns False if the table key exists with a different type
269 */
271 std::span<const double> defaultValue);
272
273 /**
274 * Returns the number array the key maps to.
275 *
276 * If the key does not exist or is of different type, it will return the
277 * default value.
278 *
279 * @param key The key to look up.
280 * @param defaultValue The value to be returned if no value is found.
281 * @return the value associated with the given key or the given default value
282 * if there is no value associated with the key
283 *
284 * @note This makes a copy of the array. If the overhead of this is a concern,
285 * use GetValue() instead.
286 */
287 static std::vector<double> GetNumberArray(
288 std::string_view key, std::span<const double> defaultValue);
289
290 /**
291 * Put a string array in the table.
292 *
293 * @param key The key to be assigned to.
294 * @param value The value that will be assigned.
295 * @return False if the table key already exists with a different type
296 */
298 std::span<const std::string> value);
299
300 /**
301 * Gets the current value in the table, setting it if it does not exist.
302 *
303 * @param key The key.
304 * @param defaultValue The default value to set if key doesn't exist.
305 * @returns False if the table key exists with a different type
306 */
308 std::span<const std::string> defaultValue);
309
310 /**
311 * Returns the string array the key maps to.
312 *
313 * If the key does not exist or is of different type, it will return the
314 * default value.
315 *
316 * @param key The key to look up.
317 * @param defaultValue The value to be returned if no value is found.
318 * @return the value associated with the given key or the given default value
319 * if there is no value associated with the key
320 *
321 * @note This makes a copy of the array. If the overhead of this is a concern,
322 * use GetValue() instead.
323 */
324 static std::vector<std::string> GetStringArray(
325 std::string_view key, std::span<const std::string> defaultValue);
326
327 /**
328 * Put a raw value (byte array) in the table.
329 *
330 * @param key The key to be assigned to.
331 * @param value The value that will be assigned.
332 * @return False if the table key already exists with a different type
333 */
334 static bool PutRaw(std::string_view key, std::span<const uint8_t> value);
335
336 /**
337 * Gets the current value in the table, setting it if it does not exist.
338 *
339 * @param key The key.
340 * @param defaultValue The default value to set if key doesn't exist.
341 * @returns False if the table key exists with a different type
342 */
344 std::span<const uint8_t> defaultValue);
345
346 /**
347 * Returns the raw value (byte array) the key maps to.
348 *
349 * If the key does not exist or is of different type, it will return the
350 * default value.
351 *
352 * @param key The key to look up.
353 * @param defaultValue The value to be returned if no value is found.
354 * @return the value associated with the given key or the given default value
355 * if there is no value associated with the key
356 *
357 * @note This makes a copy of the raw contents. If the overhead of this is a
358 * concern, use GetValue() instead.
359 */
360 static std::vector<uint8_t> GetRaw(std::string_view key,
361 std::span<const uint8_t> defaultValue);
362
363 /**
364 * Maps the specified key to the specified complex value (such as an array) in
365 * this table.
366 *
367 * The value can be retrieved by calling the RetrieveValue method with a key
368 * that is equal to the original key.
369 *
370 * @param keyName the key
371 * @param value the value
372 * @return False if the table key already exists with a different type
373 */
374 static bool PutValue(std::string_view keyName, const nt::Value& value);
375
376 /**
377 * Gets the current value in the table, setting it if it does not exist.
378 *
379 * @param key the key
380 * @param defaultValue The default value to set if key doesn't exist.
381 * @returns False if the table key exists with a different type
382 */
384 const nt::Value& defaultValue);
385
386 /**
387 * Retrieves the complex value (such as an array) in this table into the
388 * complex data object.
389 *
390 * @param keyName the key
391 */
393
394 /**
395 * Posts a task from a listener to the ListenerExecutor, so that it can be run
396 * synchronously from the main loop on the next call to updateValues().
397 *
398 * @param task The task to run synchronously from the main thread.
399 */
400 static void PostListenerTask(std::function<void()> task);
401
402 /**
403 * Puts all sendable data to the dashboard.
404 */
405 static void UpdateValues();
406};
407
408} // namespace frc
Definition: SmartDashboard.h:23
static wpi::Sendable * GetData(std::string_view keyName)
Returns the value at the specified key.
static void init()
static bool IsPersistent(std::string_view key)
Returns whether the value is persistent through program restarts.
static void UpdateValues()
Puts all sendable data to the dashboard.
static void PutData(wpi::Sendable *value)
Maps the specified key (where the key is the name of the Sendable) to the specified value in this tab...
static bool SetDefaultNumber(std::string_view key, double defaultValue)
Gets the current value in the table, setting it if it does not exist.
static bool PutBoolean(std::string_view keyName, bool value)
Maps the specified key to the specified value in this table.
static bool SetDefaultNumberArray(std::string_view key, std::span< const double > defaultValue)
Gets the current value in the table, setting it if it does not exist.
static bool GetBoolean(std::string_view keyName, bool defaultValue)
Returns the value at the specified key.
static void SetPersistent(std::string_view key)
Makes a key's value persistent through program restarts.
static bool PutBooleanArray(std::string_view key, std::span< const int > value)
Put a boolean array in the table.
static bool SetDefaultStringArray(std::string_view key, std::span< const std::string > defaultValue)
Gets the current value in the table, setting it if it does not exist.
static bool SetDefaultBoolean(std::string_view key, bool defaultValue)
Gets the current value in the table, setting it if it does not exist.
static nt::Value GetValue(std::string_view keyName)
Retrieves the complex value (such as an array) in this table into the complex data object.
static std::string GetString(std::string_view keyName, std::string_view defaultValue)
Returns the value at the specified key.
static nt::NetworkTableEntry GetEntry(std::string_view key)
Returns an NT Entry mapping to the specified key.
static bool SetDefaultBooleanArray(std::string_view key, std::span< const int > defaultValue)
Gets the current value in the table, setting it if it does not exist.
static bool PutNumber(std::string_view keyName, double value)
Maps the specified key to the specified value in this table.
static std::vector< double > GetNumberArray(std::string_view key, std::span< const double > defaultValue)
Returns the number array the key maps to.
static std::vector< std::string > GetStringArray(std::string_view key, std::span< const std::string > defaultValue)
Returns the string array the key maps to.
static std::vector< uint8_t > GetRaw(std::string_view key, std::span< const uint8_t > defaultValue)
Returns the raw value (byte array) the key maps to.
static bool SetDefaultString(std::string_view key, std::string_view defaultValue)
Gets the current value in the table, setting it if it does not exist.
static bool PutValue(std::string_view keyName, const nt::Value &value)
Maps the specified key to the specified complex value (such as an array) in this table.
static bool SetDefaultRaw(std::string_view key, std::span< const uint8_t > defaultValue)
Gets the current value in the table, setting it if it does not exist.
static void ClearPersistent(std::string_view key)
Stop making a key's value persistent through program restarts.
static bool SetDefaultValue(std::string_view key, const nt::Value &defaultValue)
Gets the current value in the table, setting it if it does not exist.
static bool ContainsKey(std::string_view key)
Determines whether the given key is in this table.
static double GetNumber(std::string_view keyName, double defaultValue)
Returns the value at the specified key.
static bool PutStringArray(std::string_view key, std::span< const std::string > value)
Put a string array in the table.
static void PutData(std::string_view key, wpi::Sendable *data)
Maps the specified key to the specified value in this table.
static void PostListenerTask(std::function< void()> task)
Posts a task from a listener to the ListenerExecutor, so that it can be run synchronously from the ma...
static std::vector< int > GetBooleanArray(std::string_view key, std::span< const int > defaultValue)
Returns the boolean array the key maps to.
static bool PutString(std::string_view keyName, std::string_view value)
Maps the specified key to the specified value in this table.
static bool PutRaw(std::string_view key, std::span< const uint8_t > value)
Put a raw value (byte array) in the table.
static bool PutNumberArray(std::string_view key, std::span< const double > value)
Put a number array in the table.
static std::vector< std::string > GetKeys(int types=0)
NetworkTables Entry.
Definition: NetworkTableEntry.h:34
A network table entry value.
Definition: NetworkTableValue.h:32
Interface for Sendable objects.
Definition: Sendable.h:16
basic_string_view< char > string_view
Definition: core.h:501
Definition: AprilTagPoseEstimator.h:15
Definition: ntcore_cpp.h:26