Files
allwpilib/wpilibc/src/main/native/cpp/smartdashboard/SendableBuilderImpl.cpp

335 lines
11 KiB
C++
Raw Normal View History

// 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.
#include "frc/smartdashboard/SendableBuilderImpl.h"
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>
#include <ntcore_cpp.h>
2022-10-08 10:01:31 -07:00
#include <wpi/SmallVector.h>
#include "frc/smartdashboard/SmartDashboard.h"
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);
}
}
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);
}
std::shared_ptr<nt::NetworkTable> SendableBuilderImpl::GetTable() {
return m_table;
}
bool SendableBuilderImpl::IsPublished() const {
return m_table != nullptr;
}
bool SendableBuilderImpl::IsActuator() const {
return m_actuator;
}
void SendableBuilderImpl::Update() {
uint64_t time = nt::Now();
for (auto& property : m_properties) {
2022-10-08 10:01:31 -07:00
property->Update(m_controllable, time);
}
for (auto& updateTable : m_updateTables) {
updateTable();
}
}
void SendableBuilderImpl::StartListeners() {
2022-10-08 10:01:31 -07:00
m_controllable = true;
if (m_controllablePublisher) {
m_controllablePublisher.Set(true);
}
}
void SendableBuilderImpl::StopListeners() {
2022-10-08 10:01:31 -07:00
m_controllable = false;
if (m_controllablePublisher) {
m_controllablePublisher.Set(false);
}
}
void SendableBuilderImpl::StartLiveWindowMode() {
if (m_safeState) {
m_safeState();
}
StartListeners();
}
void SendableBuilderImpl::StopLiveWindowMode() {
StopListeners();
if (m_safeState) {
m_safeState();
}
}
void SendableBuilderImpl::ClearProperties() {
m_properties.clear();
}
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);
}
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);
m_actuator = value;
}
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) {
m_updateTables.emplace_back(std::move(func));
}
2022-10-08 10:01:31 -07:00
nt::Topic SendableBuilderImpl::GetTopic(std::string_view key) {
return m_table->GetTopic(key);
}
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>>();
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);
};
}
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);
}
};
}
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));
}
void SendableBuilderImpl::AddDoubleProperty(
std::string_view key, std::function<double()> getter,
std::function<void(double)> setter) {
2022-10-08 10:01:31 -07:00
AddPropertyImpl(m_table->GetDoubleTopic(key), std::move(getter),
std::move(setter));
}
void SendableBuilderImpl::AddStringProperty(
std::string_view key, std::function<std::string()> getter,
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));
}
void SendableBuilderImpl::AddBooleanArrayProperty(
std::string_view key, std::function<std::vector<int>()> getter,
std::function<void(wpi::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,
std::function<void(wpi::span<const int64_t>)> setter) {
AddPropertyImpl(m_table->GetIntegerArrayTopic(key), std::move(getter),
std::move(setter));
}
void SendableBuilderImpl::AddFloatArrayProperty(
std::string_view key, std::function<std::vector<float>()> getter,
std::function<void(wpi::span<const float>)> setter) {
AddPropertyImpl(m_table->GetFloatArrayTopic(key), std::move(getter),
std::move(setter));
}
void SendableBuilderImpl::AddDoubleArrayProperty(
std::string_view key, std::function<std::vector<double>()> getter,
std::function<void(wpi::span<const double>)> setter) {
2022-10-08 10:01:31 -07:00
AddPropertyImpl(m_table->GetDoubleArrayTopic(key), std::move(getter),
std::move(setter));
}
void SendableBuilderImpl::AddStringArrayProperty(
std::string_view key, std::function<std::vector<std::string>()> getter,
std::function<void(wpi::span<const std::string>)> setter) {
2022-10-08 10:01:31 -07:00
AddPropertyImpl(m_table->GetStringArrayTopic(key), std::move(getter),
std::move(setter));
}
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,
std::function<void(wpi::span<const uint8_t>)> setter) {
auto topic = m_table->GetRawTopic(key);
auto prop = std::make_unique<PropertyImpl<nt::RawTopic>>();
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);
};
}
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);
}
};
}
2022-10-08 10:01:31 -07:00
m_properties.emplace_back(std::move(prop));
}
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>>();
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);
};
}
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);
}
};
}
2022-10-08 10:01:31 -07:00
m_properties.emplace_back(std::move(prop));
}
void SendableBuilderImpl::AddSmallStringProperty(
std::string_view key,
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));
}
void SendableBuilderImpl::AddSmallBooleanArrayProperty(
std::string_view key,
std::function<wpi::span<const int>(wpi::SmallVectorImpl<int>& buf)> getter,
std::function<void(wpi::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,
std::function<wpi::span<const int64_t>(wpi::SmallVectorImpl<int64_t>& buf)>
getter,
std::function<void(wpi::span<const int64_t>)> setter) {
AddSmallPropertyImpl<int64_t, 16>(m_table->GetIntegerArrayTopic(key),
std::move(getter), std::move(setter));
}
void SendableBuilderImpl::AddSmallFloatArrayProperty(
std::string_view key,
std::function<wpi::span<const float>(wpi::SmallVectorImpl<float>& buf)>
getter,
std::function<void(wpi::span<const float>)> setter) {
AddSmallPropertyImpl<float, 16>(m_table->GetFloatArrayTopic(key),
std::move(getter), std::move(setter));
}
void SendableBuilderImpl::AddSmallDoubleArrayProperty(
std::string_view key,
std::function<wpi::span<const double>(wpi::SmallVectorImpl<double>& buf)>
getter,
std::function<void(wpi::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));
}
void SendableBuilderImpl::AddSmallStringArrayProperty(
std::string_view key,
std::function<
wpi::span<const std::string>(wpi::SmallVectorImpl<std::string>& buf)>
getter,
std::function<void(wpi::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));
}
void SendableBuilderImpl::AddSmallRawProperty(
2022-10-08 10:01:31 -07:00
std::string_view key, std::string_view typeString,
std::function<wpi::span<uint8_t>(wpi::SmallVectorImpl<uint8_t>& buf)>
getter,
std::function<void(wpi::span<const uint8_t>)> setter) {
auto topic = m_table->GetRawTopic(key);
auto prop = std::make_unique<PropertyImpl<nt::RawTopic>>();
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);
};
}
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);
}
};
}
2022-10-08 10:01:31 -07:00
m_properties.emplace_back(std::move(prop));
}