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/DIO.h"
|
|
|
|
|
|
|
|
|
|
#include <imgui.h>
|
|
|
|
|
|
|
|
|
|
#include "glass/DataSource.h"
|
|
|
|
|
#include "glass/hardware/Encoder.h"
|
|
|
|
|
#include "glass/support/IniSaverInfo.h"
|
|
|
|
|
|
|
|
|
|
using namespace glass;
|
|
|
|
|
|
|
|
|
|
static void LabelSimDevice(const char* name, const char* simDeviceName) {
|
|
|
|
|
ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(96, 96, 96, 255));
|
|
|
|
|
ImGui::LabelText(name, "%s", simDeviceName);
|
|
|
|
|
ImGui::PopStyleColor();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DisplayDIOImpl(DIOModel* model, int index, bool outputsEnabled) {
|
|
|
|
|
auto dpwm = model->GetDPWM();
|
|
|
|
|
auto dutyCycle = model->GetDutyCycle();
|
|
|
|
|
auto encoder = model->GetEncoder();
|
|
|
|
|
|
|
|
|
|
auto dioData = model->GetValueData();
|
|
|
|
|
auto dpwmData = dpwm ? dpwm->GetValueData() : nullptr;
|
|
|
|
|
auto dutyCycleData = dutyCycle ? dutyCycle->GetValueData() : nullptr;
|
|
|
|
|
|
|
|
|
|
bool exists = model->Exists();
|
|
|
|
|
auto& info = dioData->GetNameInfo();
|
|
|
|
|
char label[128];
|
|
|
|
|
if (exists && dpwmData) {
|
|
|
|
|
dpwmData->GetNameInfo().GetLabel(label, sizeof(label), "PWM", index);
|
|
|
|
|
if (auto simDevice = dpwm->GetSimDevice()) {
|
|
|
|
|
LabelSimDevice(label, simDevice);
|
|
|
|
|
} else {
|
|
|
|
|
dpwmData->LabelText(label, "%0.3f", dpwmData->GetValue());
|
|
|
|
|
}
|
|
|
|
|
} else if (exists && encoder) {
|
|
|
|
|
info.GetLabel(label, sizeof(label), " In", index);
|
|
|
|
|
if (auto simDevice = encoder->GetSimDevice()) {
|
|
|
|
|
LabelSimDevice(label, simDevice);
|
|
|
|
|
} else {
|
|
|
|
|
ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(96, 96, 96, 255));
|
|
|
|
|
ImGui::LabelText(label, "Encoder[%d,%d]", encoder->GetChannelA(),
|
|
|
|
|
encoder->GetChannelB());
|
|
|
|
|
ImGui::PopStyleColor();
|
|
|
|
|
}
|
|
|
|
|
} else if (exists && dutyCycleData) {
|
|
|
|
|
dutyCycleData->GetNameInfo().GetLabel(label, sizeof(label), "Dty", index);
|
|
|
|
|
if (auto simDevice = dutyCycle->GetSimDevice()) {
|
|
|
|
|
LabelSimDevice(label, simDevice);
|
|
|
|
|
} else {
|
|
|
|
|
double val = dutyCycleData->GetValue();
|
|
|
|
|
if (dutyCycleData->InputDouble(label, &val)) {
|
|
|
|
|
dutyCycle->SetValue(val);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
const char* name = model->GetName();
|
2020-12-28 12:58:06 -08:00
|
|
|
if (name[0] != '\0') {
|
2020-09-12 10:55:46 -07:00
|
|
|
info.GetLabel(label, sizeof(label), name);
|
2020-12-28 12:58:06 -08:00
|
|
|
} else {
|
2020-09-12 10:55:46 -07:00
|
|
|
info.GetLabel(label, sizeof(label), model->IsInput() ? " In" : "Out",
|
|
|
|
|
index);
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
if (auto simDevice = model->GetSimDevice()) {
|
|
|
|
|
LabelSimDevice(label, simDevice);
|
|
|
|
|
} else {
|
|
|
|
|
if (!exists) {
|
|
|
|
|
ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(96, 96, 96, 255));
|
|
|
|
|
dioData->LabelText(label, "unknown");
|
|
|
|
|
ImGui::PopStyleColor();
|
|
|
|
|
} else if (model->IsReadOnly()) {
|
|
|
|
|
dioData->LabelText(
|
|
|
|
|
label, "%s",
|
|
|
|
|
outputsEnabled ? (dioData->GetValue() != 0 ? "1 (high)" : "0 (low)")
|
|
|
|
|
: "1 (disabled)");
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
static const char* options[] = {"0 (low)", "1 (high)"};
|
|
|
|
|
int val = dioData->GetValue() != 0 ? 1 : 0;
|
|
|
|
|
if (dioData->Combo(label, &val, options, 2)) {
|
|
|
|
|
model->SetValue(val);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (info.PopupEditName(index)) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (dpwmData) {
|
|
|
|
|
dpwmData->SetName(info.GetName());
|
|
|
|
|
}
|
|
|
|
|
if (dutyCycleData) {
|
|
|
|
|
dutyCycleData->SetName(info.GetName());
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void glass::DisplayDIO(DIOModel* model, int index, bool outputsEnabled) {
|
|
|
|
|
ImGui::PushItemWidth(ImGui::GetFontSize() * 8);
|
|
|
|
|
DisplayDIOImpl(model, index, outputsEnabled);
|
|
|
|
|
ImGui::PopItemWidth();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void glass::DisplayDIOs(DIOsModel* model, bool outputsEnabled,
|
2021-06-06 16:13:58 -07:00
|
|
|
std::string_view noneMsg) {
|
2020-09-12 10:55:46 -07:00
|
|
|
bool hasAny = false;
|
|
|
|
|
|
|
|
|
|
ImGui::PushItemWidth(ImGui::GetFontSize() * 8);
|
|
|
|
|
model->ForEachDIO([&](DIOModel& dio, int i) {
|
|
|
|
|
hasAny = true;
|
|
|
|
|
ImGui::PushID(i);
|
|
|
|
|
DisplayDIOImpl(&dio, i, outputsEnabled);
|
|
|
|
|
ImGui::PopID();
|
|
|
|
|
});
|
|
|
|
|
ImGui::PopItemWidth();
|
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
|
|
|
}
|