2020-12-26 14:12:05 -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.
|
2019-11-17 16:35:45 -08:00
|
|
|
|
|
|
|
|
#include "AddressableLEDGui.h"
|
|
|
|
|
|
2020-09-12 10:55:46 -07:00
|
|
|
#include <glass/hardware/LEDDisplay.h>
|
|
|
|
|
|
2019-11-17 16:35:45 -08:00
|
|
|
#include <hal/Ports.h>
|
2020-06-27 22:11:24 -07:00
|
|
|
#include <hal/simulation/AddressableLEDData.h>
|
2019-11-17 16:35:45 -08:00
|
|
|
#include <imgui.h>
|
|
|
|
|
|
|
|
|
|
#include "HALSimGui.h"
|
|
|
|
|
|
|
|
|
|
using namespace halsimgui;
|
|
|
|
|
|
2019-12-01 21:28:02 -08:00
|
|
|
namespace {
|
2020-09-12 10:55:46 -07:00
|
|
|
class AddressableLEDModel : public glass::LEDDisplayModel {
|
|
|
|
|
public:
|
|
|
|
|
explicit AddressableLEDModel(int32_t index) : m_index{index} {}
|
2020-01-20 21:49:03 -08:00
|
|
|
|
2020-09-12 10:55:46 -07:00
|
|
|
void Update() override {}
|
|
|
|
|
bool Exists() override {
|
|
|
|
|
return HALSIM_GetAddressableLEDInitialized(m_index);
|
|
|
|
|
}
|
2019-12-01 21:28:02 -08:00
|
|
|
|
2020-09-12 10:55:46 -07:00
|
|
|
bool IsRunning() override { return HALSIM_GetAddressableLEDRunning(m_index); }
|
|
|
|
|
|
2022-10-15 16:33:14 -07:00
|
|
|
std::span<const Data> GetData(wpi::SmallVectorImpl<Data>&) override {
|
2020-09-12 10:55:46 -07:00
|
|
|
size_t length = HALSIM_GetAddressableLEDData(m_index, m_data);
|
|
|
|
|
return {reinterpret_cast<Data*>(m_data), length};
|
2019-11-17 16:35:45 -08:00
|
|
|
}
|
|
|
|
|
|
2020-09-12 10:55:46 -07:00
|
|
|
private:
|
|
|
|
|
int32_t m_index;
|
2019-11-17 16:35:45 -08:00
|
|
|
|
2020-09-12 10:55:46 -07:00
|
|
|
HAL_AddressableLEDData m_data[HAL_kAddressableLEDMaxLength];
|
|
|
|
|
};
|
2019-11-17 16:35:45 -08:00
|
|
|
|
2020-09-12 10:55:46 -07:00
|
|
|
class AddressableLEDsModel : public glass::LEDDisplaysModel {
|
|
|
|
|
public:
|
|
|
|
|
AddressableLEDsModel() : m_models(HAL_GetNumAddressableLEDs()) {}
|
2019-11-17 16:35:45 -08:00
|
|
|
|
2020-09-12 10:55:46 -07:00
|
|
|
void Update() override;
|
|
|
|
|
bool Exists() override;
|
2019-11-17 16:35:45 -08:00
|
|
|
|
2020-09-12 10:55:46 -07:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
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);
|
2019-11-17 16:35:45 -08:00
|
|
|
}
|
2020-12-28 12:58:06 -08:00
|
|
|
if (model) {
|
|
|
|
|
model->Update();
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
} else {
|
|
|
|
|
model.reset();
|
2019-11-17 16:35:45 -08:00
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool AddressableLEDsModel::Exists() {
|
|
|
|
|
for (auto&& model : m_models) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (model && model->Exists()) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-11-17 16:35:45 -08:00
|
|
|
|
2020-09-12 10:55:46 -07:00
|
|
|
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) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (m_models[i]) {
|
|
|
|
|
func(*m_models[i], i);
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool AddressableLEDsExists() {
|
|
|
|
|
static const int numLED = HAL_GetNumAddressableLEDs();
|
|
|
|
|
for (int i = 0; i < numLED; ++i) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (HALSIM_GetAddressableLEDInitialized(i)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2019-11-17 16:35:45 -08:00
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
return false;
|
2019-11-17 16:35:45 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AddressableLEDGui::Initialize() {
|
[glass] Use JSON files for storage instead of imgui ini
Storage is now nested.
Separate "roots" can be configured which save to separate files.
In particular, this is used to save wpigui and ImGui window position
to a -window.json file.
ImGui's ini (for window position) is mapped to JSON.
You can optionally specify a directory to load from on the command line.
If one isn't provided, it uses the global system directory.
Any changes made are automatically saved here.
Workspace | Open: select directory, the current layout is replaced with that
workspace, and future auto-saves also switch to that location. The main
window size/location is not changed, only the contents.
Workspace | Save As: select directory, the current layout is saved there,
and future auto-saves also switch to that location.
Workspace | Reset: window locations are preserved, but all other settings
are reset to default (including e.g. removing plot windows). This will also
end up clearing the current save file. as with load, the main window
size/location is not changed.
Workspace | Save As Global: "save as" to the global system location
Notably, the main window size/location is only loaded at startup, but is
auto-saved as part of the current workspace.
2021-11-25 00:51:00 -08:00
|
|
|
HALSimGui::halProvider->Register(
|
2020-09-12 10:55:46 -07:00
|
|
|
"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));
|
|
|
|
|
});
|
|
|
|
|
});
|
2019-11-17 16:35:45 -08:00
|
|
|
}
|