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 "PWMGui.h"
|
|
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cstring>
|
2019-11-17 16:35:45 -08:00
|
|
|
#include <memory>
|
2019-09-23 00:24:10 -07:00
|
|
|
|
|
|
|
|
#include <hal/Ports.h>
|
2020-06-27 22:11:24 -07:00
|
|
|
#include <hal/simulation/AddressableLEDData.h>
|
|
|
|
|
#include <hal/simulation/PWMData.h>
|
2019-09-23 00:24:10 -07:00
|
|
|
#include <imgui.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
|
|
|
|
|
|
|
|
using namespace halsimgui;
|
|
|
|
|
|
2020-01-20 22:47:36 -08:00
|
|
|
static IniSaver<NameInfo> gPWM{"PWM"};
|
|
|
|
|
|
2019-09-23 00:24:10 -07:00
|
|
|
static void DisplayPWMs() {
|
|
|
|
|
bool hasOutputs = false;
|
2019-11-17 16:35:45 -08:00
|
|
|
static const int numPWM = HAL_GetNumPWMChannels();
|
|
|
|
|
static const int numLED = HAL_GetNumAddressableLEDs();
|
|
|
|
|
static auto ledMap = std::make_unique<int[]>(numPWM);
|
|
|
|
|
|
|
|
|
|
std::memset(ledMap.get(), 0, numPWM * sizeof(ledMap[0]));
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < numLED; ++i) {
|
|
|
|
|
if (HALSIM_GetAddressableLEDInitialized(i)) {
|
|
|
|
|
int channel = HALSIM_GetAddressableLEDOutputPort(i);
|
|
|
|
|
if (channel >= 0 && channel < numPWM) ledMap[channel] = i + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-23 00:24:10 -07:00
|
|
|
// struct History {
|
|
|
|
|
// History() { std::memset(data, 0, 90 * sizeof(float)); }
|
|
|
|
|
// History(const History&) = delete;
|
|
|
|
|
// History& operator=(const History&) = delete;
|
|
|
|
|
// float data[90];
|
|
|
|
|
// int display_offset = 0;
|
|
|
|
|
// int save_offset = 0;
|
|
|
|
|
//};
|
|
|
|
|
// static std::vector<std::unique_ptr<History>> history;
|
|
|
|
|
bool first = true;
|
2020-01-20 22:47:36 -08:00
|
|
|
ImGui::PushItemWidth(ImGui::GetFontSize() * 4);
|
2019-09-23 00:24:10 -07:00
|
|
|
for (int i = 0; i < numPWM; ++i) {
|
|
|
|
|
if (HALSIM_GetPWMInitialized(i)) {
|
|
|
|
|
hasOutputs = true;
|
|
|
|
|
|
|
|
|
|
if (!first)
|
|
|
|
|
ImGui::Separator();
|
|
|
|
|
else
|
|
|
|
|
first = false;
|
|
|
|
|
|
2020-01-20 22:47:36 -08:00
|
|
|
char name[128];
|
|
|
|
|
auto& info = gPWM[i];
|
|
|
|
|
info.GetName(name, sizeof(name), "PWM", i);
|
2019-11-17 16:35:45 -08:00
|
|
|
if (ledMap[i] > 0) {
|
2020-01-20 22:47:36 -08:00
|
|
|
ImGui::LabelText(name, "LED[%d]", ledMap[i] - 1);
|
2019-11-17 16:35:45 -08:00
|
|
|
} else {
|
|
|
|
|
float val = HALSimGui::AreOutputsDisabled() ? 0 : HALSIM_GetPWMSpeed(i);
|
2020-01-20 22:47:36 -08:00
|
|
|
ImGui::LabelText(name, "%0.3f", val);
|
2019-11-17 16:35:45 -08:00
|
|
|
}
|
2020-01-20 22:47:36 -08:00
|
|
|
info.PopupEditName(i);
|
2019-09-23 00:24:10 -07:00
|
|
|
|
|
|
|
|
// lazily build history storage
|
|
|
|
|
// if (static_cast<unsigned int>(i) > history.size())
|
|
|
|
|
// history.resize(i + 1);
|
|
|
|
|
// if (!history[i]) history[i] = std::make_unique<History>();
|
|
|
|
|
|
|
|
|
|
// save history
|
|
|
|
|
|
|
|
|
|
// ImGui::PlotLines(labels[i].c_str(), values.data(), values.size(),
|
|
|
|
|
// );
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-20 22:47:36 -08:00
|
|
|
ImGui::PopItemWidth();
|
2019-09-23 00:24:10 -07:00
|
|
|
if (!hasOutputs) ImGui::Text("No PWM outputs");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PWMGui::Initialize() {
|
2020-01-20 22:47:36 -08:00
|
|
|
gPWM.Initialize();
|
2019-09-23 00:24:10 -07:00
|
|
|
HALSimGui::AddWindow("PWM Outputs", DisplayPWMs,
|
|
|
|
|
ImGuiWindowFlags_AlwaysAutoResize);
|
|
|
|
|
HALSimGui::SetDefaultWindowPos("PWM Outputs", 910, 20);
|
|
|
|
|
}
|