mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-05 03:21:42 +00:00
[hal,wpilib] Move Alert to HAL (#8646)
SystemCore implementation is not yet connected to MRCComm.
This commit is contained in:
@@ -4,30 +4,28 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include "wpi/hal/Alert.h"
|
||||
#include "wpi/hal/Types.h"
|
||||
|
||||
namespace wpi {
|
||||
|
||||
/**
|
||||
* Persistent alert to be sent via NetworkTables. Alerts are tagged with a type
|
||||
* of kError, kWarning, or kInfo to denote urgency. See Alert::AlertType for
|
||||
* suggested usage of each type. Alerts can be displayed on supported
|
||||
* dashboards, and are shown in a priority order based on type and recency of
|
||||
* activation, with newly activated alerts first.
|
||||
* Persistent alert to be sent to the driver station. Alerts are tagged with a
|
||||
* type of HIGH/ERROR, MEDIUM/WARNING, or LOW/INFO to denote urgency. See
|
||||
* Alert::Level for suggested usage of each type. Alerts can be displayed on
|
||||
* supported dashboards, and are shown in a priority order based on type and
|
||||
* recency of activation, with newly activated alerts first.
|
||||
*
|
||||
* Alerts should be created once and stored persistently, then updated to
|
||||
* "active" or "inactive" as necessary. Set(bool) can be safely called
|
||||
* periodically.
|
||||
*
|
||||
* This API is new for 2025, but is likely to change in future seasons to
|
||||
* facilitate deeper integration with the robot control system.
|
||||
*
|
||||
* <pre>
|
||||
* class Robot {
|
||||
* wpi::Alert alert{"Something went wrong", wpi::Alert::AlertType::kWarning};
|
||||
* wpi::Alert alert{"Something went wrong", wpi::Alert::Level::WARNING};
|
||||
* }
|
||||
*
|
||||
* Robot::periodic() {
|
||||
@@ -40,28 +38,36 @@ class Alert {
|
||||
/**
|
||||
* Represents an alert's level of urgency.
|
||||
*/
|
||||
enum class AlertType {
|
||||
enum class Level {
|
||||
/**
|
||||
* High priority alert - displayed first on the dashboard with a red "X"
|
||||
* High priority alert - displayed first with a red "X"
|
||||
* symbol. Use this type for problems which will seriously affect the
|
||||
* robot's functionality and thus require immediate attention.
|
||||
*/
|
||||
kError,
|
||||
HIGH = HAL_ALERT_HIGH,
|
||||
|
||||
/** Alternate name for a high priority alert. */
|
||||
ERROR = HIGH,
|
||||
|
||||
/**
|
||||
* Medium priority alert - displayed second on the dashboard with a yellow
|
||||
* "!" symbol. Use this type for problems which could affect the robot's
|
||||
* functionality but do not necessarily require immediate attention.
|
||||
* Medium priority alert - displayed second with a yellow "!" symbol.
|
||||
* Use this type for problems which could affect the robot's functionality
|
||||
* but do not necessarily require immediate attention.
|
||||
*/
|
||||
kWarning,
|
||||
MEDIUM = HAL_ALERT_MEDIUM,
|
||||
|
||||
/** Alternate name for a medium priority alert. */
|
||||
WARNING = MEDIUM,
|
||||
|
||||
/**
|
||||
* Low priority alert - displayed last on the dashboard with a green "i"
|
||||
* symbol. Use this type for problems which are unlikely to affect the
|
||||
* robot's functionality, or any other alerts which do not fall under the
|
||||
* other categories.
|
||||
* Low priority alert - displayed last with a green "i" symbol. Use this
|
||||
* type for problems which are unlikely to affect the robot's functionality,
|
||||
* or any other alerts which do not fall under the other categories.
|
||||
*/
|
||||
kInfo
|
||||
LOW = HAL_ALERT_LOW,
|
||||
|
||||
/** Alternate name for a low priority alert. */
|
||||
INFO = LOW
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -69,9 +75,9 @@ class Alert {
|
||||
* to be instantiated, the appropriate entries will be added to NetworkTables.
|
||||
*
|
||||
* @param text Text to be displayed when the alert is active.
|
||||
* @param type Alert urgency level.
|
||||
* @param level Alert urgency level.
|
||||
*/
|
||||
Alert(std::string_view text, AlertType type);
|
||||
Alert(std::string_view text, Level level);
|
||||
|
||||
/**
|
||||
* Creates a new alert. If this is the first to be instantiated in its group,
|
||||
@@ -79,17 +85,9 @@ class Alert {
|
||||
*
|
||||
* @param group Group identifier, used as the entry name in NetworkTables.
|
||||
* @param text Text to be displayed when the alert is active.
|
||||
* @param type Alert urgency level.
|
||||
* @param level Alert urgency level.
|
||||
*/
|
||||
Alert(std::string_view group, std::string_view text, AlertType type);
|
||||
|
||||
Alert(Alert&&);
|
||||
Alert& operator=(Alert&&);
|
||||
|
||||
Alert(const Alert&) = default;
|
||||
Alert& operator=(const Alert&) = default;
|
||||
|
||||
~Alert();
|
||||
Alert(std::string_view group, std::string_view text, Level level);
|
||||
|
||||
/**
|
||||
* Sets whether the alert should currently be displayed. This method can be
|
||||
@@ -103,7 +101,7 @@ class Alert {
|
||||
* Gets whether the alert is active.
|
||||
* @return whether the alert is active.
|
||||
*/
|
||||
bool Get() const { return m_active; }
|
||||
bool Get() const;
|
||||
|
||||
/**
|
||||
* Updates current alert text. Use this method to dynamically change the
|
||||
@@ -117,25 +115,17 @@ class Alert {
|
||||
* Gets the current alert text.
|
||||
* @return the current text.
|
||||
*/
|
||||
std::string GetText() const { return m_text; }
|
||||
std::string GetText() const;
|
||||
|
||||
/**
|
||||
* Get the type of this alert.
|
||||
* @return the type
|
||||
*/
|
||||
AlertType GetType() const { return m_type; }
|
||||
Level GetType() const { return m_type; }
|
||||
|
||||
private:
|
||||
class PublishedAlert;
|
||||
class SendableAlerts;
|
||||
|
||||
AlertType m_type;
|
||||
std::string m_text;
|
||||
std::set<PublishedAlert>* m_activeAlerts;
|
||||
bool m_active = false;
|
||||
uint64_t m_activeStartTime;
|
||||
Level m_type;
|
||||
wpi::hal::Handle<HAL_AlertHandle, HAL_DestroyAlert> m_handle;
|
||||
};
|
||||
|
||||
std::string format_as(Alert::AlertType type);
|
||||
|
||||
} // namespace wpi
|
||||
70
wpilibc/src/main/native/include/wpi/simulation/AlertSim.hpp
Normal file
70
wpilibc/src/main/native/include/wpi/simulation/AlertSim.hpp
Normal file
@@ -0,0 +1,70 @@
|
||||
// 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 <stdint.h>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "wpi/driverstation/Alert.hpp"
|
||||
#include "wpi/hal/Types.h"
|
||||
|
||||
namespace wpi::sim {
|
||||
|
||||
/**
|
||||
* Class to get info on simulated alerts.
|
||||
*/
|
||||
class AlertSim final {
|
||||
public:
|
||||
AlertSim() = delete;
|
||||
|
||||
/** Information about an alert. */
|
||||
struct AlertInfo {
|
||||
/** The handle of the alert. */
|
||||
HAL_AlertHandle handle;
|
||||
|
||||
/** The group of the alert. */
|
||||
std::string group;
|
||||
|
||||
/** The text of the alert. */
|
||||
std::string text;
|
||||
|
||||
/** The time the alert became active. 0 if not active. */
|
||||
int64_t activeStartTime;
|
||||
|
||||
/** The level of the alert (HIGH, MEDIUM, or LOW). */
|
||||
Alert::Level level;
|
||||
|
||||
/**
|
||||
* Returns whether the alert is currently active.
|
||||
*
|
||||
* @return true if the alert is active, false otherwise
|
||||
*/
|
||||
bool isActive() const { return activeStartTime != 0; }
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the number of alerts. Note: this is not guaranteed to be consistent
|
||||
* with the number of alerts returned by GetAll.
|
||||
*
|
||||
* @return the number of alerts
|
||||
*/
|
||||
static int32_t GetCount();
|
||||
|
||||
/**
|
||||
* Gets detailed information about each alert.
|
||||
*
|
||||
* @return Alerts
|
||||
*/
|
||||
static std::vector<AlertInfo> GetAll();
|
||||
|
||||
/**
|
||||
* Resets all alert simulation data.
|
||||
*/
|
||||
static void ResetData();
|
||||
};
|
||||
|
||||
} // namespace wpi::sim
|
||||
Reference in New Issue
Block a user