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"
|
|
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
|
2021-06-13 16:38:05 -07:00
|
|
|
#include <networktables/NTSendableBuilder.h>
|
2021-04-30 23:43:59 +03:00
|
|
|
|
|
|
|
|
using namespace frc;
|
|
|
|
|
|
|
|
|
|
static constexpr char kBackgroundColor[] = "backgroundColor";
|
|
|
|
|
static constexpr char kDims[] = "dims";
|
|
|
|
|
|
|
|
|
|
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) {
|
2021-04-30 23:43:59 +03:00
|
|
|
auto& obj = m_roots[name];
|
|
|
|
|
if (obj) {
|
|
|
|
|
return obj.get();
|
|
|
|
|
}
|
|
|
|
|
obj = std::make_unique<MechanismRoot2d>(name, x, y,
|
|
|
|
|
MechanismRoot2d::private_init{});
|
|
|
|
|
if (m_table) {
|
|
|
|
|
obj->Update(m_table->GetSubTable(name));
|
|
|
|
|
}
|
|
|
|
|
return obj.get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Mechanism2d::SetBackgroundColor(const Color8Bit& color) {
|
|
|
|
|
std::snprintf(m_color, sizeof(m_color), "#%02X%02X%02X", color.red,
|
|
|
|
|
color.green, color.blue);
|
|
|
|
|
if (m_table) {
|
|
|
|
|
m_table->GetEntry(kBackgroundColor).SetString(m_color);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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");
|
|
|
|
|
m_table = builder.GetTable();
|
|
|
|
|
|
|
|
|
|
m_table->GetEntry(kDims).SetDoubleArray({m_width, m_height});
|
|
|
|
|
m_table->GetEntry(kBackgroundColor).SetString(m_color);
|
|
|
|
|
|
|
|
|
|
std::scoped_lock lock(m_mutex);
|
|
|
|
|
for (const auto& entry : m_roots) {
|
|
|
|
|
const auto& root = entry.getValue().get();
|
|
|
|
|
root->Update(m_table->GetSubTable(entry.getKey()));
|
|
|
|
|
}
|
|
|
|
|
}
|