2020-12-26 14:31:24 -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 "glass/hardware/Encoder.h"
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
#include <fmt/format.h>
|
2020-09-12 10:55:46 -07:00
|
|
|
#include <imgui.h>
|
|
|
|
|
|
|
|
|
|
#include "glass/Context.h"
|
|
|
|
|
#include "glass/DataSource.h"
|
[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
|
|
|
#include "glass/Storage.h"
|
2020-09-12 10:55:46 -07:00
|
|
|
|
|
|
|
|
using namespace glass;
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
void EncoderModel::SetName(std::string_view name) {
|
|
|
|
|
if (name.empty()) {
|
2020-09-12 10:55:46 -07:00
|
|
|
if (auto distancePerPulse = GetDistancePerPulseData()) {
|
|
|
|
|
distancePerPulse->SetName("");
|
|
|
|
|
}
|
|
|
|
|
if (auto count = GetCountData()) {
|
|
|
|
|
count->SetName("");
|
|
|
|
|
}
|
|
|
|
|
if (auto period = GetPeriodData()) {
|
|
|
|
|
period->SetName("");
|
|
|
|
|
}
|
|
|
|
|
if (auto direction = GetDirectionData()) {
|
|
|
|
|
direction->SetName("");
|
|
|
|
|
}
|
|
|
|
|
if (auto distance = GetDistanceData()) {
|
|
|
|
|
distance->SetName("");
|
|
|
|
|
}
|
|
|
|
|
if (auto rate = GetRateData()) {
|
|
|
|
|
rate->SetName("");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (auto distancePerPulse = GetDistancePerPulseData()) {
|
2021-06-06 16:13:58 -07:00
|
|
|
distancePerPulse->SetName(fmt::format("{} Distance/Count", name));
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
if (auto count = GetCountData()) {
|
2021-06-06 16:13:58 -07:00
|
|
|
count->SetName(fmt::format("{} Count", name));
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
if (auto period = GetPeriodData()) {
|
2021-06-06 16:13:58 -07:00
|
|
|
period->SetName(fmt::format("{} Period", name));
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
if (auto direction = GetDirectionData()) {
|
2021-06-06 16:13:58 -07:00
|
|
|
direction->SetName(fmt::format("{} Direction", name));
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
if (auto distance = GetDistanceData()) {
|
2021-06-06 16:13:58 -07:00
|
|
|
distance->SetName(fmt::format("{} Distance", name));
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
if (auto rate = GetRateData()) {
|
2021-06-06 16:13:58 -07:00
|
|
|
rate->SetName(fmt::format("{} Rate", name));
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void glass::DisplayEncoder(EncoderModel* model) {
|
|
|
|
|
if (auto simDevice = model->GetSimDevice()) {
|
|
|
|
|
ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(96, 96, 96, 255));
|
|
|
|
|
ImGui::TextUnformatted(simDevice);
|
|
|
|
|
ImGui::PopStyleColor();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int chA = model->GetChannelA();
|
|
|
|
|
int chB = model->GetChannelB();
|
|
|
|
|
|
|
|
|
|
// build header label
|
[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
|
|
|
std::string& name = GetStorage().GetString("name");
|
2020-09-12 10:55:46 -07:00
|
|
|
char label[128];
|
[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
|
|
|
if (!name.empty()) {
|
|
|
|
|
std::snprintf(label, sizeof(label), "%s [%d,%d]###name", name.c_str(), chA,
|
2020-09-12 10:55:46 -07:00
|
|
|
chB);
|
|
|
|
|
} else {
|
|
|
|
|
std::snprintf(label, sizeof(label), "Encoder[%d,%d]###name", chA, chB);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// header
|
|
|
|
|
bool open = CollapsingHeader(label);
|
|
|
|
|
|
|
|
|
|
// context menu to change name
|
[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
|
|
|
if (PopupEditName("name", &name)) {
|
|
|
|
|
model->SetName(name);
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!open) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
|
|
|
|
|
ImGui::PushItemWidth(ImGui::GetFontSize() * 8);
|
|
|
|
|
// distance per pulse
|
|
|
|
|
if (auto distancePerPulseData = model->GetDistancePerPulseData()) {
|
|
|
|
|
double value = distancePerPulseData->GetValue();
|
|
|
|
|
distancePerPulseData->LabelText("Dist/Count", "%.6f", value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// count
|
|
|
|
|
if (auto countData = model->GetCountData()) {
|
|
|
|
|
int value = countData->GetValue();
|
2020-12-28 12:58:06 -08:00
|
|
|
if (ImGui::InputInt("##input", &value)) {
|
|
|
|
|
model->SetCount(value);
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
ImGui::SameLine();
|
|
|
|
|
if (ImGui::Button("Reset")) {
|
|
|
|
|
model->SetCount(0);
|
|
|
|
|
}
|
|
|
|
|
ImGui::SameLine();
|
|
|
|
|
ImGui::Selectable("Count");
|
|
|
|
|
countData->EmitDrag();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// max period
|
|
|
|
|
{
|
|
|
|
|
double maxPeriod = model->GetMaxPeriod();
|
|
|
|
|
ImGui::LabelText("Max Period", "%.6f", maxPeriod);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// period
|
|
|
|
|
if (auto periodData = model->GetPeriodData()) {
|
|
|
|
|
double value = periodData->GetValue();
|
|
|
|
|
if (periodData->InputDouble("Period", &value, 0, 0, "%.6g")) {
|
|
|
|
|
model->SetPeriod(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// reverse direction
|
|
|
|
|
ImGui::LabelText("Reverse Direction", "%s",
|
|
|
|
|
model->GetReverseDirection() ? "true" : "false");
|
|
|
|
|
|
|
|
|
|
// direction
|
|
|
|
|
if (auto directionData = model->GetDirectionData()) {
|
|
|
|
|
static const char* options[] = {"reverse", "forward"};
|
|
|
|
|
int value = directionData->GetValue() ? 1 : 0;
|
|
|
|
|
if (directionData->Combo("Direction", &value, options, 2)) {
|
|
|
|
|
model->SetDirection(value != 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// distance
|
|
|
|
|
if (auto distanceData = model->GetDistanceData()) {
|
|
|
|
|
double value = distanceData->GetValue();
|
|
|
|
|
if (distanceData->InputDouble("Distance", &value, 0, 0, "%.6g")) {
|
|
|
|
|
model->SetDistance(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// rate
|
|
|
|
|
if (auto rateData = model->GetRateData()) {
|
|
|
|
|
double value = rateData->GetValue();
|
|
|
|
|
if (rateData->InputDouble("Rate", &value, 0, 0, "%.6g")) {
|
|
|
|
|
model->SetRate(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ImGui::PopItemWidth();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
void glass::DisplayEncoders(EncodersModel* model, std::string_view noneMsg) {
|
2020-09-12 10:55:46 -07:00
|
|
|
bool hasAny = false;
|
|
|
|
|
model->ForEachEncoder([&](EncoderModel& encoder, int i) {
|
|
|
|
|
hasAny = true;
|
|
|
|
|
PushID(i);
|
|
|
|
|
DisplayEncoder(&encoder);
|
|
|
|
|
PopID();
|
|
|
|
|
});
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!hasAny && !noneMsg.empty()) {
|
2021-06-06 16:13:58 -07:00
|
|
|
ImGui::TextUnformatted(noneMsg.data(), noneMsg.data() + noneMsg.size());
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|