[glass] Add glass: an application for display of robot data

This reuses many pieces of the current simulation GUI.  The common pieces have
been refactored into the libglass library.

The libglass library is designed to be usable for other standalone data
visualization applications (e.g. viewing data logs).

The name "glass" comes from "glass cockpit", as the application features
several multi-function displays that can be adjusted to display robot
information as needed.
This commit is contained in:
Peter Johnson
2020-09-12 10:55:46 -07:00
parent 727940d847
commit 2a5ca77454
151 changed files with 10386 additions and 4565 deletions

View File

@@ -7,120 +7,103 @@
#include "AddressableLEDGui.h"
#include <glass/hardware/LEDDisplay.h>
#include <hal/Ports.h>
#include <hal/simulation/AddressableLEDData.h>
#include <imgui.h>
#include <imgui_internal.h>
#include <wpi/SmallVector.h>
#include <wpi/StringRef.h>
#include "ExtraGuiWidgets.h"
#include "HALSimGui.h"
#include "IniSaver.h"
#include "IniSaverInfo.h"
using namespace halsimgui;
namespace {
struct LEDDisplayInfo {
int numColumns = 10;
LEDConfig config;
class AddressableLEDModel : public glass::LEDDisplayModel {
public:
explicit AddressableLEDModel(int32_t index) : m_index{index} {}
bool ReadIni(wpi::StringRef name, wpi::StringRef value);
void WriteIni(ImGuiTextBuffer* out);
void Update() override {}
bool Exists() override {
return HALSIM_GetAddressableLEDInitialized(m_index);
}
bool IsRunning() override { return HALSIM_GetAddressableLEDRunning(m_index); }
wpi::ArrayRef<Data> GetData(wpi::SmallVectorImpl<Data>&) override {
size_t length = HALSIM_GetAddressableLEDData(m_index, m_data);
return {reinterpret_cast<Data*>(m_data), length};
}
private:
int32_t m_index;
HAL_AddressableLEDData m_data[HAL_kAddressableLEDMaxLength];
};
class AddressableLEDsModel : public glass::LEDDisplaysModel {
public:
AddressableLEDsModel() : m_models(HAL_GetNumAddressableLEDs()) {}
void Update() override;
bool Exists() override;
size_t GetNumLEDDisplays() override { return m_models.size(); }
void ForEachLEDDisplay(
wpi::function_ref<void(glass::LEDDisplayModel& model, int index)> func)
override;
private:
std::vector<std::unique_ptr<AddressableLEDModel>> m_models;
};
} // namespace
static IniSaver<LEDDisplayInfo> gDisplaySettings{"AddressableLED"};
bool LEDDisplayInfo::ReadIni(wpi::StringRef name, wpi::StringRef value) {
if (name == "columns") {
int num;
if (value.getAsInteger(10, num)) return true;
numColumns = num;
} else if (name == "serpentine") {
int num;
if (value.getAsInteger(10, num)) return true;
config.serpentine = num != 0;
} else if (name == "order") {
int num;
if (value.getAsInteger(10, num)) return true;
config.order = static_cast<LEDConfig::Order>(num);
} else if (name == "start") {
int num;
if (value.getAsInteger(10, num)) return true;
config.start = static_cast<LEDConfig::Start>(num);
} else {
return false;
}
return true;
}
void LEDDisplayInfo::WriteIni(ImGuiTextBuffer* out) {
out->appendf("columns=%d\nserpentine=%d\norder=%d\nstart=%d\n", numColumns,
config.serpentine ? 1 : 0, static_cast<int>(config.order),
static_cast<int>(config.start));
}
static void DisplayAddressableLEDs() {
bool hasAny = false;
static const int numLED = HAL_GetNumAddressableLEDs();
for (int i = 0; i < numLED; ++i) {
if (!HALSIM_GetAddressableLEDInitialized(i)) continue;
hasAny = true;
if (numLED > 1) ImGui::Text("LEDs[%d]", i);
static HAL_AddressableLEDData data[HAL_kAddressableLEDMaxLength];
int length = HALSIM_GetAddressableLEDData(i, data);
bool running = HALSIM_GetAddressableLEDRunning(i);
auto& info = gDisplaySettings[i];
ImGui::PushItemWidth(ImGui::GetFontSize() * 6);
ImGui::LabelText("Length", "%d", length);
ImGui::LabelText("Running", "%s", running ? "Yes" : "No");
ImGui::InputInt("Columns", &info.numColumns);
{
static const char* options[] = {"Row Major", "Column Major"};
int val = info.config.order;
if (ImGui::Combo("Order", &val, options, 2))
info.config.order = static_cast<LEDConfig::Order>(val);
}
{
static const char* options[] = {"Upper Left", "Lower Left", "Upper Right",
"Lower Right"};
int val = info.config.start;
if (ImGui::Combo("Start", &val, options, 4))
info.config.start = static_cast<LEDConfig::Start>(val);
}
ImGui::Checkbox("Serpentine", &info.config.serpentine);
if (info.numColumns < 1) info.numColumns = 1;
ImGui::PopItemWidth();
// show as LED indicators
static int values[HAL_kAddressableLEDMaxLength];
static ImU32 colors[HAL_kAddressableLEDMaxLength];
if (!running) {
colors[0] = IM_COL32(128, 128, 128, 255);
for (int j = 0; j < length; ++j) values[j] = -1;
} else {
for (int j = 0; j < length; ++j) {
values[j] = j + 1;
colors[j] = IM_COL32(data[j].r, data[j].g, data[j].b, 255);
void AddressableLEDsModel::Update() {
for (int i = 0; i < static_cast<int>(m_models.size()); ++i) {
auto& model = m_models[i];
if (HALSIM_GetAddressableLEDInitialized(i)) {
if (!model) {
model = std::make_unique<AddressableLEDModel>(i);
}
if (model) model->Update();
} else {
model.reset();
}
DrawLEDs(values, length, info.numColumns, colors, 0, 0, info.config);
}
if (!hasAny) ImGui::Text("No addressable LEDs");
}
bool AddressableLEDsModel::Exists() {
for (auto&& model : m_models) {
if (model && model->Exists()) return true;
}
return false;
}
void AddressableLEDsModel::ForEachLEDDisplay(
wpi::function_ref<void(glass::LEDDisplayModel& model, int index)> func) {
for (int i = 0; i < static_cast<int>(m_models.size()); ++i) {
if (m_models[i]) func(*m_models[i], i);
}
}
static bool AddressableLEDsExists() {
static const int numLED = HAL_GetNumAddressableLEDs();
for (int i = 0; i < numLED; ++i) {
if (HALSIM_GetAddressableLEDInitialized(i)) return true;
}
return false;
}
void AddressableLEDGui::Initialize() {
gDisplaySettings.Initialize();
HALSimGui::AddWindow("Addressable LEDs", DisplayAddressableLEDs,
ImGuiWindowFlags_AlwaysAutoResize);
HALSimGui::SetWindowVisibility("Addressable LEDs", HALSimGui::kHide);
HALSimGui::SetDefaultWindowPos("Addressable LEDs", 290, 100);
HALSimGui::halProvider.Register(
"Addressable LEDs", [] { return AddressableLEDsExists(); },
[] { return std::make_unique<AddressableLEDsModel>(); },
[](glass::Window* win, glass::Model* model) {
win->SetFlags(ImGuiWindowFlags_AlwaysAutoResize);
win->SetDefaultPos(290, 100);
return glass::MakeFunctionView([=] {
glass::DisplayLEDDisplays(static_cast<AddressableLEDsModel*>(model));
});
});
}