blob: 9fbf60551b26d780d911cc413c225ef324c48c5c [file] [log] [blame]
Brian Silverman26e4e522015-12-17 01:56:40 -05001/*----------------------------------------------------------------------------*/
Brian Silverman1a675112016-02-20 20:42:49 -05002/* Copyright (c) FIRST 2011-2016. All Rights Reserved. */
Brian Silverman26e4e522015-12-17 01:56:40 -05003/* Open Source Software - may be modified and shared by FRC teams. The code */
Brian Silverman1a675112016-02-20 20:42:49 -05004/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
Brian Silverman26e4e522015-12-17 01:56:40 -05006/*----------------------------------------------------------------------------*/
7
8#include "SmartDashboard/SmartDashboard.h"
9
10//#include "NetworkCommunication/UsageReporting.h"
11#include "SmartDashboard/NamedSendable.h"
12#include "WPIErrors.h"
13#include "networktables/NetworkTable.h"
14#include "HLUsageReporting.h"
15
16std::shared_ptr<ITable> SmartDashboard::m_table;
17std::map<std::shared_ptr<ITable> , Sendable *> SmartDashboard::m_tablesToData;
18
19void SmartDashboard::init() {
20 m_table = NetworkTable::GetTable("SmartDashboard");
21
22 HLUsageReporting::ReportSmartDashboard();
23}
24
25/**
26 * Maps the specified key to the specified value in this table.
27 * The key can not be nullptr.
28 * The value can be retrieved by calling the get method with a key that is equal
29 * to the original key.
30 * @param keyName the key
31 * @param value the value
32 */
33void SmartDashboard::PutData(llvm::StringRef key, Sendable *data) {
34 if (data == nullptr) {
35 wpi_setGlobalWPIErrorWithContext(NullParameter, "value");
36 return;
37 }
38 std::shared_ptr<ITable> dataTable(m_table->GetSubTable(key));
39 dataTable->PutString("~TYPE~", data->GetSmartDashboardType());
40 data->InitTable(dataTable);
41 m_tablesToData[dataTable] = data;
42}
43
44/**
45 * Maps the specified key (where the key is the name of the {@link
46 * SmartDashboardNamedData}
47 * to the specified value in this table.
48 * The value can be retrieved by calling the get method with a key that is equal
49 * to the original key.
50 * @param value the value
51 */
52void SmartDashboard::PutData(NamedSendable *value) {
53 if (value == nullptr) {
54 wpi_setGlobalWPIErrorWithContext(NullParameter, "value");
55 return;
56 }
57 PutData(value->GetName(), value);
58}
59
60/**
61 * Returns the value at the specified key.
62 * @param keyName the key
63 * @return the value
64 */
65Sendable *SmartDashboard::GetData(llvm::StringRef key) {
66 std::shared_ptr<ITable> subtable(m_table->GetSubTable(key));
67 Sendable *data = m_tablesToData[subtable];
68 if (data == nullptr) {
69 wpi_setGlobalWPIErrorWithContext(SmartDashboardMissingKey, key);
70 return nullptr;
71 }
72 return data;
73}
74
75/**
76 * Maps the specified key to the specified complex value (such as an array) in
77 * this table.
78 * The key can not be nullptr.
79 * The value can be retrieved by calling the RetrieveValue method with a key
80 * that is equal to the original key.
81 * @param keyName the key
82 * @param value the value
83 */
84void SmartDashboard::PutValue(llvm::StringRef keyName,
85 std::shared_ptr<nt::Value> value) {
86 m_table->PutValue(keyName, value);
87}
88
89/**
90 * Retrieves the complex value (such as an array) in this table into the complex
91 * data object
92 * The key can not be nullptr.
93 * @param keyName the key
94 * @param value the object to retrieve the value into
95 */
96std::shared_ptr<nt::Value> SmartDashboard::GetValue(llvm::StringRef keyName) {
97 return m_table->GetValue(keyName);
98}
99
100/**
101 * Maps the specified key to the specified value in this table.
102 * The key can not be nullptr.
103 * The value can be retrieved by calling the get method with a key that is equal
104 * to the original key.
105 * @param keyName the key
106 * @param value the value
107 */
108void SmartDashboard::PutBoolean(llvm::StringRef keyName, bool value) {
109 m_table->PutBoolean(keyName, value);
110}
111
112/**
113 * Returns the value at the specified key. If the key is not found, returns the
114 * default value.
115 * @param keyName the key
116 * @return the value
117 */
118bool SmartDashboard::GetBoolean(llvm::StringRef keyName, bool defaultValue) {
119 return m_table->GetBoolean(keyName, defaultValue);
120}
121
122/**
123 * Maps the specified key to the specified value in this table.
124 * The key can not be nullptr.
125 * The value can be retrieved by calling the get method with a key that is equal
126 * to the original key.
127 * @param keyName the key
128 * @param value the value
129 */
130void SmartDashboard::PutNumber(llvm::StringRef keyName, double value) {
131 m_table->PutNumber(keyName, value);
132}
133
134/**
135 * Returns the value at the specified key. If the key is not found, returns the
136 * default value.
137 * @param keyName the key
138 * @return the value
139 */
140double SmartDashboard::GetNumber(llvm::StringRef keyName, double defaultValue) {
141 return m_table->GetNumber(keyName, defaultValue);
142}
143
144/**
145 * Maps the specified key to the specified value in this table.
146 * Neither the key nor the value can be nullptr.
147 * The value can be retrieved by calling the get method with a key that is equal
148 * to the original key.
149 * @param keyName the key
150 * @param value the value
151 */
152void SmartDashboard::PutString(llvm::StringRef keyName, llvm::StringRef value) {
153 m_table->PutString(keyName, value);
154}
155
156/**
157 * Returns the value at the specified key. If the key is not found, returns the
158 * default value.
159 * @param keyName the key
160 * @return the value
161 */
162std::string SmartDashboard::GetString(llvm::StringRef keyName,
163 llvm::StringRef defaultValue) {
164 return m_table->GetString(keyName, defaultValue);
165}