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 "AnalogOutGui.h"
|
|
|
|
|
|
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/AnalogOutData.h>
|
2019-09-23 00:24:10 -07:00
|
|
|
#include <imgui.h>
|
|
|
|
|
|
2020-08-14 20:02:35 -07:00
|
|
|
#include "GuiDataSource.h"
|
|
|
|
|
#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
|
|
|
#include "SimDeviceGui.h"
|
|
|
|
|
|
|
|
|
|
using namespace halsimgui;
|
|
|
|
|
|
2020-08-14 20:02:35 -07:00
|
|
|
namespace {
|
|
|
|
|
HALSIMGUI_DATASOURCE_DOUBLE_INDEXED(AnalogOutVoltage, "AOut");
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2020-01-20 22:47:36 -08:00
|
|
|
static IniSaver<NameInfo> gAnalogOuts{"AnalogOut"}; // indexed by channel
|
2020-08-14 20:02:35 -07:00
|
|
|
static std::vector<std::unique_ptr<AnalogOutVoltageSource>> gAnalogOutSources;
|
2020-01-20 22:47:36 -08:00
|
|
|
|
2020-08-14 20:02:35 -07:00
|
|
|
static void UpdateAnalogOutSources() {
|
|
|
|
|
for (int i = 0, iend = gAnalogOutSources.size(); i < iend; ++i) {
|
|
|
|
|
auto& source = gAnalogOutSources[i];
|
|
|
|
|
if (HALSIM_GetAnalogOutInitialized(i)) {
|
|
|
|
|
if (!source) {
|
|
|
|
|
source = std::make_unique<AnalogOutVoltageSource>(i);
|
|
|
|
|
source->SetName(gAnalogOuts[i].GetName());
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
source.reset();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-23 00:24:10 -07:00
|
|
|
|
2020-08-14 20:02:35 -07:00
|
|
|
static void DisplayAnalogOutputs() {
|
2019-09-23 00:24:10 -07:00
|
|
|
int count = 0;
|
2020-08-14 20:02:35 -07:00
|
|
|
for (auto&& source : gAnalogOutSources) {
|
|
|
|
|
if (source) ++count;
|
2019-09-23 00:24:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (count == 0) return;
|
|
|
|
|
|
|
|
|
|
if (SimDeviceGui::StartDevice("Analog Outputs")) {
|
2020-08-14 20:02:35 -07:00
|
|
|
for (int i = 0, iend = gAnalogOutSources.size(); i < iend; ++i) {
|
|
|
|
|
if (auto source = gAnalogOutSources[i].get()) {
|
|
|
|
|
ImGui::PushID(i);
|
|
|
|
|
|
|
|
|
|
auto& info = gAnalogOuts[i];
|
|
|
|
|
char label[128];
|
|
|
|
|
info.GetLabel(label, sizeof(label), "Out", i);
|
|
|
|
|
HAL_Value value = HAL_MakeDouble(source->GetValue());
|
|
|
|
|
SimDeviceGui::DisplayValueSource(label, true, &value, source);
|
|
|
|
|
|
|
|
|
|
if (info.PopupEditName(i)) {
|
|
|
|
|
if (source) source->SetName(info.GetName());
|
|
|
|
|
}
|
|
|
|
|
ImGui::PopID();
|
|
|
|
|
}
|
2019-09-23 00:24:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SimDeviceGui::FinishDevice();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-20 22:47:36 -08:00
|
|
|
void AnalogOutGui::Initialize() {
|
|
|
|
|
gAnalogOuts.Initialize();
|
2020-08-14 20:02:35 -07:00
|
|
|
gAnalogOutSources.resize(HAL_GetNumAnalogOutputs());
|
|
|
|
|
HALSimGui::AddExecute(UpdateAnalogOutSources);
|
2020-01-20 22:47:36 -08:00
|
|
|
SimDeviceGui::Add(DisplayAnalogOutputs);
|
|
|
|
|
}
|