2019-09-23 00:24:10 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2020-01-20 21:49:03 -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 "SimDeviceGui.h"
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
2020-08-14 20:02:35 -07:00
|
|
|
#include <functional>
|
|
|
|
|
#include <memory>
|
2019-09-23 00:24:10 -07:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
#include <hal/SimDevice.h>
|
2020-06-27 22:11:24 -07:00
|
|
|
#include <hal/simulation/SimDeviceData.h>
|
2019-09-23 00:24:10 -07:00
|
|
|
#include <imgui.h>
|
2020-08-14 20:02:35 -07:00
|
|
|
#include <wpi/DenseMap.h>
|
2019-09-23 00:24:10 -07:00
|
|
|
|
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 21:49:03 -08:00
|
|
|
#include "IniSaverInfo.h"
|
|
|
|
|
#include "IniSaverString.h"
|
2019-09-23 00:24:10 -07:00
|
|
|
|
|
|
|
|
using namespace halsimgui;
|
|
|
|
|
|
|
|
|
|
namespace {
|
2020-08-14 20:02:35 -07:00
|
|
|
|
2020-03-19 23:21:29 -07:00
|
|
|
struct ElementInfo : public NameInfo, public OpenInfo {
|
|
|
|
|
bool ReadIni(wpi::StringRef name, wpi::StringRef value) {
|
|
|
|
|
if (NameInfo::ReadIni(name, value)) return true;
|
|
|
|
|
if (OpenInfo::ReadIni(name, value)) return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
void WriteIni(ImGuiTextBuffer* out) {
|
|
|
|
|
NameInfo::WriteIni(out);
|
|
|
|
|
OpenInfo::WriteIni(out);
|
|
|
|
|
}
|
2020-01-20 21:49:03 -08:00
|
|
|
bool visible = true; // not saved
|
2019-09-23 00:24:10 -07:00
|
|
|
};
|
2020-08-14 20:02:35 -07:00
|
|
|
|
|
|
|
|
class SimValueSource : public GuiDataSource {
|
|
|
|
|
public:
|
|
|
|
|
explicit SimValueSource(HAL_SimValueHandle handle, const char* device,
|
|
|
|
|
const char* name)
|
|
|
|
|
: GuiDataSource(wpi::Twine{device} + wpi::Twine{'-'} + name),
|
|
|
|
|
m_callback{HALSIM_RegisterSimValueChangedCallback(
|
|
|
|
|
handle, this, CallbackFunc, true)} {}
|
|
|
|
|
~SimValueSource() {
|
|
|
|
|
if (m_callback != 0) HALSIM_CancelSimValueChangedCallback(m_callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
static void CallbackFunc(const char*, void* param, HAL_SimValueHandle,
|
|
|
|
|
HAL_Bool, const HAL_Value* value) {
|
|
|
|
|
auto source = static_cast<SimValueSource*>(param);
|
|
|
|
|
if (value->type == HAL_BOOLEAN) {
|
|
|
|
|
source->SetValue(value->data.v_boolean);
|
|
|
|
|
source->SetDigital(true);
|
|
|
|
|
} else if (value->type == HAL_DOUBLE) {
|
|
|
|
|
source->SetValue(value->data.v_double);
|
|
|
|
|
source->SetDigital(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t m_callback;
|
|
|
|
|
};
|
|
|
|
|
|
2019-09-23 00:24:10 -07:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
static std::vector<std::function<void()>> gDeviceExecutors;
|
2020-01-20 21:49:03 -08:00
|
|
|
static IniSaverString<ElementInfo> gElements{"Device"};
|
2020-08-14 20:02:35 -07:00
|
|
|
static wpi::DenseMap<HAL_SimValueHandle, std::unique_ptr<SimValueSource>>
|
|
|
|
|
gSimValueSources;
|
|
|
|
|
|
|
|
|
|
static void UpdateSimValueSources() {
|
|
|
|
|
HALSIM_EnumerateSimDevices(
|
|
|
|
|
"", nullptr, [](const char* name, void*, HAL_SimDeviceHandle handle) {
|
|
|
|
|
HALSIM_EnumerateSimValues(
|
|
|
|
|
handle, const_cast<char*>(name),
|
|
|
|
|
[](const char* name, void* deviceV, HAL_SimValueHandle handle,
|
|
|
|
|
HAL_Bool readonly, const HAL_Value* value) {
|
|
|
|
|
auto device = static_cast<const char*>(deviceV);
|
|
|
|
|
auto& source = gSimValueSources[handle];
|
|
|
|
|
if (!source) {
|
|
|
|
|
source = std::make_unique<SimValueSource>(handle, device, name);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-09-23 00:24:10 -07:00
|
|
|
|
|
|
|
|
void SimDeviceGui::Hide(const char* name) { gElements[name].visible = false; }
|
|
|
|
|
|
|
|
|
|
void SimDeviceGui::Add(std::function<void()> execute) {
|
|
|
|
|
if (execute) gDeviceExecutors.emplace_back(std::move(execute));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SimDeviceGui::StartDevice(const char* label, ImGuiTreeNodeFlags flags) {
|
|
|
|
|
auto& element = gElements[label];
|
|
|
|
|
if (!element.visible) return false;
|
|
|
|
|
|
2020-03-19 23:21:29 -07:00
|
|
|
char name[128];
|
2020-08-14 20:02:35 -07:00
|
|
|
element.GetLabel(name, sizeof(name), label);
|
2020-03-19 23:21:29 -07:00
|
|
|
|
|
|
|
|
bool open = ImGui::CollapsingHeader(
|
|
|
|
|
name, flags | (element.IsOpen() ? ImGuiTreeNodeFlags_DefaultOpen : 0));
|
|
|
|
|
element.SetOpen(open);
|
|
|
|
|
element.PopupEditName(label);
|
|
|
|
|
|
|
|
|
|
if (open) ImGui::PushID(label);
|
|
|
|
|
return open;
|
2019-09-23 00:24:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SimDeviceGui::FinishDevice() { ImGui::PopID(); }
|
|
|
|
|
|
2020-08-14 20:02:35 -07:00
|
|
|
static bool DisplayValueImpl(const char* name, bool readonly, HAL_Value* value,
|
|
|
|
|
const char** options, int32_t numOptions) {
|
2019-09-23 00:24:10 -07:00
|
|
|
// read-only
|
|
|
|
|
if (readonly) {
|
|
|
|
|
switch (value->type) {
|
|
|
|
|
case HAL_BOOLEAN:
|
|
|
|
|
ImGui::LabelText(name, "%s", value->data.v_boolean ? "true" : "false");
|
|
|
|
|
break;
|
|
|
|
|
case HAL_DOUBLE:
|
|
|
|
|
ImGui::LabelText(name, "%.6f", value->data.v_double);
|
|
|
|
|
break;
|
|
|
|
|
case HAL_ENUM: {
|
|
|
|
|
int current = value->data.v_enum;
|
|
|
|
|
if (current < 0 || current >= numOptions)
|
|
|
|
|
ImGui::LabelText(name, "%d (unknown)", current);
|
|
|
|
|
else
|
|
|
|
|
ImGui::LabelText(name, "%s", options[current]);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case HAL_INT:
|
|
|
|
|
ImGui::LabelText(name, "%d", static_cast<int>(value->data.v_int));
|
|
|
|
|
break;
|
|
|
|
|
case HAL_LONG:
|
|
|
|
|
ImGui::LabelText(name, "%lld",
|
|
|
|
|
static_cast<long long int>( // NOLINT(runtime/int)
|
|
|
|
|
value->data.v_long));
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// writable
|
|
|
|
|
switch (value->type) {
|
|
|
|
|
case HAL_BOOLEAN: {
|
|
|
|
|
static const char* boolOptions[] = {"false", "true"};
|
|
|
|
|
int val = value->data.v_boolean ? 1 : 0;
|
|
|
|
|
if (ImGui::Combo(name, &val, boolOptions, 2)) {
|
|
|
|
|
value->data.v_boolean = val;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case HAL_DOUBLE: {
|
|
|
|
|
if (ImGui::InputDouble(name, &value->data.v_double, 0, 0, "%.6f",
|
|
|
|
|
ImGuiInputTextFlags_EnterReturnsTrue))
|
|
|
|
|
return true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case HAL_ENUM: {
|
|
|
|
|
int current = value->data.v_enum;
|
|
|
|
|
if (ImGui::Combo(name, ¤t, options, numOptions)) {
|
|
|
|
|
value->data.v_enum = current;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case HAL_INT: {
|
|
|
|
|
if (ImGui::InputScalar(name, ImGuiDataType_S32, &value->data.v_int,
|
|
|
|
|
nullptr, nullptr, nullptr,
|
|
|
|
|
ImGuiInputTextFlags_EnterReturnsTrue))
|
|
|
|
|
return true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case HAL_LONG: {
|
|
|
|
|
if (ImGui::InputScalar(name, ImGuiDataType_S64, &value->data.v_long,
|
|
|
|
|
nullptr, nullptr, nullptr,
|
|
|
|
|
ImGuiInputTextFlags_EnterReturnsTrue))
|
|
|
|
|
return true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-14 20:02:35 -07:00
|
|
|
static bool DisplayValueSourceImpl(const char* name, bool readonly,
|
|
|
|
|
HAL_Value* value,
|
|
|
|
|
const GuiDataSource* source,
|
|
|
|
|
const char** options, int32_t numOptions) {
|
|
|
|
|
if (!source)
|
|
|
|
|
return DisplayValueImpl(name, readonly, value, options, numOptions);
|
|
|
|
|
ImGui::PushID(name);
|
|
|
|
|
bool rv = DisplayValueImpl("", readonly, value, options, numOptions);
|
|
|
|
|
ImGui::SameLine(0, ImGui::GetStyle().ItemInnerSpacing.x);
|
|
|
|
|
ImGui::Selectable(name);
|
|
|
|
|
source->EmitDrag();
|
|
|
|
|
ImGui::PopID();
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-23 00:24:10 -07:00
|
|
|
bool SimDeviceGui::DisplayValue(const char* name, bool readonly,
|
|
|
|
|
HAL_Value* value, const char** options,
|
|
|
|
|
int32_t numOptions) {
|
2020-08-14 20:02:35 -07:00
|
|
|
return DisplayValueSource(name, readonly, value, nullptr, options,
|
|
|
|
|
numOptions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SimDeviceGui::DisplayValueSource(const char* name, bool readonly,
|
|
|
|
|
HAL_Value* value,
|
|
|
|
|
const GuiDataSource* source,
|
|
|
|
|
const char** options,
|
|
|
|
|
int32_t numOptions) {
|
2019-09-23 00:24:10 -07:00
|
|
|
ImGui::SetNextItemWidth(ImGui::GetWindowWidth() * 0.5f);
|
2020-08-14 20:02:35 -07:00
|
|
|
return DisplayValueSourceImpl(name, readonly, value, source, options,
|
|
|
|
|
numOptions);
|
2019-09-23 00:24:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void SimDeviceDisplayValue(const char* name, void*,
|
|
|
|
|
HAL_SimValueHandle handle, HAL_Bool readonly,
|
|
|
|
|
const HAL_Value* value) {
|
|
|
|
|
int32_t numOptions = 0;
|
|
|
|
|
const char** options = nullptr;
|
|
|
|
|
|
|
|
|
|
if (value->type == HAL_ENUM)
|
|
|
|
|
options = HALSIM_GetSimValueEnumOptions(handle, &numOptions);
|
|
|
|
|
|
|
|
|
|
HAL_Value valueCopy = *value;
|
2020-08-14 20:02:35 -07:00
|
|
|
if (DisplayValueSourceImpl(name, readonly, &valueCopy,
|
|
|
|
|
gSimValueSources[handle].get(), options,
|
|
|
|
|
numOptions))
|
2019-09-23 00:24:10 -07:00
|
|
|
HAL_SetSimValue(handle, valueCopy);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void SimDeviceDisplayDevice(const char* name, void*,
|
|
|
|
|
HAL_SimDeviceHandle handle) {
|
|
|
|
|
auto it = gElements.find(name);
|
|
|
|
|
if (it != gElements.end() && !it->second.visible) return;
|
|
|
|
|
|
|
|
|
|
if (SimDeviceGui::StartDevice(name)) {
|
|
|
|
|
ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
|
|
|
|
|
HALSIM_EnumerateSimValues(handle, nullptr, SimDeviceDisplayValue);
|
|
|
|
|
ImGui::PopItemWidth();
|
|
|
|
|
SimDeviceGui::FinishDevice();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void DisplayDeviceTree() {
|
|
|
|
|
for (auto&& execute : gDeviceExecutors) {
|
|
|
|
|
if (execute) execute();
|
|
|
|
|
}
|
|
|
|
|
HALSIM_EnumerateSimDevices("", nullptr, SimDeviceDisplayDevice);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SimDeviceGui::Initialize() {
|
2020-01-20 21:49:03 -08:00
|
|
|
gElements.Initialize();
|
2020-08-14 20:02:35 -07:00
|
|
|
HALSimGui::AddExecute(UpdateSimValueSources);
|
2019-09-23 00:24:10 -07:00
|
|
|
HALSimGui::AddWindow("Other Devices", DisplayDeviceTree);
|
|
|
|
|
HALSimGui::SetDefaultWindowPos("Other Devices", 1025, 20);
|
|
|
|
|
HALSimGui::SetDefaultWindowSize("Other Devices", 250, 695);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
|
|
void HALSIMGUI_DeviceTreeAdd(void* param, void (*execute)(void*)) {
|
|
|
|
|
if (execute) SimDeviceGui::Add([=] { execute(param); });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HALSIMGUI_DeviceTreeHide(const char* name) { SimDeviceGui::Hide(name); }
|
|
|
|
|
|
|
|
|
|
HAL_Bool HALSIMGUI_DeviceTreeDisplayValue(const char* name, HAL_Bool readonly,
|
|
|
|
|
struct HAL_Value* value,
|
|
|
|
|
const char** options,
|
|
|
|
|
int32_t numOptions) {
|
|
|
|
|
return SimDeviceGui::DisplayValue(name, readonly, value, options, numOptions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HAL_Bool HALSIMGUI_DeviceTreeStartDevice(const char* label, int32_t flags) {
|
|
|
|
|
return SimDeviceGui::StartDevice(label, flags);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HALSIMGUI_DeviceTreeFinishDevice(void) { SimDeviceGui::FinishDevice(); }
|
|
|
|
|
|
|
|
|
|
} // extern "C"
|