2021-04-30 23:43:59 +03: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.
|
|
|
|
|
|
|
|
|
|
#include "frc/smartdashboard/Mechanism2d.h"
|
|
|
|
|
|
2024-09-20 17:43:39 -07:00
|
|
|
#include <memory>
|
2022-10-08 10:01:31 -07:00
|
|
|
#include <string_view>
|
2021-04-30 23:43:59 +03:00
|
|
|
|
2021-06-13 16:38:05 -07:00
|
|
|
#include <networktables/NTSendableBuilder.h>
|
2021-04-30 23:43:59 +03:00
|
|
|
|
|
|
|
|
using namespace frc;
|
|
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
static constexpr std::string_view kBackgroundColor = "backgroundColor";
|
|
|
|
|
static constexpr std::string_view kDims = "dims";
|
2021-04-30 23:43:59 +03:00
|
|
|
|
|
|
|
|
Mechanism2d::Mechanism2d(double width, double height,
|
|
|
|
|
const Color8Bit& backgroundColor)
|
|
|
|
|
: m_width{width}, m_height{height} {
|
|
|
|
|
SetBackgroundColor(backgroundColor);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
MechanismRoot2d* Mechanism2d::GetRoot(std::string_view name, double x,
|
|
|
|
|
double y) {
|
2024-10-25 14:15:02 -07:00
|
|
|
auto [it, isNew] =
|
|
|
|
|
m_roots.try_emplace(name, name, x, y, MechanismRoot2d::private_init{});
|
|
|
|
|
if (isNew && m_table) {
|
|
|
|
|
it->second.Update(m_table->GetSubTable(name));
|
2021-04-30 23:43:59 +03:00
|
|
|
}
|
2024-10-25 14:15:02 -07:00
|
|
|
return &it->second;
|
2021-04-30 23:43:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Mechanism2d::SetBackgroundColor(const Color8Bit& color) {
|
2022-10-04 22:36:51 +03:00
|
|
|
m_color = color.HexString();
|
2022-10-08 10:01:31 -07:00
|
|
|
if (m_colorPub) {
|
|
|
|
|
m_colorPub.Set(m_color);
|
2021-04-30 23:43:59 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 16:38:05 -07:00
|
|
|
void Mechanism2d::InitSendable(nt::NTSendableBuilder& builder) {
|
2021-04-30 23:43:59 +03:00
|
|
|
builder.SetSmartDashboardType("Mechanism2d");
|
|
|
|
|
|
2021-09-24 16:04:02 -07:00
|
|
|
std::scoped_lock lock(m_mutex);
|
|
|
|
|
m_table = builder.GetTable();
|
2022-10-08 10:01:31 -07:00
|
|
|
m_dimsPub = m_table->GetDoubleArrayTopic(kDims).Publish();
|
|
|
|
|
m_dimsPub.Set({{m_width, m_height}});
|
|
|
|
|
m_colorPub = m_table->GetStringTopic(kBackgroundColor).Publish();
|
|
|
|
|
m_colorPub.Set(m_color);
|
2024-10-25 14:15:02 -07:00
|
|
|
for (auto& entry : m_roots) {
|
|
|
|
|
entry.second.Update(m_table->GetSubTable(entry.first));
|
2021-04-30 23:43:59 +03:00
|
|
|
}
|
|
|
|
|
}
|