2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2019-07-07 19:17:14 -07:00
|
|
|
/* Copyright (c) 2011-2019 FIRST. All Rights Reserved. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/smartdashboard/SmartDashboard.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include <hal/HAL.h>
|
2017-12-07 23:34:29 -08:00
|
|
|
#include <networktables/NetworkTable.h>
|
|
|
|
|
#include <networktables/NetworkTableInstance.h>
|
2018-04-29 23:33:19 -07:00
|
|
|
#include <wpi/StringMap.h>
|
|
|
|
|
#include <wpi/mutex.h>
|
2017-09-02 00:17:43 -07:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/WPIErrors.h"
|
|
|
|
|
#include "frc/smartdashboard/Sendable.h"
|
|
|
|
|
#include "frc/smartdashboard/SendableBuilderImpl.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
namespace {
|
2017-12-26 17:18:02 -06:00
|
|
|
class SmartDashboardData {
|
|
|
|
|
public:
|
|
|
|
|
SmartDashboardData() = default;
|
|
|
|
|
explicit SmartDashboardData(Sendable* sendable_) : sendable(sendable_) {}
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
Sendable* sendable = nullptr;
|
|
|
|
|
SendableBuilderImpl builder;
|
|
|
|
|
};
|
|
|
|
|
|
2017-12-04 23:50:27 -08:00
|
|
|
class Singleton {
|
|
|
|
|
public:
|
|
|
|
|
static Singleton& GetInstance();
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<nt::NetworkTable> table;
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::StringMap<SmartDashboardData> tablesToData;
|
2017-12-04 23:50:27 -08:00
|
|
|
wpi::mutex tablesToDataMutex;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
Singleton() {
|
|
|
|
|
table = nt::NetworkTableInstance::GetDefault().GetTable("SmartDashboard");
|
2018-07-08 15:41:31 -07:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_SmartDashboard, 0);
|
2017-12-04 23:50:27 -08:00
|
|
|
}
|
|
|
|
|
Singleton(const Singleton&) = delete;
|
|
|
|
|
Singleton& operator=(const Singleton&) = delete;
|
|
|
|
|
};
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2017-12-04 23:50:27 -08:00
|
|
|
} // namespace
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2017-12-04 23:50:27 -08:00
|
|
|
Singleton& Singleton::GetInstance() {
|
|
|
|
|
static Singleton instance;
|
|
|
|
|
return instance;
|
2014-08-04 14:43:31 -04:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2017-12-04 23:50:27 -08:00
|
|
|
void SmartDashboard::init() { Singleton::GetInstance(); }
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
bool SmartDashboard::ContainsKey(wpi::StringRef key) {
|
2017-12-04 23:50:27 -08:00
|
|
|
return Singleton::GetInstance().table->ContainsKey(key);
|
2016-10-03 09:59:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> SmartDashboard::GetKeys(int types) {
|
2017-12-04 23:50:27 -08:00
|
|
|
return Singleton::GetInstance().table->GetKeys(types);
|
2016-10-03 09:59:18 -07:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
void SmartDashboard::SetPersistent(wpi::StringRef key) {
|
2017-12-04 23:50:27 -08:00
|
|
|
Singleton::GetInstance().table->GetEntry(key).SetPersistent();
|
2016-10-03 09:59:18 -07:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
void SmartDashboard::ClearPersistent(wpi::StringRef key) {
|
2017-12-04 23:50:27 -08:00
|
|
|
Singleton::GetInstance().table->GetEntry(key).ClearPersistent();
|
2016-10-03 09:59:18 -07:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
bool SmartDashboard::IsPersistent(wpi::StringRef key) {
|
2017-12-04 23:50:27 -08:00
|
|
|
return Singleton::GetInstance().table->GetEntry(key).IsPersistent();
|
2016-10-03 09:59:18 -07:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
void SmartDashboard::SetFlags(wpi::StringRef key, unsigned int flags) {
|
2017-12-04 23:50:27 -08:00
|
|
|
Singleton::GetInstance().table->GetEntry(key).SetFlags(flags);
|
2016-10-03 09:59:18 -07:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
void SmartDashboard::ClearFlags(wpi::StringRef key, unsigned int flags) {
|
2017-12-04 23:50:27 -08:00
|
|
|
Singleton::GetInstance().table->GetEntry(key).ClearFlags(flags);
|
2016-10-03 09:59:18 -07:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
unsigned int SmartDashboard::GetFlags(wpi::StringRef key) {
|
2017-12-04 23:50:27 -08:00
|
|
|
return Singleton::GetInstance().table->GetEntry(key).GetFlags();
|
2016-10-03 09:59:18 -07:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
void SmartDashboard::Delete(wpi::StringRef key) {
|
2017-12-04 23:50:27 -08:00
|
|
|
Singleton::GetInstance().table->Delete(key);
|
|
|
|
|
}
|
2016-10-03 09:59:18 -07:00
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
void SmartDashboard::PutData(wpi::StringRef key, Sendable* data) {
|
2015-06-23 04:49:51 -07:00
|
|
|
if (data == nullptr) {
|
2015-06-25 15:07:55 -04:00
|
|
|
wpi_setGlobalWPIErrorWithContext(NullParameter, "value");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-12-04 23:50:27 -08:00
|
|
|
auto& inst = Singleton::GetInstance();
|
2019-07-07 19:17:14 -07:00
|
|
|
std::lock_guard lock(inst.tablesToDataMutex);
|
2017-12-04 23:50:27 -08:00
|
|
|
auto& sddata = inst.tablesToData[key];
|
2017-12-04 23:28:33 -08:00
|
|
|
if (!sddata.sendable || sddata.sendable != data) {
|
2017-12-26 17:18:02 -06:00
|
|
|
sddata = SmartDashboardData(data);
|
2018-01-02 14:39:16 -08:00
|
|
|
auto dataTable = inst.table->GetSubTable(key);
|
|
|
|
|
sddata.builder.SetTable(dataTable);
|
2017-12-04 23:28:33 -08:00
|
|
|
data->InitSendable(sddata.builder);
|
2017-12-26 17:18:02 -06:00
|
|
|
sddata.builder.UpdateTable();
|
|
|
|
|
sddata.builder.StartListeners();
|
2018-01-02 14:39:16 -08:00
|
|
|
dataTable->GetEntry(".name").SetString(key);
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
void SmartDashboard::PutData(Sendable* value) {
|
2015-06-23 04:49:51 -07:00
|
|
|
if (value == nullptr) {
|
2015-06-25 15:07:55 -04:00
|
|
|
wpi_setGlobalWPIErrorWithContext(NullParameter, "value");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
PutData(value->GetName(), value);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
Sendable* SmartDashboard::GetData(wpi::StringRef key) {
|
2017-12-04 23:50:27 -08:00
|
|
|
auto& inst = Singleton::GetInstance();
|
2019-07-07 19:17:14 -07:00
|
|
|
std::lock_guard lock(inst.tablesToDataMutex);
|
2017-12-04 23:50:27 -08:00
|
|
|
auto data = inst.tablesToData.find(key);
|
|
|
|
|
if (data == inst.tablesToData.end()) {
|
2015-08-13 23:17:19 -07:00
|
|
|
wpi_setGlobalWPIErrorWithContext(SmartDashboardMissingKey, key);
|
2015-06-23 04:49:51 -07:00
|
|
|
return nullptr;
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2017-12-04 23:28:33 -08:00
|
|
|
return data->getValue().sendable;
|
2014-08-04 14:43:31 -04:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
bool SmartDashboard::PutBoolean(wpi::StringRef keyName, bool value) {
|
2017-12-04 23:50:27 -08:00
|
|
|
return Singleton::GetInstance().table->GetEntry(keyName).SetBoolean(value);
|
2016-10-03 09:59:18 -07:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
bool SmartDashboard::SetDefaultBoolean(wpi::StringRef key, bool defaultValue) {
|
2017-12-04 23:50:27 -08:00
|
|
|
return Singleton::GetInstance().table->GetEntry(key).SetDefaultBoolean(
|
|
|
|
|
defaultValue);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
bool SmartDashboard::GetBoolean(wpi::StringRef keyName, bool defaultValue) {
|
2017-12-04 23:50:27 -08:00
|
|
|
return Singleton::GetInstance().table->GetEntry(keyName).GetBoolean(
|
|
|
|
|
defaultValue);
|
2014-10-15 17:03:04 -04:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
bool SmartDashboard::PutNumber(wpi::StringRef keyName, double value) {
|
2017-12-04 23:50:27 -08:00
|
|
|
return Singleton::GetInstance().table->GetEntry(keyName).SetDouble(value);
|
2016-10-03 09:59:18 -07:00
|
|
|
}
|
|
|
|
|
|
2018-05-13 17:09:56 -07:00
|
|
|
bool SmartDashboard::SetDefaultNumber(wpi::StringRef key, double defaultValue) {
|
2017-12-04 23:50:27 -08:00
|
|
|
return Singleton::GetInstance().table->GetEntry(key).SetDefaultDouble(
|
|
|
|
|
defaultValue);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
double SmartDashboard::GetNumber(wpi::StringRef keyName, double defaultValue) {
|
2017-12-04 23:50:27 -08:00
|
|
|
return Singleton::GetInstance().table->GetEntry(keyName).GetDouble(
|
|
|
|
|
defaultValue);
|
2014-10-15 17:03:04 -04:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
bool SmartDashboard::PutString(wpi::StringRef keyName, wpi::StringRef value) {
|
2017-12-04 23:50:27 -08:00
|
|
|
return Singleton::GetInstance().table->GetEntry(keyName).SetString(value);
|
2016-10-03 09:59:18 -07:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
bool SmartDashboard::SetDefaultString(wpi::StringRef key,
|
|
|
|
|
wpi::StringRef defaultValue) {
|
2017-12-04 23:50:27 -08:00
|
|
|
return Singleton::GetInstance().table->GetEntry(key).SetDefaultString(
|
|
|
|
|
defaultValue);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
std::string SmartDashboard::GetString(wpi::StringRef keyName,
|
|
|
|
|
wpi::StringRef defaultValue) {
|
2017-12-04 23:50:27 -08:00
|
|
|
return Singleton::GetInstance().table->GetEntry(keyName).GetString(
|
|
|
|
|
defaultValue);
|
2014-10-15 17:03:04 -04:00
|
|
|
}
|
2016-10-03 09:59:18 -07:00
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
bool SmartDashboard::PutBooleanArray(wpi::StringRef key,
|
|
|
|
|
wpi::ArrayRef<int> value) {
|
2017-12-04 23:50:27 -08:00
|
|
|
return Singleton::GetInstance().table->GetEntry(key).SetBooleanArray(value);
|
2016-10-03 09:59:18 -07:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
bool SmartDashboard::SetDefaultBooleanArray(wpi::StringRef key,
|
|
|
|
|
wpi::ArrayRef<int> defaultValue) {
|
2017-12-04 23:50:27 -08:00
|
|
|
return Singleton::GetInstance().table->GetEntry(key).SetDefaultBooleanArray(
|
|
|
|
|
defaultValue);
|
2016-10-03 09:59:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<int> SmartDashboard::GetBooleanArray(
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::StringRef key, wpi::ArrayRef<int> defaultValue) {
|
2017-12-04 23:50:27 -08:00
|
|
|
return Singleton::GetInstance().table->GetEntry(key).GetBooleanArray(
|
|
|
|
|
defaultValue);
|
2016-10-03 09:59:18 -07:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
bool SmartDashboard::PutNumberArray(wpi::StringRef key,
|
|
|
|
|
wpi::ArrayRef<double> value) {
|
2017-12-04 23:50:27 -08:00
|
|
|
return Singleton::GetInstance().table->GetEntry(key).SetDoubleArray(value);
|
2016-10-03 09:59:18 -07:00
|
|
|
}
|
|
|
|
|
|
2018-05-13 17:09:56 -07:00
|
|
|
bool SmartDashboard::SetDefaultNumberArray(wpi::StringRef key,
|
|
|
|
|
wpi::ArrayRef<double> defaultValue) {
|
2017-12-04 23:50:27 -08:00
|
|
|
return Singleton::GetInstance().table->GetEntry(key).SetDefaultDoubleArray(
|
|
|
|
|
defaultValue);
|
2016-10-03 09:59:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<double> SmartDashboard::GetNumberArray(
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::StringRef key, wpi::ArrayRef<double> defaultValue) {
|
2017-12-04 23:50:27 -08:00
|
|
|
return Singleton::GetInstance().table->GetEntry(key).GetDoubleArray(
|
|
|
|
|
defaultValue);
|
2016-10-03 09:59:18 -07:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
bool SmartDashboard::PutStringArray(wpi::StringRef key,
|
|
|
|
|
wpi::ArrayRef<std::string> value) {
|
2017-12-04 23:50:27 -08:00
|
|
|
return Singleton::GetInstance().table->GetEntry(key).SetStringArray(value);
|
2016-10-03 09:59:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SmartDashboard::SetDefaultStringArray(
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::StringRef key, wpi::ArrayRef<std::string> defaultValue) {
|
2017-12-04 23:50:27 -08:00
|
|
|
return Singleton::GetInstance().table->GetEntry(key).SetDefaultStringArray(
|
|
|
|
|
defaultValue);
|
2016-10-03 09:59:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> SmartDashboard::GetStringArray(
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::StringRef key, wpi::ArrayRef<std::string> defaultValue) {
|
2017-12-04 23:50:27 -08:00
|
|
|
return Singleton::GetInstance().table->GetEntry(key).GetStringArray(
|
|
|
|
|
defaultValue);
|
2016-10-03 09:59:18 -07:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
bool SmartDashboard::PutRaw(wpi::StringRef key, wpi::StringRef value) {
|
2017-12-04 23:50:27 -08:00
|
|
|
return Singleton::GetInstance().table->GetEntry(key).SetRaw(value);
|
2016-10-03 09:59:18 -07:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
bool SmartDashboard::SetDefaultRaw(wpi::StringRef key,
|
|
|
|
|
wpi::StringRef defaultValue) {
|
2017-12-04 23:50:27 -08:00
|
|
|
return Singleton::GetInstance().table->GetEntry(key).SetDefaultRaw(
|
|
|
|
|
defaultValue);
|
2016-10-03 09:59:18 -07:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
std::string SmartDashboard::GetRaw(wpi::StringRef key,
|
|
|
|
|
wpi::StringRef defaultValue) {
|
2017-12-04 23:50:27 -08:00
|
|
|
return Singleton::GetInstance().table->GetEntry(key).GetRaw(defaultValue);
|
2016-10-03 09:59:18 -07:00
|
|
|
}
|
2017-12-26 17:18:02 -06:00
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
bool SmartDashboard::PutValue(wpi::StringRef keyName,
|
|
|
|
|
std::shared_ptr<nt::Value> value) {
|
|
|
|
|
return Singleton::GetInstance().table->GetEntry(keyName).SetValue(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SmartDashboard::SetDefaultValue(wpi::StringRef key,
|
|
|
|
|
std::shared_ptr<nt::Value> defaultValue) {
|
|
|
|
|
return Singleton::GetInstance().table->GetEntry(key).SetDefaultValue(
|
|
|
|
|
defaultValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<nt::Value> SmartDashboard::GetValue(wpi::StringRef keyName) {
|
|
|
|
|
return Singleton::GetInstance().table->GetEntry(keyName).GetValue();
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 17:18:02 -06:00
|
|
|
void SmartDashboard::UpdateValues() {
|
|
|
|
|
auto& inst = Singleton::GetInstance();
|
2019-07-07 19:17:14 -07:00
|
|
|
std::lock_guard lock(inst.tablesToDataMutex);
|
2017-12-26 17:18:02 -06:00
|
|
|
for (auto& i : inst.tablesToData) {
|
|
|
|
|
i.getValue().builder.UpdateTable();
|
|
|
|
|
}
|
|
|
|
|
}
|