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.
|
2020-09-12 10:55:46 -07:00
|
|
|
|
|
|
|
|
#include "PWMSimGui.h"
|
|
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2024-09-20 17:43:39 -07:00
|
|
|
#include <glass/hardware/PWM.h>
|
2020-09-12 10:55:46 -07:00
|
|
|
#include <hal/Ports.h>
|
|
|
|
|
#include <hal/simulation/AddressableLEDData.h>
|
|
|
|
|
#include <hal/simulation/PWMData.h>
|
|
|
|
|
|
|
|
|
|
#include "HALDataSource.h"
|
|
|
|
|
#include "HALSimGui.h"
|
|
|
|
|
|
|
|
|
|
using namespace halsimgui;
|
|
|
|
|
|
|
|
|
|
namespace {
|
2025-03-20 19:23:22 -07:00
|
|
|
HALSIMGUI_DATASOURCE_DOUBLE_INDEXED(PWMPulseMicrosecond, "PWM");
|
2020-09-12 10:55:46 -07:00
|
|
|
|
|
|
|
|
class PWMSimModel : public glass::PWMModel {
|
|
|
|
|
public:
|
|
|
|
|
explicit PWMSimModel(int32_t index) : m_index{index}, m_speed{m_index} {}
|
|
|
|
|
|
|
|
|
|
void Update() override {}
|
|
|
|
|
|
|
|
|
|
bool Exists() override { return HALSIM_GetPWMInitialized(m_index); }
|
|
|
|
|
|
2025-01-03 13:36:40 -08:00
|
|
|
glass::DoubleSource* GetSpeedData() override { return &m_speed; }
|
2020-09-12 10:55:46 -07:00
|
|
|
|
2025-03-20 19:23:22 -07:00
|
|
|
void SetSpeed(double val) override {
|
|
|
|
|
HALSIM_SetPWMPulseMicrosecond(m_index, val);
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
int32_t m_index;
|
2025-03-20 19:23:22 -07:00
|
|
|
PWMPulseMicrosecondSource m_speed;
|
2020-09-12 10:55:46 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class PWMsSimModel : public glass::PWMsModel {
|
|
|
|
|
public:
|
|
|
|
|
PWMsSimModel() : m_sources(HAL_GetNumPWMChannels()) {}
|
|
|
|
|
|
|
|
|
|
void Update() override;
|
|
|
|
|
|
|
|
|
|
bool Exists() override { return true; }
|
|
|
|
|
|
|
|
|
|
void ForEachPWM(
|
|
|
|
|
wpi::function_ref<void(glass::PWMModel& model, int index)> func) override;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
// indexed by channel
|
|
|
|
|
std::vector<std::unique_ptr<PWMSimModel>> m_sources;
|
|
|
|
|
};
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
void PWMsSimModel::Update() {
|
|
|
|
|
const int32_t numPWM = m_sources.size();
|
|
|
|
|
for (int32_t i = 0; i < numPWM; ++i) {
|
|
|
|
|
auto& model = m_sources[i];
|
|
|
|
|
if (HALSIM_GetPWMInitialized(i)) {
|
|
|
|
|
if (!model) {
|
|
|
|
|
model = std::make_unique<PWMSimModel>(i);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
model.reset();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PWMsSimModel::ForEachPWM(
|
|
|
|
|
wpi::function_ref<void(glass::PWMModel& model, int index)> func) {
|
|
|
|
|
const int32_t numPWM = m_sources.size();
|
|
|
|
|
for (int32_t i = 0; i < numPWM; ++i) {
|
|
|
|
|
if (auto model = m_sources[i].get()) {
|
|
|
|
|
func(*model, i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool PWMsAnyInitialized() {
|
|
|
|
|
static const int32_t num = HAL_GetNumPWMChannels();
|
|
|
|
|
for (int32_t i = 0; i < num; ++i) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (HALSIM_GetPWMInitialized(i)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PWMSimGui::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
|
|
|
"PWM Outputs", PWMsAnyInitialized,
|
|
|
|
|
[] { return std::make_unique<PWMsSimModel>(); },
|
|
|
|
|
[](glass::Window* win, glass::Model* model) {
|
|
|
|
|
win->SetFlags(ImGuiWindowFlags_AlwaysAutoResize);
|
|
|
|
|
win->SetDefaultPos(910, 20);
|
|
|
|
|
return glass::MakeFunctionView([=] {
|
|
|
|
|
glass::DisplayPWMs(static_cast<PWMsSimModel*>(model),
|
[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->AreOutputsEnabled());
|
2020-09-12 10:55:46 -07:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|