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
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
|
#include <memory>
|
2022-10-15 16:33:14 -07:00
|
|
|
#include <span>
|
2017-12-04 23:28:33 -08:00
|
|
|
#include <string>
|
2021-05-26 17:44:18 -07:00
|
|
|
#include <string_view>
|
2017-12-04 23:28:33 -08:00
|
|
|
#include <utility>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
#include <networktables/BooleanTopic.h>
|
2021-06-13 16:38:05 -07:00
|
|
|
#include <networktables/NTSendableBuilder.h>
|
2017-12-07 23:34:29 -08:00
|
|
|
#include <networktables/NetworkTable.h>
|
2022-10-08 10:01:31 -07:00
|
|
|
#include <networktables/StringTopic.h>
|
|
|
|
|
#include <wpi/FunctionExtras.h>
|
2018-04-29 23:33:19 -07:00
|
|
|
#include <wpi/SmallVector.h>
|
2017-12-04 23:28:33 -08:00
|
|
|
|
|
|
|
|
namespace frc {
|
|
|
|
|
|
2021-06-13 16:38:05 -07:00
|
|
|
class SendableBuilderImpl : public nt::NTSendableBuilder {
|
2017-12-04 23:28:33 -08:00
|
|
|
public:
|
|
|
|
|
SendableBuilderImpl() = default;
|
|
|
|
|
~SendableBuilderImpl() override = default;
|
|
|
|
|
|
2018-09-24 00:08:25 -07:00
|
|
|
SendableBuilderImpl(SendableBuilderImpl&&) = default;
|
|
|
|
|
SendableBuilderImpl& operator=(SendableBuilderImpl&&) = default;
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
/**
|
|
|
|
|
* Set the network table. Must be called prior to any Add* functions being
|
|
|
|
|
* called.
|
|
|
|
|
* @param table Network table
|
|
|
|
|
*/
|
|
|
|
|
void SetTable(std::shared_ptr<nt::NetworkTable> table);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the network table.
|
|
|
|
|
* @return The network table
|
|
|
|
|
*/
|
2020-10-26 00:15:21 -07:00
|
|
|
std::shared_ptr<nt::NetworkTable> GetTable() override;
|
2017-12-04 23:28:33 -08:00
|
|
|
|
2019-10-17 22:01:31 -07:00
|
|
|
/**
|
|
|
|
|
* Return whether this sendable has an associated table.
|
|
|
|
|
* @return True if it has a table, false if not.
|
|
|
|
|
*/
|
2021-06-13 16:38:05 -07:00
|
|
|
bool IsPublished() const override;
|
2019-10-17 22:01:31 -07:00
|
|
|
|
2018-07-28 14:04:46 -07:00
|
|
|
/**
|
|
|
|
|
* Return whether this sendable should be treated as an actuator.
|
|
|
|
|
* @return True if actuator, false if not.
|
|
|
|
|
*/
|
|
|
|
|
bool IsActuator() const;
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
/**
|
2022-10-08 10:01:31 -07:00
|
|
|
* Synchronize with network table values by calling the getters for all
|
|
|
|
|
* properties and setters when the network table value has changed.
|
2017-12-04 23:28:33 -08:00
|
|
|
*/
|
2021-06-13 16:38:05 -07:00
|
|
|
void Update() override;
|
2017-12-04 23:28:33 -08:00
|
|
|
|
|
|
|
|
/**
|
2017-12-26 17:18:02 -06:00
|
|
|
* Hook setters for all properties.
|
|
|
|
|
*/
|
|
|
|
|
void StartListeners();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Unhook setters for all properties.
|
|
|
|
|
*/
|
|
|
|
|
void StopListeners();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Start LiveWindow mode by hooking the setters for all properties. Also
|
|
|
|
|
* calls the SafeState function if one was provided.
|
2017-12-04 23:28:33 -08:00
|
|
|
*/
|
|
|
|
|
void StartLiveWindowMode();
|
|
|
|
|
|
|
|
|
|
/**
|
2017-12-26 17:18:02 -06:00
|
|
|
* Stop LiveWindow mode by unhooking the setters for all properties. Also
|
|
|
|
|
* calls the SafeState function if one was provided.
|
2017-12-04 23:28:33 -08:00
|
|
|
*/
|
|
|
|
|
void StopLiveWindowMode();
|
|
|
|
|
|
2019-10-17 22:01:31 -07:00
|
|
|
/**
|
|
|
|
|
* Clear properties.
|
|
|
|
|
*/
|
2021-06-13 16:38:05 -07:00
|
|
|
void ClearProperties() override;
|
2019-10-17 22:01:31 -07:00
|
|
|
|
2021-05-26 17:44:18 -07:00
|
|
|
void SetSmartDashboardType(std::string_view type) override;
|
2018-07-28 14:04:46 -07:00
|
|
|
void SetActuator(bool value) override;
|
2017-12-04 23:28:33 -08:00
|
|
|
void SetSafeState(std::function<void()> func) override;
|
2022-10-08 10:01:31 -07:00
|
|
|
void SetUpdateTable(wpi::unique_function<void()> func) override;
|
|
|
|
|
nt::Topic GetTopic(std::string_view key) override;
|
2017-12-04 23:28:33 -08:00
|
|
|
|
2021-05-26 17:44:18 -07:00
|
|
|
void AddBooleanProperty(std::string_view key, std::function<bool()> getter,
|
2017-12-04 23:28:33 -08:00
|
|
|
std::function<void(bool)> setter) override;
|
|
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
void AddIntegerProperty(std::string_view key, std::function<int64_t()> getter,
|
|
|
|
|
std::function<void(int64_t)> setter) override;
|
|
|
|
|
|
|
|
|
|
void AddFloatProperty(std::string_view key, std::function<float()> getter,
|
|
|
|
|
std::function<void(float)> setter) override;
|
|
|
|
|
|
2021-05-26 17:44:18 -07:00
|
|
|
void AddDoubleProperty(std::string_view key, std::function<double()> getter,
|
2017-12-04 23:28:33 -08:00
|
|
|
std::function<void(double)> setter) override;
|
|
|
|
|
|
2021-05-26 17:44:18 -07:00
|
|
|
void AddStringProperty(std::string_view key,
|
2017-12-04 23:28:33 -08:00
|
|
|
std::function<std::string()> getter,
|
2021-06-06 16:13:58 -07:00
|
|
|
std::function<void(std::string_view)> setter) override;
|
2017-12-04 23:28:33 -08:00
|
|
|
|
|
|
|
|
void 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) override;
|
2017-12-04 23:28:33 -08:00
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
void 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) override;
|
2022-10-08 10:01:31 -07:00
|
|
|
|
|
|
|
|
void 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) override;
|
2022-10-08 10:01:31 -07:00
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
void 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) override;
|
2017-12-04 23:28:33 -08:00
|
|
|
|
|
|
|
|
void 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) override;
|
2017-12-04 23:28:33 -08:00
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
void AddRawProperty(
|
|
|
|
|
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) override;
|
2017-12-04 23:28:33 -08:00
|
|
|
|
|
|
|
|
void 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) override;
|
2017-12-04 23:28:33 -08:00
|
|
|
|
|
|
|
|
void 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)>
|
2021-06-06 19:51:14 -07:00
|
|
|
getter,
|
2022-10-15 16:33:14 -07:00
|
|
|
std::function<void(std::span<const int>)> setter) override;
|
2017-12-04 23:28:33 -08:00
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
void AddSmallIntegerArrayProperty(
|
|
|
|
|
std::string_view key,
|
|
|
|
|
std::function<
|
2022-10-15 16:33:14 -07:00
|
|
|
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) override;
|
2022-10-08 10:01:31 -07:00
|
|
|
|
|
|
|
|
void 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) override;
|
2022-10-08 10:01:31 -07:00
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
void 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) override;
|
2017-12-04 23:28:33 -08:00
|
|
|
|
|
|
|
|
void 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) override;
|
2017-12-04 23:28:33 -08:00
|
|
|
|
|
|
|
|
void 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) override;
|
2017-12-04 23:28:33 -08:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
struct Property {
|
2022-10-08 10:01:31 -07:00
|
|
|
virtual ~Property() = default;
|
|
|
|
|
virtual void Update(bool controllable, int64_t time) = 0;
|
2017-12-04 23:28:33 -08:00
|
|
|
};
|
|
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
template <typename Topic>
|
|
|
|
|
struct PropertyImpl : public Property {
|
|
|
|
|
void Update(bool controllable, int64_t time) override;
|
|
|
|
|
|
|
|
|
|
using Publisher = typename Topic::PublisherType;
|
|
|
|
|
using Subscriber = typename Topic::SubscriberType;
|
|
|
|
|
Publisher pub;
|
|
|
|
|
Subscriber sub;
|
|
|
|
|
std::function<void(Publisher& pub, int64_t time)> updateNetwork;
|
|
|
|
|
std::function<void(Subscriber& sub)> updateLocal;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename Topic, typename Getter, typename Setter>
|
|
|
|
|
void AddPropertyImpl(Topic topic, Getter getter, Setter setter);
|
|
|
|
|
|
|
|
|
|
template <typename T, size_t Size, typename Topic, typename Getter,
|
|
|
|
|
typename Setter>
|
|
|
|
|
void AddSmallPropertyImpl(Topic topic, Getter getter, Setter setter);
|
|
|
|
|
|
|
|
|
|
std::vector<std::unique_ptr<Property>> m_properties;
|
2017-12-04 23:28:33 -08:00
|
|
|
std::function<void()> m_safeState;
|
2022-10-08 10:01:31 -07:00
|
|
|
std::vector<wpi::unique_function<void()>> m_updateTables;
|
2017-12-04 23:28:33 -08:00
|
|
|
std::shared_ptr<nt::NetworkTable> m_table;
|
2022-10-08 10:01:31 -07:00
|
|
|
bool m_controllable = false;
|
2018-07-28 14:04:46 -07:00
|
|
|
bool m_actuator = false;
|
2022-10-08 10:01:31 -07:00
|
|
|
|
|
|
|
|
nt::BooleanPublisher m_controllablePublisher;
|
|
|
|
|
nt::StringPublisher m_typePublisher;
|
|
|
|
|
nt::BooleanPublisher m_actuatorPublisher;
|
2017-12-04 23:28:33 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace frc
|