2020-12-26 14:31:24 -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.
|
2020-09-12 10:55:46 -07:00
|
|
|
|
|
|
|
|
#include "glass/hardware/Encoder.h"
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
#include <fmt/format.h>
|
2020-09-12 10:55:46 -07:00
|
|
|
#include <imgui.h>
|
|
|
|
|
|
|
|
|
|
#include "glass/Context.h"
|
|
|
|
|
#include "glass/DataSource.h"
|
|
|
|
|
|
|
|
|
|
using namespace glass;
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
void EncoderModel::SetName(std::string_view name) {
|
|
|
|
|
if (name.empty()) {
|
2020-09-12 10:55:46 -07:00
|
|
|
if (auto distancePerPulse = GetDistancePerPulseData()) {
|
|
|
|
|
distancePerPulse->SetName("");
|
|
|
|
|
}
|
|
|
|
|
if (auto count = GetCountData()) {
|
|
|
|
|
count->SetName("");
|
|
|
|
|
}
|
|
|
|
|
if (auto period = GetPeriodData()) {
|
|
|
|
|
period->SetName("");
|
|
|
|
|
}
|
|
|
|
|
if (auto direction = GetDirectionData()) {
|
|
|
|
|
direction->SetName("");
|
|
|
|
|
}
|
|
|
|
|
if (auto distance = GetDistanceData()) {
|
|
|
|
|
distance->SetName("");
|
|
|
|
|
}
|
|
|
|
|
if (auto rate = GetRateData()) {
|
|
|
|
|
rate->SetName("");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (auto distancePerPulse = GetDistancePerPulseData()) {
|
2021-06-06 16:13:58 -07:00
|
|
|
distancePerPulse->SetName(fmt::format("{} Distance/Count", name));
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
if (auto count = GetCountData()) {
|
2021-06-06 16:13:58 -07:00
|
|
|
count->SetName(fmt::format("{} Count", name));
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
if (auto period = GetPeriodData()) {
|
2021-06-06 16:13:58 -07:00
|
|
|
period->SetName(fmt::format("{} Period", name));
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
if (auto direction = GetDirectionData()) {
|
2021-06-06 16:13:58 -07:00
|
|
|
direction->SetName(fmt::format("{} Direction", name));
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
if (auto distance = GetDistanceData()) {
|
2021-06-06 16:13:58 -07:00
|
|
|
distance->SetName(fmt::format("{} Distance", name));
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
if (auto rate = GetRateData()) {
|
2021-06-06 16:13:58 -07:00
|
|
|
rate->SetName(fmt::format("{} Rate", name));
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void glass::DisplayEncoder(EncoderModel* model) {
|
|
|
|
|
if (auto simDevice = model->GetSimDevice()) {
|
|
|
|
|
ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(96, 96, 96, 255));
|
|
|
|
|
ImGui::TextUnformatted(simDevice);
|
|
|
|
|
ImGui::PopStyleColor();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int chA = model->GetChannelA();
|
|
|
|
|
int chB = model->GetChannelB();
|
|
|
|
|
|
|
|
|
|
// build header label
|
|
|
|
|
std::string* name = GetStorage().GetStringRef("name");
|
|
|
|
|
char label[128];
|
|
|
|
|
if (!name->empty()) {
|
|
|
|
|
std::snprintf(label, sizeof(label), "%s [%d,%d]###name", name->c_str(), chA,
|
|
|
|
|
chB);
|
|
|
|
|
} else {
|
|
|
|
|
std::snprintf(label, sizeof(label), "Encoder[%d,%d]###name", chA, chB);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// header
|
|
|
|
|
bool open = CollapsingHeader(label);
|
|
|
|
|
|
|
|
|
|
// context menu to change name
|
|
|
|
|
if (PopupEditName("name", name)) {
|
|
|
|
|
model->SetName(name->c_str());
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!open) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
|
|
|
|
|
ImGui::PushItemWidth(ImGui::GetFontSize() * 8);
|
|
|
|
|
// distance per pulse
|
|
|
|
|
if (auto distancePerPulseData = model->GetDistancePerPulseData()) {
|
|
|
|
|
double value = distancePerPulseData->GetValue();
|
|
|
|
|
distancePerPulseData->LabelText("Dist/Count", "%.6f", value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// count
|
|
|
|
|
if (auto countData = model->GetCountData()) {
|
|
|
|
|
int value = countData->GetValue();
|
2020-12-28 12:58:06 -08:00
|
|
|
if (ImGui::InputInt("##input", &value)) {
|
|
|
|
|
model->SetCount(value);
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
ImGui::SameLine();
|
|
|
|
|
if (ImGui::Button("Reset")) {
|
|
|
|
|
model->SetCount(0);
|
|
|
|
|
}
|
|
|
|
|
ImGui::SameLine();
|
|
|
|
|
ImGui::Selectable("Count");
|
|
|
|
|
countData->EmitDrag();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// max period
|
|
|
|
|
{
|
|
|
|
|
double maxPeriod = model->GetMaxPeriod();
|
|
|
|
|
ImGui::LabelText("Max Period", "%.6f", maxPeriod);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// period
|
|
|
|
|
if (auto periodData = model->GetPeriodData()) {
|
|
|
|
|
double value = periodData->GetValue();
|
|
|
|
|
if (periodData->InputDouble("Period", &value, 0, 0, "%.6g")) {
|
|
|
|
|
model->SetPeriod(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// reverse direction
|
|
|
|
|
ImGui::LabelText("Reverse Direction", "%s",
|
|
|
|
|
model->GetReverseDirection() ? "true" : "false");
|
|
|
|
|
|
|
|
|
|
// direction
|
|
|
|
|
if (auto directionData = model->GetDirectionData()) {
|
|
|
|
|
static const char* options[] = {"reverse", "forward"};
|
|
|
|
|
int value = directionData->GetValue() ? 1 : 0;
|
|
|
|
|
if (directionData->Combo("Direction", &value, options, 2)) {
|
|
|
|
|
model->SetDirection(value != 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// distance
|
|
|
|
|
if (auto distanceData = model->GetDistanceData()) {
|
|
|
|
|
double value = distanceData->GetValue();
|
|
|
|
|
if (distanceData->InputDouble("Distance", &value, 0, 0, "%.6g")) {
|
|
|
|
|
model->SetDistance(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// rate
|
|
|
|
|
if (auto rateData = model->GetRateData()) {
|
|
|
|
|
double value = rateData->GetValue();
|
|
|
|
|
if (rateData->InputDouble("Rate", &value, 0, 0, "%.6g")) {
|
|
|
|
|
model->SetRate(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ImGui::PopItemWidth();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
void glass::DisplayEncoders(EncodersModel* model, std::string_view noneMsg) {
|
2020-09-12 10:55:46 -07:00
|
|
|
bool hasAny = false;
|
|
|
|
|
model->ForEachEncoder([&](EncoderModel& encoder, int i) {
|
|
|
|
|
hasAny = true;
|
|
|
|
|
PushID(i);
|
|
|
|
|
DisplayEncoder(&encoder);
|
|
|
|
|
PopID();
|
|
|
|
|
});
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!hasAny && !noneMsg.empty()) {
|
2021-06-06 16:13:58 -07:00
|
|
|
ImGui::TextUnformatted(noneMsg.data(), noneMsg.data() + noneMsg.size());
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|