2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this 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
|
|
|
|
2019-11-08 22:53:20 -08:00
|
|
|
#include <hal/FRCUsageReporting.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>
|
2021-06-13 16:38:05 -07:00
|
|
|
#include <wpi/sendable/SendableRegistry.h>
|
2017-09-02 00:17:43 -07:00
|
|
|
|
2021-04-18 20:35:29 -07:00
|
|
|
#include "frc/Errors.h"
|
2021-06-13 16:38:05 -07:00
|
|
|
#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-04 23:50:27 -08:00
|
|
|
class Singleton {
|
|
|
|
|
public:
|
|
|
|
|
static Singleton& GetInstance();
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<nt::NetworkTable> table;
|
2021-06-13 16:38:05 -07:00
|
|
|
wpi::StringMap<wpi::SendableRegistry::UID> 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
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void SmartDashboard::init() {
|
|
|
|
|
Singleton::GetInstance();
|
|
|
|
|
}
|
2017-12-04 23:50:27 -08:00
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
bool SmartDashboard::ContainsKey(std::string_view 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
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
void SmartDashboard::SetPersistent(std::string_view key) {
|
2017-12-04 23:50:27 -08:00
|
|
|
Singleton::GetInstance().table->GetEntry(key).SetPersistent();
|
2016-10-03 09:59:18 -07:00
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
void SmartDashboard::ClearPersistent(std::string_view key) {
|
2017-12-04 23:50:27 -08:00
|
|
|
Singleton::GetInstance().table->GetEntry(key).ClearPersistent();
|
2016-10-03 09:59:18 -07:00
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
bool SmartDashboard::IsPersistent(std::string_view key) {
|
2017-12-04 23:50:27 -08:00
|
|
|
return Singleton::GetInstance().table->GetEntry(key).IsPersistent();
|
2016-10-03 09:59:18 -07:00
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
void SmartDashboard::SetFlags(std::string_view 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
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
void SmartDashboard::ClearFlags(std::string_view 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
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
unsigned int SmartDashboard::GetFlags(std::string_view key) {
|
2017-12-04 23:50:27 -08:00
|
|
|
return Singleton::GetInstance().table->GetEntry(key).GetFlags();
|
2016-10-03 09:59:18 -07:00
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
void SmartDashboard::Delete(std::string_view key) {
|
2017-12-04 23:50:27 -08:00
|
|
|
Singleton::GetInstance().table->Delete(key);
|
|
|
|
|
}
|
2016-10-03 09:59:18 -07:00
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
nt::NetworkTableEntry SmartDashboard::GetEntry(std::string_view key) {
|
2019-11-12 17:11:32 -08:00
|
|
|
return Singleton::GetInstance().table->GetEntry(key);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 16:38:05 -07:00
|
|
|
void SmartDashboard::PutData(std::string_view key, wpi::Sendable* data) {
|
2021-04-18 20:35:29 -07:00
|
|
|
if (!data) {
|
2021-05-23 19:33:33 -07:00
|
|
|
throw FRC_MakeError(err::NullParameter, "{}", "value");
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2017-12-04 23:50:27 -08:00
|
|
|
auto& inst = Singleton::GetInstance();
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(inst.tablesToDataMutex);
|
2019-10-17 22:01:31 -07:00
|
|
|
auto& uid = inst.tablesToData[key];
|
2021-06-13 16:38:05 -07:00
|
|
|
auto& registry = wpi::SendableRegistry::GetInstance();
|
|
|
|
|
wpi::Sendable* sddata = registry.GetSendable(uid);
|
2019-10-17 22:01:31 -07:00
|
|
|
if (sddata != data) {
|
|
|
|
|
uid = registry.GetUniqueId(data);
|
2018-01-02 14:39:16 -08:00
|
|
|
auto dataTable = inst.table->GetSubTable(key);
|
2021-06-13 16:38:05 -07:00
|
|
|
auto builder = std::make_unique<SendableBuilderImpl>();
|
|
|
|
|
auto builderPtr = builder.get();
|
|
|
|
|
builderPtr->SetTable(dataTable);
|
|
|
|
|
registry.Publish(uid, std::move(builder));
|
|
|
|
|
builderPtr->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
|
|
|
}
|
|
|
|
|
|
2021-06-13 16:38:05 -07:00
|
|
|
void SmartDashboard::PutData(wpi::Sendable* value) {
|
2021-04-18 20:35:29 -07:00
|
|
|
if (!value) {
|
2021-05-23 19:33:33 -07:00
|
|
|
throw FRC_MakeError(err::NullParameter, "{}", "value");
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2021-06-13 16:38:05 -07:00
|
|
|
auto name = wpi::SendableRegistry::GetInstance().GetName(value);
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!name.empty()) {
|
|
|
|
|
PutData(name, value);
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2021-06-13 16:38:05 -07:00
|
|
|
wpi::Sendable* SmartDashboard::GetData(std::string_view key) {
|
2017-12-04 23:50:27 -08:00
|
|
|
auto& inst = Singleton::GetInstance();
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(inst.tablesToDataMutex);
|
2019-10-17 22:01:31 -07:00
|
|
|
auto it = inst.tablesToData.find(key);
|
|
|
|
|
if (it == inst.tablesToData.end()) {
|
2021-05-23 19:33:33 -07:00
|
|
|
throw FRC_MakeError(err::SmartDashboardMissingKey, "{}", key);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2021-06-13 16:38:05 -07:00
|
|
|
return wpi::SendableRegistry::GetInstance().GetSendable(it->getValue());
|
2014-08-04 14:43:31 -04:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
bool SmartDashboard::PutBoolean(std::string_view 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
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
bool SmartDashboard::SetDefaultBoolean(std::string_view 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
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
bool SmartDashboard::GetBoolean(std::string_view 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
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
bool SmartDashboard::PutNumber(std::string_view 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
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
bool SmartDashboard::SetDefaultNumber(std::string_view 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
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
double SmartDashboard::GetNumber(std::string_view 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
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
bool SmartDashboard::PutString(std::string_view keyName,
|
|
|
|
|
std::string_view 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
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
bool SmartDashboard::SetDefaultString(std::string_view key,
|
|
|
|
|
std::string_view 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
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
std::string SmartDashboard::GetString(std::string_view keyName,
|
|
|
|
|
std::string_view 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
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
bool SmartDashboard::PutBooleanArray(std::string_view key,
|
2021-06-06 19:51:14 -07:00
|
|
|
wpi::span<const 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
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
bool SmartDashboard::SetDefaultBooleanArray(std::string_view key,
|
2021-06-06 19:51:14 -07:00
|
|
|
wpi::span<const 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(
|
2021-06-06 19:51:14 -07:00
|
|
|
std::string_view key, wpi::span<const 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
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
bool SmartDashboard::PutNumberArray(std::string_view key,
|
2021-06-06 19:51:14 -07:00
|
|
|
wpi::span<const 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
|
|
|
}
|
|
|
|
|
|
2021-06-06 19:51:14 -07:00
|
|
|
bool SmartDashboard::SetDefaultNumberArray(
|
|
|
|
|
std::string_view key, wpi::span<const 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(
|
2021-06-06 19:51:14 -07:00
|
|
|
std::string_view key, wpi::span<const 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
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
bool SmartDashboard::PutStringArray(std::string_view key,
|
2021-06-06 19:51:14 -07:00
|
|
|
wpi::span<const 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(
|
2021-06-06 19:51:14 -07:00
|
|
|
std::string_view key, wpi::span<const 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(
|
2021-06-06 19:51:14 -07:00
|
|
|
std::string_view key, wpi::span<const 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
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
bool SmartDashboard::PutRaw(std::string_view key, std::string_view 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
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
bool SmartDashboard::SetDefaultRaw(std::string_view key,
|
|
|
|
|
std::string_view 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
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
std::string SmartDashboard::GetRaw(std::string_view key,
|
|
|
|
|
std::string_view 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
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
bool SmartDashboard::PutValue(std::string_view keyName,
|
2018-05-31 20:47:15 -07:00
|
|
|
std::shared_ptr<nt::Value> value) {
|
|
|
|
|
return Singleton::GetInstance().table->GetEntry(keyName).SetValue(value);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
bool SmartDashboard::SetDefaultValue(std::string_view key,
|
2018-05-31 20:47:15 -07:00
|
|
|
std::shared_ptr<nt::Value> defaultValue) {
|
|
|
|
|
return Singleton::GetInstance().table->GetEntry(key).SetDefaultValue(
|
|
|
|
|
defaultValue);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
std::shared_ptr<nt::Value> SmartDashboard::GetValue(std::string_view keyName) {
|
2018-05-31 20:47:15 -07:00
|
|
|
return Singleton::GetInstance().table->GetEntry(keyName).GetValue();
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-03 18:08:06 -04:00
|
|
|
detail::ListenerExecutor SmartDashboard::listenerExecutor;
|
|
|
|
|
|
|
|
|
|
void SmartDashboard::PostListenerTask(std::function<void()> task) {
|
|
|
|
|
listenerExecutor.Execute(task);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 17:18:02 -06:00
|
|
|
void SmartDashboard::UpdateValues() {
|
2021-06-13 16:38:05 -07:00
|
|
|
auto& registry = wpi::SendableRegistry::GetInstance();
|
2017-12-26 17:18:02 -06:00
|
|
|
auto& inst = Singleton::GetInstance();
|
2020-11-30 19:24:12 -08:00
|
|
|
listenerExecutor.RunListenerTasks();
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(inst.tablesToDataMutex);
|
2020-12-28 12:58:06 -08:00
|
|
|
for (auto& i : inst.tablesToData) {
|
|
|
|
|
registry.Update(i.getValue());
|
|
|
|
|
}
|
2017-12-26 17:18:02 -06:00
|
|
|
}
|