Files
allwpilib/simulation/halsim_gui/src/main/native/cpp/TimingGui.cpp
Peter Johnson 2a5ca77454 [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.
2020-12-04 00:36:55 -08:00

80 lines
2.5 KiB
C++

/*----------------------------------------------------------------------------*/
/* Copyright (c) 2019-2020 FIRST. All Rights Reserved. */
/* 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 "TimingGui.h"
#include <glass/Model.h>
#include <glass/View.h>
#include <cstdio>
#include <cstring>
#include <vector>
#include <hal/HALBase.h>
#include <hal/simulation/MockHooks.h>
#include <hal/simulation/NotifierData.h>
#include <imgui.h>
#include "HALSimGui.h"
using namespace halsimgui;
namespace {
class TimingModel : public glass::Model {
public:
void Update() override {}
bool Exists() override { return true; }
};
} // namespace
static void DisplayTiming() {
int32_t status = 0;
uint64_t curTime = HAL_GetFPGATime(&status);
if (ImGui::Button("Run")) HALSIM_ResumeTiming();
ImGui::SameLine();
if (ImGui::Button("Pause")) HALSIM_PauseTiming();
ImGui::SameLine();
ImGui::PushButtonRepeat(true);
if (ImGui::Button("Step")) {
HALSIM_PauseTiming();
uint64_t nextTimeout = HALSIM_GetNextNotifierTimeout();
if (nextTimeout != UINT64_MAX)
HALSIM_StepTimingAsync(nextTimeout - curTime);
}
ImGui::PopButtonRepeat();
ImGui::PushItemWidth(ImGui::GetFontSize() * 4);
ImGui::LabelText("FPGA Time", "%.3f", curTime / 1000000.0);
ImGui::PopItemWidth();
static std::vector<HALSIM_NotifierInfo> notifiers;
int32_t num = HALSIM_GetNotifierInfo(notifiers.data(), notifiers.size());
if (static_cast<uint32_t>(num) > notifiers.size()) {
notifiers.resize(num);
HALSIM_GetNotifierInfo(notifiers.data(), notifiers.size());
}
if (num > 0) ImGui::Separator();
ImGui::PushItemWidth(ImGui::GetFontSize() * 4);
for (int32_t i = 0; i < num; ++i)
ImGui::LabelText(notifiers[i].name, "%.3f",
notifiers[i].timeout / 1000000.0);
ImGui::PopItemWidth();
}
void TimingGui::Initialize() {
HALSimGui::halProvider.Register(
"Timing", [] { return true; },
[] { return std::make_unique<TimingModel>(); },
[](glass::Window* win, glass::Model* model) {
win->DisableRenamePopup();
win->SetFlags(ImGuiWindowFlags_AlwaysAutoResize);
win->SetDefaultPos(5, 150);
return glass::MakeFunctionView(DisplayTiming);
});
HALSimGui::halProvider.ShowDefault("Timing");
}