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.
|
2017-12-04 23:28:33 -08:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/smartdashboard/SendableBuilderImpl.h"
|
2017-12-04 23:28:33 -08:00
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
#include <networktables/BooleanArrayTopic.h>
|
|
|
|
|
#include <networktables/BooleanTopic.h>
|
|
|
|
|
#include <networktables/DoubleArrayTopic.h>
|
|
|
|
|
#include <networktables/DoubleTopic.h>
|
|
|
|
|
#include <networktables/FloatArrayTopic.h>
|
|
|
|
|
#include <networktables/FloatTopic.h>
|
|
|
|
|
#include <networktables/IntegerArrayTopic.h>
|
|
|
|
|
#include <networktables/IntegerTopic.h>
|
|
|
|
|
#include <networktables/RawTopic.h>
|
|
|
|
|
#include <networktables/StringArrayTopic.h>
|
2018-07-20 00:03:45 -07:00
|
|
|
#include <ntcore_cpp.h>
|
2022-10-08 10:01:31 -07:00
|
|
|
#include <wpi/SmallVector.h>
|
2017-12-04 23:28:33 -08:00
|
|
|
|
2019-08-03 18:08:06 -04:00
|
|
|
#include "frc/smartdashboard/SmartDashboard.h"
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
template <typename Topic>
|
|
|
|
|
void SendableBuilderImpl::PropertyImpl<Topic>::Update(bool controllable,
|
|
|
|
|
int64_t time) {
|
|
|
|
|
if (controllable && sub && updateLocal) {
|
|
|
|
|
updateLocal(sub);
|
|
|
|
|
} else if (pub && updateNetwork) {
|
|
|
|
|
updateNetwork(pub, time);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
void SendableBuilderImpl::SetTable(std::shared_ptr<nt::NetworkTable> table) {
|
|
|
|
|
m_table = table;
|
2022-10-08 10:01:31 -07:00
|
|
|
m_controllablePublisher = table->GetBooleanTopic(".controllable").Publish();
|
|
|
|
|
m_controllablePublisher.SetDefault(false);
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<nt::NetworkTable> SendableBuilderImpl::GetTable() {
|
|
|
|
|
return m_table;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 16:38:05 -07:00
|
|
|
bool SendableBuilderImpl::IsPublished() const {
|
2020-12-28 12:58:06 -08:00
|
|
|
return m_table != nullptr;
|
|
|
|
|
}
|
2019-10-17 22:01:31 -07:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
bool SendableBuilderImpl::IsActuator() const {
|
|
|
|
|
return m_actuator;
|
|
|
|
|
}
|
2018-07-28 14:04:46 -07:00
|
|
|
|
2021-06-13 16:38:05 -07:00
|
|
|
void SendableBuilderImpl::Update() {
|
2017-12-04 23:28:33 -08:00
|
|
|
uint64_t time = nt::Now();
|
|
|
|
|
for (auto& property : m_properties) {
|
2022-10-08 10:01:31 -07:00
|
|
|
property->Update(m_controllable, time);
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
2020-01-10 14:49:55 -08:00
|
|
|
for (auto& updateTable : m_updateTables) {
|
|
|
|
|
updateTable();
|
|
|
|
|
}
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
|
2017-12-26 17:18:02 -06:00
|
|
|
void SendableBuilderImpl::StartListeners() {
|
2022-10-08 10:01:31 -07:00
|
|
|
m_controllable = true;
|
|
|
|
|
if (m_controllablePublisher) {
|
|
|
|
|
m_controllablePublisher.Set(true);
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2017-12-26 17:18:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::StopListeners() {
|
2022-10-08 10:01:31 -07:00
|
|
|
m_controllable = false;
|
|
|
|
|
if (m_controllablePublisher) {
|
|
|
|
|
m_controllablePublisher.Set(false);
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2017-12-26 17:18:02 -06:00
|
|
|
}
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
void SendableBuilderImpl::StartLiveWindowMode() {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (m_safeState) {
|
|
|
|
|
m_safeState();
|
|
|
|
|
}
|
2017-12-26 17:18:02 -06:00
|
|
|
StartListeners();
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::StopLiveWindowMode() {
|
2017-12-26 17:18:02 -06:00
|
|
|
StopListeners();
|
2020-12-28 12:58:06 -08:00
|
|
|
if (m_safeState) {
|
|
|
|
|
m_safeState();
|
|
|
|
|
}
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void SendableBuilderImpl::ClearProperties() {
|
|
|
|
|
m_properties.clear();
|
|
|
|
|
}
|
2019-10-17 22:01:31 -07:00
|
|
|
|
2021-05-26 17:44:18 -07:00
|
|
|
void SendableBuilderImpl::SetSmartDashboardType(std::string_view type) {
|
2022-10-08 10:01:31 -07:00
|
|
|
if (!m_typePublisher) {
|
|
|
|
|
m_typePublisher = m_table->GetStringTopic(".type").Publish();
|
|
|
|
|
}
|
|
|
|
|
m_typePublisher.Set(type);
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
|
2018-07-28 14:04:46 -07:00
|
|
|
void SendableBuilderImpl::SetActuator(bool value) {
|
2022-10-08 10:01:31 -07:00
|
|
|
if (!m_actuatorPublisher) {
|
|
|
|
|
m_actuatorPublisher = m_table->GetBooleanTopic(".actuator").Publish();
|
|
|
|
|
}
|
|
|
|
|
m_actuatorPublisher.Set(value);
|
2018-07-28 14:04:46 -07:00
|
|
|
m_actuator = value;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
void SendableBuilderImpl::SetSafeState(std::function<void()> func) {
|
|
|
|
|
m_safeState = func;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
void SendableBuilderImpl::SetUpdateTable(wpi::unique_function<void()> func) {
|
2020-01-10 14:49:55 -08:00
|
|
|
m_updateTables.emplace_back(std::move(func));
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
nt::Topic SendableBuilderImpl::GetTopic(std::string_view key) {
|
|
|
|
|
return m_table->GetTopic(key);
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
template <typename Topic, typename Getter, typename Setter>
|
|
|
|
|
void SendableBuilderImpl::AddPropertyImpl(Topic topic, Getter getter,
|
|
|
|
|
Setter setter) {
|
|
|
|
|
auto prop = std::make_unique<PropertyImpl<Topic>>();
|
2017-12-04 23:28:33 -08:00
|
|
|
if (getter) {
|
2022-10-08 10:01:31 -07:00
|
|
|
prop->pub = topic.Publish();
|
|
|
|
|
prop->updateNetwork = [=](auto& pub, int64_t time) {
|
|
|
|
|
pub.Set(getter(), time);
|
2017-12-04 23:28:33 -08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
if (setter) {
|
2022-10-08 10:01:31 -07:00
|
|
|
prop->sub = topic.Subscribe({});
|
|
|
|
|
prop->updateLocal = [=](auto& sub) {
|
|
|
|
|
for (auto&& val : sub.ReadQueue()) {
|
|
|
|
|
setter(val.value);
|
|
|
|
|
}
|
2017-12-04 23:28:33 -08:00
|
|
|
};
|
|
|
|
|
}
|
2022-10-08 10:01:31 -07:00
|
|
|
m_properties.emplace_back(std::move(prop));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::AddBooleanProperty(std::string_view key,
|
|
|
|
|
std::function<bool()> getter,
|
|
|
|
|
std::function<void(bool)> setter) {
|
|
|
|
|
AddPropertyImpl(m_table->GetBooleanTopic(key), std::move(getter),
|
|
|
|
|
std::move(setter));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::AddIntegerProperty(
|
|
|
|
|
std::string_view key, std::function<int64_t()> getter,
|
|
|
|
|
std::function<void(int64_t)> setter) {
|
|
|
|
|
AddPropertyImpl(m_table->GetIntegerTopic(key), std::move(getter),
|
|
|
|
|
std::move(setter));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::AddFloatProperty(std::string_view key,
|
|
|
|
|
std::function<float()> getter,
|
|
|
|
|
std::function<void(float)> setter) {
|
|
|
|
|
AddPropertyImpl(m_table->GetFloatTopic(key), std::move(getter),
|
|
|
|
|
std::move(setter));
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::AddDoubleProperty(
|
2021-05-26 17:44:18 -07:00
|
|
|
std::string_view key, std::function<double()> getter,
|
2017-12-04 23:28:33 -08:00
|
|
|
std::function<void(double)> setter) {
|
2022-10-08 10:01:31 -07:00
|
|
|
AddPropertyImpl(m_table->GetDoubleTopic(key), std::move(getter),
|
|
|
|
|
std::move(setter));
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::AddStringProperty(
|
2021-05-26 17:44:18 -07:00
|
|
|
std::string_view key, std::function<std::string()> getter,
|
2021-06-06 16:13:58 -07:00
|
|
|
std::function<void(std::string_view)> setter) {
|
2022-10-08 10:01:31 -07:00
|
|
|
AddPropertyImpl(m_table->GetStringTopic(key), std::move(getter),
|
|
|
|
|
std::move(setter));
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::AddBooleanArrayProperty(
|
2021-05-26 17:44:18 -07:00
|
|
|
std::string_view key, std::function<std::vector<int>()> getter,
|
2022-10-15 16:33:14 -07:00
|
|
|
std::function<void(std::span<const int>)> setter) {
|
2022-10-08 10:01:31 -07:00
|
|
|
AddPropertyImpl(m_table->GetBooleanArrayTopic(key), std::move(getter),
|
|
|
|
|
std::move(setter));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::AddIntegerArrayProperty(
|
|
|
|
|
std::string_view key, std::function<std::vector<int64_t>()> getter,
|
2022-10-15 16:33:14 -07:00
|
|
|
std::function<void(std::span<const int64_t>)> setter) {
|
2022-10-08 10:01:31 -07:00
|
|
|
AddPropertyImpl(m_table->GetIntegerArrayTopic(key), std::move(getter),
|
|
|
|
|
std::move(setter));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::AddFloatArrayProperty(
|
|
|
|
|
std::string_view key, std::function<std::vector<float>()> getter,
|
2022-10-15 16:33:14 -07:00
|
|
|
std::function<void(std::span<const float>)> setter) {
|
2022-10-08 10:01:31 -07:00
|
|
|
AddPropertyImpl(m_table->GetFloatArrayTopic(key), std::move(getter),
|
|
|
|
|
std::move(setter));
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::AddDoubleArrayProperty(
|
2021-05-26 17:44:18 -07:00
|
|
|
std::string_view key, std::function<std::vector<double>()> getter,
|
2022-10-15 16:33:14 -07:00
|
|
|
std::function<void(std::span<const double>)> setter) {
|
2022-10-08 10:01:31 -07:00
|
|
|
AddPropertyImpl(m_table->GetDoubleArrayTopic(key), std::move(getter),
|
|
|
|
|
std::move(setter));
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::AddStringArrayProperty(
|
2021-05-26 17:44:18 -07:00
|
|
|
std::string_view key, std::function<std::vector<std::string>()> getter,
|
2022-10-15 16:33:14 -07:00
|
|
|
std::function<void(std::span<const std::string>)> setter) {
|
2022-10-08 10:01:31 -07:00
|
|
|
AddPropertyImpl(m_table->GetStringArrayTopic(key), std::move(getter),
|
|
|
|
|
std::move(setter));
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::AddRawProperty(
|
2022-10-08 10:01:31 -07:00
|
|
|
std::string_view key, std::string_view typeString,
|
|
|
|
|
std::function<std::vector<uint8_t>()> getter,
|
2022-10-15 16:33:14 -07:00
|
|
|
std::function<void(std::span<const uint8_t>)> setter) {
|
2022-10-08 10:01:31 -07:00
|
|
|
auto topic = m_table->GetRawTopic(key);
|
|
|
|
|
auto prop = std::make_unique<PropertyImpl<nt::RawTopic>>();
|
2017-12-04 23:28:33 -08:00
|
|
|
if (getter) {
|
2022-10-08 10:01:31 -07:00
|
|
|
prop->pub = topic.Publish(typeString);
|
|
|
|
|
prop->updateNetwork = [=](auto& pub, int64_t time) {
|
|
|
|
|
pub.Set(getter(), time);
|
2017-12-04 23:28:33 -08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
if (setter) {
|
2022-10-08 10:01:31 -07:00
|
|
|
prop->sub = topic.Subscribe(typeString, {});
|
|
|
|
|
prop->updateLocal = [=](auto& sub) {
|
|
|
|
|
for (auto&& val : sub.ReadQueue()) {
|
|
|
|
|
setter(val.value);
|
|
|
|
|
}
|
2017-12-04 23:28:33 -08:00
|
|
|
};
|
|
|
|
|
}
|
2022-10-08 10:01:31 -07:00
|
|
|
m_properties.emplace_back(std::move(prop));
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
template <typename T, size_t Size, typename Topic, typename Getter,
|
|
|
|
|
typename Setter>
|
|
|
|
|
void SendableBuilderImpl::AddSmallPropertyImpl(Topic topic, Getter getter,
|
|
|
|
|
Setter setter) {
|
|
|
|
|
auto prop = std::make_unique<PropertyImpl<Topic>>();
|
2017-12-04 23:28:33 -08:00
|
|
|
if (getter) {
|
2022-10-08 10:01:31 -07:00
|
|
|
prop->pub = topic.Publish();
|
|
|
|
|
prop->updateNetwork = [=](auto& pub, int64_t time) {
|
|
|
|
|
wpi::SmallVector<T, Size> buf;
|
|
|
|
|
pub.Set(getter(buf), time);
|
2017-12-04 23:28:33 -08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
if (setter) {
|
2022-10-08 10:01:31 -07:00
|
|
|
prop->sub = topic.Subscribe({});
|
|
|
|
|
prop->updateLocal = [=](auto& sub) {
|
|
|
|
|
for (auto&& val : sub.ReadQueue()) {
|
|
|
|
|
setter(val.value);
|
|
|
|
|
}
|
2017-12-04 23:28:33 -08:00
|
|
|
};
|
|
|
|
|
}
|
2022-10-08 10:01:31 -07:00
|
|
|
m_properties.emplace_back(std::move(prop));
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::AddSmallStringProperty(
|
2021-05-26 17:44:18 -07:00
|
|
|
std::string_view key,
|
2021-06-06 16:13:58 -07:00
|
|
|
std::function<std::string_view(wpi::SmallVectorImpl<char>& buf)> getter,
|
|
|
|
|
std::function<void(std::string_view)> setter) {
|
2022-10-08 10:01:31 -07:00
|
|
|
AddSmallPropertyImpl<char, 128>(m_table->GetStringTopic(key),
|
|
|
|
|
std::move(getter), std::move(setter));
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::AddSmallBooleanArrayProperty(
|
2021-05-26 17:44:18 -07:00
|
|
|
std::string_view key,
|
2022-10-15 16:33:14 -07:00
|
|
|
std::function<std::span<const int>(wpi::SmallVectorImpl<int>& buf)> getter,
|
|
|
|
|
std::function<void(std::span<const int>)> setter) {
|
2022-10-08 10:01:31 -07:00
|
|
|
AddSmallPropertyImpl<int, 16>(m_table->GetBooleanArrayTopic(key),
|
|
|
|
|
std::move(getter), std::move(setter));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::AddSmallIntegerArrayProperty(
|
|
|
|
|
std::string_view key,
|
2022-10-15 16:33:14 -07:00
|
|
|
std::function<std::span<const int64_t>(wpi::SmallVectorImpl<int64_t>& buf)>
|
2022-10-08 10:01:31 -07:00
|
|
|
getter,
|
2022-10-15 16:33:14 -07:00
|
|
|
std::function<void(std::span<const int64_t>)> setter) {
|
2022-10-08 10:01:31 -07:00
|
|
|
AddSmallPropertyImpl<int64_t, 16>(m_table->GetIntegerArrayTopic(key),
|
|
|
|
|
std::move(getter), std::move(setter));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::AddSmallFloatArrayProperty(
|
|
|
|
|
std::string_view key,
|
2022-10-15 16:33:14 -07:00
|
|
|
std::function<std::span<const float>(wpi::SmallVectorImpl<float>& buf)>
|
2022-10-08 10:01:31 -07:00
|
|
|
getter,
|
2022-10-15 16:33:14 -07:00
|
|
|
std::function<void(std::span<const float>)> setter) {
|
2022-10-08 10:01:31 -07:00
|
|
|
AddSmallPropertyImpl<float, 16>(m_table->GetFloatArrayTopic(key),
|
|
|
|
|
std::move(getter), std::move(setter));
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::AddSmallDoubleArrayProperty(
|
2021-05-26 17:44:18 -07:00
|
|
|
std::string_view key,
|
2022-10-15 16:33:14 -07:00
|
|
|
std::function<std::span<const double>(wpi::SmallVectorImpl<double>& buf)>
|
2017-12-04 23:28:33 -08:00
|
|
|
getter,
|
2022-10-15 16:33:14 -07:00
|
|
|
std::function<void(std::span<const double>)> setter) {
|
2022-10-08 10:01:31 -07:00
|
|
|
AddSmallPropertyImpl<double, 16>(m_table->GetDoubleArrayTopic(key),
|
|
|
|
|
std::move(getter), std::move(setter));
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::AddSmallStringArrayProperty(
|
2021-05-26 17:44:18 -07:00
|
|
|
std::string_view key,
|
2017-12-04 23:28:33 -08:00
|
|
|
std::function<
|
2022-10-15 16:33:14 -07:00
|
|
|
std::span<const std::string>(wpi::SmallVectorImpl<std::string>& buf)>
|
2017-12-04 23:28:33 -08:00
|
|
|
getter,
|
2022-10-15 16:33:14 -07:00
|
|
|
std::function<void(std::span<const std::string>)> setter) {
|
2022-10-08 10:01:31 -07:00
|
|
|
AddSmallPropertyImpl<std::string, 16>(m_table->GetStringArrayTopic(key),
|
|
|
|
|
std::move(getter), std::move(setter));
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SendableBuilderImpl::AddSmallRawProperty(
|
2022-10-08 10:01:31 -07:00
|
|
|
std::string_view key, std::string_view typeString,
|
2022-10-15 16:33:14 -07:00
|
|
|
std::function<std::span<uint8_t>(wpi::SmallVectorImpl<uint8_t>& buf)>
|
2022-10-08 10:01:31 -07:00
|
|
|
getter,
|
2022-10-15 16:33:14 -07:00
|
|
|
std::function<void(std::span<const uint8_t>)> setter) {
|
2022-10-08 10:01:31 -07:00
|
|
|
auto topic = m_table->GetRawTopic(key);
|
|
|
|
|
auto prop = std::make_unique<PropertyImpl<nt::RawTopic>>();
|
2017-12-04 23:28:33 -08:00
|
|
|
if (getter) {
|
2022-10-08 10:01:31 -07:00
|
|
|
prop->pub = topic.Publish(typeString);
|
|
|
|
|
prop->updateNetwork = [=](auto& pub, int64_t time) {
|
|
|
|
|
wpi::SmallVector<uint8_t, 128> buf;
|
|
|
|
|
pub.Set(getter(buf), time);
|
2017-12-04 23:28:33 -08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
if (setter) {
|
2022-10-08 10:01:31 -07:00
|
|
|
prop->sub = topic.Subscribe(typeString, {});
|
|
|
|
|
prop->updateLocal = [=](auto& sub) {
|
|
|
|
|
for (auto&& val : sub.ReadQueue()) {
|
|
|
|
|
setter(val.value);
|
|
|
|
|
}
|
2017-12-04 23:28:33 -08:00
|
|
|
};
|
|
|
|
|
}
|
2022-10-08 10:01:31 -07:00
|
|
|
m_properties.emplace_back(std::move(prop));
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|