2019-09-23 00:24:10 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2020-01-20 22:47:36 -08:00
|
|
|
/* Copyright (c) 2019-2020 FIRST. All Rights Reserved. */
|
2019-09-23 00:24:10 -07:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "RelayGui.h"
|
|
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cstring>
|
2020-08-14 20:02:35 -07:00
|
|
|
#include <memory>
|
|
|
|
|
#include <vector>
|
2019-09-23 00:24:10 -07:00
|
|
|
|
|
|
|
|
#include <hal/Ports.h>
|
2020-06-27 22:11:24 -07:00
|
|
|
#include <hal/simulation/RelayData.h>
|
2019-09-23 00:24:10 -07:00
|
|
|
#include <imgui.h>
|
|
|
|
|
|
|
|
|
|
#include "ExtraGuiWidgets.h"
|
2020-08-14 20:02:35 -07:00
|
|
|
#include "GuiDataSource.h"
|
2019-09-23 00:24:10 -07:00
|
|
|
#include "HALSimGui.h"
|
2020-01-20 22:47:36 -08:00
|
|
|
#include "IniSaver.h"
|
|
|
|
|
#include "IniSaverInfo.h"
|
2019-09-23 00:24:10 -07:00
|
|
|
|
|
|
|
|
using namespace halsimgui;
|
|
|
|
|
|
2020-08-14 20:02:35 -07:00
|
|
|
namespace {
|
|
|
|
|
HALSIMGUI_DATASOURCE_BOOLEAN_INDEXED(RelayForward, "RelayFwd");
|
|
|
|
|
HALSIMGUI_DATASOURCE_BOOLEAN_INDEXED(RelayReverse, "RelayRev");
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2020-01-20 22:47:36 -08:00
|
|
|
static IniSaver<NameInfo> gRelays{"Relay"};
|
2020-08-14 20:02:35 -07:00
|
|
|
static std::vector<std::unique_ptr<RelayForwardSource>> gRelayForwardSources;
|
|
|
|
|
static std::vector<std::unique_ptr<RelayReverseSource>> gRelayReverseSources;
|
|
|
|
|
|
|
|
|
|
static void UpdateRelaySources() {
|
|
|
|
|
for (int i = 0, iend = gRelayForwardSources.size(); i < iend; ++i) {
|
|
|
|
|
auto& source = gRelayForwardSources[i];
|
|
|
|
|
if (HALSIM_GetRelayInitializedForward(i)) {
|
|
|
|
|
if (!source) {
|
|
|
|
|
source = std::make_unique<RelayForwardSource>(i);
|
|
|
|
|
source->SetName(gRelays[i].GetName());
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
source.reset();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0, iend = gRelayReverseSources.size(); i < iend; ++i) {
|
|
|
|
|
auto& source = gRelayReverseSources[i];
|
|
|
|
|
if (HALSIM_GetRelayInitializedReverse(i)) {
|
|
|
|
|
if (!source) {
|
|
|
|
|
source = std::make_unique<RelayReverseSource>(i);
|
|
|
|
|
source->SetName(gRelays[i].GetName());
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
source.reset();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-20 22:47:36 -08:00
|
|
|
|
2019-09-23 00:24:10 -07:00
|
|
|
static void DisplayRelays() {
|
|
|
|
|
bool hasOutputs = false;
|
|
|
|
|
bool first = true;
|
2020-08-14 20:02:35 -07:00
|
|
|
for (int i = 0, iend = gRelayForwardSources.size(); i < iend; ++i) {
|
|
|
|
|
auto forwardSource = gRelayForwardSources[i].get();
|
|
|
|
|
auto reverseSource = gRelayReverseSources[i].get();
|
2019-09-23 00:24:10 -07:00
|
|
|
|
2020-08-14 20:02:35 -07:00
|
|
|
if (forwardSource || reverseSource) {
|
2019-09-23 00:24:10 -07:00
|
|
|
hasOutputs = true;
|
|
|
|
|
|
|
|
|
|
if (!first)
|
|
|
|
|
ImGui::Separator();
|
|
|
|
|
else
|
|
|
|
|
first = false;
|
|
|
|
|
|
2019-10-21 00:33:51 -07:00
|
|
|
bool forward = false;
|
|
|
|
|
bool reverse = false;
|
|
|
|
|
if (!HALSimGui::AreOutputsDisabled()) {
|
2020-08-14 20:02:35 -07:00
|
|
|
if (forwardSource) forward = forwardSource->GetValue();
|
|
|
|
|
if (reverseSource) reverse = reverseSource->GetValue();
|
2019-10-21 00:33:51 -07:00
|
|
|
}
|
2019-09-23 00:24:10 -07:00
|
|
|
|
2020-01-20 22:47:36 -08:00
|
|
|
auto& info = gRelays[i];
|
|
|
|
|
info.PushEditNameId(i);
|
|
|
|
|
if (info.HasName())
|
|
|
|
|
ImGui::Text("%s [%d]", info.GetName(), i);
|
|
|
|
|
else
|
|
|
|
|
ImGui::Text("Relay[%d]", i);
|
|
|
|
|
ImGui::PopID();
|
2020-08-14 20:02:35 -07:00
|
|
|
if (info.PopupEditName(i)) {
|
|
|
|
|
if (forwardSource) forwardSource->SetName(info.GetName());
|
|
|
|
|
if (reverseSource) reverseSource->SetName(info.GetName());
|
|
|
|
|
}
|
2019-09-23 00:24:10 -07:00
|
|
|
ImGui::SameLine();
|
|
|
|
|
|
|
|
|
|
// show forward and reverse as LED indicators
|
|
|
|
|
static const ImU32 colors[] = {IM_COL32(255, 255, 102, 255),
|
|
|
|
|
IM_COL32(255, 0, 0, 255),
|
|
|
|
|
IM_COL32(128, 128, 128, 255)};
|
2020-08-14 20:02:35 -07:00
|
|
|
int values[2] = {reverseSource ? (reverse ? 2 : -2) : -3,
|
|
|
|
|
forwardSource ? (forward ? 1 : -1) : -3};
|
|
|
|
|
GuiDataSource* sources[2] = {reverseSource, forwardSource};
|
|
|
|
|
ImGui::PushID(i);
|
|
|
|
|
DrawLEDSources(values, sources, 2, 2, colors);
|
|
|
|
|
ImGui::PopID();
|
2019-09-23 00:24:10 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!hasOutputs) ImGui::Text("No relays");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RelayGui::Initialize() {
|
2020-01-20 22:47:36 -08:00
|
|
|
gRelays.Initialize();
|
2020-08-14 20:02:35 -07:00
|
|
|
int numRelays = HAL_GetNumRelayHeaders();
|
|
|
|
|
gRelayForwardSources.resize(numRelays);
|
|
|
|
|
gRelayReverseSources.resize(numRelays);
|
|
|
|
|
HALSimGui::AddExecute(UpdateRelaySources);
|
2019-09-23 00:24:10 -07:00
|
|
|
HALSimGui::AddWindow("Relays", DisplayRelays,
|
|
|
|
|
ImGuiWindowFlags_AlwaysAutoResize);
|
|
|
|
|
HALSimGui::SetDefaultWindowPos("Relays", 180, 20);
|
|
|
|
|
}
|