Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 2 | /* Copyright (c) FIRST 2011-2016. All Rights Reserved. */ |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 6 | /*----------------------------------------------------------------------------*/ |
| 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 | |
| 16 | std::shared_ptr<ITable> SmartDashboard::m_table; |
| 17 | std::map<std::shared_ptr<ITable> , Sendable *> SmartDashboard::m_tablesToData; |
| 18 | |
| 19 | void 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 | */ |
| 33 | void 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 | */ |
| 52 | void 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 | */ |
| 65 | Sendable *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 | */ |
| 84 | void 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 | */ |
| 96 | std::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 | */ |
| 108 | void 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 | */ |
| 118 | bool 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 | */ |
| 130 | void 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 | */ |
| 140 | double 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 | */ |
| 152 | void 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 | */ |
| 162 | std::string SmartDashboard::GetString(llvm::StringRef keyName, |
| 163 | llvm::StringRef defaultValue) { |
| 164 | return m_table->GetString(keyName, defaultValue); |
| 165 | } |