Skip to content

Commit

Permalink
[glass] Add Alerts widget (#7219)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmheuer authored Oct 16, 2024
1 parent 68715aa commit f7dddb8
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 0 deletions.
32 changes: 32 additions & 0 deletions glass/src/lib/native/cpp/other/Alerts.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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/other/Alerts.h"

#include <IconsFontAwesome6.h>
#include <imgui.h>

using namespace glass;

void glass::DisplayAlerts(AlertsModel* model) {
auto& infos = model->GetInfos();
auto& warnings = model->GetWarnings();
auto& errors = model->GetErrors();

const ImVec4 kWarningColor{0.85f, 0.56f, 0.0f, 1.0f};
const ImVec4 kErrorColor{0.9f, 0.25f, 0.25f, 1.0f};

// show higher severity alerts on top
for (auto&& error : errors) {
ImGui::TextColored(kErrorColor, "%s %s", ICON_FA_CIRCLE_XMARK,
error.c_str());
}
for (auto&& warning : warnings) {
ImGui::TextColored(kWarningColor, "%s %s", ICON_FA_TRIANGLE_EXCLAMATION,
warning.c_str());
}
for (auto&& info : infos) {
ImGui::Text("%s %s", ICON_FA_CIRCLE_INFO, info.c_str());
}
}
23 changes: 23 additions & 0 deletions glass/src/lib/native/include/glass/other/Alerts.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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.

#pragma once

#include <string>
#include <vector>

#include "glass/Model.h"

namespace glass {

class AlertsModel : public Model {
public:
virtual const std::vector<std::string>& GetInfos() = 0;
virtual const std::vector<std::string>& GetWarnings() = 0;
virtual const std::vector<std::string>& GetErrors() = 0;
};

void DisplayAlerts(AlertsModel* model);

} // namespace glass
51 changes: 51 additions & 0 deletions glass/src/libnt/native/cpp/NTAlerts.cpp
Original file line number Diff line number Diff line change
@@ -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();
}
11 changes: 11 additions & 0 deletions glass/src/libnt/native/cpp/StandardNetworkTables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand Down
49 changes: 49 additions & 0 deletions glass/src/libnt/native/include/glass/networktables/NTAlerts.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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.

#pragma once

#include <string>
#include <string_view>
#include <vector>

#include <networktables/NetworkTableInstance.h>
#include <networktables/StringArrayTopic.h>

#include "glass/other/Alerts.h"

namespace glass {

class NTAlertsModel : public AlertsModel {
public:
static constexpr const char* kType = "Alerts";

// path is to the table containing ".type", excluding the trailing /
explicit NTAlertsModel(std::string_view path);
NTAlertsModel(nt::NetworkTableInstance inst, std::string_view path);

const std::vector<std::string>& GetInfos() override { return m_infosValue; }

const std::vector<std::string>& GetWarnings() override {
return m_warningsValue;
}

const std::vector<std::string>& GetErrors() override { return m_errorsValue; }

void Update() override;
bool Exists() override;
bool IsReadOnly() override { return false; }

private:
nt::NetworkTableInstance m_inst;
nt::StringArraySubscriber m_infos;
nt::StringArraySubscriber m_warnings;
nt::StringArraySubscriber m_errors;

std::vector<std::string> m_infosValue;
std::vector<std::string> m_warningsValue;
std::vector<std::string> m_errorsValue;
};

} // namespace glass

0 comments on commit f7dddb8

Please sign in to comment.