[glass] Add Alerts widget (#7219)

This commit is contained in:
Ryan Heuer
2024-10-16 14:45:56 -05:00
committed by GitHub
parent 68715aa484
commit f7dddb8014
5 changed files with 166 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
// 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.
#include "glass/networktables/NTAlerts.h"
#include <utility>
#include <fmt/format.h>
using namespace glass;
NTAlertsModel::NTAlertsModel(std::string_view path)
: NTAlertsModel{nt::NetworkTableInstance::GetDefault(), path} {}
NTAlertsModel::NTAlertsModel(nt::NetworkTableInstance inst,
std::string_view path)
: m_inst{inst},
m_infos{m_inst.GetStringArrayTopic(fmt::format("{}/infos", path))
.Subscribe({})},
m_warnings{m_inst.GetStringArrayTopic(fmt::format("{}/warnings", path))
.Subscribe({})},
m_errors{m_inst.GetStringArrayTopic(fmt::format("{}/errors", path))
.Subscribe({})} {}
void NTAlertsModel::Update() {
if (!m_infos.Exists()) {
m_infosValue.clear();
}
for (auto&& v : m_infos.ReadQueue()) {
m_infosValue = std::move(v.value);
}
if (!m_warnings.Exists()) {
m_warningsValue.clear();
}
for (auto&& v : m_warnings.ReadQueue()) {
m_warningsValue = std::move(v.value);
}
if (!m_errors.Exists()) {
m_errorsValue.clear();
}
for (auto&& v : m_errors.ReadQueue()) {
m_errorsValue = std::move(v.value);
}
}
bool NTAlertsModel::Exists() {
return m_infos.Exists();
}

View File

@@ -4,6 +4,7 @@
#include <memory>
#include "glass/networktables/NTAlerts.h"
#include "glass/networktables/NTCommandScheduler.h"
#include "glass/networktables/NTCommandSelector.h"
#include "glass/networktables/NTDifferentialDrive.h"
@@ -24,6 +25,16 @@
using namespace glass;
void glass::AddStandardNetworkTablesViews(NetworkTablesProvider& provider) {
provider.Register(
NTAlertsModel::kType,
[](nt::NetworkTableInstance inst, const char* path) {
return std::make_unique<NTAlertsModel>(inst, path);
},
[](Window* win, Model* model, const char*) {
win->SetDefaultSize(300, 150);
return MakeFunctionView(
[=] { DisplayAlerts(static_cast<NTAlertsModel*>(model)); });
});
provider.Register(
NTCommandSchedulerModel::kType,
[](nt::NetworkTableInstance inst, const char* path) {